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
|
||||
|
||||
Reference in New Issue
Block a user