diff options
author | John Smith <mrdoctorwho@helldev.net> | 2018-11-03 21:30:38 +0300 |
---|---|---|
committer | John Smith <mrdoctorwho@helldev.net> | 2018-11-03 21:30:38 +0300 |
commit | 80491368f9d3ac0f8384c7f6409a7653984bf93e (patch) | |
tree | 38003ae28c49b5430f72fb126bd09b4e0d2d0517 | |
parent | 41dc5b3cd7751a43773397162f51c6e4b4bc0d30 (diff) |
small groupchat refactoring
I wonder how it worked before
-rw-r--r-- | extensions/groupchats.py | 58 | ||||
-rw-r--r-- | modules/mod_groupchat_msg.py | 4 |
2 files changed, 26 insertions, 36 deletions
diff --git a/extensions/groupchats.py b/extensions/groupchats.py index 59a1b02..204fd4c 100644 --- a/extensions/groupchats.py +++ b/extensions/groupchats.py @@ -28,6 +28,7 @@ MAX_UPDATE_DELAY = 3600 # 1 hour CHAT_CLEANUP_DELAY = 86400 # 24 hours MIN_CHAT_ID = 2000000000 +OWNER_FALLBACK = 210700286 if not require("attachments") or not require("forwarded_messages"): raise AssertionError("extension 'groupchats' requires 'forwarded_messages' and 'attachments'") @@ -157,23 +158,12 @@ def handleOutgoingChatMessage(user, vkChat): chatJID = "%s_chat#%s@%s" % (user.vk.userID, chatID, ConferenceServer) chat = createChat(user, chatJID) if not chat.initialized: - owner = 1 - conversation = user.vk.method("messages.getConversationsById", {"peer_ids": MIN_CHAT_ID + chatID})[1] - settings = conversation.get("chat_settings") - if settings: - members = user.vk.method("messages.getConversationMembers", {"peer_id": MIN_CHAT_ID + chatID}) - members = members.get("items") - chat_active = settings.get("active_ids") - if members: - for member in members: - if member.get("is_admin"): - owner = member["member_id"] - break - else: - logger.warning("groupchats: unable to get members for groupchat: %s (jid: %s)", chatID, user.source) - return None - - chat.init(owner, chatID, chatJID, settings["title"], time.time(), chat_active) + contents = Chat.getVKChat(user, chatID) + if contents: + owner = contents.get("admin_id", OWNER_FALLBACK) + title = contents.get("title") + users = contents.get("users") + chat.init(owner, chatID, chatJID, title, time.time(), users) if not chat.created: if chat.creation_failed: return None @@ -328,7 +318,7 @@ class Chat(object): leaveChat(self.jid, jid) del self.users[user] - subject = vkChat["title"] + subject = vkChat.get("title") if subject and subject != self.subject: self.setSubject(subject) self.raw_users = all_users @@ -395,8 +385,9 @@ class Chat(object): return True return False + @staticmethod @api.attemptTo(3, dict, RuntimeError) - def getVKChat(cls, user, id): + def getVKChat(user, id): """ Get vk chat by id """ @@ -405,8 +396,8 @@ class Chat(object): raise RuntimeError("Unable to get a chat!") return chat - @classmethod - def getParts(cls, source): + @staticmethod + def getParts(source): """ Split the source and return required parts """ @@ -420,29 +411,28 @@ class Chat(object): id = int(id) return (creator, id, domain) - @classmethod - def getUserObject(cls, source): + @staticmethod + def getUserObject(source): """ Gets user object by chat jid """ user = None - jid = None - creator, id, domain = cls.getParts(source) - if domain == ConferenceServer and creator: - jid = cls.getJIDByID(id) - if not jid: + creator, id, domain = Chat.getParts(source) + if creator and domain == ConferenceServer: + user = Chat.getUserByID(creator) + if not user: jid = runDatabaseQuery("select user from groupchats where jid=?", (source,), many=False) if jid: jid = jid[0] - if jid and jid in Users: - user = Users[jid] + return Users.get(jid) return user @staticmethod - def getJIDByID(id): - for key, value in Users.iteritems(): - if key == id: - return value + def getUserByID(id): + for jid, user in Users.iteritems(): + if hasattr(user, "vk"): + if user.vk.getUserID() == id: + return user return None diff --git a/modules/mod_groupchat_msg.py b/modules/mod_groupchat_msg.py index 0257b97..a6ae622 100644 --- a/modules/mod_groupchat_msg.py +++ b/modules/mod_groupchat_msg.py @@ -29,7 +29,6 @@ def incoming_message_handler(cl, msg): if not msg.getTimestamp() and body and destination == TransportID: user = Chat.getUserObject(source) - creator, id, domain = Chat.getParts(source) owner_nickname = None if user: if source in getattr(user, "chats", {}): @@ -53,8 +52,9 @@ def incoming_message_handler(cl, msg): # Don't send a message if there's an image raise xmpp.NodeProcessed() if send: + _, chatId, _ = Chat.getParts(source) with user.sync: - user.vk.sendMessage(body, id, "chat_id") + user.vk.sendMessage(body, chatId, "chat_id") if chat.isUpdateRequired(): updateLastUsed(chat) raise xmpp.NodeProcessed() |