def addFriend(self, dbID, name, group=None, shadowMode=False): error = self._checkCooldown(CLIENT_ACTION_ID.ADD_FRIEND) if error and not shadowMode: 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.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: 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)) if not shadowMode: self.__cooldown.process(CLIENT_ACTION_ID.ADD_FRIEND) return self.__addTasks(CLIENT_ACTION_ID.ADD_FRIEND, jid, shadowMode, *tasks)
def moveFriendToGroup(self, dbID, include=None, exclude=None): error = self.__checkCooldown(CLIENT_ACTION_ID.CHANGE_GROUP) 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)) if contact.getItemType() != XMPP_ITEM_TYPE.ROSTER_ITEM: return (False, ClientContactError(CONTACT_ERROR_ID.ROSTER_ITEM_NOT_FOUND, contact.getFullName())) groups = contact.getGroups() if include: if not self.usersStorage.isGroupExists(include): return (False, ClientContactError(CONTACT_ERROR_ID.GROUP_NOT_FOUND, include)) groups.add(include) if exclude: if not self.usersStorage.isGroupExists(exclude): return (False, ClientContactError(CONTACT_ERROR_ID.GROUP_NOT_FOUND, exclude)) groups.discard(exclude) jid = contact.getJID() self.__cooldown.process(CLIENT_ACTION_ID.CHANGE_GROUP) return self.__addTasks( CLIENT_ACTION_ID.CHANGE_GROUP, jid, roster_tasks.ChangeRosterItemGroupsTask( jid, contact.getName(), groups, {exclude} if exclude else None))