bookmark collections
This commit is contained in:
@@ -352,19 +352,35 @@ class BookmarkCollections(Model):
|
||||
return not (self.has_posts() or self.has_threads())
|
||||
|
||||
def get_threads(self):
|
||||
q = 'SELECT thread_id FROM bookmarked_threads WHERE collection_id = ?'
|
||||
q = 'SELECT id FROM bookmarked_threads WHERE collection_id = ?'
|
||||
res = db.query(q, self.id)
|
||||
return [Threads.find({'id': bt['thread_id']}) for bt in res]
|
||||
return [BookmarkedThreads.find({'id': bt['id']}) for bt in res]
|
||||
|
||||
def get_posts(self):
|
||||
q = 'SELECT post_id FROM bookmarked_posts WHERE collection_id = ?'
|
||||
q = 'SELECT id FROM bookmarked_posts WHERE collection_id = ?'
|
||||
res = db.query(q, self.id)
|
||||
return [Posts.find({'id': bt['post_id']}) for bt in res]
|
||||
return [BookmarkedPosts.find({'id': bt['id']}) for bt in res]
|
||||
|
||||
def get_threads_count(self):
|
||||
q = 'SELECT COUNT(*) as tc FROM bookmarked_threads WHERE collection_id = ?'
|
||||
res = db.fetch_one(q, self.id)
|
||||
return int(res['tc'])
|
||||
|
||||
def get_posts_count(self):
|
||||
q = 'SELECT COUNT(*) as pc FROM bookmarked_posts WHERE collection_id = ?'
|
||||
res = db.fetch_one(q, self.id)
|
||||
return int(res['pc'])
|
||||
|
||||
|
||||
class BookmarkedPosts(Model):
|
||||
table = 'bookmarked_posts'
|
||||
|
||||
def get_post(self):
|
||||
return Posts.find({'id': self.post_id})
|
||||
|
||||
|
||||
class BookmarkedThreads(Model):
|
||||
table = 'bookmarked_threads'
|
||||
|
||||
def get_thread(self):
|
||||
return Threads.find({'id': self.thread_id})
|
||||
|
||||
@@ -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
|
||||
from ..models import APIRateLimits, Threads, Reactions, Users, BookmarkCollections
|
||||
from ..db import db
|
||||
|
||||
bp = Blueprint("api", __name__, url_prefix="/api/")
|
||||
@@ -96,3 +96,46 @@ def remove_reaction(post_id):
|
||||
reaction.delete()
|
||||
|
||||
return {'status': 'removed'}
|
||||
|
||||
@bp.post('/manage-bookmark-collections/<user_id>')
|
||||
def manage_bookmark_collections(user_id):
|
||||
if not is_logged_in():
|
||||
return {'error': 'not authorized', 'error_code': 401}, 401
|
||||
|
||||
target_user = Users.find({'id': user_id})
|
||||
if target_user.id != get_active_user().id:
|
||||
return {'error': 'forbidden', 'error_code': 403}, 403
|
||||
|
||||
if target_user.is_guest():
|
||||
return {'error': 'forbidden', 'error_code': 403}, 403
|
||||
|
||||
collections_data = request.json
|
||||
for idx, coll_data in enumerate(collections_data.get('collections')):
|
||||
if coll_data['is_new']:
|
||||
collection = BookmarkCollections.create({
|
||||
'name': coll_data['name'],
|
||||
'user_id': target_user.id,
|
||||
'sort_order': idx,
|
||||
})
|
||||
else:
|
||||
collection = BookmarkCollections.find({'id': coll_data['id']})
|
||||
if not collection:
|
||||
continue
|
||||
|
||||
update = {'name': coll_data['name']}
|
||||
if not collection.is_default:
|
||||
update['sort_order'] = idx
|
||||
collection.update(update)
|
||||
|
||||
for removed_id in collections_data.get('removed_collections'):
|
||||
collection = BookmarkCollections.find({'id': removed_id})
|
||||
if not collection:
|
||||
continue
|
||||
|
||||
if collection.is_default:
|
||||
continue
|
||||
|
||||
collection.delete()
|
||||
|
||||
|
||||
return {'status': 'ok'}, 200
|
||||
|
||||
@@ -4,7 +4,7 @@ from flask import (
|
||||
from functools import wraps
|
||||
from ..db import db
|
||||
from ..lib.babycode import babycode_to_html, BABYCODE_VERSION
|
||||
from ..models import Users, Sessions, Subscriptions, Avatars, PasswordResetLinks, InviteKeys, BookmarkCollections
|
||||
from ..models import Users, Sessions, Subscriptions, Avatars, PasswordResetLinks, InviteKeys, BookmarkCollections, BookmarkedThreads
|
||||
from ..constants import InfoboxKind, PermissionLevel
|
||||
from ..auth import digest, verify
|
||||
from wand.image import Image
|
||||
@@ -700,4 +700,16 @@ def bookmarks(username):
|
||||
return redirect(url_for('.bookmarks', username=get_active_user().username))
|
||||
|
||||
collections = target_user.get_bookmark_collections()
|
||||
|
||||
return render_template('users/bookmarks.html', collections=collections)
|
||||
|
||||
|
||||
@bp.get('/<username>/bookmarks/collections')
|
||||
@login_required
|
||||
def bookmark_collections(username):
|
||||
target_user = Users.find({'username': username})
|
||||
if not target_user or target_user.username != get_active_user().username:
|
||||
return redirect(url_for('.bookmark_collections', username=get_active_user().username))
|
||||
|
||||
collections = target_user.get_bookmark_collections()
|
||||
return render_template('users/bookmark_collections.html', collections=collections)
|
||||
|
||||
@@ -100,7 +100,12 @@
|
||||
</form>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro full_post(post, render_sig = True, is_latest = False, editing = False, active_user = None, no_reply = false, Reactions = none, show_thread_title = false, show_bookmark = false) %}
|
||||
{% macro full_post(
|
||||
post, render_sig = True, is_latest = False,
|
||||
editing = False, active_user = None, no_reply = false,
|
||||
Reactions = none, show_thread_title = false,
|
||||
show_bookmark = false, memo = None, bookmark_message = "Bookmark…"
|
||||
) %}
|
||||
{% set postclass = "post" %}
|
||||
{% if editing %}
|
||||
{% set postclass = postclass + " editing" %}
|
||||
@@ -122,6 +127,9 @@
|
||||
<div class="post-content-container" {{ "id=latest-post" if is_latest else "" }}>
|
||||
<div class="post-info">
|
||||
<span>
|
||||
{% if memo -%}
|
||||
Memo: <i>{{ memo }}</i> •
|
||||
{%- endif %}
|
||||
{% if show_thread_title %}
|
||||
<a href="{{ url_for('threads.thread', slug=post.thread_slug) }}">Thread: {{ post.thread_title }}</a>
|
||||
•
|
||||
@@ -174,7 +182,7 @@
|
||||
{% endif %}
|
||||
|
||||
{% if show_bookmark %}
|
||||
<button type="button" class="contain-svg inline icon">{{ icn_bookmark(20) }}Bookmark</button>
|
||||
<button type="button" class="contain-svg inline icon">{{ icn_bookmark(20) }}{{ bookmark_message | safe }}</button>
|
||||
{% endif %}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if can_bookmark %}
|
||||
<button type="button" class="contain-svg inline icon">{{ icn_bookmark(20) }}Bookmark</button>
|
||||
<button type="button" class="contain-svg inline icon">{{ icn_bookmark(20) }}Bookmark…</button>
|
||||
{% endif %}
|
||||
{% if can_lock %}
|
||||
<form class="modform" action="{{ url_for("threads.lock", slug=thread.slug) }}" method="post">
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
</span>
|
||||
<span>
|
||||
{% if active_user and not active_user.is_guest() -%}
|
||||
<button class="thread-info-bookmark-button contain-svg icon" type="button">{{ icn_bookmark(20) }}Bookmark</button>
|
||||
<button class="thread-info-bookmark-button contain-svg icon" type="button">{{ icn_bookmark(20) }}Bookmark…</button>
|
||||
{%- endif %}
|
||||
</span>
|
||||
</span>
|
||||
|
||||
35
app/templates/users/bookmark_collections.html
Normal file
35
app/templates/users/bookmark_collections.html
Normal file
@@ -0,0 +1,35 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}managing bookmark collections{% endblock %}
|
||||
{% block content %}
|
||||
<div class="darkbg settings-container">
|
||||
<h1>Manage bookmark collections</h1>
|
||||
<p>Drag collections to reoder them. You cannot move or remove the default collection, but you can rename it.</p>
|
||||
<div>
|
||||
<button type="button" id="add-collection-button">Add new collection</button>
|
||||
<div id="collections-container">
|
||||
{% for collection in collections | sort(attribute='sort_order') %}
|
||||
<div class="draggable-collection {{ "default" if collection.is_default else ""}}"
|
||||
{% if not collection.is_default %}
|
||||
draggable="true"
|
||||
ondragover="dragOver(event)"
|
||||
ondragstart="dragStart(event)"
|
||||
ondragend="dragEnd()"
|
||||
{% else %}
|
||||
id="default-collection"
|
||||
{% endif %}
|
||||
data-collection-id="{{ collection.id }}">
|
||||
<input type="text" class="collection-name" value="{{ collection.name }}" placeholder="Collection name" required autocomplete="off" maxlength="60"><br>
|
||||
<div>{{ collection.get_threads_count() }} {{ "thread" | pluralize(num=collection.get_threads_count()) }}, {{ collection.get_posts_count() }} {{ "post" | pluralize(num=collection.get_posts_count()) }}</div>
|
||||
{% if collection.is_default %}
|
||||
<i>Default collection</i>
|
||||
{% else %}
|
||||
<button type="button" class="delete-button critical">Delete</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<button type="button" id="save-button" data-submit-href="{{ url_for('api.manage_bookmark_collections', user_id=active_user.id) }}">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
<script src="{{ "/static/js/manage-bookmark-collections.js" | cachebust }}"></script>
|
||||
{% endblock %}
|
||||
@@ -1,9 +1,10 @@
|
||||
{% from "common/macros.html" import accordion, full_post %}
|
||||
{% from "common/icons.html" import icn_bookmark %}
|
||||
{% extends "base.html" %}
|
||||
{% block title %}bookmarks{% endblock %}
|
||||
{% block content %}
|
||||
<div class="darkbg inbox-container">
|
||||
{% for collection in collections %}
|
||||
{% for collection in collections | sort(attribute='sort_order') %}
|
||||
{% call(section) accordion(disabled=collection.is_empty()) %}
|
||||
{% if section == 'header' %}
|
||||
<h1 class="thread-title">{{ collection.name }}</h1>{{" (no bookmarks)" if collection.is_empty() else ""}}
|
||||
@@ -12,19 +13,34 @@
|
||||
{% if inner_section == 'header' %}
|
||||
Threads{{" (no bookmarks)" if not collection.has_threads() else ""}}
|
||||
{% else %}
|
||||
<ul>
|
||||
{% for thread in collection.get_threads()|sort(attribute='created_at', reverse=true) %}
|
||||
<li><a href="{{ url_for('threads.thread', slug=thread.slug) }}">{{ thread.title }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<table class="colorful-table">
|
||||
<thead>
|
||||
<th>Title</th>
|
||||
<th>Memo</th>
|
||||
<th class="small">Manage</th>
|
||||
</thead>
|
||||
{% for thread in collection.get_threads() %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{ url_for('threads.thread', slug=thread.get_thread().slug) }}">{{ thread.get_thread().title }}</a>
|
||||
</td>
|
||||
<td>
|
||||
<i>{{ thread.note }}</i>
|
||||
</td>
|
||||
<td>
|
||||
<button type="button" class="contain-svg inline icon">{{ icn_bookmark(20) }}Manage…</button>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endif %}
|
||||
{% endcall %}
|
||||
{% call(inner_section) accordion(disabled=not collection.has_posts()) %}
|
||||
{% if inner_section == 'header' %}
|
||||
Posts{{" (no bookmarks)" if not collection.has_posts() else ""}}
|
||||
{% else %}
|
||||
{% for post in collection.get_posts()|sort(attribute='created_at', reverse=true) %}
|
||||
{{ full_post(post.get_full_post_view(), no_reply=false, render_sig=false, show_thread_title=true) }}
|
||||
{% for post in collection.get_posts() %}
|
||||
{{ full_post(post.get_post().get_full_post_view(), no_reply=false, render_sig=false, show_thread_title=true, show_bookmark=true, memo=post.note, bookmark_message="Manage…") }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endcall %}
|
||||
|
||||
Reference in New Issue
Block a user