Skip to content

Commit aa22934

Browse files
committed
chore: log error for push notifications
1 parent cf8700e commit aa22934

File tree

1 file changed

+99
-94
lines changed

1 file changed

+99
-94
lines changed

raven/notification.py

Lines changed: 99 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -38,122 +38,127 @@ def send_push_notification_via_raven_cloud(message, raven_settings):
3838
if channel_doc.is_self_message:
3939
return
4040

41-
push_tokens = get_push_tokens_for_channel(message.channel_id)
41+
try:
4242

43-
mentioned_users = [user.get("user") for user in message.mentions]
43+
push_tokens = get_push_tokens_for_channel(message.channel_id)
4444

45-
replied_to = None
45+
mentioned_users = [user.get("user") for user in message.mentions]
4646

47-
if message.linked_message:
48-
replied_message_details = message.replied_message_details
47+
replied_to = None
4948

50-
if isinstance(replied_message_details, str):
51-
replied_message_details = json.loads(message.replied_message_details)
49+
if message.linked_message:
50+
replied_message_details = message.replied_message_details
5251

53-
replied_to = replied_message_details.get("owner")
52+
if isinstance(replied_message_details, str):
53+
replied_message_details = json.loads(message.replied_message_details)
5454

55-
mentioned_tokens = []
56-
replied_tokens = []
57-
final_tokens = []
55+
replied_to = replied_message_details.get("owner")
5856

59-
# If this is a bot message, then we should not filter out the push tokens of the message owner since we need to send the notification to the owner as well (it's coming from the bot)
60-
if not message.is_bot_message:
61-
# Filter out the push tokens of the message owner
62-
push_tokens = [token for token in push_tokens if token.user != message.owner]
57+
mentioned_tokens = []
58+
replied_tokens = []
59+
final_tokens = []
6360

64-
for token in push_tokens:
65-
if token.user == replied_to:
66-
replied_tokens.append(token.fcm_token)
67-
elif token.user in mentioned_users:
68-
mentioned_tokens.append(token.fcm_token)
69-
else:
70-
final_tokens.append(token.fcm_token)
61+
# If this is a bot message, then we should not filter out the push tokens of the message owner since we need to send the notification to the owner as well (it's coming from the bot)
62+
if not message.is_bot_message:
63+
# Filter out the push tokens of the message owner
64+
push_tokens = [token for token in push_tokens if token.user != message.owner]
7165

72-
# We now need to construct the payload for the push notification
66+
for token in push_tokens:
67+
if token.user == replied_to:
68+
replied_tokens.append(token.fcm_token)
69+
elif token.user in mentioned_users:
70+
mentioned_tokens.append(token.fcm_token)
71+
else:
72+
final_tokens.append(token.fcm_token)
7373

74-
if not mentioned_tokens and not replied_tokens and not final_tokens:
75-
return
74+
# We now need to construct the payload for the push notification
7675

77-
messages = []
76+
if not mentioned_tokens and not replied_tokens and not final_tokens:
77+
return
7878

79-
channel_name = f" in #{channel_doc.channel_name}"
79+
messages = []
8080

81-
if channel_doc.is_thread:
82-
channel_name = " in thread"
81+
channel_name = f" in #{channel_doc.channel_name}"
8382

84-
if channel_doc.is_direct_message:
85-
channel_name = ""
83+
if channel_doc.is_thread:
84+
channel_name = " in thread"
8685

87-
content = message.get_notification_message_content()
86+
if channel_doc.is_direct_message:
87+
channel_name = ""
8888

89-
message_owner, message_owner_image = message.get_message_owner_details()
89+
content = message.get_notification_message_content()
9090

91-
workspace = "" if channel_doc.is_dm_thread else channel_doc.workspace
91+
message_owner, message_owner_image = message.get_message_owner_details()
9292

93-
url = frappe.utils.get_url() + "/raven/"
94-
if workspace:
95-
url += f"{workspace}/"
96-
else:
97-
url += "channels/"
93+
workspace = "" if channel_doc.is_dm_thread else channel_doc.workspace
9894

