View friends and send chats to them with a button in the buffer

This commit is contained in:
Manuel Cortez 2016-05-25 11:33:57 -05:00
parent 2b44c72999
commit 1252b7feac
6 changed files with 68 additions and 33 deletions

View File

@ -288,4 +288,17 @@ class chatBuffer(baseBuffer):
def send_chat_to_user(self, *args, **kwargs): def send_chat_to_user(self, *args, **kwargs):
text = self.tab.text.GetValue() text = self.tab.text.GetValue()
if text == "": return if text == "": return
response = self.session.vk.client.messages.send(user_id=self.kwargs["user_id"], message=text) response = self.session.vk.client.messages.send(user_id=self.kwargs["user_id"], message=text)
class peopleBuffer(feedBuffer):
def create_tab(self, parent):
self.tab = home.peopleTab(parent)
def connect_events(self):
super(peopleBuffer, self).connect_events()
widgetUtils.connect_event(self.tab.new_chat, widgetUtils.BUTTON_PRESSED, self.new_chat)
def new_chat(self, *args, **kwargs):
user_id = self.session.db[self.name]["items"][self.tab.list.get_selected()]["id"]
pub.sendMessage("new-chat", user_id=user_id)

View File

@ -13,10 +13,10 @@ class worker(threading.Thread):
while self.session.is_logged == True: while self.session.is_logged == True:
p = self.l.check() p = self.l.check()
for i in p: for i in p:
print i.message_id, i.flags, i.from_id, i.user_id, i.mask, i.byself, i.message_flags # print i.message_id, i.flags, i.from_id, i.user_id, i.mask, i.byself, i.message_flags
# if i.flags == 4 or i.flags == 51 or i.flags == 49: # if i.flags == 4 or i.flags == 51 or i.flags == 49:
if i.text != None and i.from_id != None and i.flags != None and i.message_flags != None: if i.text != None and i.from_id != None and i.flags != None and i.message_flags != None:
print i.message_id, i.flags, i.from_id, i.user_id, i.mask, i.byself, i.message_flags # print i.message_id, i.flags, i.from_id, i.user_id, i.mask, i.byself, i.message_flags
# if i.from_id != None: # if i.from_id != None:
print "ordering sent stuff" # print "ordering sent stuff"
pub.sendMessage("order-sent-message", obj=i) pub.sendMessage("order-sent-message", obj=i)

View File

