diff options
author | John Smith <mrdoctorwho@gmail.com> | 2016-11-17 15:48:56 +0300 |
---|---|---|
committer | John Smith <mrdoctorwho@gmail.com> | 2016-11-17 15:48:56 +0300 |
commit | d77546bb403578bf4b4af722775c3eb9e40b1ecf (patch) | |
tree | 633a4b55f91a520437c5e475173ae9cd4f427e26 | |
parent | 8f370bfda4ba731ae3a76a4ce04a03882167cb7a (diff) |
Update chat's last_used field on a presence
-rw-r--r-- | extensions/groupchats.py | 41 | ||||
-rw-r--r-- | modules/mod_groupchat_msg.py | 6 | ||||
-rw-r--r-- | modules/mod_groupchat_prs.py | 8 |
3 files changed, 50 insertions, 5 deletions
diff --git a/extensions/groupchats.py b/extensions/groupchats.py index 53550a9..3edd230 100644 --- a/extensions/groupchats.py +++ b/extensions/groupchats.py @@ -26,6 +26,8 @@ mod_groupchat_prs for presence handling mod_groupchat_msg for message handling """ +MAX_UPDATE_DELAY = 3600 + if not require("attachments") or not require("forwarded_messages"): raise AssertionError("extension 'groupchats' requires 'forwarded_messages' and 'attachments'") @@ -193,6 +195,7 @@ class Chat(object): self.creation_failed = False self.raw_users = {} self.users = {} + self.last_update = 0 def init(self, owner, id, jid, topic, date, users=[]): """ @@ -348,6 +351,19 @@ class Chat(object): # TODO: We repeat it twice on each message. We shouldn't. self.handleMessage(user, vkChat, False) + def isUpdateRequired(self): + """ + Tells whether it's required to update the chat's last_used time + Returns: + True if required + """ + if not self.last_update: + return True + if (time.time() - self.last_update) > MAX_UPDATE_DELAY: + return True + return False + + @api.attemptTo(3, dict, RuntimeError) def getVKChat(cls, user, id): """ @@ -397,11 +413,29 @@ class Chat(object): def createFakeChat(user, source): + """ + Creates a fake chat to use it before we actually know that it exists + Args: + user: the User object + source: the chat's jid + """ if not hasattr(user, "chats"): user.chats = {} if source not in user.chats: user.chats[source] = chat = Chat() chat.invited = True # the user has joined themselves and we don't need to intvite them + else: + chat = user.chats[source] + return chat + + +def updateLastUsed(chat): + """ + Updates the last_used field in the database + Args: + chat: the Chat object + """ + runDatabaseQuery("update groupchats set last_used=? where jid=?", (time.time(), chat.source), set=True) def exterminateChats(user=None, chats=[]): @@ -410,6 +444,13 @@ def exterminateChats(user=None, chats=[]): The chats argument must be a list of tuples """ def exterminated(cl, stanza, jid): + """ + The callback that's being called when the stanza we sent's got an answer + Args: + cl: the xmpp.Client object + stanza: the result stanza + jid: the jid stanza's sent from (?) + """ chat = stanza.getFrom().getStripped() if xmpp.isResultNode(stanza): logger.debug("groupchats: target exterminated! Yay! target:%s (jid: %s)" % (chat, jid)) diff --git a/modules/mod_groupchat_msg.py b/modules/mod_groupchat_msg.py index 23223f0..77b9898 100644 --- a/modules/mod_groupchat_msg.py +++ b/modules/mod_groupchat_msg.py @@ -30,7 +30,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) - send = False owner_nickname = None if user: if source in getattr(user, "chats", {}): @@ -43,7 +42,7 @@ def incoming_message_handler(cl, msg): # If we don't and nick (as in settings) is tied to the chat, then we can determine who sent the message send = ((nick == owner_nickname and user.settings.tie_chat_to_nickname) or user.settings.force_vk_date_group) - createFakeChat(user, source) + chat = createFakeChat(user, source) if html and html.getTag("body"): logger.debug("groupchats: fetched xhtml image (jid: %s)" % source) try: @@ -56,7 +55,8 @@ def incoming_message_handler(cl, msg): if send: with user.sync: user.vk.sendMessage(body, id, "chat_id") - runDatabaseQuery("update groupchats set last_used=? where jid=?", (time.time(), source), set=True) + if chat.isUpdateRequired(): + updateLastUsed(chat) raise xmpp.NodeProcessed() diff --git a/modules/mod_groupchat_prs.py b/modules/mod_groupchat_prs.py index dd1954f..068e0a2 100644 --- a/modules/mod_groupchat_prs.py +++ b/modules/mod_groupchat_prs.py @@ -56,8 +56,8 @@ def handleChatPresences(source, prs): """ Makes the old users leave Args: - * source: stanza source - * prs: xmpp.Presence object + source: stanza source + prs: xmpp.Presence object """ jid = prs.getJid() or "" if "@" in jid: @@ -76,6 +76,10 @@ def handleChatPresences(source, prs): if jid != TransportID: runDatabaseQuery("update groupchats set owner=? where jid=?", (source, jid), set=True) + if chat.isUpdateRequired(): + updateLastUsed(chat) + + # TODO: don't rewrite it every time we get a presence if jid.split("/")[0] == user.source: chat.owner_nickname = prs.getFrom().getResource() runDatabaseQuery("update groupchats set nick=? where jid=? ", (chat.owner_nickname, source), set=True) |