Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 047084f

Browse files
committed
Handle inactive group chats
Fixes #72.
1 parent a1aac41 commit 047084f

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

skpy/chat.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ def rawToFields(cls, raw={}):
2929
return {"id": raw.get("id"),
3030
"alerts": False if raw.get("properties", {}).get("alerts") == "false" else True}
3131

32+
@classmethod
33+
def fromRaw(cls, skype=None, raw={}):
34+
id = raw.get("id")
35+
if "threadProperties" in raw:
36+
active = True
37+
try:
38+
info = skype.conn("GET", "{0}/threads/{1}".format(skype.conn.msgsHost, raw.get("id")),
39+
auth=SkypeConnection.Auth.RegToken,
40+
params={"view": "msnp24Equivalent"}).json()
41+
except SkypeApiException as e:
42+
if e.args[1].status_code != 404:
43+
raise
44+
active = False
45+
else:
46+
raw.update(info)
47+
return SkypeGroupChat(skype, raw, **SkypeGroupChat.rawToFields(raw, active=active))
48+
else:
49+
return SkypeSingleChat(skype, raw, **SkypeSingleChat.rawToFields(raw))
50+
3251
def getMsgs(self):
3352
"""
3453
Retrieve a batch of messages from the conversation.
@@ -315,12 +334,15 @@ class SkypeGroupChat(SkypeChat):
315334
URL to retrieve the conversation picture.
316335
joinUrl (str):
317336
Public ``join.skype.com`` URL for any other users to access the conversation.
337+
active (bool):
338+
Whether the full group chat was retrieved from the server. This may be ``False`` if a group conversation
339+
still appears in the recent list despite being left or deleted.
318340
"""
319341

320-
attrs = SkypeChat.attrs + ("topic", "creatorId", "userIds", "adminIds", "open", "history", "picture")
342+
attrs = SkypeChat.attrs + ("topic", "creatorId", "userIds", "adminIds", "open", "history", "picture", "active")
321343

322344
@classmethod
323-
def rawToFields(cls, raw={}):
345+
def rawToFields(cls, raw={}, active=False):
324346
fields = super(SkypeGroupChat, cls).rawToFields(raw)
325347
props = raw.get("properties", {})
326348
userIds = []
@@ -336,7 +358,8 @@ def rawToFields(cls, raw={}):
336358
"adminIds": adminIds,
337359
"open": props.get("joiningenabled", "") == "true",
338360
"history": props.get("historydisclosed", "") == "true",
339-
"picture": props.get("picture", "")[4:] or None})
361+
"picture": props.get("picture", "")[4:] or None,
362+
"active": active})
340363
return fields
341364

342365
@property
@@ -450,14 +473,8 @@ def recent(self):
450473
resp = self.skype.conn.syncStateCall("GET", url, params, auth=SkypeConnection.Auth.RegToken).json()
451474
chats = {}
452475
for json in resp.get("conversations", []):
453-
cls = SkypeSingleChat
454-
if "threadProperties" in json:
455-
info = self.skype.conn("GET", "{0}/threads/{1}".format(self.skype.conn.msgsHost, json.get("id")),
456-
auth=SkypeConnection.Auth.RegToken,
457-
params={"view": "msnp24Equivalent"}).json()
458-
json.update(info)
459-
cls = SkypeGroupChat
460-
chats[json.get("id")] = self.merge(cls.fromRaw(self.skype, json))
476+
chat = SkypeChat.fromRaw(self.skype, json)
477+
chats[chat.id] = self.merge(chat)
461478
return chats
462479

463480
def chat(self, id):
@@ -466,16 +483,13 @@ def chat(self, id):
466483
467484
Args:
468485
id (str): single or group chat identifier
486+
487+
Returns:
488+
:class:`SkypeChat`: retrieved conversation
469489
"""
470490
json = self.skype.conn("GET", "{0}/users/ME/conversations/{1}".format(self.skype.conn.msgsHost, id),
471491
auth=SkypeConnection.Auth.RegToken, params={"view": "msnp24Equivalent"}).json()
472-
cls = SkypeSingleChat
473-
if "threadProperties" in json:
474-
info = self.skype.conn("GET", "{0}/threads/{1}".format(self.skype.conn.msgsHost, json.get("id")),
475-
auth=SkypeConnection.Auth.RegToken, params={"view": "msnp24Equivalent"}).json()
476-
json.update(info)
477-
cls = SkypeGroupChat
478-
return self.merge(cls.fromRaw(self.skype, json))
492+
return self.merge(SkypeChat.fromRaw(self.skype, json))
479493

480494
def create(self, members=(), admins=()):
481495
"""
@@ -487,6 +501,9 @@ def create(self, members=(), admins=()):
487501
Args:
488502
members (str list): user identifiers to initially join the conversation
489503
admins (str list): user identifiers to gain admin privileges
504+
505+
Returns:
506+
:class:`SkypeGroupChat`: newly created group conversation
490507
"""
491508
memberObjs = [{"id": "8:{0}".format(self.skype.userId), "role": "Admin"}]
492509
for id in members:

0 commit comments

Comments
 (0)