/
conversation_handlers.py
414 lines (338 loc) · 16.5 KB
/
conversation_handlers.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from uuid import uuid4
from psycopg2.extensions import AsIs
import skygear
from skygear.encoding import serialize_record
from skygear.utils import db
from skygear.utils.context import current_user_id
from .conversation import Conversation
from .exc import (ConversationAlreadyExistsException, InvalidArgumentException,
NotAdminConversationException, NotInConversationException,
NotSupportedException, SkygearChatException)
from .hooks import (send_after_conversation_created_hook,
send_after_conversation_deleted_hook,
send_after_conversation_updated_hook,
send_after_users_added_to_conversation_hook,
send_after_users_removed_from_conversation_hook)
from .message import Message
from .pubsub import _publish_record_event
from .roles import RolesHelper
from .user import User
from .user_conversation import UserConversation
from .utils import (_get_container, _get_schema_name,
current_context_has_master_key)
def __validate_user_is_admin(conversation_id):
uc = UserConversation.fetch_one(conversation_id)
if uc is None:
raise NotInConversationException()
if not uc['is_admin']:
raise NotAdminConversationException()
def __validate_conversation(participants):
first_row = None
with db.conn() as conn:
result = conn.execute("""
SELECT c._id
FROM %(schema_name)s.conversation AS c
WHERE c._id IN (
SELECT uc.conversation
FROM %(schema_name)s.user_conversation AS uc
WHERE uc.conversation IN (
SELECT uc.conversation
FROM %(schema_name)s.user_conversation
AS uc
WHERE uc.user IN %(user_ids)s
GROUP BY uc.conversation
HAVING
COUNT(DISTINCT uc.user) = %(count)s)
GROUP BY uc.conversation
HAVING COUNT(DISTINCT uc.user) = %(count)s)
ORDER BY c._created_at DESC
""", {'schema_name': AsIs(_get_schema_name()),
'user_ids': tuple(participants),
'count': len(participants)})
first_row = result.first()
valid = first_row is None
if not valid:
raise ConversationAlreadyExistsException(first_row[0])
def __update_roles(container, users, role, flag):
roles_helper = RolesHelper(container)
roles_helper.set_roles(users, [role], flag)
def __update_participant_roles(container, conversation_id, users, flag):
__update_roles(container,
users,
Conversation.get_participant_role(conversation_id),
flag)
def __update_admin_roles(container, conversation_id, users, flag):
__update_roles(container,
users,
Conversation.get_admin_role(conversation_id),
flag)
def __update_admin_flags(container, conversation_id, user_ids, flag):
for user_id in user_ids:
uc = UserConversation.fetch_one(conversation_id, user_id=user_id)
if uc is None:
c = Conversation.new(conversation_id, user_id)
uc = UserConversation.new(c, user_id)
uc.mark_admin(flag)
def notify_users(record,
unchanged_participants,
old_participants,
new_participants):
for each_user in unchanged_participants:
_publish_record_event(each_user,
"conversation",
"update",
record)
for each_user in old_participants:
_publish_record_event(each_user,
"conversation",
"delete",
record)
for each_user in new_participants:
_publish_record_event(each_user,
"conversation",
"create",
record)
def handle_conversation_before_delete(record, conn):
raise NotSupportedException("Call chat:delete_conversation instead.")
def handle_leave_conversation(conversation_id):
uc = UserConversation.fetch_one(conversation_id)
if uc is None:
raise NotInConversationException()
uc.delete()
return {'status': 'OK'}
def handle_add_participants(conversation_id,
participant_ids,
is_first_time=False):
existing_participants = []
for participant in participant_ids:
uc = UserConversation.fetch_one(conversation_id,
user_id=participant)
if uc is not None:
existing_participants.append(participant)
participant_ids = [x for x in participant_ids
if x not in existing_participants]
container = _get_container()
__update_participant_roles(container,
conversation_id,
participant_ids, True)
conversation = Conversation.new(conversation_id, current_user_id())
for participant_id in participant_ids:
UserConversation.new(conversation, participant_id).save()
serialized_conversation = serialize_record(conversation)
if not is_first_time:
conversation.mark_non_distinct()
send_after_users_added_to_conversation_hook(serialized_conversation,
existing_participants,
participant_ids)
conversation = Conversation.fetch_one(conversation_id)
existing_participants = conversation['participant_ids']
notify_users(conversation,
existing_participants,
[],
participant_ids)
return {'conversation': serialized_conversation}
def handle_remove_participants(conversation_id, participant_ids):
container = _get_container()
__update_participant_roles(container,
conversation_id,
participant_ids,
False)
conversation = Conversation.new(conversation_id, current_user_id())
ucs = [UserConversation.new(conversation, participant_id)
for participant_id in participant_ids]
UserConversation.delete_all(ucs)
conversation.mark_non_distinct()
conversation = Conversation.fetch_one(conversation_id, with_uc=False)
serialized_conversation = serialize_record(conversation)
notify_users(conversation,
conversation['participant_ids'],
participant_ids,
[])
new_participant_ids = serialized_conversation['participant_ids']
send_after_users_removed_from_conversation_hook(serialized_conversation,
new_participant_ids,
participant_ids)
return {'conversation': serialized_conversation}
def handle_admins_lambda(conversation_id, admin_ids, flag):
container = _get_container()
__update_admin_roles(container, conversation_id, admin_ids, flag)
__update_admin_flags(container, conversation_id, admin_ids, flag)
r = Conversation.fetch_one(conversation_id)
return {'conversation': serialize_record(r)}
def check_if_context_has_master_key(msg):
if not current_context_has_master_key():
raise NotSupportedException(msg)
def register_conversation_hooks(settings):
@skygear.before_save("conversation", async=False)
def conversation_before_save_handler(record, original_record, conn):
if original_record is None:
check_if_context_has_master_key(
"Call chat:create_conversation instead")
disallowed_keys = ['last_read_message_ref',
'last_message',
'admin_ids',
'last_read_message',
'unread_count',
'last_message_ref',
'participant_ids']
for key in disallowed_keys:
if key in record:
del record[key]
@skygear.after_save('conversation', async=True)
def conversation_after_save_handler(record, original_record, db):
if original_record is not None:
conversation = Conversation.fetch_one(original_record.id.key)
if not Conversation.equal_record(record, original_record):
serialized_conversation = serialize_record(conversation)
participants = serialized_conversation['participant_ids']
send_after_conversation_updated_hook(serialized_conversation,
participants)
@skygear.before_delete("conversation", async=False)
def conversation_before_delete_handler(record, conn):
check_if_context_has_master_key(
"Deleting a conversation is not supported")
def __get_messsage_refs_from_conversation(conversation):
refs = []
last_message_ref = conversation.get('last_message_ref', None)
if last_message_ref is not None:
refs.append(last_message_ref)
last_read_message_ref = conversation.get('last_read_message_ref', None)
if last_read_message_ref is not None:
refs.append(last_read_message_ref)
return refs
def __update_conversation_messages(conversation, messages):
last_message_ref = conversation.get('last_message_ref')
last_read_message_ref = conversation.get('last_read_message_ref')
if last_message_ref is not None:
key = last_message_ref.recordID.key
conversation['last_message'] = messages[key]
if last_read_message_ref is not None:
key = last_read_message_ref.recordID.key
conversation['last_read_message'] = messages[key]
return conversation
def handle_get_conversation_lambda(conversation_id, include_last_message):
conversation = Conversation.fetch_one(conversation_id)
if conversation is None:
raise SkygearChatException("Conversation not found.")
if include_last_message:
message_refs = __get_messsage_refs_from_conversation(conversation)
messages = Message.fetch_all(message_refs)
messages = {message.id.key: serialize_record(message)
for message in messages}
conversation = __update_conversation_messages(conversation, messages)
return {'conversation': serialize_record(conversation)}
def handle_get_conversations_lambda(page,
page_size,
include_last_message,
order='desc'):
result = Conversation.fetch_all_with_paging(page, page_size, order)
if include_last_message:
messages = {}
message_refs = []
for conversation in result:
message_refs = message_refs +\
__get_messsage_refs_from_conversation(conversation)
messages = Message.fetch_all(message_refs)
messages = {message.id.key: serialize_record(message)
for message in messages}
n = len(result)
for i in range(0, n):
result[i] = __update_conversation_messages(
result[i], messages)
return {"conversations": [serialize_record(r) for r in result]}
def handle_delete_conversation_lambda(conversation_id):
__validate_user_is_admin(conversation_id)
conversation = Conversation.fetch_one(conversation_id)
handle_admins_lambda(conversation_id, conversation['admin_ids'], False)
handle_remove_participants(conversation_id,
conversation['participant_ids'])
serialized_conversation = serialize_record(conversation)
participants = list(set(serialized_conversation['admin_ids'] +
serialized_conversation['participant_ids']))
send_after_conversation_deleted_hook(serialized_conversation, participants)
return None
def handle_create_conversation_lambda(participants, title, meta, options):
try:
participants = [p if isinstance(p, str) else
User.deserialize(p).id.key for p in participants]
except Exception:
raise InvalidArgumentException('Invalid users.', participants)
if options is None:
options = {}
user_id = current_user_id()
admins = [user_id]
admins_from_options = [a[5:] if a[:5] == 'user/'
else a for a in options.get('adminIDs', [])]
admins = list(set(admins + admins_from_options))
participants = list(set(participants + admins))
is_distinct = options.get('distinctByParticipants', False)
if is_distinct:
__validate_conversation(participants)
conversation_id = str(uuid4())
conversation = Conversation.new(conversation_id, user_id)
conversation['title'] = title
conversation['distinct_by_participants'] = is_distinct
conversation['metadata'] = meta
conversation.save()
conversation['admin_ids'] = []
conversation['participant_ids'] = []
handle_add_participants(conversation_id, participants, True)
handle_admins_lambda(conversation_id, admins, True)
conversation['admin_ids'] = admins
participants = list(set(participants + admins))
conversation['participant_ids'] = list(set(participants + admins))
serialized_conversation = serialize_record(conversation)
send_after_conversation_created_hook(serialized_conversation,
participants)
return {'conversation': serialized_conversation}
def register_conversation_lambdas(settings):
@skygear.op("chat:leave_conversation",
auth_required=True, user_required=True)
def leave_conversation_lambda(conversation_id):
return handle_leave_conversation(conversation_id)
@skygear.op("chat:add_participants",
auth_required=True, user_required=True)
def add_participants_lambda(conversation_id, participants):
__validate_user_is_admin(conversation_id)
return handle_add_participants(conversation_id, participants)
@skygear.op("chat:remove_participants",
auth_required=True, user_required=True)
def remove_participants_lambda(conversation_id, participants):
__validate_user_is_admin(conversation_id)
return handle_remove_participants(conversation_id, participants)
@skygear.op("chat:add_admins",
auth_required=True, user_required=True)
def add_admins_lambda(conversation_id, admins):
__validate_user_is_admin(conversation_id)
return handle_admins_lambda(conversation_id, admins, True)
@skygear.op("chat:remove_admins",
auth_required=True, user_required=True)
def remove_admins_lambda(conversation_id, admins):
__validate_user_is_admin(conversation_id)
return handle_admins_lambda(conversation_id, admins, False)
@skygear.op("chat:get_conversations",
auth_required=True, user_required=True)
def get_conversations_lambda(page,
page_size,
include_last_message,
order='desc'):
return handle_get_conversations_lambda(page,
page_size,
include_last_message,
order)
@skygear.op("chat:get_conversation",
auth_required=True, user_required=True)
def get_conversation_lambda(conversation_id, include_last_message):
return handle_get_conversation_lambda(conversation_id,
include_last_message)
@skygear.op("chat:create_conversation",
auth_required=True, user_required=True)
def create_conversation_lambda(participants, title, meta, options):
return handle_create_conversation_lambda(participants,
title,
meta,
options)
@skygear.op("chat:delete_conversation",
auth_required=True, user_required=True)
def delete_conversation_lambda(conversation_id):
return handle_delete_conversation_lambda(conversation_id)