Added basic support to chat groups. Closes #23

This commit is contained in:
Manuel Cortez 2019-01-07 17:52:14 -06:00
parent febf9f2c3e
commit d5c975d0b6
3 changed files with 18 additions and 15 deletions

View File

@ -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)) * 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 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)) * 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) ## Changes in version 0.17 (01.01.2019)

View File

@ -926,7 +926,7 @@ class chatBuffer(baseBuffer):
owner_id = r["owner_id"] owner_id = r["owner_id"]
local_attachments += "audio{0}_{1},".format(owner_id, id) local_attachments += "audio{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "voice_message": 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"] id = r["audio_message"]["id"]
owner_id = r["audio_message"]["owner_id"] owner_id = r["audio_message"]["owner_id"]
local_attachments += "audio_message{0}_{1},".format(owner_id, 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. # At the moment we just calculate len(text)_user_id, hope that will work.
random_id = random.randint(0, 100000) random_id = random.randint(0, 100000)
if hasattr(self, "attachments_to_be_sent"): 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: 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: except ValueError as ex:
if ex.code == 9: 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.")) 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."))

View File

@ -378,7 +378,7 @@ class Controller(object):
def search_chat_buffer(self, user_id): def search_chat_buffer(self, user_id):
for i in self.buffers: for i in self.buffers:
if "_messages" in i.name: 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 return None
def chat_from_id(self, user_id, setfocus=True, unread=False): def chat_from_id(self, user_id, setfocus=True, unread=False):
@ -389,10 +389,17 @@ class Controller(object):
self.window.change_buffer(pos) self.window.change_buffer(pos)
return b.tab.text.SetFocus() return b.tab.text.SetFocus()
return 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) 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. # 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: if setfocus:
pos = self.window.search(buffer.name) pos = self.window.search(buffer.name)
self.window.change_buffer(pos) 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. """ 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.""" obj vk_api.longpoll.EventType: an event wich defines some data from the vk's long poll server."""
message = {} message = {}
# If someone else sends a message to the current user. uid = obj.peer_id
if obj.to_me: buffer = self.search_chat_buffer(uid)
buffer = self.search_chat_buffer(obj.user_id) if obj.from_me:
uid = obj.user_id
message.update(out=0) 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 there is no buffer, we must create one in a wxThread so it will not crash.
if buffer == None: if buffer == None:
wx.CallAfter(self.chat_from_id, uid, setfocus=self.session.settings["chat"]["automove_to_conversations"], unread=True) wx.CallAfter(self.chat_from_id, uid, setfocus=self.session.settings["chat"]["automove_to_conversations"], unread=True)
self.session.soundplayer.play("conversation_opened.ogg") self.session.soundplayer.play("conversation_opened.ogg")
return return
# If the chat already exists, let's create a dictionary wich will contains data of the received message. # 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. # 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. # Todo: code improvements. We shouldn't need to request the same message again just for these attachments.
if len(message["attachments"]) != 0: if len(message["attachments"]) != 0: