Esempio n. 1
0
 def __removeTmpIgnored(self, dbID, forced = False):
     if not forced:
         error = self._checkCooldown(CLIENT_ACTION_ID.REMOVE_IGNORED)
         if error:
             return (False, error)
     contact = self.usersStorage.getUser(dbID, PROTO_TYPE.XMPP)
     if not contact:
         return (False, ClientContactError(CONTACT_ERROR_ID.CONTACT_ITEM_NOT_FOUND))
     itemType = contact.getItemType()
     if itemType not in XMPP_ITEM_TYPE.TMP_BLOCKING_LIST:
         return (False, ClientContactError(CONTACT_ERROR_ID.BLOCK_ITEM_NOT_FOUND, contact.getFullName()))
     jid = contact.getJID()
     tasks = [block_tasks.RemoveTmpBlockItemTask(jid, contact.getName())]
     if not forced:
         self.__cooldown.process(CLIENT_ACTION_ID.REMOVE_IGNORED)
     return self.__addTasks(CLIENT_ACTION_ID.REMOVE_IGNORED, jid, *tasks)
    def addFriend(self, dbID, name, group=None):
        error = self._checkCooldown(CLIENT_ACTION_ID.ADD_FRIEND)
        if error:
            return (False, error)
        else:
            if group:
                if not self.usersStorage.isGroupExists(group):
                    return (False,
                            ClientContactError(
                                CONTACT_ERROR_ID.GROUP_NOT_FOUND, group))
                groups = {group}
            else:
                groups = None
            contact = self.usersStorage.getUser(dbID, PROTO_TYPE.XMPP)
            tasks, itemType = [], XMPP_ITEM_TYPE.EMPTY_ITEM
            if contact:
                if contact.isCurrentPlayer():
                    return (False,
                            ClientActionError(CLIENT_ACTION_ID.ADD_FRIEND,
                                              CLIENT_ERROR_ID.GENERIC))
                jid = contact.getJID()
                itemType = contact.getItemType()
                if itemType in XMPP_ITEM_TYPE.ROSTER_ITEMS:
                    return (False,
                            ClientContactError(
                                CONTACT_ERROR_ID.ROSTER_ITEM_EXISTS,
                                contact.getFullName()))
                subTo = contact.getSubscription()[0]
            else:
                jid = makeContactJID(dbID)
                subTo = _SUB.OFF
            result, error = self.__subsRestrictions.canAddFriends()
            if not result:
                return (False, error)
            if itemType == XMPP_ITEM_TYPE.TMP_BLOCK_ITEM:
                tasks.append(block_tasks.RemoveTmpBlockItemTask(jid, name))
                tasks.append(roster_tasks.AddRosterItemTask(jid, name, groups))
            elif itemType == XMPP_ITEM_TYPE.ROSTER_TMP_BLOCK_ITEM:
                tasks.append(block_tasks.RemoveTmpBlockItemTask(jid, name))
            elif itemType == XMPP_ITEM_TYPE.BLOCK_ITEM:
                tasks.append(block_tasks.RemoveBlockItemTask(jid, name))
                tasks.append(roster_tasks.AddRosterItemTask(jid, name, groups))
            elif itemType == XMPP_ITEM_TYPE.ROSTER_BLOCK_ITEM:
                tasks.append(block_tasks.RemoveBlockItemTask(jid, name))
                task, exclude = None, set()
                rosterGroups = contact.getItem().getRosterGroups()
                for rosterGroup in rosterGroups:
                    if self.usersStorage.isGroupEmpty(rosterGroup):
                        exclude.add(rosterGroup)

                if groups:
                    if groups != exclude:
                        task = roster_tasks.ChangeRosterItemGroupsTask(
                            jid, name, groups, exclude)
                elif rosterGroups:
                    task = roster_tasks.ChangeRosterItemGroupsTask(
                        jid, name, set(), exclude)
                if task:
                    tasks.append(task)
            elif itemType in XMPP_ITEM_TYPE.SUB_PENDING_ITEMS:
                if itemType == XMPP_ITEM_TYPE.SUB_PENDING_TMP_BLOCK_ITEM:
                    tasks.append(block_tasks.RemoveTmpBlockItemTask(jid, name))
                tasks.append(sub_tasks.ApproveSubscriptionTask(jid, name))
                if groups:
                    tasks.append(
                        roster_tasks.ChangeRosterItemGroupsTask(
                            jid, name, groups))
            else:
                tasks.append(roster_tasks.AddRosterItemTask(jid, name, groups))
            if subTo == _SUB.OFF:
                tasks.append(sub_tasks.AskSubscriptionTask(jid))
            self.__cooldown.process(CLIENT_ACTION_ID.ADD_FRIEND)
            return self.__addTasks(CLIENT_ACTION_ID.ADD_FRIEND, jid, *tasks)