From fa649bb8abdcb7b7cfeb6f38bb6c2b753d5c7f62 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sat, 10 Feb 2024 19:58:34 +1300 Subject: [PATCH] better notifications --- app/activitypub/util.py | 7 +++++-- app/cli.py | 20 +++----------------- app/post/routes.py | 10 ++++++++-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index d4aa0d09..be3e605a 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -6,6 +6,7 @@ from datetime import timedelta from random import randint from typing import Union, Tuple from flask import current_app, request, g, url_for +from flask_babel import _ from sqlalchemy import text, func from app import db, cache, constants, celery from app.models import User, Post, Community, BannedInstances, File, PostReply, AllowedInstances, Instance, utcnow, \ @@ -985,7 +986,9 @@ def create_post_reply(activity_log: ActivityPubLog, community: Community, in_rep if notification_target.notify_author and post_reply.user_id != notification_target.user_id and notification_target.author.ap_id is None: if isinstance(notification_target, PostReply): anchor = f"comment_{post_reply.id}" - notification = Notification(title='Reply from ' + post_reply.author.display_name(), + notification = Notification(title=shorten_string(_('Reply from %(name)s on %(post_title)s', + name=post_reply.author.display_name(), + post_title=post.title), 50), user_id=notification_target.user_id, author_id=post_reply.user_id, url=url_for('activitypub.post_ap', post_id=post.id, _anchor=anchor)) @@ -1098,7 +1101,7 @@ def notify_about_post(post: Post): people_to_notify = CommunityMember.query.filter_by(community_id=post.community_id, notify_new_posts=True, is_banned=False) for person in people_to_notify: if person.user_id != post.user_id: - new_notification = Notification(title=shorten_string(post.title, 25), url=f"/post/{post.id}", user_id=person.user_id, author_id=post.user_id) + new_notification = Notification(title=shorten_string(post.title, 50), url=f"/post/{post.id}", user_id=person.user_id, author_id=post.user_id) db.session.add(new_notification) user = User.query.get(person.user_id) # todo: make this more efficient by doing a join with CommunityMember at the start of the function user.unread_notifications += 1 diff --git a/app/cli.py b/app/cli.py index 784f8691..a8b29d7b 100644 --- a/app/cli.py +++ b/app/cli.py @@ -150,30 +150,16 @@ def register(app): if filesize > 0 and num_content > 0: print(f'{user.id},"{user.ap_id}",{filesize},{num_content}') - @app.cli.command("cleanupcovers") - def cleanupcovers(): - with app.app_context(): - for user in User.query.all(): - if not user.is_local(): - if user.cover_id: - file = user.cover - if file.file_path and file.thumbnail_path: - if os.path.exists(file.file_path): - os.unlink(file.file_path) - print(file.file_path) - file.file_path = '' - db.session.commit() - def list_files(directory): for root, dirs, files in os.walk(directory): for file in files: yield os.path.join(root, file) - @app.cli.command("findorphanfiles") - def findorphanfiles(): + @app.cli.command("remove_orphan_files") + def remove_orphan_files(): + """ Any user-uploaded file that does not have a corresponding entry in the File table should be deleted """ with app.app_context(): for file_path in list_files('app/static/media/users'): - if 'thumbnail' in file_path: f = File.query.filter(File.thumbnail_path == file_path).first() else: diff --git a/app/post/routes.py b/app/post/routes.py index 25bd8fa8..b49e5690 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -78,7 +78,10 @@ def show_post(post_id: int): from_bot=current_user.bot, up_votes=1, nsfw=post.nsfw, nsfl=post.nsfl, notify_author=form.notify_author.data) if post.notify_author and current_user.id != post.user_id: - notification = Notification(title=_('Reply from %(name)s ', name=current_user.display_name()), user_id=post.user_id, + notification = Notification(title=shorten_string(_('Reply from %(name)s on %(post_title)s', + name=current_user.display_name(), + post_title=post.title), 50), + user_id=post.user_id, author_id=current_user.id, url=url_for('activitypub.post_ap', post_id=post.id)) db.session.add(notification) post.author.unread_notifications += 1 @@ -418,7 +421,10 @@ def add_reply(post_id: int, comment_id: int): notify_author=form.notify_author.data, instance_id=1) db.session.add(reply) if in_reply_to.notify_author and current_user.id != in_reply_to.user_id and in_reply_to.author.ap_id is None: # todo: check if replier is blocked - notification = Notification(title=_('Reply from %(name)s', name=current_user.display_name()), user_id=in_reply_to.user_id, + notification = Notification(title=shorten_string(_('Reply from %(name)s on %(post_title)s', + name=current_user.display_name(), + post_title=post.title), 50), + user_id=in_reply_to.user_id, author_id=current_user.id, url=url_for('activitypub.post_ap', post_id=post.id)) db.session.add(notification) in_reply_to.author.unread_notifications += 1