33import uuid
44import requests
55from .models import db , SandboxToken , TimelinePin , UserTimeline , TimelineTopic , TimelineTopicSubscription , AppGlance
6- from .utils import get_uid , api_error , pin_valid , glance_valid
6+ from .utils import get_uid , api_error , pin_valid , glance_valid , send_fcm_message , send_fcm_message_to_topics , subscribe_to_fcm_topic , unsubscribe_from_fcm_topic
77from .settings import config
88
99import beeline
@@ -79,6 +79,33 @@ def sync():
7979 }
8080 return jsonify (result )
8181
82+ @api .route ('/user/fcm_token/<token>' )
83+ def fcm_token ():
84+ user_id = get_uid ()
85+
86+ if request .method == 'PUT' :
87+ fcm_token_json = request .json
88+
89+ fcm_token = FcmToken .query .filter_by (user_id = user_id , token = token ).one_or_none ()
90+ if fcm_token is None :
91+ fcm_token = FcmToken .from_json (fcm_token_json , token , user_id )
92+ if fcm_token is None :
93+ return api_error (400 )
94+
95+ db .session .add (fcm_token )
96+ db .session .commit ()
97+ # TODO: Also subscribe to user's topics
98+ # TODO: Resend UserTimeline for the pins about the topic
99+
100+ elif request .method == 'DELETE' :
101+ fcm_token = FcmToken .query .filter_by (user_id = user_id , token = token ).first_or_404 ()
102+ fcm_token .delete ()
103+
104+ db .session .commit ()
105+ # TODO: Also unsubscribe from user's topics
106+ # TODO: Send UserTimeline to delete pins the user no longer has a subscription for
107+ return 'OK'
108+
82109
83110@api .route ('/user/pins/<pin_id>' , methods = ['PUT' , 'DELETE' ])
84111def user_pin (pin_id ):
@@ -107,6 +134,8 @@ def user_pin(pin_id):
107134 db .session .add (pin )
108135 db .session .add (user_timeline )
109136 db .session .commit ()
137+
138+ send_fcm_message (user_id , { 'type' : 'timeline.pin.create' })
110139 else : # update pin
111140 try :
112141 pin .update_from_json (pin_json )
@@ -122,6 +151,8 @@ def user_pin(pin_id):
122151 db .session .add (pin )
123152 db .session .add (user_timeline )
124153 db .session .commit ()
154+
155+ send_fcm_message (user_id , { 'type' : 'timeline.pin.create' })
125156 except (KeyError , ValueError ):
126157 beeline .add_context_field ('timeline.failure.cause' , 'update_pin' )
127158 return api_error (400 )
@@ -138,6 +169,8 @@ def user_pin(pin_id):
138169 pin = pin )
139170 db .session .add (user_timeline )
140171 db .session .commit ()
172+
173+ send_fcm_message (user_id , { 'type' : 'timeline.pin.delete' })
141174 return 'OK'
142175
143176
@@ -152,7 +185,6 @@ def get_app_info(timeline_token):
152185 return app_info ['app_uuid' ], f"uuid:{ app_info ['app_uuid' ]} "
153186
154187
155-
156188@api .route ('/shared/pins/<pin_id>' , methods = ['PUT' , 'DELETE' ])
157189def shared_pin (pin_id ):
158190 try :
@@ -198,6 +230,8 @@ def shared_pin(pin_id):
198230 db .session .add (user_timeline )
199231
200232 db .session .commit ()
233+
234+ send_fcm_message_to_topics (topics , { 'type' : 'timeline.pin.create' })
201235 else : # update pin
202236 try :
203237 pin .update_from_json (pin_json )
@@ -217,12 +251,15 @@ def shared_pin(pin_id):
217251 db .session .add (user_timeline )
218252
219253 db .session .commit ()
254+
255+ send_fcm_message_to_topics (topics , { 'type' : 'timeline.pin.create' })
220256 except (KeyError , ValueError ):
221257 beeline .add_context_field ('timeline.failure.cause' , 'update_pin' )
222258 return api_error (400 )
223259
224260 elif request .method == 'DELETE' :
225261 pin = TimelinePin .query .filter_by (app_uuid = app_uuid , user_id = None , id = pin_id ).first_or_404 ()
262+ topics = pin .topics
226263
227264 # No need to post even old create events, since nobody will render
228265 # them, after all.
@@ -236,6 +273,9 @@ def shared_pin(pin_id):
236273 db .session .add (user_timeline )
237274
238275 db .session .commit ()
276+
277+ send_fcm_message_to_topics (topics , { 'type' : 'timeline.pin.delete' })
278+
239279 return 'OK'
240280
241281
@@ -276,11 +316,17 @@ def user_subscriptions_manage(topic_string):
276316
277317 db .session .commit ()
278318
319+ subscribe_to_fcm_topic (user_id , topic )
320+ send_fcm_message (user_id , { 'type' : 'timeline.topic.subscription' })
321+
279322 elif request .method == 'DELETE' :
280323 TimelineTopicSubscription .query .filter_by (user_id = user_id , topic = topic ).delete ()
281324
282325 db .session .commit ()
283326
327+ unsubscribe_from_fcm_topic (user_id , topic )
328+ send_fcm_message (user_id , { 'type' : 'timeline.topic.unsubscription' })
329+
284330 return 'OK'
285331
286332
@@ -305,7 +351,11 @@ def user_app_glance():
305351 return api_error (400 )
306352
307353 db .session .add (glance )
354+
308355 db .session .commit ()
356+
357+ send_fcm_message (user_id , { 'type' : 'appglance.slice.create' })
358+
309359 return 'OK'
310360
311361
0 commit comments