Update routes/sendnotifications.py
Browse files- routes/sendnotifications.py +27 -7
routes/sendnotifications.py
CHANGED
@@ -146,23 +146,30 @@ async def send_notification(
|
|
146 |
if data.keyword == "like":
|
147 |
post_info = await get_post_info(data.reference)
|
148 |
target_user_id = post_info["user_id"]
|
|
|
149 |
elif data.keyword == "newmessage":
|
150 |
# Get last message in the chat
|
151 |
-
messages = await fetch_supabase("messages", "sender_id,content", {
|
|
|
|
|
152 |
if not messages:
|
153 |
raise HTTPException(status_code=404, detail="No messages found in chat")
|
154 |
|
155 |
last_message = messages[0]
|
156 |
sender_id = last_message["sender_id"]
|
157 |
-
content = last_message
|
|
|
158 |
|
159 |
# Get participants
|
160 |
-
chats = await fetch_supabase("chats", "client_id,stylist_id", {
|
|
|
|
|
161 |
if not chats:
|
162 |
raise HTTPException(status_code=404, detail="Chat not found")
|
163 |
|
164 |
chat = chats[0]
|
165 |
target_user_id = chat["stylist_id"] if sender_id == chat["client_id"] else chat["client_id"]
|
|
|
166 |
else:
|
167 |
target_user_id = data.target_user_id
|
168 |
|
@@ -193,23 +200,36 @@ async def send_notification(
|
|
193 |
collapse_id = short_collapse_key(data.keyword, sender_id, target_user_id)
|
194 |
|
195 |
# Notification content
|
|
|
|
|
196 |
if data.keyword == "follow":
|
197 |
title = "🎉 New Follower!"
|
198 |
body = f"{actor_name} started following you."
|
199 |
-
|
200 |
elif data.keyword == "like":
|
201 |
desc = post_info["description"]
|
202 |
title = "❤️ New Like!"
|
203 |
body = f"{actor_name} liked your post" + (f": \"{desc}\"" if desc else ".")
|
204 |
image_url = post_info["image_url"]
|
|
|
205 |
elif data.keyword == "subscriber":
|
206 |
title = "💼 New Subscriber!"
|
207 |
body = f"{actor_name} just subscribed to your styling services."
|
208 |
-
|
209 |
elif data.keyword == "newmessage":
|
210 |
title = "💬 New Message"
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
else:
|
214 |
raise HTTPException(status_code=400, detail="Unsupported keyword")
|
215 |
|
|
|
146 |
if data.keyword == "like":
|
147 |
post_info = await get_post_info(data.reference)
|
148 |
target_user_id = post_info["user_id"]
|
149 |
+
|
150 |
elif data.keyword == "newmessage":
|
151 |
# Get last message in the chat
|
152 |
+
messages = await fetch_supabase("messages", "sender_id,content,file", {
|
153 |
+
"chat_id": data.reference
|
154 |
+
})
|
155 |
if not messages:
|
156 |
raise HTTPException(status_code=404, detail="No messages found in chat")
|
157 |
|
158 |
last_message = messages[0]
|
159 |
sender_id = last_message["sender_id"]
|
160 |
+
content = (last_message.get("content") or "").strip()
|
161 |
+
file_url = last_message.get("file")
|
162 |
|
163 |
# Get participants
|
164 |
+
chats = await fetch_supabase("chats", "client_id,stylist_id", {
|
165 |
+
"id": data.reference
|
166 |
+
})
|
167 |
if not chats:
|
168 |
raise HTTPException(status_code=404, detail="Chat not found")
|
169 |
|
170 |
chat = chats[0]
|
171 |
target_user_id = chat["stylist_id"] if sender_id == chat["client_id"] else chat["client_id"]
|
172 |
+
|
173 |
else:
|
174 |
target_user_id = data.target_user_id
|
175 |
|
|
|
200 |
collapse_id = short_collapse_key(data.keyword, sender_id, target_user_id)
|
201 |
|
202 |
# Notification content
|
203 |
+
title = image_url = None
|
204 |
+
|
205 |
if data.keyword == "follow":
|
206 |
title = "🎉 New Follower!"
|
207 |
body = f"{actor_name} started following you."
|
208 |
+
|
209 |
elif data.keyword == "like":
|
210 |
desc = post_info["description"]
|
211 |
title = "❤️ New Like!"
|
212 |
body = f"{actor_name} liked your post" + (f": \"{desc}\"" if desc else ".")
|
213 |
image_url = post_info["image_url"]
|
214 |
+
|
215 |
elif data.keyword == "subscriber":
|
216 |
title = "💼 New Subscriber!"
|
217 |
body = f"{actor_name} just subscribed to your styling services."
|
218 |
+
|
219 |
elif data.keyword == "newmessage":
|
220 |
title = "💬 New Message"
|
221 |
+
if content:
|
222 |
+
body = f"{actor_name}: {content}"
|
223 |
+
elif file_url:
|
224 |
+
is_image = file_url.lower().endswith((".jpg", ".jpeg", ".png", ".gif", ".webp"))
|
225 |
+
if is_image:
|
226 |
+
body = f"{actor_name} sent you a photo"
|
227 |
+
image_url = file_url
|
228 |
+
else:
|
229 |
+
body = f"{actor_name} sent you a file"
|
230 |
+
else:
|
231 |
+
body = f"{actor_name} sent you a message"
|
232 |
+
|
233 |
else:
|
234 |
raise HTTPException(status_code=400, detail="Unsupported keyword")
|
235 |
|