Handle a user leaving a group chat.

This commit is contained in:
moyamo 2015-11-29 16:32:33 +02:00
parent bd74021ca6
commit b34643a756
3 changed files with 38 additions and 3 deletions

View file

@ -32,4 +32,4 @@ class Group():
self.joined = False self.joined = False
self.nick = "me" self.nick = "me"
self.participants = { } self.participants = []

View file

@ -484,12 +484,34 @@ class Session(YowsupApp):
self.bot.send("You have been added to group: %s@%s (%s)" self.bot.send("You have been added to group: %s@%s (%s)"
% (self._shortenGroupId(room), subject, self.backend.spectrum_jid)) % (self._shortenGroupId(room), subject, self.backend.spectrum_jid))
# Called by superclass
def onParticipantsAddedToGroup(self, group): def onParticipantsAddedToGroup(self, group):
self.logger.debug("Participants added to group: %s", group) self.logger.debug("Participants added to group: %s", group)
room = group.getGroupId().split('@')[0] room = group.getGroupId().split('@')[0]
self.groups[room].participants.extend(group.getParticipants()) self.groups[room].participants.extend(group.getParticipants())
self._refreshParticipants(room) self._refreshParticipants(room)
# Called by superclass
def onParticipantsRemovedFromGroup(self, room, participants):
self.logger.debug("Participants removed from group: %s, %s",
room, participants)
group = self.groups[room]
for jid in participants:
group.participants.remove(jid)
buddy = jid.split("@")[0]
try:
nick = self.buddies[buddy].nick
except KeyError:
nick = buddy
if nick == "":
nick = buddy
if buddy == self.legacyName:
nick = group.nick
flags = protocol_pb2.PARTICIPANT_FLAG_NONE
self.backend.handleParticipantChanged(
self.user, nick, self._shortenGroupId(room), flags,
protocol_pb2.STATUS_NONE, buddy)
def onPresenceReceived(self, _type, name, jid, lastseen): def onPresenceReceived(self, _type, name, jid, lastseen):
self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen) self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen)
buddy = jid.split("@")[0] buddy = jid.split("@")[0]
@ -643,7 +665,6 @@ class Session(YowsupApp):
group = self.groups[room] group = self.groups[room]
for jid in group.participants: for jid in group.participants:
buddy = jid.split("@")[0] buddy = jid.split("@")[0]
self.logger.info("Added %s to room %s", buddy, room)
try: try:
nick = self.buddies[buddy].nick nick = self.buddies[buddy].nick
except KeyError: except KeyError:

View file

@ -501,6 +501,15 @@ class YowsupApp(object):
"""Called when participants have been added to a group""" """Called when participants have been added to a group"""
pass pass
def onParticipantsRemovedFromGroup(self, group, participants):
"""Called when participants have been removed from a group
Args:
- group: (str) id of the group (e.g. 27831788123-144024456)
- participants: (list) jids of participants that are removed
"""
pass
def sendEntity(self, entity): def sendEntity(self, entity):
"""Sends an entity down the stack (as if YowsupAppLayer called toLower)""" """Sends an entity down the stack (as if YowsupAppLayer called toLower)"""
self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT, self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT,
@ -596,12 +605,17 @@ class YowsupAppLayer(YowInterfaceLayer):
""" """
Sends ack automatically Sends ack automatically
""" """
self.logger.debug("Received notification: %s", entity) self.logger.debug("Received notification (%s): %s", type(entity), entity)
self.toLower(entity.ack()) self.toLower(entity.ack())
if isinstance(entity, CreateGroupsNotificationProtocolEntity): if isinstance(entity, CreateGroupsNotificationProtocolEntity):
self.caller.onAddedToGroup(entity) self.caller.onAddedToGroup(entity)
elif isinstance(entity, AddGroupsNotificationProtocolEntity): elif isinstance(entity, AddGroupsNotificationProtocolEntity):
self.caller.onParticipantsAddedToGroup(entity) self.caller.onParticipantsAddedToGroup(entity)
elif isinstance(entity, RemoveGroupsNotificationProtocolEntity):
self.caller.onParticipantsRemovedFromGroup(
entity.getGroupId().split('@')[0],
entity.getParticipants().keys()
)
@ProtocolEntityCallback('message') @ProtocolEntityCallback('message')
def onMessageReceived(self, entity): def onMessageReceived(self, entity):