better notifications

This commit is contained in:
rimu 2024-02-10 19:58:34 +13:00
parent 4974d2cddb
commit fa649bb8ab
3 changed files with 16 additions and 21 deletions

View file

@ -6,6 +6,7 @@ from datetime import timedelta
from random import randint from random import randint
from typing import Union, Tuple from typing import Union, Tuple
from flask import current_app, request, g, url_for from flask import current_app, request, g, url_for
from flask_babel import _
from sqlalchemy import text, func from sqlalchemy import text, func
from app import db, cache, constants, celery from app import db, cache, constants, celery
from app.models import User, Post, Community, BannedInstances, File, PostReply, AllowedInstances, Instance, utcnow, \ 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 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): if isinstance(notification_target, PostReply):
anchor = f"comment_{post_reply.id}" 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, user_id=notification_target.user_id,
author_id=post_reply.user_id, author_id=post_reply.user_id,
url=url_for('activitypub.post_ap', post_id=post.id, _anchor=anchor)) 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) 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: for person in people_to_notify:
if person.user_id != post.user_id: 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) 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 = 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 user.unread_notifications += 1

View file

@ -150,30 +150,16 @@ def register(app):
if filesize > 0 and num_content > 0: if filesize > 0 and num_content > 0:
print(f'{user.id},"{user.ap_id}",{filesize},{num_content}') 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): def list_files(directory):
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
for file in files: for file in files:
yield os.path.join(root, file) yield os.path.join(root, file)
@app.cli.command("findorphanfiles") @app.cli.command("remove_orphan_files")
def findorphanfiles(): 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(): with app.app_context():
for file_path in list_files('app/static/media/users'): for file_path in list_files('app/static/media/users'):
if 'thumbnail' in file_path: if 'thumbnail' in file_path:
f = File.query.filter(File.thumbnail_path == file_path).first() f = File.query.filter(File.thumbnail_path == file_path).first()
else: else:

View file

@ -78,7 +78,10 @@ def show_post(post_id: int):
from_bot=current_user.bot, up_votes=1, nsfw=post.nsfw, nsfl=post.nsfl, from_bot=current_user.bot, up_votes=1, nsfw=post.nsfw, nsfl=post.nsfl,
notify_author=form.notify_author.data) notify_author=form.notify_author.data)
if post.notify_author and current_user.id != post.user_id: 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)) author_id=current_user.id, url=url_for('activitypub.post_ap', post_id=post.id))
db.session.add(notification) db.session.add(notification)
post.author.unread_notifications += 1 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) notify_author=form.notify_author.data, instance_id=1)
db.session.add(reply) 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 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)) author_id=current_user.id, url=url_for('activitypub.post_ap', post_id=post.id))
db.session.add(notification) db.session.add(notification)
in_reply_to.author.unread_notifications += 1 in_reply_to.author.unread_notifications += 1