andykr1k commited on
Commit
72faa04
·
1 Parent(s): 0980190

Adding tag notifications

Browse files
Files changed (1) hide show
  1. main.py +121 -2
main.py CHANGED
@@ -12,6 +12,7 @@ import uvicorn
12
  import threading
13
  import asyncio
14
  from slack_sdk import WebClient
 
15
 
16
  app = FastAPI()
17
 
@@ -94,6 +95,106 @@ def send_notification(title, body, token, id, post_id, notif_type):
94
  except requests.exceptions.RequestException as e:
95
  print(f'Failed to send notification: {e}')
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  def send_slack_message(event_type: str, category: str, username: str, post_id: str = None, list_id: str = None):
98
  try:
99
  print("reach 1")
@@ -139,10 +240,12 @@ def handle_tags_update(event_payload):
139
  print('Received update in post tags:')
140
  print(event_payload)
141
 
 
142
  record = event_payload['record']
143
- tags_str = record.get('tags', '[]')
144
  try:
145
- tagged_user_ids = json.loads(tags_str)
 
146
  except json.JSONDecodeError:
147
  print('Failed to parse tags.')
148
  return
@@ -395,6 +498,22 @@ def setup_likes_subscription(SUPABASE_ID, SUPABASE_KEY):
395
  channel_posts_for_tags = s.set_channel("realtime:public:posts")
396
  channel_posts_for_tags.join().on(
397
  "INSERT", lambda payload: executor.submit(handle_tags_update, payload))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
 
399
  channel_support = s.set_channel("realtime:public:Support")
400
  channel_support.join().on(
 
12
  import threading
13
  import asyncio
14
  from slack_sdk import WebClient
15
+ import re
16
 
17
  app = FastAPI()
18
 
 
95
  except requests.exceptions.RequestException as e:
96
  print(f'Failed to send notification: {e}')
97
 
98
+ def handle_list_comment_tags_update(event_payload):
99
+ print('Received update in comment tags:')
100
+ print(event_payload)
101
+
102
+ record = event_payload['record']
103
+
104
+ try:
105
+ content = record.get('comment', '[]')
106
+ tagged_user_ids = re.findall(r'@<([^>]+)>', content)
107
+ except json.JSONDecodeError:
108
+ print('Failed to parse tags.')
109
+ return
110
+
111
+ author_id = record['user_id']
112
+ username = getUsername(author_id).lower()
113
+ title = 'Picturelock'
114
+ body = f'@{username} tagged you in a comment!'
115
+ post_id = record['post_id']
116
+
117
+ for tagged_user_id in tagged_user_ids:
118
+ if tagged_user_id != author_id:
119
+ token = getAuthorNotifToken(tagged_user_id)
120
+ if token:
121
+ send_notification(title, body, token, author_id, post_id, "list")
122
+
123
+ def handle_list_reply_tags_update(event_payload):
124
+ print('Received update in reply tags:')
125
+ print(event_payload)
126
+
127
+ record = event_payload['record']
128
+
129
+ try:
130
+ content = record.get('comment', '[]')
131
+ tagged_user_ids = re.findall(r'@<([^>]+)>', content)
132
+ except json.JSONDecodeError:
133
+ print('Failed to parse tags.')
134
+ return
135
+
136
+ author_id = record['author_id']
137
+ username = getUsername(author_id).lower()
138
+ title = 'Picturelock'
139
+ body = f'@{username} tagged you in a reply!'
140
+ post_id = record['post_id']
141
+
142
+ for tagged_user_id in tagged_user_ids:
143
+ if tagged_user_id != author_id:
144
+ token = getAuthorNotifToken(tagged_user_id)
145
+ if token:
146
+ send_notification(title, body, token, author_id, post_id, "list")
147
+
148
+ def handle_comment_tags_update(event_payload):
149
+ print('Received update in comment tags:')
150
+ print(event_payload)
151
+
152
+ record = event_payload['record']
153
+
154
+ try:
155
+ content = record.get('comment', '[]')
156
+ tagged_user_ids = re.findall(r'@<([^>]+)>', content)
157
+ except json.JSONDecodeError:
158
+ print('Failed to parse tags.')
159
+ return
160
+
161
+ author_id = record['user_id']
162
+ username = getUsername(author_id).lower()
163
+ title = 'Picturelock'
164
+ body = f'@{username} tagged you in a comment!'
165
+ post_id = record['post_id']
166
+
167
+ for tagged_user_id in tagged_user_ids:
168
+ if tagged_user_id != author_id:
169
+ token = getAuthorNotifToken(tagged_user_id)
170
+ if token:
171
+ send_notification(title, body, token, author_id, post_id, "post")
172
+
173
+ def handle_reply_tags_update(event_payload):
174
+ print('Received update in reply tags:')
175
+ print(event_payload)
176
+
177
+ record = event_payload['record']
178
+
179
+ try:
180
+ content = record.get('comment', '[]')
181
+ tagged_user_ids = re.findall(r'@<([^>]+)>', content)
182
+ except json.JSONDecodeError:
183
+ print('Failed to parse tags.')
184
+ return
185
+
186
+ author_id = record['author_id']
187
+ username = getUsername(author_id).lower()
188
+ title = 'Picturelock'
189
+ body = f'@{username} tagged you in a reply!'
190
+ post_id = record['post_id']
191
+
192
+ for tagged_user_id in tagged_user_ids:
193
+ if tagged_user_id != author_id:
194
+ token = getAuthorNotifToken(tagged_user_id)
195
+ if token:
196
+ send_notification(title, body, token, author_id, post_id, "post")
197
+
198
  def send_slack_message(event_type: str, category: str, username: str, post_id: str = None, list_id: str = None):
199
  try:
200
  print("reach 1")
 
240
  print('Received update in post tags:')
241
  print(event_payload)
242
 
243
+
244
  record = event_payload['record']
245
+
246
  try:
247
+ content = record.get('content', '[]')
248
+ tagged_user_ids = re.findall(r'@<([^>]+)>', content)
249
  except json.JSONDecodeError:
250
  print('Failed to parse tags.')
251
  return
 
498
  channel_posts_for_tags = s.set_channel("realtime:public:posts")
499
  channel_posts_for_tags.join().on(
500
  "INSERT", lambda payload: executor.submit(handle_tags_update, payload))
501
+
502
+ channel_posts_for_tags = s.set_channel("realtime:public:comments")
503
+ channel_posts_for_tags.join().on(
504
+ "INSERT", lambda payload: executor.submit(handle_comment_tags_update, payload))
505
+
506
+ channel_posts_for_tags = s.set_channel("realtime:public:replies")
507
+ channel_posts_for_tags.join().on(
508
+ "INSERT", lambda payload: executor.submit(handle_reply_tags_update, payload))
509
+
510
+ channel_posts_for_tags = s.set_channel("realtime:public:listcomments")
511
+ channel_posts_for_tags.join().on(
512
+ "INSERT", lambda payload: executor.submit(handle_list_comment_tags_update, payload))
513
+
514
+ channel_posts_for_tags = s.set_channel("realtime:public:listreplies")
515
+ channel_posts_for_tags.join().on(
516
+ "INSERT", lambda payload: executor.submit(handle_list_reply_tags_update, payload))
517
 
518
  channel_support = s.set_channel("realtime:public:Support")
519
  channel_support.join().on(