Skip to content

Commit 86757fc

Browse files
committed
Added notifications for likes, messages and views
1 parent 9af6018 commit 86757fc

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

backend/PyMatcha/routes/api/like.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from flask_jwt_extended import jwt_required
88
from PyMatcha.models.like import Like
99
from PyMatcha.models.match import Match
10+
from PyMatcha.models.notification import Notification
1011
from PyMatcha.models.user import get_user
1112
from PyMatcha.utils.decorators import validate_params
1213
from PyMatcha.utils.errors import BadRequestError
@@ -53,11 +54,35 @@ def like_profile(uid):
5354

5455
if u.already_likes(current_user.id):
5556
Match.create(user_1=current_user.id, user_2=u.id)
57+
Notification.create(
58+
user_id=u.id, content=f"{u.first_name} liked you! Go check them out!", type="like", link_to=f"users/{u.id}"
59+
)
60+
Notification.create(
61+
user_id=u.id,
62+
content=f"You and {current_user.first_name} matched!",
63+
type="match",
64+
link_to=f"conversation/{current_user.id}",
65+
)
66+
Notification.create(
67+
user_id=current_user.id,
68+
content=f"You and {u.first_name} matched!",
69+
type="match",
70+
link_to=f"conversation/{u.id}",
71+
)
5672
return Success("It's a match !")
5773

5874
if is_superlike:
75+
Notification.create(
76+
user_id=u.id,
77+
content=f"{u.first_name} superliked you 😏! Go check them out!",
78+
type="superlike",
79+
link_to=f"users/{u.id}",
80+
)
5981
return Success("Superliked user.")
6082
else:
83+
Notification.create(
84+
user_id=u.id, content=f"{u.first_name} liked you! Go check them out!", type="like", link_to=f"users/{u.id}"
85+
)
6186
return Success("Liked user.")
6287

6388

@@ -73,4 +98,7 @@ def unlike_profile(uid):
7398
if not current_user.already_likes(u.id):
7499
raise BadRequestError("You never liked this person in the first place.")
75100
Like.get_multi(liked_id=u.id, liker_id=current_user.id).delete()
101+
102+
Notification.create(user_id=u.id, content=f"{u.first_name} unliked you.", type="unlike", link_to=None)
103+
76104
return Success(f"Unliked user {u.id}.")

backend/PyMatcha/routes/api/messages.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from flask_jwt_extended import current_user
2525
from flask_jwt_extended import jwt_required
2626
from PyMatcha.models.message import Message
27+
from PyMatcha.models.notification import Notification
2728
from PyMatcha.models.user import get_user
2829
from PyMatcha.utils.decorators import validate_params
2930
from PyMatcha.utils.errors import BadRequestError
@@ -71,6 +72,12 @@ def send_message():
7172

7273
current_user.send_message(to_id=to_user.id, content=content)
7374
current_app.logger.debug("/messages -> Message successfully sent to {}.".format(to_uid))
75+
Notification.create(
76+
user_id=to_user.id,
77+
content=f"{current_user.first_name} said: {content}",
78+
type="message",
79+
link_to=f"conversation/{current_user.id}",
80+
)
7481
return Success("Message successfully sent to {}.".format(to_uid))
7582

7683

@@ -119,6 +126,12 @@ def like_message(message_id):
119126
raise BadRequestError("Message is already liked.")
120127
message.is_liked = True
121128
message.save()
129+
Notification.create(
130+
user_id=message.from_id,
131+
content=f"{current_user.first_name} liked you message!",
132+
type="message_like",
133+
link_to=f"conversation/{current_user.id}",
134+
)
122135
return Success(f"Liked message {message_id}.")
123136

124137

backend/PyMatcha/routes/api/profile/view.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
from flask import Blueprint
2020
from flask_jwt_extended import current_user
2121
from flask_jwt_extended import jwt_required
22+
from PyMatcha.models.notification import Notification
2223
from PyMatcha.models.user import get_user
2324
from PyMatcha.models.view import View
25+
from PyMatcha.utils.errors import BadRequestError
2426
from PyMatcha.utils.errors import NotFoundError
2527
from PyMatcha.utils.match_score import _get_distance
2628
from PyMatcha.utils.success import SuccessOutput
@@ -36,11 +38,19 @@ def view_profile(uid):
3638
except NotFoundError:
3739
raise NotFoundError(f"User {uid} not found.")
3840

39-
if current_user.id != u.id:
40-
View.create(profile_id=u.id, viewer_id=current_user.id)
41+
if current_user.id == u.id:
42+
raise BadRequestError("Cannot view yoursel")
43+
View.create(profile_id=u.id, viewer_id=current_user.id)
4144

4245
user_dict = u.to_dict()
4346
# TODO: Update swagger with this
4447
user_dict["distance"] = _get_distance(current_user.geohash, u.geohash)
4548

49+
Notification.create(
50+
user_id=u.id,
51+
content=f"{u.first_name} just viewed your profile! Go check their profile out!",
52+
type="view",
53+
link_to=f"users/{u.id}",
54+
)
55+
4656
return SuccessOutput("profile", user_dict)

0 commit comments

Comments
 (0)