22from urllib .parse import urlparse
33
44import frappe
5- from frappe .frappeclient import FrappeClient
5+
6+ from raven .utils import make_api_call
67
78# ----- Utility Functions -----
89
@@ -20,14 +21,13 @@ def add_token_to_raven_cloud(user_id, token):
2021 """
2122 raven_settings = frappe .get_single ("Raven Settings" )
2223
23- client = FrappeClient (
24- url = raven_settings .push_notification_server_url ,
24+ url = f"{ raven_settings .push_notification_server_url } /api/method/raven_cloud.api.notification.create_user_token"
25+
26+ make_api_call (
27+ url = url ,
2528 api_key = raven_settings .push_notification_api_key ,
2629 api_secret = raven_settings .get_password ("push_notification_api_secret" ),
27- )
28-
29- client .post_api (
30- "raven_cloud.api.notification.create_user_token" ,
30+ method = "POST" ,
3131 params = {
3232 "user_id" : user_id ,
3333 "token" : token ,
@@ -42,14 +42,13 @@ def delete_token_from_raven_cloud(user_id, token):
4242 """
4343 raven_settings = frappe .get_single ("Raven Settings" )
4444
45- client = FrappeClient (
46- url = raven_settings .push_notification_server_url ,
45+ url = f"{ raven_settings .push_notification_server_url } /api/method/raven_cloud.api.notification.delete_user_token"
46+
47+ make_api_call (
48+ url = url ,
4749 api_key = raven_settings .push_notification_api_key ,
4850 api_secret = raven_settings .get_password ("push_notification_api_secret" ),
49- )
50-
51- client .post_api (
52- "raven_cloud.api.notification.delete_user_token" ,
51+ method = "POST" ,
5352 params = {
5453 "user_id" : user_id ,
5554 "token" : token ,
@@ -65,33 +64,22 @@ def sync_users_tokens_to_raven_cloud():
6564 raven_settings = frappe .get_single ("Raven Settings" )
6665 tokens = frappe .db .get_all ("Raven Push Token" , fields = ["user" , "fcm_token" ], order_by = "user" )
6766
68- # Group tokens by user to ensure all tokens for a user are in the same chunk
69- user_tokens = {}
70- for token in tokens :
71- user_tokens .setdefault (token ["user" ], []).append (token )
72-
73- # Build chunks where all tokens for a user stay together
74- chunks = []
75- current_chunk = []
76- for user , user_token_list in user_tokens .items ():
77- if len (current_chunk ) + len (user_token_list ) > 10 and current_chunk :
78- chunks .append (current_chunk )
79- current_chunk = []
80- current_chunk .extend (user_token_list )
81- if current_chunk :
82- chunks .append (current_chunk )
83-
84- for chunk in chunks :
85- client = FrappeClient (
86- url = raven_settings .push_notification_server_url ,
87- api_key = raven_settings .push_notification_api_key ,
88- api_secret = raven_settings .get_password ("push_notification_api_secret" ),
89- )
67+ url = f"{ raven_settings .push_notification_server_url } /api/method/raven_cloud.api.notification.import_user_tokens"
9068
91- client .post_api (
92- "raven_cloud.api.notification.import_user_tokens" ,
93- params = {
94- "site_name" : get_site_name (),
95- "tokens" : json .dumps (chunk ),
96- },
69+ response = make_api_call (
70+ url = url ,
71+ api_key = raven_settings .push_notification_api_key ,
72+ api_secret = raven_settings .get_password ("push_notification_api_secret" ),
73+ method = "POST" ,
74+ params = {"site_name" : get_site_name (), "tokens" : json .dumps (tokens )},
75+ )
76+ message = response .get ("message" )
77+
78+ if message .get ("status" ) == "success" :
79+ return "Tokens synced successfully"
80+ else :
81+ frappe .log_error (
82+ title = "Raven Cloud Sync Tokens Error" ,
83+ message = f"Failed to sync tokens: { message .get ('message' )} " ,
9784 )
85+ return "Failed to sync tokens"
0 commit comments