Notify user when a group message is sent to a group they have not yet joined

Before messages sent to groups the user had not joined were lost. Now
the message is sent via the bot to the user. The bot suggests that the
user join the group and also relays the contents of the message.
This commit is contained in:
moyamo 2015-11-23 17:06:14 +02:00
parent 1cec0a4e30
commit 8e08efabc9
3 changed files with 32 additions and 8 deletions

View file

@ -29,6 +29,7 @@ class Group():
self.subject = subject self.subject = subject
self.subjectOwner = subjectOwner self.subjectOwner = subjectOwner
self.owner = owner self.owner = owner
self.joined = False
self.nick = "me" self.nick = "me"
self.participants = { } self.participants = { }

View file

@ -223,9 +223,18 @@ class Session(YowsupApp):
self.backend.handleRoomNicknameChanged( self.backend.handleRoomNicknameChanged(
self.user, self._shortenGroupId(room), group.subject self.user, self._shortenGroupId(room), group.subject
) )
group.joined = True
else: else:
self.logger.warn("Room doesn't exist: %s", room) self.logger.warn("Room doesn't exist: %s", room)
def leaveRoom(self, room):
if room in self.groups:
self.logger.info("Leaving room: %s room=%s", self.legacyName, room)
group = self.groups[room]
group.joined = False
else:
self.logger.warn("Room doesn't exist: %s. Unable to leave.", room)
def _refreshParticipants(self, room): def _refreshParticipants(self, room):
group = self.groups[room] group = self.groups[room]
for jid in group.participants: for jid in group.participants:
@ -681,9 +690,22 @@ class Session(YowsupApp):
else: else:
self.logger.debug("Group message sent from %s (%s) to %s: %s", self.logger.debug("Group message sent from %s (%s) to %s: %s",
buddy, nick, room, messageContent) buddy, nick, room, messageContent)
try:
group = self.groups[room]
if group.joined:
self.backend.handleMessage(self.user,room, messageContent,
nick, "", timestamp)
else:
self.bot.send("You have received a message in group: %s@%s"
% (room, self.backend.spectrum_jid))
self.bot.send("Join the group in order to reply")
self.bot.send("%s: %s" % (nick, messageContent))
except KeyError:
self.logger.warn("Group is not in group list")
self.backend.handleMessage(self.user, self._shortenGroupId(room), self.backend.handleMessage(self.user, self._shortenGroupId(room),
messageContent, nick, "", timestamp) messageContent, nick, "", timestamp)
def changeStatus(self, status): def changeStatus(self, status):
if status != self.status: if status != self.status:
self.logger.info("Status changed: %s", status) self.logger.info("Status changed: %s", status)

View file

@ -78,6 +78,10 @@ class WhatsAppBackend(SpectrumBackend):
self.logger.debug("handleJoinRoomRequest(user=%s, room=%s, nickname=%s)", user, room, nickname) self.logger.debug("handleJoinRoomRequest(user=%s, room=%s, nickname=%s)", user, room, nickname)
self.sessions[user].joinRoom(room, nickname) self.sessions[user].joinRoom(room, nickname)
def handleLeaveRoomRequest(self, user, room):
self.logger.debug("handleLeaveRoomRequest(user=%s, room=%s)", user, room)
self.sessions[user].leaveRoom(room)
def handleStatusChangeRequest(self, user, status, statusMessage): def handleStatusChangeRequest(self, user, status, statusMessage):
self.logger.debug("handleStatusChangeRequest(user=%s, status=%d, statusMessage=%s)", user, status, statusMessage) self.logger.debug("handleStatusChangeRequest(user=%s, status=%d, statusMessage=%s)", user, status, statusMessage)
self.sessions[user].changeStatusMessage(statusMessage) self.sessions[user].changeStatusMessage(statusMessage)
@ -112,9 +116,6 @@ class WhatsAppBackend(SpectrumBackend):
def handleBuddyBlockToggled(self, user, buddy, blocked): def handleBuddyBlockToggled(self, user, buddy, blocked):
pass pass
def handleLeaveRoomRequest(self, user, room):
pass
def handleVCardUpdatedRequest(self, user, photo, nickname): def handleVCardUpdatedRequest(self, user, photo, nickname):
pass pass