@ -76,6 +76,12 @@ class Controller(object):
r_audio = buffers.audioBuffer(parent=self.window.tb, name="recommended_audio", composefunc="compose_audio", session=self.session, endpoint="getRecommendations", parent_endpoint="audio", full_list=True, count=self.session.settings["buffers"]["count_for_audio_buffers"]) r_audio = buffers.audioBuffer(parent=self.window.tb, name="recommended_audio", composefunc="compose_audio", session=self.session, endpoint="getRecommendations", parent_endpoint="audio", full_list=True, count=self.session.settings["buffers"]["count_for_audio_buffers"])
self.buffers.append(r_audio) self.buffers.append(r_audio)
self.window.insert_buffer(r_audio.tab, _(u"Recommendations"), self.window.search("audios")) self.window.insert_buffer(r_audio.tab, _(u"Recommendations"), self.window.search("audios"))
people = buffers.empty(parent=self.window.tb, name="people")
self.buffers.append(people)
self.window.add_buffer(people.tab, _(u"People"))
friends = buffers.peopleBuffer(parent=self.window.tb, name="friends_", composefunc="compose_person", session=self.session, endpoint="get", parent_endpoint="friends", count=5000, fields="uid, first_name, last_name, last_seen")
self.buffers.append(friends)
self.window.insert_buffer(friends.tab, _(u"Friends"), self.window.search("people"))
chats = buffers.empty(parent=self.window.tb, name="chats") chats = buffers.empty(parent=self.window.tb, name="chats")
self.buffers.append(chats) self.buffers.append(chats)
self.window.add_buffer(chats.tab, _(u"Chats")) self.window.add_buffer(chats.tab, _(u"Chats"))
@ -91,6 +97,7 @@ class Controller(object):
pub.subscribe(self.play_audios, "play-audios") pub.subscribe(self.play_audios, "play-audios")
pub.subscribe(self.view_post, "open-post") pub.subscribe(self.view_post, "open-post")
pub.subscribe(self.update_status_bar, "update-status-bar") pub.subscribe(self.update_status_bar, "update-status-bar")
pub.subscribe(self.chat_from_id, "new-chat")
widgetUtils.connect_event(self.window, widgetUtils.CLOSE_EVENT, self.exit) widgetUtils.connect_event(self.window, widgetUtils.CLOSE_EVENT, self.exit)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.update_buffer, menuitem=self.window.update_buffer) widgetUtils.connect_event(self.window, widgetUtils.MENU, self.update_buffer, menuitem=self.window.update_buffer)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.check_for_updates, menuitem=self.window.check_for_updates) widgetUtils.connect_event(self.window, widgetUtils.MENU, self.check_for_updates, menuitem=self.window.check_for_updates)
@ -251,31 +258,6 @@ class Controller(object):
return return
self.window.insert_buffer(buffer.tab, name_, position) self.window.insert_buffer(buffer.tab, name_, position)
def new_chat(self, *args, **kwargs):
b = self.get_current_buffer()
if not hasattr(b, "get_users"):
b = self.search("home_timeline")
d = []
for i in self.session.db["users"]:
d.append((i, self.session.get_user_name(i)))
for i in self.session.db["groups"]:
d.append((-i, self.session.get_user_name(-i)))
a = timeline.timelineDialog([i[1] for i in d])
if a.get_response() == widgetUtils.OK:
user = a.get_user()
buffertype = a.get_buffer_type()
user_id = ""
for i in d:
if i[1] == user:
user_id = i[0]
if user_id == None:
commonMessages.no_user_exist()
return
buffer = buffers.chatBuffer(parent=self.window.tb, name="{0}_messages".format(user_id,), composefunc="compose_message", session=self.session, count=200, user_id=user_id, rev=1)
self.buffers.append(buffer)
self.window.insert_buffer(buffer.tab, _(u"Chat with {0}").format(self.session.get_user_name(user_id,)), self.window.search("chats"))
buffer.get_items()
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:
@ -283,10 +265,18 @@ class Controller(object):
return None return None
def chat_from_id(self, user_id): def chat_from_id(self, user_id):
b = self.search_chat_buffer(user_id)
if b != None:
pos = self.window.search(b.name)
self.window.change_buffer(pos)
return b.tab.text.SetFocus()
buffer = buffers.chatBuffer(parent=self.window.tb, name="{0}_messages".format(user_id,), composefunc="compose_message", session=self.session, count=200, user_id=user_id, rev=1) buffer = buffers.chatBuffer(parent=self.window.tb, name="{0}_messages".format(user_id,), composefunc="compose_message", session=self.session, count=200, user_id=user_id, rev=1)
self.buffers.append(buffer) self.buffers.append(buffer)
self.window.insert_buffer(buffer.tab, _(u"Chat with {0}").format(self.session.get_user_name(user_id,)), self.window.search("chats")) self.window.insert_buffer(buffer.tab, _(u"Chat with {0}").format(self.session.get_user_name(user_id,)), self.window.search("chats"))
buffer.get_items() pos = self.window.search(buffer.name)
self.window.change_buffer(pos)
wx.CallAfter(buffer.get_items)
buffer.tab.text.SetFocus()
return True return True
def get_chat(self, obj=None): def get_chat(self, obj=None):

View File

