Added work on popular board listing

This commit is contained in:
Kevin Froman 2020-03-28 00:16:42 -05:00
parent 418881fc32
commit 179ec4a3be
5 changed files with 33 additions and 22 deletions

View File

@ -1,6 +1,6 @@
"""Onionr - Private P2P Communication. """Onionr - Private P2P Communication.
This file primarily serves to allow specific fetching of flow board messages This file primarily serves to allow specific fetching of circles board messages
""" """
import json import json
import os import os
@ -24,16 +24,23 @@ from utils import identifyhome
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
flask_blueprint = Blueprint('flow', __name__) flask_blueprint = Blueprint('circles', __name__)
with open(os.path.dirname(os.path.realpath(__file__)) + '/info.json', 'r') as info_file: with open(os.path.dirname(os.path.realpath(__file__)) + '/info.json', 'r') as info_file:
data = info_file.read().strip() data = info_file.read().strip()
version = json.loads(data, strict=False)['version'] version = json.loads(data, strict=False)['version']
@flask_blueprint.route('/flow/getpostsbyboard/<board>') BOARD_CACHE_FILE = identifyhome.identify_home() + '/board-index.cache.json',
read_only_cache = DeadSimpleKV(
BOARD_CACHE_FILE,
flush_on_exit=False,
refresh_seconds=30)
@flask_blueprint.route('/circles/getpostsbyboard/<board>')
def get_post_by_board(board): def get_post_by_board(board):
board_cache = DeadSimpleKV( board_cache = DeadSimpleKV(
identifyhome.identify_home() + '/board-index.cache.json', BOARD_CACHE_FILE,
flush_on_exit=False) flush_on_exit=False)
board_cache.refresh() board_cache.refresh()
posts = board_cache.get(board) posts = board_cache.get(board)
@ -43,12 +50,12 @@ def get_post_by_board(board):
posts = ','.join(posts) posts = ','.join(posts)
return Response(posts) return Response(posts)
@flask_blueprint.route('/flow/getpostsbyboard/<board>/<offset>') @flask_blueprint.route('/circles/getpostsbyboard/<board>/<offset>')
def get_post_by_board_with_offset(board, offset): def get_post_by_board_with_offset(board, offset):
offset = int(offset) offset = int(offset)
OFFSET_COUNT = 10 OFFSET_COUNT = 10
board_cache = DeadSimpleKV( board_cache = DeadSimpleKV(
identifyhome.identify_home() + '/board-index.cache.json', BOARD_CACHE_FILE,
flush_on_exit=False) flush_on_exit=False)
board_cache.refresh() board_cache.refresh()
posts = board_cache.get(board) posts = board_cache.get(board)
@ -59,14 +66,13 @@ def get_post_by_board_with_offset(board, offset):
posts = ','.join(posts[offset:offset + OFFSET_COUNT]) posts = ','.join(posts[offset:offset + OFFSET_COUNT])
return Response(posts) return Response(posts)
@flask_blueprint.route('/flow/version') @flask_blueprint.route('/circles/version')
def get_version(): def get_version():
return Response(version) return Response(version)
@flask_blueprint.route('/flow/removefromcache/<board>/<name>', methods=['POST']) @flask_blueprint.route('/circles/removefromcache/<board>/<name>', methods=['POST'])
def remove_from_cache(board, name): def remove_from_cache(board, name):
board_cache = DeadSimpleKV(identifyhome.identify_home() + board_cache = DeadSimpleKV(BOARD_CACHE_FILE,
'/board-index.cache.json',
flush_on_exit=False) flush_on_exit=False)
board_cache.refresh() board_cache.refresh()
posts = board_cache.get(board) posts = board_cache.get(board)
@ -76,3 +82,8 @@ def remove_from_cache(board, name):
pass pass
board_cache.put(board, posts) board_cache.put(board, posts)
return Response('success') return Response('success')
#@flask_blueprint.route('/circles/getpopular/<count>')
#def get_popular(count):
#boards = read_only_cache.get

View File

@ -0,0 +1,4 @@
{ "name": "circles",
"version": "1.0.0",
"author": "onionr"
}

View File

@ -38,7 +38,7 @@ import flowapi # noqa
flask_blueprint = flowapi.flask_blueprint flask_blueprint = flowapi.flask_blueprint
security_whitelist = ['staticfiles.boardContent', 'staticfiles.board'] security_whitelist = ['staticfiles.boardContent', 'staticfiles.board']
plugin_name = 'flow' plugin_name = 'circles'
PLUGIN_VERSION = '0.1.0' PLUGIN_VERSION = '0.1.0'
EXPIRE_TIME = 43200 EXPIRE_TIME = 43200
@ -67,7 +67,7 @@ class OnionrFlow:
if self.channel == "": if self.channel == "":
self.channel = "global" self.channel = "global"
try: try:
message = logger.readline('\nInsert message into flow:').strip().replace( message = logger.readline(f'\nInsert message into {plugin_name}:').strip().replace(
'\n', '\\n').replace('\r', '\\r') '\n', '\\n').replace('\r', '\\r')
except EOFError: except EOFError:
pass pass
@ -84,7 +84,7 @@ class OnionrFlow:
meta = { meta = {
'ch': self.channel}) 'ch': self.channel})
logger.info("Flow is exiting, goodbye", terminal=True) logger.info(f"{plugin_name} is exiting, goodbye", terminal=True)
return return
def showOutput(self): def showOutput(self):
@ -115,11 +115,11 @@ class OnionrFlow:
self.flowRunning = False self.flowRunning = False
def on_flow_cmd(api, data=None): def on_circles_cmd(api, data=None):
OnionrFlow().start() OnionrFlow().start()
def on_flowsend_cmd(api, data=None): def on_circlesend_cmd(api, data=None):
err_msg = "Second arg is board name, third is quoted message" err_msg = "Second arg is board name, third is quoted message"
try: try:
sys.argv[2] sys.argv[2]

View File

@ -1,4 +0,0 @@
{ "name": "flow",
"version": "0.1.0",
"author": "onionr"
}

View File

@ -35,7 +35,7 @@ let toggleLoadingMessage = function(){
} }
} }
fetch('/flow/version', { fetch('/circles/version', {
method: 'GET', method: 'GET',
headers: { headers: {
"token": webpass "token": webpass
@ -147,7 +147,7 @@ function getBlocks(){
} }
fetch('/flow/getpostsbyboard/' + ch, { fetch('/circles/getpostsbyboard/' + ch, {
method: 'GET', method: 'GET',
headers: { headers: {
"token": webpass "token": webpass
@ -181,7 +181,7 @@ function loadMessage(blockHash, blockList, count, channel){
if (!response.ok) { if (!response.ok) {
let on404 = function() { let on404 = function() {
if (response.status == 404){ if (response.status == 404){
fetch('/flow/removefromcache/' + channel + '/' + blockHash, { fetch('/circles/removefromcache/' + channel + '/' + blockHash, {
method: 'POST', method: 'POST',
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",