99-
if channel_doc.is_thread:
100-
url += f"thread/{channel_doc.name}/"
101-
else:
102-
url += f"{channel_doc.name}/"
103-
104-
image = get_image_absolute_url(message_owner_image)
105-
106-
data = {
107-
"base_url": frappe.utils.get_url(),
108-
"sitename": frappe.local.site,
109-
"message_id": message.name,
110-
"channel_id": message.channel_id,
111-
"raven_message_type": message.message_type,
112-
"channel_type": "DM" if channel_doc.is_direct_message else "Channel",
113-
"content": message.content,
114-
"from_user": message.owner,
115-
"type": "New message",
116-
"is_thread": "1" if channel_doc.is_thread else "0",
117-
"creation": get_milliseconds_since_epoch(message.creation),
118-
}
119-
120-
if replied_tokens:
121-
messages.append(
122-
{
123-
"tokens": replied_tokens,
124-
"notification": {"title": f"{message_owner} replied{channel_name}", "body": content},
125-
"data": data,
126-
"tag": message.channel_id,
127-
"click_action": url,
128-
"image": image,
129-
}
130-
)
95+
url = frappe.utils.get_url() + "/raven/"
96+
if workspace:
97+
url += f"{workspace}/"
98+
else:
99+
url += "channels/"
131100

132-
if mentioned_tokens:
133-
messages.append(
134-
{
135-
"tokens": mentioned_tokens,
136-
"notification": {"title": f"{message_owner} mentioned you{channel_name}", "body": content},
137-
"data": data,
138-
"tag": message.channel_id,
139-
"click_action": url,
140-
"image": image,
141-
}
142-
)
101+
if channel_doc.is_thread:
102+
url += f"thread/{channel_doc.name}/"
103+
else:
104+
url += f"{channel_doc.name}/"
105+
106+
image = get_image_absolute_url(message_owner_image)
107+
108+
data = {
109+
"base_url": frappe.utils.get_url(),
110+
"sitename": frappe.local.site,
111+
"message_id": message.name,
112+
"channel_id": message.channel_id,
113+
"raven_message_type": message.message_type,
114+
"channel_type": "DM" if channel_doc.is_direct_message else "Channel",
115+
"content": message.content,
116+
"from_user": message.owner,
117+
"type": "New message",
118+
"is_thread": "1" if channel_doc.is_thread else "0",
119+
"creation": get_milliseconds_since_epoch(message.creation),
120+
}
121+
122+
if replied_tokens:
123+
messages.append(
124+
{
125+
"tokens": replied_tokens,
126+
"notification": {"title": f"{message_owner} replied{channel_name}", "body": content},
127+
"data": data,
128+
"tag": message.channel_id,
129+
"click_action": url,
130+
"image": image,
131+
}
132+
)
143133

144-
if final_tokens:
145-
messages.append(
146-
{
147-
"tokens": final_tokens,
148-
"notification": {"title": f"{message_owner}{channel_name}", "body": content},
149-
"data": data,
150-
"tag": message.channel_id,
151-
"click_action": url,
152-
"image": image,
153-
}
154-
)
134+
if mentioned_tokens:
135+
messages.append(
136+
{
137+
"tokens": mentioned_tokens,
138+
"notification": {"title": f"{message_owner} mentioned you{channel_name}", "body": content},
139+
"data": data,
140+
"tag": message.channel_id,
141+
"click_action": url,
142+
"image": image,
143+
}
144+
)
145+
146+
if final_tokens:
147+
messages.append(
148+
{
149+
"tokens": final_tokens,
150+
"notification": {"title": f"{message_owner}{channel_name}", "body": content},
151+
"data": data,
152+
"tag": message.channel_id,
153+
"click_action": url,
154+
"image": image,
155+
}
156+
)
157+
158+
make_post_call_for_notification(messages, raven_settings)
155159

156-
make_post_call_for_notification(messages, raven_settings)
160+
except Exception as e:
161+
frappe.log_error(title="Raven Cloud Push Notification Error")
157162

158163

159164
def make_post_call_for_notification(messages, raven_settings):

0 commit comments

Comments
 (0)