@ -17,6 +17,15 @@ sessions = {}
# I've added the Date identifier (this is a field in unix time format), for special objects (like friendship indicators) because these objects doesn't have an own identifier. # I've added the Date identifier (this is a field in unix time format), for special objects (like friendship indicators) because these objects doesn't have an own identifier.
identifiers = ["aid", "gid", "uid", "pid", "id", "post_id", "nid", "date"] identifiers = ["aid", "gid", "uid", "pid", "id", "post_id", "nid", "date"]
platforms = {1: _(u"Movile application"),
2: _(u"iPhone"),
3: _(u"iPad"),
4: _(u"Android"),
5: _(u"Windows phone"),
6: _(u"Windows 8"),
7: _(u"Web or desktop app")
}
def find_item(list, item): def find_item(list, item):
""" Finds an item in a list by taking an identifier""" """ Finds an item in a list by taking an identifier"""
# determines the kind of identifier that we are using # determines the kind of identifier that we are using
@ -61,6 +70,12 @@ def add_text(status):
message = utils.clean_text(txt[:139]) message = utils.clean_text(txt[:139])
return message return message
def compose_person(status, session):
global platforms
original_date = arrow.get(status["last_seen"]["time"])
last_seen = _(u"{0} from {1}").format(original_date.humanize(locale=languageHandler.getLanguage()), platforms[status["last_seen"]["platform"]])
return [u"{0} {1}".format(status["first_name"], status["last_name"]), last_seen]
def compose_new(status, session): def compose_new(status, session):
""" This method is used to compose an item of the news feed.""" """ This method is used to compose an item of the news feed."""
user = session.get_user_name(status["source_id"]) user = session.get_user_name(status["source_id"])
@ -243,6 +258,9 @@ class vkSession(object):
if data != None: if data != None:
if type(data) == dict: if type(data) == dict:
num = self.order_buffer(name, data["items"], show_nextpage) num = self.order_buffer(name, data["items"], show_nextpage)
if data["items"][0].has_key("first_name"):
for i in data["items"]:
self.db["users"][i["id"]] = u"{0} {1}".format(i["first_name"], i["last_name"])
if data.has_key("profiles") and data.has_key("groups"): if data.has_key("profiles") and data.has_key("groups"):
self.process_usernames(data) self.process_usernames(data)
else: else:
@ -252,11 +270,9 @@ class vkSession(object):
def get_messages(self, name="", *args, **kwargs): def get_messages(self, name="", *args, **kwargs):
data = self.vk.client.messages.getHistory(*args, **kwargs) data = self.vk.client.messages.getHistory(*args, **kwargs)
if data != None: if data != None:
print data
num = self.order_buffer(name, data["items"], False) num = self.order_buffer(name, data["items"], False)
return num return num
def get_user_name(self, user_id): def get_user_name(self, user_id):
if user_id > 0: if user_id > 0:
if self.db["users"].has_key(user_id): if self.db["users"].has_key(user_id):

View File

@ -50,7 +50,7 @@ class mainWindow(wx.Frame):
self.tb.AddPage(buffer, name) self.tb.AddPage(buffer, name)
def insert_buffer(self, buffer, name, pos): def insert_buffer(self, buffer, name, pos):
self.tb.InsertSubPage(pos, buffer, name) return self.tb.InsertSubPage(pos, buffer, name)
def search(self, name_): def search(self, name_):
for i in xrange(0, self.tb.GetPageCount()): for i in xrange(0, self.tb.GetPageCount()):

View File

@ -139,3 +139,19 @@ class chatTab(wx.Panel):
box.Add(self.text, 0, wx.ALL, 5) box.Add(self.text, 0, wx.ALL, 5)
return box return box
class peopleTab(homeTab):
def create_list(self):
self.lbl = wx.StaticText(self, wx.NewId(), _(u"Friends"))
self.list = widgetUtils.list(self, *[_(u"Name"), _(u"Last seen")], style=wx.LC_REPORT)
self.list.set_windows_size(0, 190)
self.list.set_windows_size(1, 100)
self.list.set_size()
self.list.list.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnKeyDown)
def create_post_buttons(self):
self.post = wx.Button(self, -1, _(u"&Post"))
self.new_chat = wx.Button(self, wx.NewId(), _(u"Send message"))
self.postBox = wx.BoxSizer(wx.HORIZONTAL)
self.postBox.Add(self.post, 0, wx.ALL, 5)
self.postBox.Add(self.new_chat, 0, wx.ALL, 5)