2019-04-25 20:41:45 +00:00
|
|
|
import Spectrum2
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
class Group():
|
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def __init__(self, id, owner, subject, subjectOwner, backend, user):
|
|
|
|
self.id = id
|
|
|
|
self.subject = subject
|
|
|
|
self.subjectOwner = subjectOwner
|
|
|
|
self.owner = owner
|
|
|
|
self.joined = False
|
|
|
|
self.backend = backend
|
|
|
|
self.user = user
|
2015-12-28 12:12:47 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
self.nick = "me"
|
|
|
|
# Participants is a number -> nickname dict
|
|
|
|
self.participants = {}
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def addParticipants(self, participants, buddies, yourNumber):
|
|
|
|
"""
|
|
|
|
Adds participants to the group.
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
Args:
|
|
|
|
- participants: (Iterable) phone numbers of participants
|
|
|
|
- buddies: (dict) Used to get the nicknames of the participants
|
|
|
|
- yourNumber: The number you are using
|
|
|
|
"""
|
|
|
|
for jid in participants:
|
|
|
|
number = jid.split('@')[0]
|
|
|
|
try:
|
|
|
|
nick = buddies[number].nick
|
|
|
|
except KeyError:
|
|
|
|
nick = number
|
|
|
|
if number == yourNumber:
|
|
|
|
nick = self.nick
|
|
|
|
if nick == "":
|
|
|
|
nick = number
|
|
|
|
self.participants[number] = nick
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def sendParticipantsToSpectrum(self, yourNumber):
|
|
|
|
for number, nick in self.participants.iteritems():
|
|
|
|
if number == self.owner:
|
|
|
|
flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_MODERATOR
|
|
|
|
else:
|
|
|
|
flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE
|
|
|
|
if number == yourNumber:
|
|
|
|
flags = flags | Spectrum2.protocol_pb2.PARTICIPANT_FLAG_ME
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_ONLINE,
|
|
|
|
self.backend.sessions[self.user].buddies[number].image_hash)
|
|
|
|
except KeyError:
|
|
|
|
self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_ONLINE)
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def removeParticipants(self, participants):
|
|
|
|
for jid in participants:
|
|
|
|
number = jid.split('@')[0]
|
|
|
|
nick = self.participants[number]
|
|
|
|
flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE
|
|
|
|
self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_NONE)
|
|
|
|
del self.participants[number]
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def leaveRoom(self):
|
|
|
|
for number in self.participants:
|
|
|
|
nick = self.participants[number]
|
|
|
|
flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_ROOM_NOT_FOUND
|
|
|
|
self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_NONE)
|
2016-01-06 14:33:28 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def changeNick(self, number, new_nick):
|
|
|
|
if self.participants[number] == new_nick:
|
|
|
|
return
|
|
|
|
if number == self.owner:
|
|
|
|
flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_MODERATOR
|
|
|
|
else:
|
|
|
|
flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE
|
|
|
|
self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_ONLINE, new_nick)
|
|
|
|
self.participants[number] = new_nick
|
2016-01-01 09:42:11 +00:00
|
|
|
|
2019-04-25 22:37:34 +00:00
|
|
|
def _updateParticipant(self, number, flags, status, imageHash = "", newNick = ""):
|
|
|
|
nick = self.participants[number]
|
|
|
|
# Notice the status message is the buddy's number
|
|
|
|
if self.joined:
|
|
|
|
self.backend.handleParticipantChanged(
|
|
|
|
self.user, nick, self.id, flags,
|
|
|
|
status, number, newname = newNick, iconHash = imageHash)
|