add ability to bookmark posts and threads, courtesy of bitty
This commit is contained in:
@@ -2,7 +2,7 @@ from flask import Blueprint, request, url_for
|
||||
from ..lib.babycode import babycode_to_html
|
||||
from ..constants import REACTION_EMOJI
|
||||
from .users import is_logged_in, get_active_user
|
||||
from ..models import APIRateLimits, Threads, Reactions, Users, BookmarkCollections
|
||||
from ..models import APIRateLimits, Threads, Reactions, Users, BookmarkCollections, BookmarkedThreads, BookmarkedPosts
|
||||
from ..db import db
|
||||
|
||||
bp = Blueprint("api", __name__, url_prefix="/api/")
|
||||
@@ -139,3 +139,76 @@ def manage_bookmark_collections(user_id):
|
||||
|
||||
|
||||
return {'status': 'ok'}, 200
|
||||
|
||||
|
||||
@bp.post('/bookmark-post/<post_id>')
|
||||
def bookmark_post(post_id):
|
||||
if not is_logged_in():
|
||||
return {'error': 'not authorized', 'error_code': 401}, 401
|
||||
|
||||
operation = request.json.get('operation')
|
||||
if operation == 'remove' and request.json.get('collection_id', '') == '':
|
||||
return {'status': 'not modified'}, 304
|
||||
collection_id = int(request.json.get('collection_id'))
|
||||
post_id = int(post_id)
|
||||
memo = request.json.get('memo', '')
|
||||
|
||||
if operation == 'move':
|
||||
bm = BookmarkedPosts.find({'post_id': post_id})
|
||||
if not bm:
|
||||
BookmarkedPosts.create({
|
||||
'post_id': post_id,
|
||||
'collection_id': collection_id,
|
||||
'note': memo,
|
||||
})
|
||||
else:
|
||||
bm.update({
|
||||
'collection_id': collection_id,
|
||||
'note': memo,
|
||||
})
|
||||
elif operation == 'remove':
|
||||
bm = BookmarkedPosts.find({'post_id': post_id})
|
||||
if bm:
|
||||
bm.delete()
|
||||
else:
|
||||
return {'error': 'bad request'}, 400
|
||||
|
||||
return {'status': 'ok'}, 200
|
||||
|
||||
|
||||
@bp.post('/bookmark-thread/<thread_id>')
|
||||
def bookmark_thread(thread_id):
|
||||
if not is_logged_in():
|
||||
return {'error': 'not authorized', 'error_code': 401}, 401
|
||||
|
||||
operation = request.json.get('operation')
|
||||
if operation == 'remove' and request.json.get('collection_id', '') == '':
|
||||
return {'status': 'not modified'}, 304
|
||||
collection_id = int(request.json.get('collection_id'))
|
||||
thread_id = int(thread_id)
|
||||
memo = request.json.get('memo', '')
|
||||
|
||||
if operation == 'move':
|
||||
bm = BookmarkedThreads.find({'thread_id': thread_id})
|
||||
if not bm:
|
||||
BookmarkedThreads.create({
|
||||
'thread_id': thread_id,
|
||||
'collection_id': collection_id,
|
||||
'note': memo,
|
||||
})
|
||||
else:
|
||||
bm.update({
|
||||
'collection_id': collection_id,
|
||||
'note': memo,
|
||||
})
|
||||
elif operation == 'remove':
|
||||
bm = BookmarkedThreads.find({
|
||||
'thread_id': thread_id,
|
||||
'note': memo,
|
||||
})
|
||||
if bm:
|
||||
bm.delete()
|
||||
else:
|
||||
return {'error': 'bad request'}, 400
|
||||
|
||||
return {'status': 'ok'}, 200
|
||||
|
||||
53
app/routes/hyperapi.py
Normal file
53
app/routes/hyperapi.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from flask import Blueprint, render_template, abort, request
|
||||
from .users import get_active_user, is_logged_in
|
||||
from ..models import BookmarkCollections, BookmarkedPosts, BookmarkedThreads
|
||||
from functools import wraps
|
||||
|
||||
bp = Blueprint('hyperapi', __name__, url_prefix='/hyperapi/')
|
||||
|
||||
def login_required(view_func):
|
||||
@wraps(view_func)
|
||||
def dec(*args, **kwargs):
|
||||
if not is_logged_in():
|
||||
abort(403)
|
||||
return view_func(*args, **kwargs)
|
||||
return dec
|
||||
|
||||
def account_required(view_func):
|
||||
@wraps(view_func)
|
||||
def dec(*args, **kwargs):
|
||||
if get_active_user().is_guest():
|
||||
abort(403)
|
||||
return view_func(*args, **kwargs)
|
||||
return dec
|
||||
|
||||
@bp.errorhandler(403)
|
||||
def handle_403(e):
|
||||
return "<h1>forbidden</h1>", 403
|
||||
|
||||
|
||||
@bp.get('bookmarks-dropdown/<bookmark_type>')
|
||||
@login_required
|
||||
@account_required
|
||||
def bookmarks_dropdown(bookmark_type):
|
||||
collections = BookmarkCollections.findall({'user_id': get_active_user().id})
|
||||
concept_id = request.args.get('id')
|
||||
require_reload = bool(int(request.args.get('require_reload', default=0)))
|
||||
if bookmark_type.lower() == 'thread':
|
||||
selected = next(filter(lambda bc: bc.has_thread(concept_id), collections), None)
|
||||
elif bookmark_type.lower() == 'post':
|
||||
selected = next(filter(lambda bc: bc.has_post(concept_id), collections), None)
|
||||
else:
|
||||
abort(400)
|
||||
return
|
||||
|
||||
if selected:
|
||||
if bookmark_type.lower() == 'thread':
|
||||
memo = BookmarkedThreads.find({'collection_id': selected.id, 'thread_id': int(concept_id)}).note
|
||||
else:
|
||||
memo = BookmarkedPosts.find({'collection_id': selected.id, 'post_id': int(concept_id)}).note
|
||||
else:
|
||||
memo = ''
|
||||
|
||||
|
||||
return render_template('components/bookmarks_dropdown.html', collections=collections, id=concept_id, selected=selected, type=bookmark_type, memo=memo, require_reload=require_reload)
|
||||
Reference in New Issue
Block a user