Added work on popular board listing
This commit is contained in:
parent
418881fc32
commit
179ec4a3be
5 changed files with 33 additions and 22 deletions
89
static-data/default-plugins/circles/flowapi.py
Executable file
89
static-data/default-plugins/circles/flowapi.py
Executable file
|
@ -0,0 +1,89 @@
|
|||
"""Onionr - Private P2P Communication.
|
||||
|
||||
This file primarily serves to allow specific fetching of circles board messages
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
|
||||
from flask import Response, Blueprint
|
||||
from deadsimplekv import DeadSimpleKV
|
||||
|
||||
from utils import identifyhome
|
||||
"""
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
flask_blueprint = Blueprint('circles', __name__)
|
||||
|
||||
with open(os.path.dirname(os.path.realpath(__file__)) + '/info.json', 'r') as info_file:
|
||||
data = info_file.read().strip()
|
||||
version = json.loads(data, strict=False)['version']
|
||||
|
||||
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):
|
||||
board_cache = DeadSimpleKV(
|
||||
BOARD_CACHE_FILE,
|
||||
flush_on_exit=False)
|
||||
board_cache.refresh()
|
||||
posts = board_cache.get(board)
|
||||
if posts is None:
|
||||
posts = ''
|
||||
else:
|
||||
posts = ','.join(posts)
|
||||
return Response(posts)
|
||||
|
||||
@flask_blueprint.route('/circles/getpostsbyboard/<board>/<offset>')
|
||||
def get_post_by_board_with_offset(board, offset):
|
||||
offset = int(offset)
|
||||
OFFSET_COUNT = 10
|
||||
board_cache = DeadSimpleKV(
|
||||
BOARD_CACHE_FILE,
|
||||
flush_on_exit=False)
|
||||
board_cache.refresh()
|
||||
posts = board_cache.get(board)
|
||||
if posts is None:
|
||||
posts = ''
|
||||
else:
|
||||
posts.reverse()
|
||||
posts = ','.join(posts[offset:offset + OFFSET_COUNT])
|
||||
return Response(posts)
|
||||
|
||||
@flask_blueprint.route('/circles/version')
|
||||
def get_version():
|
||||
return Response(version)
|
||||
|
||||
@flask_blueprint.route('/circles/removefromcache/<board>/<name>', methods=['POST'])
|
||||
def remove_from_cache(board, name):
|
||||
board_cache = DeadSimpleKV(BOARD_CACHE_FILE,
|
||||
flush_on_exit=False)
|
||||
board_cache.refresh()
|
||||
posts = board_cache.get(board)
|
||||
try:
|
||||
posts.remove(name)
|
||||
except ValueError:
|
||||
pass
|
||||
board_cache.put(board, posts)
|
||||
return Response('success')
|
||||
|
||||
#@flask_blueprint.route('/circles/getpopular/<count>')
|
||||
#def get_popular(count):
|
||||
#boards = read_only_cache.get
|
||||
|
4
static-data/default-plugins/circles/info.json
Executable file
4
static-data/default-plugins/circles/info.json
Executable file
|
@ -0,0 +1,4 @@
|
|||
{ "name": "circles",
|
||||
"version": "1.0.0",
|
||||
"author": "onionr"
|
||||
}
|
174
static-data/default-plugins/circles/main.py
Executable file
174
static-data/default-plugins/circles/main.py
Executable file
|
@ -0,0 +1,174 @@
|
|||
"""Onionr - Private P2P Communication.
|
||||
|
||||
This default plugin handles "flow" messages
|
||||
(global chatroom style communication)
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import deadsimplekv as simplekv
|
||||
from utils import identifyhome, reconstructhash
|
||||
from coredb import blockmetadb
|
||||
import threading
|
||||
import time
|
||||
import locale
|
||||
from onionrblocks.onionrblockapi import Block
|
||||
import logger
|
||||
import onionrblocks
|
||||
from onionrutils import escapeansi, epoch, bytesconverter
|
||||
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
||||
# import after path insert
|
||||
import flowapi # noqa
|
||||
"""
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
flask_blueprint = flowapi.flask_blueprint
|
||||
security_whitelist = ['staticfiles.boardContent', 'staticfiles.board']
|
||||
|
||||
plugin_name = 'circles'
|
||||
PLUGIN_VERSION = '0.1.0'
|
||||
|
||||
EXPIRE_TIME = 43200
|
||||
|
||||
class OnionrFlow:
|
||||
def __init__(self):
|
||||
self.alreadyOutputed = []
|
||||
self.flowRunning = False
|
||||
self.channel = ""
|
||||
return
|
||||
|
||||
def start(self):
|
||||
logger.warn(
|
||||
"Please note: everything said here is public, " +
|
||||
"even if a random channel name is used.", terminal=True)
|
||||
message = ""
|
||||
self.flowRunning = True
|
||||
try:
|
||||
self.channel = logger.readline(
|
||||
"Enter a channel name or none for default:").strip()
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
self.flowRunning = False
|
||||
newThread = threading.Thread(target=self.showOutput, daemon=True)
|
||||
newThread.start()
|
||||
while self.flowRunning:
|
||||
if self.channel == "":
|
||||
self.channel = "global"
|
||||
try:
|
||||
message = logger.readline(f'\nInsert message into {plugin_name}:').strip().replace(
|
||||
'\n', '\\n').replace('\r', '\\r')
|
||||
except EOFError:
|
||||
pass
|
||||
except KeyboardInterrupt:
|
||||
self.flowRunning = False
|
||||
else:
|
||||
if message == "q":
|
||||
self.flowRunning = False
|
||||
expireTime = epoch.get_epoch() + EXPIRE_TIME
|
||||
if len(message) > 0:
|
||||
logger.info('Inserting message as block...', terminal=True)
|
||||
onionrblocks.insert(message, header='brd',
|
||||
expire=expireTime,
|
||||
meta = {
|
||||
'ch': self.channel})
|
||||
|
||||
logger.info(f"{plugin_name} is exiting, goodbye", terminal=True)
|
||||
return
|
||||
|
||||
def showOutput(self):
|
||||
while type(self.channel) is type(None) and self.flowRunning:
|
||||
time.sleep(1)
|
||||
try:
|
||||
while self.flowRunning:
|
||||
for block in blockmetadb.get_blocks_by_type('brd'):
|
||||
if block in self.alreadyOutputed:
|
||||
continue
|
||||
block = Block(block)
|
||||
b_hash = bytesconverter.bytes_to_str(block.getHash())
|
||||
if block.getMetadata('ch') != self.channel:
|
||||
continue
|
||||
if not self.flowRunning:
|
||||
break
|
||||
logger.info('\n------------------------',
|
||||
prompt=False, terminal=True)
|
||||
content = block.getContent()
|
||||
# Escape new lines, remove trailing whitespace, and escape ansi sequences
|
||||
content = escapeansi.escape_ANSI(content.replace(
|
||||
'\n', '\\n').replace('\r', '\\r').strip())
|
||||
logger.info(block.getDate().strftime(
|
||||
"%m/%d %H:%M") + ' - ' + logger.colors.reset + content, prompt=False, terminal=True)
|
||||
self.alreadyOutputed.append(b_hash)
|
||||
time.sleep(5)
|
||||
except KeyboardInterrupt:
|
||||
self.flowRunning = False
|
||||
|
||||
|
||||
def on_circles_cmd(api, data=None):
|
||||
OnionrFlow().start()
|
||||
|
||||
|
||||
def on_circlesend_cmd(api, data=None):
|
||||
err_msg = "Second arg is board name, third is quoted message"
|
||||
try:
|
||||
sys.argv[2]
|
||||
except IndexError:
|
||||
logger.error(err_msg, terminal=True)
|
||||
try:
|
||||
sys.argv[3]
|
||||
except IndexError:
|
||||
logger.error(err_msg, terminal=True)
|
||||
|
||||
bl = onionrblocks.insert(sys.argv[3], header='brd',
|
||||
expire=(EXPIRE_TIME + epoch.get_epoch()),
|
||||
meta={'ch': sys.argv[2]})
|
||||
print(bl)
|
||||
|
||||
|
||||
|
||||
def on_softreset(api, data=None):
|
||||
try:
|
||||
os.remove(identifyhome.identify_home() + '/board-index.cache.json')
|
||||
logger.info('Cleared Circles board cache')
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
def on_processblocks(api, data=None):
|
||||
metadata = data['block'].bmetadata # Get the block metadata
|
||||
if data['type'] != 'brd':
|
||||
return
|
||||
|
||||
b_hash = reconstructhash.deconstruct_hash(
|
||||
data['block'].hash) # Get the 0-truncated block hash
|
||||
board_cache = simplekv.DeadSimpleKV(identifyhome.identify_home(
|
||||
) + '/board-index.cache.json', flush_on_exit=False) # get the board index cache
|
||||
board_cache.refresh()
|
||||
# Validate the channel name is sane for caching
|
||||
try:
|
||||
ch = metadata['ch']
|
||||
except KeyError:
|
||||
ch = 'global'
|
||||
ch_len = len(ch)
|
||||
if ch_len == 0:
|
||||
ch = 'global'
|
||||
elif ch_len > 12:
|
||||
return
|
||||
|
||||
existing_posts = board_cache.get(ch)
|
||||
if existing_posts is None:
|
||||
existing_posts = []
|
||||
existing_posts.append(data['block'].hash)
|
||||
board_cache.put(ch, existing_posts)
|
||||
board_cache.flush()
|
Loading…
Add table
Add a link
Reference in a new issue