From d5c975d0b6099118c595d2273674da388d3d72bc Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Mon, 7 Jan 2019 17:52:14 -0600 Subject: [PATCH] Added basic support to chat groups. Closes #23 --- changelog.md | 1 + src/controller/buffers.py | 6 +++--- src/controller/mainController.py | 26 ++++++++++++++------------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/changelog.md b/changelog.md index d69613d..9ffd7bf 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ * It is possible to use user domains (short names for users) to create timelines. Just write @username and the program will create the needed timeline, regardless if the user is present in your friend list. ([#18,](https://code.manuelcortez.net/manuelcortez/socializer/issues/18)) * When displaying someone's profile, the dialog should be loaded dramatically faster than before. A message will be spoken when all data of the profile is loaded. * When opening a timeline, if the current user is not allowed to do it, an error message should be displayed and the buffer will not be created. Before the buffer was partially created in the main window. ([#22.](https://code.manuelcortez.net/manuelcortez/socializer/issues/22)) +* Added basic support to handle group chats. At the current moment it is possible to receive and reply to chat messages only. Chat groups will be placed inside the conversations section. ([#23.](https://code.manuelcortez.net/manuelcortez/socializer/issues/23)) ## Changes in version 0.17 (01.01.2019) diff --git a/src/controller/buffers.py b/src/controller/buffers.py index 179ae2a..73465e5 100644 --- a/src/controller/buffers.py +++ b/src/controller/buffers.py @@ -926,7 +926,7 @@ class chatBuffer(baseBuffer): owner_id = r["owner_id"] local_attachments += "audio{0}_{1},".format(owner_id, id) elif i["from"] == "local" and i["type"] == "voice_message": - r = uploader.audio_message(i["file"], peer_id=self.kwargs["user_id"]) + r = uploader.audio_message(i["file"], peer_id=self.kwargs["peer_id"]) id = r["audio_message"]["id"] owner_id = r["audio_message"]["owner_id"] local_attachments += "audio_message{0}_{1},".format(owner_id, id) @@ -942,9 +942,9 @@ class chatBuffer(baseBuffer): # At the moment we just calculate len(text)_user_id, hope that will work. random_id = random.randint(0, 100000) if hasattr(self, "attachments_to_be_sent"): - response = self.session.vk.client.messages.send(user_id=self.kwargs["user_id"], message=text, attachment=self.attachments_to_be_sent, random_id=random_id) + response = self.session.vk.client.messages.send(peer_id=self.kwargs["peer_id"], message=text, attachment=self.attachments_to_be_sent, random_id=random_id) else: - response = self.session.vk.client.messages.send(user_id=self.kwargs["user_id"], message=text, random_id=random_id) + response = self.session.vk.client.messages.send(peer_id=self.kwargs["peer_id"], message=text, random_id=random_id) except ValueError as ex: if ex.code == 9: output.speak(_("You have been sending a message that is already sent. Try to update the buffer if you can't see the new message in the history.")) diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 5b7b240..37d06c4 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -378,7 +378,7 @@ class Controller(object): def search_chat_buffer(self, user_id): for i in self.buffers: if "_messages" in i.name: - if "user_id" in i.kwargs and i.kwargs["user_id"] == user_id: return i + if "peer_id" in i.kwargs and i.kwargs["peer_id"] == user_id: return i return None def chat_from_id(self, user_id, setfocus=True, unread=False): @@ -389,10 +389,17 @@ class Controller(object): self.window.change_buffer(pos) return b.tab.text.SetFocus() return - buffer = buffers.chatBuffer(parent=self.window.tb, name="{0}_messages".format(user_id,), composefunc="render_message", session=self.session, count=200, user_id=user_id, rev=0, extended=True, fields="id, user_id, date, read_state, out, body, attachments, deleted") + buffer = buffers.chatBuffer(parent=self.window.tb, name="{0}_messages".format(user_id,), composefunc="render_message", session=self.session, count=200, peer_id=user_id, rev=0, extended=True, fields="id, user_id, date, read_state, out, body, attachments, deleted") self.buffers.append(buffer) + # Get name based in the ID. + # for users. + if user_id > 0 and user_id < 2000000000: + name = _("Chat with {0}").format(self.session.get_user_name(user_id, "ins")) + elif user_id > 2000000000: + chat = self.session.vk.client.messages.getChat(chat_id=user_id-2000000000) + name = _("Chat in {chat_name}").format(chat_name=chat["title"],) # Translators: {0} will be replaced with an user. - self.window.insert_buffer(buffer.tab, _("Chat with {0}").format(self.session.get_user_name(user_id, "ins")), self.window.search("chats")) + self.window.insert_buffer(buffer.tab, name, self.window.search("chats")) if setfocus: pos = self.window.search(buffer.name) self.window.change_buffer(pos) @@ -420,22 +427,17 @@ class Controller(object): """ Searches or creates a chat buffer with the id of the user that is sending or receiving a message. obj vk_api.longpoll.EventType: an event wich defines some data from the vk's long poll server.""" message = {} - # If someone else sends a message to the current user. - if obj.to_me: - buffer = self.search_chat_buffer(obj.user_id) - uid = obj.user_id + uid = obj.peer_id + buffer = self.search_chat_buffer(uid) + if obj.from_me: message.update(out=0) - # If the current user sends a message to someone else. - else: - buffer = self.search_chat_buffer(obj.peer_id) - uid = obj.peer_id # If there is no buffer, we must create one in a wxThread so it will not crash. if buffer == None: wx.CallAfter(self.chat_from_id, uid, setfocus=self.session.settings["chat"]["automove_to_conversations"], unread=True) self.session.soundplayer.play("conversation_opened.ogg") return # If the chat already exists, let's create a dictionary wich will contains data of the received message. - message.update(id=obj.message_id, user_id=uid, date=obj.timestamp, body=obj.text, attachments=obj.attachments, read_state=0) + message.update(id=obj.message_id, user_id=uid, date=obj.timestamp, body=obj.text, attachments=obj.attachments) # if attachments is true, let's request for the full message with attachments formatted in a better way. # Todo: code improvements. We shouldn't need to request the same message again just for these attachments. if len(message["attachments"]) != 0: