async def leave_group_not_owner(self, leave_member, leave_member_by, group): # leave group service for group not created in this server for user leave_member, called by leave_member_by logger.info('leave_group_not_owner') owner_workspace_domain = get_owner_workspace_domain() tmp_list_client = json.loads(group.group_clients) # PROCESS IN THIS SERVER # case new member is same server (not owner group) if leave_member.workspace_domain == owner_workspace_domain: # remove group client add more group client key group_client_key = GroupClientKey().get(group.id, leave_member.id) if group_client_key: group_client_key.delete() # remove group with owner group group.delete() # update all group with owner group lst_group = self.model.get_by_group_owner(group.owner_group_id) for group in lst_group: group.group_clients = group.group_clients group.total_member = len(tmp_list_client) group.update() # push notification for member active group_ids = (group.id for group in lst_group) list_clients = GroupClientKey().get_clients_in_groups(group_ids) push_service = NotifyPushService() for client in list_clients: data = { 'client_id': client.GroupClientKey.client_id, 'client_workspace_domain': owner_workspace_domain, 'group_id': str(group.id), 'leave_member': leave_member.id, 'leave_member_display_name': leave_member.display_name, 'leave_member_workspace_domain': leave_member.workspace_domain, 'leave_member_by': leave_member_by.id, 'leave_member_by_display_name': leave_member_by.display_name, 'leave_member_by_workspace_domain': leave_member_by.workspace_domain } logger.info(data) # TODO: maybe handling push to owner await push_service.push_text_to_client( to_client_id=client.GroupClientKey.client_id, title="Member leave", body="user leave group", from_client_id=leave_member_by.id, notify_type="member_leave", data=json.dumps(data) ) # END PROCESS IN THIS SERVER # CALL LEAVE MEMBER TO OWNER SERVER leave_group_request = group_pb2.LeaveGroupRequest( leave_member=leave_member, leave_member_by=leave_member_by, group_id=group.owner_group_id, ) ClientGroup(group.owner_workspace_domain).leave_group(leave_group_request) # for compatible with old code, should be remove in future? return group_pb2.BaseResponse()
def remove_token(self, client_id, device_id): try: NotifyPushService().delete_token(client_id=client_id, device_id=device_id) except Exception as e: logger.info(e) raise Exception(Message.UNAUTHENTICATED)
async def workspace_leave_group(self, leave_member, leave_member_by, group): # workspace called leave group, with additional info about leave_member, leave_member_by, group info logger.info('workspace_leave_group') owner_workspace_domain = get_owner_workspace_domain() tmp_list_client = json.loads(group.group_clients) owner_group_id = None group_this_workspace = self.get_group_info(group.group_id) if group_this_workspace: owner_group_id = group_this_workspace.owner_group_id # update all group with owner group if owner_group_id: lst_group = self.model.get_by_group_owner(owner_group_id) for gr in lst_group: gr.group_clients = group.group_clients gr.total_member = len(tmp_list_client) gr.update() # push notification for member active group_ids = (gr.id for gr in lst_group) list_clients = GroupClientKey().get_clients_in_groups(group_ids) push_service = NotifyPushService() for client in list_clients: data = { 'client_id': client.GroupClientKey.client_id, 'client_workspace_domain': owner_workspace_domain, 'group_id': str(client.GroupClientKey.group_id), 'leave_member': leave_member.id, 'leave_member_display_name': leave_member.display_name, 'leave_member_workspace_domain': leave_member.workspace_domain, 'leave_member_by': leave_member_by.id, 'leave_member_by_display_name': leave_member_by.display_name, 'leave_member_by_workspace_domain': leave_member_by.workspace_domain } logger.info(data) # TODO: maybe handling push to owwner await push_service.push_text_to_client( to_client_id=client.GroupClientKey.client_id, title="Member leave", body="user leave group", from_client_id=leave_member_by.id, notify_type="member_leave", data=json.dumps(data) ) if leave_member.workspace_domain == owner_workspace_domain and leave_member.id == client.GroupClientKey.client_id: client.GroupClientKey.delete() #remove group leave_member_group = self.get_group_info(client.GroupClientKey.group_id) if leave_member_group: leave_member_group.delete() # for compatible with old code, should be remove in future? return group_pb2.BaseResponse()
async def forgot_peer_groups_for_client(self, user_info): # notify all other users involved in peer chat with that this user updated public key client_id = user_info.id lst_group = self.model.get_joined(client_id) owner_workspace_domain = get_owner_workspace_domain() informed_workspace_domain = {} logger.info("start forgot peer group for client {}".format(client_id)) push_service = NotifyPushService() logger.info(lst_group) for group in lst_group: if group.GroupChat.group_type != "peer": continue logger.info("from group {}".format(group.GroupChat.id)) lst_client = json.loads(group.GroupChat.group_clients) for client in lst_client: logger.info("notify to client {}".format(client["id"])) if client["id"] != client_id: if client["workspace_domain"] == owner_workspace_domain: try: data = { 'client_id': client["id"], 'client_workspace_domain': owner_workspace_domain, 'group_id': str(group.GroupChat.id), 'deactive_account_id': client_id } await push_service.push_text_to_client( to_client_id=client["id"], title="Deactivate Member", body="A user has been deactived", from_client_id=client_id, notify_type="deactive_account", data=json.dumps(data) ) except Exception as e: logger.error("Cannot notify to client {}".format(client["id"])) logger.error(e) else: if client["workspace_domain"] not in informed_workspace_domain: informed_workspace_domain[client["workspace_domain"]] = group_pb2.WorkspaceNotifyDeactiveMember( deactive_account_id=client_id ) informed_workspace_domain[client["workspace_domain"]].client_ids.append(client["id"]) for workspace_domain in informed_workspace_domain: try: await ClientGroup(workspace_domain).workspace_notify_deactive_member(informed_workspace_domain[workspace_domain]) except Exception as e: logger.error(e)
async def workspace_notify_deactive_member(self, deactive_account_id, client_ids): # workspace call for notify all other users in different server involved in peer chat with that this user updated public key push_service = NotifyPushService() for client_id in client_ids: try: user_info = User().get(client_id) if user_info is not None: data = { 'client_id': client_id, 'deactive_account_id': deactive_account_id } await push_service.push_text_to_client( to_client_id=client_id, title="Deactivate Member", body="A user has been deactived", from_client_id=deactive_account_id, notify_type="deactive_account", data=json.dumps(data) ) except Exception as e: logger.error(e)
def __init__(self): self.service_group = GroupService() self.push_service = NotifyPushService()
def __init__(self, *kwargs): self.service = NotifyPushService()
class NotifyPushController(BaseController): def __init__(self, *kwargs): self.service = NotifyPushService() @request_logged @auth_required async def register_token(self, request, context): try: header_data = dict(context.invocation_metadata()) introspect_token = KeyCloakUtils.introspect_token(header_data['access_token']) client_id = introspect_token['sub'] device_id = request.device_id token = request.token device_type = request.device_type self.service.register_token(client_id, device_id, device_type, token) return notify_push_pb2.BaseResponse() except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.CLIENT_REGISTER_NOTIFY_TOKEN_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL) @request_logged async def push_text(self, request, context): try: title = request.title body = request.body notify_type = request.notify_type custom_data = request.custom_data to_client_id = request.to_client_id # TODO: not like actually used self.service.push_text_to_client(to_client_id=to_client_id, title=title, body=body, notify_type=notify_type, data=custom_data) return notify_push_pb2.BaseResponse() except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.CLIENT_REGISTER_NOTIFY_TOKEN_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL) @request_logged async def push_voip(self, request, context): try: str_payload = request.payload payload = json.loads(str_payload) to_client_id = request.to_client_id await self.service.push_voip_client(to_client_id=to_client_id, payload=payload) return notify_push_pb2.BaseResponse() except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.CLIENT_REGISTER_NOTIFY_TOKEN_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)
async def edit_message(self, request, context): try: group_id = request.groupId client_id = request.clientId # store message here new_message = MessageService().update_message( group_id=group_id, from_client_id=request.fromClientId, client_id=client_id, message=request.message, message_id=request.id ) # push notification for other client other_clients_in_group = [] if client_id: message_channel = "{}/message".format(client_id) if message_channel in client_message_queue: client_message_queue[message_channel].put(new_message) else: # push text notification for client other_clients_in_group.append(client_id) else: # push for other people in group lst_client_in_groups = GroupClientKey().get_clients_in_group(group_id) for client in lst_client_in_groups: if client.User.id != request.fromClientId: message_channel = "{}/message".format(client.User.id) if message_channel in client_message_queue: client_message_queue[message_channel].put(new_message) else: other_clients_in_group.append(client.User.id) if len(other_clients_in_group) > 0: push_service = NotifyPushService() message = { 'id': new_message.id, 'client_id': new_message.client_id, 'created_at': new_message.created_at, 'updated_at': new_message.updated_at, 'from_client_id': new_message.from_client_id, 'group_id': new_message.group_id, 'group_type': new_message.group_type, 'message': base64.b64encode(new_message.message).decode('utf-8') } await push_service.push_text_to_clients(other_clients_in_group, title="", body="A message has been updated", from_client_id=request.fromClientId, notify_type="edit_message", data=json.dumps(message)) return new_message except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.CLIENT_EDIT_MESSAGE_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)
async def publish_to_group_not_owner(self, request, from_client_id, group): logger.info("publish_to_group_not_owner from client_id {}, group_id={}".format(from_client_id, str(group.id))) owner_workspace_domain = get_owner_workspace_domain() message_id = str(uuid.uuid4()) created_at = datetime.now() message_res_object = message_pb2.MessageObjectResponse( id=message_id, client_id=request.clientId, group_id=group.id, group_type=group.group_type, from_client_id=from_client_id, from_client_workspace_domain=owner_workspace_domain, message=request.message, created_at=int(created_at.timestamp() * 1000), sender_message=request.sender_message ) # publish message to user in this server first lst_client = GroupService().get_clients_in_group_owner(group.owner_group_id) push_service = NotifyPushService() for client in lst_client: for notify_token in client.User.tokens: if client.User is None: continue device_id = notify_token.device_id logger.info('device_id in real loop in handle {}'.format(device_id)) if client.GroupClientKey.client_id == from_client_id and device_id == request.from_client_device_id: continue message_channel = "message/{}/{}".format(client.GroupClientKey.client_id, device_id) new_message_res_object = deepcopy(message_res_object) new_message_res_object.group_id = client.GroupClientKey.group_id new_message_res_object.client_id = client.GroupClientKey.client_id if message_channel in client_message_queue: client_message_queue[message_channel].put(new_message_res_object) else: if new_message_res_object.group_type == 'peer' and new_message_res_object.client_id == from_client_id: logger.info('using sender_message') message_content = base64.b64encode(request.sender_message).decode('utf-8') else: logger.info('using message') message_content = base64.b64encode(new_message_res_object.message).decode('utf-8') message = { 'id': new_message_res_object.id, 'client_id': new_message_res_object.client_id, 'client_workspace_domain': owner_workspace_domain, 'created_at': new_message_res_object.created_at, 'from_client_id': new_message_res_object.from_client_id, 'from_client_workspace_domain': new_message_res_object.from_client_workspace_domain, 'group_id': client.GroupClientKey.group_id, 'group_type': new_message_res_object.group_type, 'message': message_content } await push_service.push_text_to_client_with_device(client.GroupClientKey.client_id, device_id, title="", body="You have a new message", from_client_id=new_message_res_object.from_client_id, notify_type="new_message", data=json.dumps(message), from_client_device_id=request.from_client_device_id) continue # pubish message to owner server request1 = message_pb2.WorkspacePublishRequest( from_client_id=from_client_id, from_client_workspace_domain=owner_workspace_domain, client_id=request.clientId, group_id=group.owner_group_id, group_type=group.group_type, message_id=message_id, message=request.message, created_at=int(created_at.timestamp() * 1000), sender_message=request.sender_message, from_client_device_id=request.from_client_device_id, ) res_object = ClientMessage(group.owner_workspace_domain).workspace_publish_message(request1) if res_object is None: logger.error("send message to client failed") return message_res_object
async def publish_to_group_owner(self, request, from_client_id, group): logger.info("publish_to_group_owner from client_id {}, group_id={}".format(from_client_id, str(group.id))) owner_workspace_domain = get_owner_workspace_domain() # store message here message_id = str(uuid.uuid4()) created_at = datetime.now() message_res_object = MessageService().store_message( message_id=message_id, created_at=created_at, group_id=group.id, group_type=group.group_type, from_client_id=from_client_id, from_client_workspace_domain=owner_workspace_domain, client_id=request.clientId, message=request.message, sender_message=request.sender_message ) lst_client = GroupService().get_clients_in_group(group.id) push_service = NotifyPushService() for client in lst_client: if client.GroupClientKey.client_workspace_domain is None or client.GroupClientKey.client_workspace_domain == owner_workspace_domain: if client.User is None: continue new_message_res_object = deepcopy(message_res_object) new_message_res_object.client_id = client.GroupClientKey.client_id for notify_token in client.User.tokens: device_id = notify_token.device_id logger.info('device_id in real loop in handle {}'.format(device_id)) if client.GroupClientKey.client_id == from_client_id and device_id == request.from_client_device_id: continue message_channel = "message/{}/{}".format(client.GroupClientKey.client_id, device_id) if message_channel in client_message_queue: logger.info('message channel in handle {}'.format(message_channel)) client_message_queue[message_channel].put(new_message_res_object) else: if new_message_res_object.group_type == 'peer' and new_message_res_object.client_id == from_client_id: logger.info('using sender_message') message_content = base64.b64encode(request.sender_message).decode('utf-8') else: logger.info('using message') message_content = base64.b64encode(new_message_res_object.message).decode('utf-8') message = { 'id': new_message_res_object.id, 'client_id': new_message_res_object.client_id, 'client_workspace_domain': owner_workspace_domain, 'created_at': new_message_res_object.created_at, 'from_client_id': new_message_res_object.from_client_id, 'from_client_workspace_domain': owner_workspace_domain, 'group_id': new_message_res_object.group_id, 'group_type': new_message_res_object.group_type, 'message': message_content } await push_service.push_text_to_client_with_device(client.GroupClientKey.client_id, device_id, title="", body="You have a new message", from_client_id=new_message_res_object.from_client_id, notify_type="new_message", data=json.dumps(message), from_client_device_id=request.from_client_device_id) # continue else: # call to other server logger.info('push to client {} in server {}'.format(message_res_object.client_id, client.GroupClientKey.client_workspace_domain)) request2 = message_pb2.WorkspacePublishRequest( from_client_id=message_res_object.from_client_id, from_client_workspace_domain=owner_workspace_domain, client_id=message_res_object.client_id, group_id=client.GroupClientKey.client_workspace_group_id, group_type=message_res_object.group_type, message_id=message_res_object.id, message=message_res_object.message, created_at=message_res_object.created_at, updated_at=message_res_object.updated_at, from_client_device_id=request.from_client_device_id, sender_message=request.sender_message ) message_res_object2 = ClientMessage( client.GroupClientKey.client_workspace_domain).workspace_publish_message(request2) if message_res_object2 is None: logger.error("send message to client failed") return message_res_object
async def workspace_publish(self, request, context): try: logger.info("workspace_publish from client_id {}".format(request.from_client_id)) owner_workspace_domain = get_owner_workspace_domain() group = GroupService().get_group_info(request.group_id) if group.owner_workspace_domain is None or group.owner_workspace_domain == owner_workspace_domain: # store message here MessageService().store_message( message_id=request.message_id, created_at=datetime.now(), group_id=request.group_id, group_type=request.group_type, from_client_id=request.from_client_id, from_client_workspace_domain=request.from_client_workspace_domain, client_id=request.client_id, message=request.message, sender_message=request.sender_message ) new_message = message_pb2.MessageObjectResponse( id=request.message_id, client_id=request.client_id, group_id=request.group_id, group_type=request.group_type, from_client_id=request.from_client_id, from_client_workspace_domain=request.from_client_workspace_domain, message=request.message, created_at=request.created_at, updated_at=request.updated_at, sender_message=request.sender_message ) # push notification for other client lst_client = GroupService().get_clients_in_group(request.group_id) for client in lst_client: if client.GroupClientKey.client_workspace_domain != request.from_client_workspace_domain: if client.GroupClientKey.client_workspace_domain is None or client.GroupClientKey.client_workspace_domain == owner_workspace_domain: if client.User is None: continue for notify_token in client.User.tokens: device_id = notify_token.device_id logger.info('device_id in real loop in handle {}'.format(device_id)) if client.GroupClientKey.client_id == request.from_client_id and device_id == request.from_client_device_id: continue message_channel = "message/{}/{}".format(client.GroupClientKey.client_id, device_id) new_message_res_object = deepcopy(new_message) new_message_res_object.client_id = client.GroupClientKey.client_id if message_channel in client_message_queue: logger.info('message channel in handle {}'.format(message_channel)) client_message_queue[message_channel].put(new_message_res_object) else: if new_message_res_object.group_type == 'peer' and new_message_res_object.client_id == request.from_client_id: logger.info('using sender_message') message_content = base64.b64encode(request.sender_message).decode('utf-8') else: logger.info('using message') message_content = base64.b64encode(new_message_res_object.message).decode('utf-8') push_service = NotifyPushService() message = { 'id': new_message_res_object.id, 'client_id': new_message_res_object.client_id, 'client_workspace_domain': get_owner_workspace_domain(), 'created_at': new_message_res_object.created_at, 'from_client_id': new_message_res_object.from_client_id, 'from_client_workspace_domain': new_message_res_object.from_client_workspace_domain, 'group_id': new_message_res_object.group_id, 'group_type': request.group_type, 'message': message_content } await push_service.push_text_to_client_with_device(client.GroupClientKey.client_id, device_id, title="", body="You have a new message", from_client_id=new_message.from_client_id, notify_type="new_message", data=json.dumps(message), from_client_device_id=request.from_client_device_id) continue else: # call to other server request.group_id = client.GroupClientKey.client_workspace_group_id res_object = ClientMessage( client.GroupClientKey.client_workspace_domain).workspace_publish_message(request) if res_object is None: logger.error("Workspace Publish Message to client failed") return new_message except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.CLIENT_PUBLISH_MESSAGE_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)
async def leave_group_owner(self, leave_member, leave_member_by, group): # leave group service for group created in this server for user leave_member, called by leave_member_by logger.info('leave_group_owner') owner_workspace_domain = get_owner_workspace_domain() # delete group client key and inform member in this server push_service = NotifyPushService() lst_client_in_group = self.get_clients_in_group(group.id) # case group remain only one member if len(lst_client_in_group) == 1 and lst_client_in_group[0].GroupClientKey.client_id == leave_member.id: lst_client_in_group[0].GroupClientKey.delete() group.delete() return group_pb2.BaseResponse() for client in lst_client_in_group: if client.GroupClientKey.client_id == leave_member.id: client.GroupClientKey.delete() if client.GroupClientKey.client_workspace_domain is None or client.GroupClientKey.client_workspace_domain == owner_workspace_domain: if client.GroupClientKey.client_id != leave_member_by.id: data = { 'client_id': client.GroupClientKey.client_id, 'client_workspace_domain': owner_workspace_domain, 'group_id': str(group.id), 'leave_member': leave_member.id, 'leave_member_display_name': leave_member.display_name, 'leave_member_workspace_domain': leave_member.workspace_domain, 'leave_member_by': leave_member_by.id, 'leave_member_by_display_name': leave_member_by.display_name, 'leave_member_by_workspace_domain': leave_member_by.workspace_domain } logger.info(data) # TODO: maybe handling push to owwner await push_service.push_text_to_client( to_client_id=client.GroupClientKey.client_id, title="Member leave", body="user leave group", from_client_id=leave_member_by.id, notify_type="member_leave", data=json.dumps(data) ) # call workspace leave for other server informed_workspace_domain = [] for client in lst_client_in_group: if client.GroupClientKey.client_workspace_domain and client.GroupClientKey.client_workspace_domain != owner_workspace_domain \ and client.GroupClientKey.client_workspace_domain != leave_member_by.workspace_domain: # prevent loop call if client.GroupClientKey.client_workspace_domain in informed_workspace_domain: continue informed_workspace_domain.append(client.GroupClientKey.client_workspace_domain) owner_group_req = group_pb2.GroupInfo( group_id=client.GroupClientKey.client_workspace_group_id, group_name=group.group_name, group_type=group.group_type, group_clients=group.group_clients, group_workspace_domain=owner_workspace_domain, created_by=group.created_by, ) request = group_pb2.WorkspaceLeaveGroupRequest( leave_member=leave_member, leave_member_by=leave_member_by, owner_group=owner_group_req ) logger.info( "call leave member to workspace domain {}".format(client.GroupClientKey.client_workspace_domain)) ClientGroup(client.GroupClientKey.client_workspace_domain).workspace_leave_group(request) # for compatible with old code, should be remove in future? return group_pb2.BaseResponse()
async def workspace_add_member(self, added_member_info, adding_member_info, owner_group_info): # workspace adding member to group, with additional info about adding member, added member and original group logger.info('workspace_add_member') owner_workspace_domain = get_owner_workspace_domain() tmp_list_client = json.loads(owner_group_info.group_clients) is_member_workspace = False ref_group_id = None # create for new member if added_member_info.workspace_domain == owner_workspace_domain: is_member_workspace = True # add group with owner group self.model = GroupChat( owner_group_id=owner_group_info.group_id, owner_workspace_domain=owner_group_info.group_workspace_domain, group_name=owner_group_info.group_name, group_type=owner_group_info.group_type, group_clients=owner_group_info.group_clients, group_rtc_token="", total_member=len(tmp_list_client), created_by=owner_group_info.created_by, ) new_group = self.model.add() ref_group_id = new_group.id # add more group client key group_client_key = GroupClientKey().set_key( new_group.id, added_member_info.id) group_client_key.add() # update all group with owner group lst_group = self.model.get_by_group_owner(owner_group_info.group_id) for group in lst_group: group.group_clients = owner_group_info.group_clients group.total_member = len(tmp_list_client) group.update() # push notification for member active group_ids = (group.id for group in lst_group) list_clients = GroupClientKey().get_clients_in_groups(group_ids) push_service = NotifyPushService() for client in list_clients: data = { 'client_id': client.GroupClientKey.client_id, 'client_workspace_domain': owner_workspace_domain, 'group_id': str(client.GroupClientKey.group_id), 'added_member_id': added_member_info.id, 'added_member_display_name': added_member_info.display_name, 'added_member_workspace_domain': added_member_info.workspace_domain, 'adding_member_id': adding_member_info.id, 'adding_member_display_name': adding_member_info.display_name, 'adding_member_workspace_domain': adding_member_info.workspace_domain } logger.info(data) # TODO: maybe handling push to owwner await push_service.push_text_to_client( to_client_id=client.GroupClientKey.client_id, title="Member Add", body="A user has been added to the group", from_client_id=adding_member_info.id, notify_type="new_member", data=json.dumps(data) ) return group_pb2.AddMemberWorkspaceResponse( is_member_workspace=is_member_workspace, ref_group_id=ref_group_id )
async def add_member_to_group_owner(self, added_member_info, adding_member_info, group): # adding member to group created in this server, with additional info about adding member and added member logger.info('add_member_to_group_owner') owner_workspace_domain = get_owner_workspace_domain() # add more group client key for group owner client_workspace_domain = None if added_member_info.workspace_domain != owner_workspace_domain: client_workspace_domain = added_member_info.workspace_domain group_client_key = GroupClientKey().set_key( group.id, added_member_info.id, client_workspace_domain, added_member_info.ref_group_id).add() # push notification for other member in server lst_client_in_group = self.get_clients_in_group(group.id) push_service = NotifyPushService() for client in lst_client_in_group: if client.GroupClientKey.client_workspace_domain is None or client.GroupClientKey.client_workspace_domain == owner_workspace_domain: if client.GroupClientKey.client_id != adding_member_info.id: data = { 'client_id': client.GroupClientKey.client_id, 'client_workspace_domain': owner_workspace_domain, 'group_id': str(group.id), 'added_member_id': added_member_info.id, 'added_member_display_name': added_member_info.display_name, 'added_member_workspace_domain': owner_workspace_domain, 'adding_member_id': adding_member_info.id, 'adding_member_display_name': adding_member_info.display_name, 'adding_member_workspace_domain': owner_workspace_domain } logger.info(data) # TODO: maybe handling push to owwner await push_service.push_text_to_client( to_client_id=client.GroupClientKey.client_id, title="Member Add", body="A user has been added to the group", from_client_id=adding_member_info.id, notify_type="new_member", data=json.dumps(data) ) # request add member to other server informed_workspace_domain = [] for client in lst_client_in_group: if client.GroupClientKey.client_workspace_domain and client.GroupClientKey.client_workspace_domain != owner_workspace_domain \ and client.GroupClientKey.client_workspace_domain != adding_member_info.workspace_domain: # prevent loop call if client.GroupClientKey.client_workspace_domain in informed_workspace_domain: continue informed_workspace_domain.append(client.GroupClientKey.client_workspace_domain) owner_group_req = group_pb2.GroupInfo( group_id=group.id, group_name=group.group_name, group_type=group.group_type, group_clients=group.group_clients, group_workspace_domain=owner_workspace_domain, created_by=group.created_by, ) request = group_pb2.AddMemberWorkspaceRequest( added_member_info=added_member_info, adding_member_info=adding_member_info, owner_group=owner_group_req ) logger.info( "call add member to workspace domain {}".format(client.GroupClientKey.client_workspace_domain)) response = ClientGroup(client.GroupClientKey.client_workspace_domain).workspace_add_member(request) if response.is_member_workspace: logger.info("update ref_group to main server {}".format(response.ref_group_id)) group_client_key.client_workspace_group_id = response.ref_group_id group_client_key.update() # for compatible with old code, should be remove in future? return group_pb2.BaseResponse()
async def add_member_to_group_not_owner(self, added_member_info, adding_member_info, group): # adding member to group not created in this server, with additional info about adding member and added member logger.info('add_member_to_group_not_owner') owner_workspace_domain = get_owner_workspace_domain() tmp_list_client = json.loads(group.group_clients) # PROCESS IN THIS SERVER # case new member is same server (not owner group) if added_member_info.workspace_domain == owner_workspace_domain: # add group with owner group self.model = GroupChat( owner_group_id=group.owner_group_id, owner_workspace_domain=group.owner_workspace_domain, group_name=group.group_name, group_type=group.group_type, group_clients=group.group_clients, group_rtc_token="", total_member=len(tmp_list_client), created_by=group.created_by, ) new_group = self.model.add() added_member_info.ref_group_id = new_group.id # add more group client key group_client_key = GroupClientKey().set_key( new_group.id, added_member_info.id) group_client_key.add() # update all group with owner group lst_group = self.model.get_by_group_owner(group.owner_group_id) for group in lst_group: group.group_clients = group.group_clients group.total_member = len(tmp_list_client) group.update() # push notification for member active group_ids = (group.id for group in lst_group) list_clients = GroupClientKey().get_clients_in_groups(group_ids) push_service = NotifyPushService() for client in list_clients: data = { 'client_id': client.GroupClientKey.client_id, 'client_workspace_domain': owner_workspace_domain, 'group_id': str(client.GroupClientKey.group_id), 'added_member_id': added_member_info.id, 'added_member_display_name': added_member_info.display_name, 'added_member_workspace_domain': added_member_info.workspace_domain, 'adding_member_id': adding_member_info.id, 'adding_member_display_name': adding_member_info.display_name, 'adding_member_workspace_domain': adding_member_info.workspace_domain } logger.info(data) # TODO: maybe handling push to owwner await push_service.push_text_to_client( to_client_id=client.GroupClientKey.client_id, title="Member Add", body="A user has been added to the group", from_client_id=adding_member_info.id, notify_type="new_member", data=json.dumps(data) ) # END PROCESS IN THIS SERVER # CALL ADD MEMBER TO OWNER SERVER add_member_request = group_pb2.AddMemberRequest( added_member_info=added_member_info, adding_member_info=adding_member_info, group_id=group.owner_group_id, ) ClientGroup(group.owner_workspace_domain).add_member(add_member_request) # for compatible with old code, should be remove in future? return group_pb2.BaseResponse()