re-add subscriptions
This commit is contained in:
@ -3,7 +3,7 @@ from flask import (
|
||||
)
|
||||
from .users import login_required, mod_only, get_active_user, is_logged_in
|
||||
from ..db import db
|
||||
from ..models import Threads, Topics, Posts
|
||||
from ..models import Threads, Topics, Posts, Subscriptions
|
||||
from .posts import create_post
|
||||
from slugify import slugify
|
||||
import math
|
||||
@ -39,7 +39,17 @@ def thread(slug):
|
||||
topic = Topics.find({"id": thread.topic_id})
|
||||
other_topics = Topics.select()
|
||||
|
||||
#TODO: subscription last seen
|
||||
is_subscribed = False
|
||||
if is_logged_in():
|
||||
subscription = Subscriptions.find({
|
||||
'thread_id': thread.id,
|
||||
'user_id': get_active_user().id,
|
||||
})
|
||||
if subscription:
|
||||
subscription.update({
|
||||
'last_seen': int(time.time())
|
||||
})
|
||||
is_subscribed = True
|
||||
|
||||
return render_template(
|
||||
"threads/thread.html",
|
||||
@ -49,6 +59,7 @@ def thread(slug):
|
||||
posts = posts,
|
||||
topic = topic,
|
||||
topics = other_topics,
|
||||
is_subscribed = is_subscribed,
|
||||
)
|
||||
|
||||
|
||||
@ -122,3 +133,39 @@ def sticky(slug):
|
||||
@mod_only(".thread", slug = lambda slug: slug)
|
||||
def move(slug):
|
||||
pass
|
||||
|
||||
|
||||
@bp.post("/<slug>/subscribe")
|
||||
@login_required
|
||||
def subscribe(slug):
|
||||
user = get_active_user()
|
||||
thread = Threads.find({'slug': slug})
|
||||
if not thread:
|
||||
return 'no'
|
||||
subscription = Subscriptions.find({
|
||||
'user_id': user.id,
|
||||
'thread_id': thread.id,
|
||||
})
|
||||
if request.form['subscribe'] == 'subscribe':
|
||||
if subscription:
|
||||
subscription.delete()
|
||||
Subscriptions.create({
|
||||
'user_id': user.id,
|
||||
'thread_id': thread.id,
|
||||
'last_seen': int(time.time()),
|
||||
})
|
||||
elif request.form['subscribe'] == 'unsubscribe':
|
||||
if not subscription:
|
||||
return 'no'
|
||||
subscription.delete()
|
||||
elif request.form['subscribe'] == 'read':
|
||||
if not subscription:
|
||||
return 'no'
|
||||
subscription.update({
|
||||
'last_seen': int(time.time())
|
||||
})
|
||||
last_visible_post = request.form.get('last_visible_post', default=None)
|
||||
if last_visible_post is not None:
|
||||
return redirect(url_for('.thread', slug=thread.slug, after=last_visible_post))
|
||||
else:
|
||||
return redirect(url_for('users.inbox', username=user.username))
|
||||
|
@ -2,7 +2,8 @@ from flask import (
|
||||
Blueprint, render_template, request, redirect, url_for, flash, session, current_app
|
||||
)
|
||||
from functools import wraps
|
||||
from ..models import Users, Sessions
|
||||
from ..db import db
|
||||
from ..models import Users, Sessions, Subscriptions
|
||||
from ..constants import InfoboxKind, PermissionLevel
|
||||
from ..auth import digest, verify
|
||||
import secrets
|
||||
@ -202,12 +203,6 @@ def settings(username):
|
||||
return "stub"
|
||||
|
||||
|
||||
@bp.get("/<username>/inbox")
|
||||
@login_required
|
||||
def inbox(username):
|
||||
return "stub"
|
||||
|
||||
|
||||
@bp.post("/log_out")
|
||||
@login_required
|
||||
def log_out():
|
||||
@ -282,3 +277,86 @@ def guest_user(user_id):
|
||||
"permission": PermissionLevel.GUEST.value,
|
||||
})
|
||||
return redirect(url_for(".page", username=target_user.username))
|
||||
|
||||
|
||||
@bp.get("/<username>/inbox")
|
||||
@login_required
|
||||
def inbox(username):
|
||||
user = get_active_user()
|
||||
if username != user.username:
|
||||
return redirect(url_for(".inbox", username = user.username))
|
||||
|
||||
new_posts = []
|
||||
subscription = Subscriptions.find({"user_id": user.id})
|
||||
all_subscriptions = None
|
||||
total_unreads_count = None
|
||||
if subscription:
|
||||
all_subscriptions = user.get_all_subscriptions()
|
||||
q = """
|
||||
WITH thread_metadata AS (
|
||||
SELECT
|
||||
posts.thread_id, threads.slug AS thread_slug, threads.title AS thread_title, COUNT(*) AS unread_count, MAX(posts.created_at) AS newest_post_time
|
||||
FROM
|
||||
posts
|
||||
LEFT JOIN
|
||||
threads ON threads.id = posts.thread_id
|
||||
LEFT JOIN
|
||||
subscriptions ON subscriptions.thread_id = posts.thread_id
|
||||
WHERE subscriptions.user_id = ? AND posts.created_at > subscriptions.last_seen
|
||||
GROUP BY posts.thread_id
|
||||
)
|
||||
|
||||
SELECT
|
||||
tm.thread_id, tm.thread_slug, tm.thread_title, tm.unread_count, tm.newest_post_time,
|
||||
|
||||
posts.id, posts.created_at, post_history.content, post_history.edited_at, users.username, users.status, avatars.file_path AS avatar_path, posts.thread_id, users.id AS user_id, post_history.original_markup, users.signature_rendered
|
||||
FROM
|
||||
thread_metadata tm
|
||||
JOIN
|
||||
posts ON posts.thread_id = tm.thread_id
|
||||
JOIN
|
||||
post_history ON posts.current_revision_id = post_history.id
|
||||
JOIN
|
||||
users ON posts.user_id = users.id
|
||||
LEFT JOIN
|
||||
threads ON threads.id = posts.thread_id
|
||||
LEFT JOIN
|
||||
avatars ON users.avatar_id = avatars.id
|
||||
LEFT JOIN
|
||||
subscriptions ON subscriptions.thread_id = posts.thread_id
|
||||
WHERE
|
||||
subscriptions.user_id = ? AND posts.created_at > subscriptions.last_seen
|
||||
ORDER BY
|
||||
tm.newest_post_time DESC, posts.created_at ASC"""
|
||||
new_posts_raw = db.query(q, user.id, user.id)
|
||||
current_thread_id = None
|
||||
current_thread_group = None
|
||||
total_unreads_count = 0
|
||||
for row in new_posts_raw:
|
||||
if row['thread_id'] != current_thread_id:
|
||||
current_thread_group = {
|
||||
'thread_id': row['thread_id'],
|
||||
'thread_title': row['thread_title'],
|
||||
'unread_count': row['unread_count'],
|
||||
'thread_slug': row['thread_slug'],
|
||||
'newest_post_time': row['newest_post_time'],
|
||||
'posts': [],
|
||||
}
|
||||
total_unreads_count += int(row['unread_count'])
|
||||
new_posts.append(current_thread_group)
|
||||
current_thread_id = row['thread_id']
|
||||
current_thread_group['posts'].append({
|
||||
'id': row['id'],
|
||||
'created_at': row['created_at'],
|
||||
'content': row['content'],
|
||||
'edited_at': row['edited_at'],
|
||||
'username': row['username'],
|
||||
'status': row['status'],
|
||||
'avatar_path': row['avatar_path'],
|
||||
'thread_id': row['thread_id'],
|
||||
'user_id': row['user_id'],
|
||||
'original_markup': row['original_markup'],
|
||||
'signature_rendered': row['signature_rendered']
|
||||
})
|
||||
|
||||
return render_template("users/inbox.html", new_posts = new_posts, total_unreads_count = total_unreads_count, all_subscriptions = all_subscriptions)
|
||||
|
Reference in New Issue
Block a user