Skip to content

Commit

Permalink
Improve notification functions
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Jan 25, 2025
1 parent 92e1711 commit eeca287
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
5 changes: 2 additions & 3 deletions moments/blueprints/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def collect(photo_id):
return {'message': 'Already collected.'}, 400

current_user.collect(photo)
if current_user != photo.author and photo.author.receive_collect_notification:
if current_user != photo.author:
push_collect_notification(user=current_user, photo_id=photo_id, receiver=photo.author)
return {'message': 'Photo collected.'}

Expand Down Expand Up @@ -83,8 +83,7 @@ def follow(username):
return {'message': 'Already followed.'}, 400

current_user.follow(user)
if user.receive_collect_notification:
push_follow_notification(follower=current_user, receiver=user)
push_follow_notification(follower=current_user, receiver=user)
return {'message': 'User followed.'}


Expand Down
5 changes: 2 additions & 3 deletions moments/blueprints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,12 @@ def new_comment(photo_id):
replied_id = request.args.get('reply')
if replied_id:
comment.replied = db.session.get(Comment, replied_id) or abort(404)
if comment.replied.author.receive_comment_notification:
push_comment_notification(photo_id=photo.id, receiver=comment.replied.author)
push_comment_notification(photo_id=photo.id, receiver=comment.replied.author)
db.session.add(comment)
db.session.commit()
flash('Comment published.', 'success')

if current_user != photo.author and photo.author.receive_comment_notification:
if current_user != photo.author:
push_comment_notification(photo_id, receiver=photo.author, page=page)

flash_errors(form)
Expand Down
5 changes: 2 additions & 3 deletions moments/blueprints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def index(username):

page = request.args.get('page', 1, type=int)
per_page = current_app.config['MOMENTS_PHOTO_PER_PAGE']
stmt = select(Photo).filter_by(author_id=user.id).order_by(Photo.created_at.desc())
stmt = user.photos.select().order_by(Photo.created_at.desc())
pagination = db.paginate(stmt, page=page, per_page=per_page)
photos = pagination.items
return render_template('user/index.html', user=user, pagination=pagination, photos=photos)
Expand Down Expand Up @@ -63,8 +63,7 @@ def follow(username):

current_user.follow(user)
flash('User followed.', 'success')
if user.receive_follow_notification:
push_follow_notification(follower=current_user, receiver=user)
push_follow_notification(follower=current_user, receiver=user)
return redirect_back()


Expand Down
6 changes: 6 additions & 0 deletions moments/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


def push_follow_notification(follower, receiver):
if not receiver.receive_follow_notification:
return
user_url = url_for('user.index', username=follower.username)
message = f'User <a href="{user_url}">{follower.username}</a> followed you.'
notification = Notification(message=message, receiver=receiver)
Expand All @@ -13,6 +15,8 @@ def push_follow_notification(follower, receiver):


def push_comment_notification(photo_id, receiver, page=1):
if not receiver.receive_comment_notification:
return
photo_url = url_for('main.show_photo', photo_id=photo_id, page=page)
message = f'<a href="{photo_url}#comments">This photo</a> has new comment/reply.'
notification = Notification(message=message, receiver=receiver)
Expand All @@ -21,6 +25,8 @@ def push_comment_notification(photo_id, receiver, page=1):


def push_collect_notification(user, photo_id, receiver):
if not receiver.receive_collect_notification:
return
user_url = url_for('user.index', username=user.username)
photo_url = url_for('main.show_photo', photo_id=photo_id)
message = f'User <a href="{user_url}">{user.username}</a> collected your <a href="{photo_url}">photo</a>'
Expand Down
6 changes: 5 additions & 1 deletion moments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@


def generate_token(user, operation, expiration=3600, **kwargs):
payload = {'id': user.id, 'operation': operation.value, 'exp': datetime.now(timezone.utc) + timedelta(seconds=expiration)}
payload = {
'id': user.id,
'operation': operation.value,
'exp': datetime.now(timezone.utc) + timedelta(seconds=expiration)
}
payload.update(**kwargs)
return jwt.encode(payload, current_app.config['SECRET_KEY'], algorithm='HS256')

Expand Down

0 comments on commit eeca287

Please sign in to comment.