示例#1
0
    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()
示例#2
0
    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)
示例#3
0
    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()
示例#4
0
 async def workspace_notify_deactive_member(self, request, context):
     try:
         await self.service.workspace_notify_deactive_member(
             request.deactive_account_id,
             request.client_ids
         )
         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)
示例#5
0
    async def workspace_leave_group(self, request, context):
        try:
            leave_member = request.leave_member
            leave_member_by = request.leave_member_by
            owner_group = request.owner_group

            # wait for service to call workspace leave group, and return base response if no exception occured
            await self.service.workspace_leave_group(
                leave_member,
                leave_member_by,
                owner_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)
示例#6
0
    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)
示例#7
0
    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()
示例#8
0
    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()
示例#9
0
    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()