forked from the-blue-alliance/the-blue-alliance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile_main.py
323 lines (290 loc) · 13.6 KB
/
mobile_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import endpoints
import json
import logging
from google.appengine.ext import ndb
from protorpc import remote
from protorpc import message_types
import tba_config
from consts.client_type import ClientType
from helpers.media_helper import MediaParser
from helpers.push_helper import PushHelper
from helpers.notification_helper import NotificationHelper
from helpers.mytba_helper import MyTBAHelper
from helpers.suggestions.suggestion_creator import SuggestionCreator
from models.account import Account
from models.favorite import Favorite
from models.media import Media
from models.sitevar import Sitevar
from models.subscription import Subscription
from models.mobile_api_messages import BaseResponse, FavoriteCollection, FavoriteMessage, RegistrationRequest, \
SubscriptionCollection, SubscriptionMessage, ModelPreferenceMessage, \
MediaSuggestionMessage, PingRequest
from models.mobile_client import MobileClient
from models.suggestion import Suggestion
WEB_CLIENT_ID = ""
ANDROID_AUDIENCE = ""
ANDROID_CLIENT_ID = ""
IOS_CLIENT_ID = ""
client_ids_sitevar = Sitevar.get_or_insert('mobile.clientIds')
if isinstance(client_ids_sitevar.contents, dict):
WEB_CLIENT_ID = client_ids_sitevar.contents.get("web", "")
ANDROID_AUDIENCE = client_ids_sitevar.contents.get("android-audience", "")
ANDROID_CLIENT_ID = client_ids_sitevar.contents.get("android", "")
IOS_CLIENT_ID = client_ids_sitevar.contents.get("ios", "")
if not WEB_CLIENT_ID:
logging.error("Web client ID is not set, see /admin/authkeys")
if not ANDROID_CLIENT_ID:
logging.error("Android client ID is not set, see /admin/authkeys")
if not ANDROID_AUDIENCE:
logging.error("Android Audience is not set, see /admin/authkeys")
if not IOS_CLIENT_ID:
logging.error("iOS client ID is not set, see /admin/authkeys")
client_ids = [WEB_CLIENT_ID, ANDROID_CLIENT_ID, ANDROID_AUDIENCE, IOS_CLIENT_ID]
if tba_config.DEBUG:
'''
Only allow API Explorer access on dev versions
'''
client_ids.append(endpoints.API_EXPLORER_CLIENT_ID)
@endpoints.api(
name='tbaMobile',
version='v9',
description="API for TBA Mobile clients",
allowed_client_ids=client_ids,
audiences={
'firebase': ['tbatv-prod-hrd'],
'google_id_token': [ANDROID_AUDIENCE],
},
issuers={
'firebase': endpoints.Issuer(
'https://securetoken.google.com/tbatv-prod-hrd',
'https://www.googleapis.com/service_accounts/v1/metadata/x509/[email protected]'
),
'google_id_token': endpoints.Issuer(
'https://accounts.google.com',
'https://www.googleapis.com/oauth2/v3/certs'
),
}
)
class MobileAPI(remote.Service):
@endpoints.method(RegistrationRequest, BaseResponse,
path='register', http_method='POST',
name='register')
def register_client(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to register")
user_id = PushHelper.user_email_to_id(current_user.email())
gcm_id = request.mobile_id
os = ClientType.enums[request.operating_system]
name = request.name
uuid = request.device_uuid
query = MobileClient.query(
MobileClient.user_id == user_id,
MobileClient.device_uuid == uuid,
MobileClient.client_type == os)
# trying to figure out an elusive dupe bug
logging.info("DEBUGGING")
logging.info("User ID: {}".format(user_id))
logging.info("UUID: {}".format(uuid))
logging.info("Count: {}".format(query.count()))
if query.count() == 0:
# Record doesn't exist yet, so add it
MobileClient(
parent=ndb.Key(Account, user_id),
user_id=user_id,
messaging_id=gcm_id,
client_type=os,
device_uuid=uuid,
display_name=name).put()
return BaseResponse(code=200, message="Registration successful")
else:
# Record already exists, update it
client = query.fetch(1)[0]
client.messaging_id = gcm_id
client.display_name = name
client.put()
return BaseResponse(code=304, message="Client already exists")
@endpoints.method(RegistrationRequest, BaseResponse,
path='unregister', http_method='POST',
name='unregister')
def unregister_client(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to unregister")
user_id = PushHelper.user_email_to_id(current_user.email())
gcm_id = request.mobile_id
query = MobileClient.query(MobileClient.messaging_id == gcm_id, ancestor=ndb.Key(Account, user_id))\
.fetch(keys_only=True)
if len(query) == 0:
# Record doesn't exist, so we can't remove it
return BaseResponse(code=404, message="User doesn't exist. Can't remove it")
else:
ndb.delete_multi(query)
return BaseResponse(code=200, message="User deleted")
@endpoints.method(PingRequest, BaseResponse,
path='ping', http_method='POST',
name='ping')
def ping_client(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to ping client")
user_id = PushHelper.user_email_to_id(current_user.email())
gcm_id = request.mobile_id
# Find a Client for the current user with the passed GCM ID
clients = MobileClient.query(MobileClient.messaging_id == gcm_id, ancestor=ndb.Key(Account, user_id)).fetch(1)
if len(clients) == 0:
# No Client for user with that push token - bailing
return BaseResponse(code=404, message="Invalid push token for user")
else:
client = clients[0]
from helpers.tbans_helper import TBANSHelper
success = TBANSHelper.ping(client)
if success:
return BaseResponse(code=200, message="Ping sent")
else:
return BaseResponse(code=500, message="Failed to ping client")
@endpoints.method(message_types.VoidMessage, FavoriteCollection,
path='favorites/list', http_method='POST',
name='favorites.list')
def list_favorites(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return FavoriteCollection(favorites=[])
user_id = PushHelper.user_email_to_id(current_user.email())
favorites = Favorite.query(ancestor=ndb.Key(Account, user_id)).fetch()
output = []
for favorite in favorites:
output.append(FavoriteMessage(model_key=favorite.model_key, model_type=favorite.model_type))
return FavoriteCollection(favorites=output)
@endpoints.method(ModelPreferenceMessage, BaseResponse,
path="model/setPreferences", http_method="POST",
name="model.setPreferences")
def update_model_preferences(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to update model preferences")
user_id = PushHelper.user_email_to_id(current_user.email())
model_key = request.model_key
model_type = request.model_type
output = {}
code = 0
if request.favorite:
fav = Favorite(
parent=ndb.Key(Account, user_id),
user_id=user_id,
model_key=model_key,
model_type=model_type
)
result = MyTBAHelper.add_favorite(fav, request.device_key)
if result == 200:
output['favorite'] = {"code": 200,
"message": "Favorite added"}
code += 100
elif result == 304:
output['favorite'] = {"code": 304,
"message": "Favorite already exists"}
code += 304
else:
output['favorite'] = {"code": 500,
"message": "Unknown error adding favorite"}
code += 500
else:
result = MyTBAHelper.remove_favorite(user_id, model_key, model_type, request.device_key)
if result == 200:
output['favorite'] = {"code": 200,
"message": "Favorite deleted"}
code += 100
elif result == 404:
output['favorite'] = {"code": 404,
"message": "Favorite not found"}
code += 404
else:
output['favorite'] = {"code": 500,
"message": "Unknown error removing favorite"}
code += 500
if request.notifications:
sub = Subscription(
parent=ndb.Key(Account, user_id),
user_id=user_id,
model_key=model_key,
model_type=request.model_type,
notification_types=PushHelper.notification_enums_from_string(request.notifications)
)
result = MyTBAHelper.add_subscription(sub, request.device_key)
if result == 200:
output['subscription'] = {"code": 200,
"message": "Subscription updated"}
code += 100
elif result == 304:
output['subscription'] = {"code": 304,
"message": "Subscription already exists"}
code += 304
else:
output['subscription'] = {"code": 500,
"message": "Unknown error adding favorite"}
code += 500
else:
result = MyTBAHelper.remove_subscription(user_id, model_key, model_type, request.device_key)
if result == 200:
output['subscription'] = {"code": 200,
"message": "Subscription removed"}
code += 100
elif result == 404:
output['subscription'] = {"code": 404,
"message": "Subscription not found"}
code += 404
else:
output['subscription'] = {"code": 500,
"message": "Unknown error removing subscription"}
code += 500
return BaseResponse(code=code, message=json.dumps(output))
@endpoints.method(message_types.VoidMessage, SubscriptionCollection,
path='subscriptions/list', http_method='POST',
name='subscriptions.list')
def list_subscriptions(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return SubscriptionCollection(subscriptions=[])
user_id = PushHelper.user_email_to_id(current_user.email())
subscriptions = Subscription.query(ancestor=ndb.Key(Account, user_id)).fetch()
output = []
for subscription in subscriptions:
output.append(SubscriptionMessage(
model_key=subscription.model_key,
notifications=PushHelper.notification_string_from_enums(subscription.notification_types),
model_type=subscription.model_type))
return SubscriptionCollection(subscriptions=output)
@endpoints.method(MediaSuggestionMessage, BaseResponse,
path='team/media/suggest', http_method='POST',
name='team.media.suggestion')
def suggest_team_media(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to make suggestions")
user_id = PushHelper.user_email_to_id(current_user.email())
# For now, only allow team media suggestions
if request.reference_type != "team":
# Trying to suggest a media for an invalid model type
return BaseResponse(code=400, message="Bad model type")
# Need to split deletehash out into its own private dict. Don't want that to be exposed via API...
private_details_json = None
if request.details_json:
incoming_details = json.loads(request.details_json)
private_details = None
if 'deletehash' in incoming_details:
private_details = {'deletehash': incoming_details.pop('deletehash')}
private_details_json = json.dumps(private_details) if private_details else None
status = SuggestionCreator.createTeamMediaSuggestion(
author_account_key=ndb.Key(Account, user_id),
media_url=request.media_url,
team_key=request.reference_key,
year_str=str(request.year),
private_details_json=private_details_json)
if status != 'bad_url':
if status == 'success':
return BaseResponse(code=200, message="Suggestion added")
else:
return BaseResponse(code=304, message="Suggestion already exists")
else:
return BaseResponse(code=400, message="Bad suggestion url")
app = endpoints.api_server([MobileAPI])