async def leave_group(self, request, context): try: leave_member = request.leave_member leave_member_by = request.leave_member_by new_member_status = "removed" if leave_member.id == leave_member_by.id: new_member_status = "leaved" group = GroupService().get_group_info(request.group_id) group_clients = json.loads(group.group_clients) leave_member_in_group = False leave_member_by_in_group = False for e in group_clients: if e['id'] == leave_member.id: leave_member_in_group = True e['status'] = new_member_status if e['id'] == leave_member_by.id: leave_member_by_in_group = True if not leave_member_in_group: raise Exception(Message.LEAVED_MEMBER_NOT_IN_GROUP) if not leave_member_by_in_group and new_member_status == "removed": raise Exception(Message.REMOVER_MEMBER_NOT_IN_GROUP) # update field group_clients first logger.info("New group client: {}".format(group_clients)) group.group_clients = json.dumps(group_clients) group.updated_by = leave_member_by.id group.update() owner_workspace_domain = get_owner_workspace_domain() # wait for service to leave group, and return base response if no exception occured if (group.owner_workspace_domain and group.owner_workspace_domain != owner_workspace_domain): await self.service.leave_group_not_owner( leave_member, leave_member_by, group, ) else: await self.service.leave_group_owner( leave_member, leave_member_by, group, ) return group_messages.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.LEAVE_GROUP_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 add_member(self, request, context): try: group = GroupService().get_group_info(request.group_id) group_clients = json.loads(group.group_clients) added_member_info = request.added_member_info adding_member_info = request.adding_member_info #get workspace is active in group workspace_domains = list(set( [e['workspace_domain'] for e in group_clients if ('status' not in e or ('status' in e and e['status'] in ['active']))] )) logger.info(workspace_domains) #check added and adding member in group adding_member_in_group = False for e in group_clients: if 'status' not in e or e['status'] in ['active']: if e['id'] == added_member_info.id: raise Exception(Message.ADDED_USER_IS_ALREADY_MEMBER) if e['id'] == adding_member_info.id: adding_member_in_group = True if not adding_member_in_group: raise Exception(Message.ADDER_MEMBER_NOT_IN_GROUP) # new group clients new_group_clients = [] is_old_member = False for e in group_clients: if e['id'] == added_member_info.id: e['status'] = 'active' # turn into active member is_old_member = True new_group_clients.append(e) if not is_old_member: added_member_info.status = 'active' new_group_clients.append( MessageToDict( added_member_info, preserving_proto_field_name=True ) ) # update group members first group.group_clients = json.dumps(new_group_clients) group.updated_by = adding_member_info.id group.updated_at = datetime.datetime.now() group.update() owner_workspace_domain = get_owner_workspace_domain() # wait for service to add member to group, and return base response if no exception occured if (group.owner_workspace_domain and group.owner_workspace_domain != owner_workspace_domain): await self.service.add_member_to_group_not_owner( added_member_info, adding_member_info, group, ) else: await self.service.add_member_to_group_owner( added_member_info, adding_member_info, group ) return group_messages.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.ADD_MEMBER_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)