Add Group class
And pull more things out of the Channel class and into the Thread class. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
fcd2789c29
commit
6859f8c3ec
|
@ -11,6 +11,7 @@ os.chdir(os.path.dirname(path))
|
||||||
from . import api
|
from . import api
|
||||||
from . import auth
|
from . import auth
|
||||||
from . import channels
|
from . import channels
|
||||||
|
from . import groups
|
||||||
from . import users
|
from . import users
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,17 +12,9 @@ class Channel(threads.Thread):
|
||||||
def __init__(self, json):
|
def __init__(self, json):
|
||||||
threads.Thread.__init__(self, json)
|
threads.Thread.__init__(self, json)
|
||||||
self.__member = json.get("is_member", False)
|
self.__member = json.get("is_member", False)
|
||||||
self.__unread = json.get("unread_count", None)
|
|
||||||
self.__last_ts = json.get("last_read", None)
|
|
||||||
|
|
||||||
def fetch_info(self):
|
def do_fetch_info(self):
|
||||||
json = info(self.__id)
|
return info(self.id())
|
||||||
if json != None:
|
|
||||||
self.__unread = json.get("unread_count", None)
|
|
||||||
self.__last_ts = json.get("last_read", None)
|
|
||||||
|
|
||||||
def id(self):
|
|
||||||
return self.__id
|
|
||||||
|
|
||||||
def is_member(self):
|
def is_member(self):
|
||||||
return self.__member
|
return self.__member
|
||||||
|
@ -30,11 +22,6 @@ class Channel(threads.Thread):
|
||||||
def post(self, text):
|
def post(self, text):
|
||||||
chat.postMessage(self.__id, text)
|
chat.postMessage(self.__id, text)
|
||||||
|
|
||||||
def unread_count(self):
|
|
||||||
if self.__unread == None:
|
|
||||||
self.fetch_info()
|
|
||||||
return self.__unread
|
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
if self.__last_ts == None:
|
if self.__last_ts == None:
|
||||||
self.fetch_info()
|
self.fetch_info()
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
#
|
||||||
|
# Copyright 2015 (c) Anna Schumaker.
|
||||||
|
#
|
||||||
|
from . import api
|
||||||
|
from . import auth
|
||||||
|
from . import threads
|
||||||
|
|
||||||
|
|
||||||
|
class Group(threads.Thread):
|
||||||
|
def __init__(self, json):
|
||||||
|
threads.Thread.__init__(self, json)
|
||||||
|
|
||||||
|
def do_fetch_info(self):
|
||||||
|
return info(self.id())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def info(group):
|
||||||
|
ret = api.call("groups.info", token = auth.token(), channel = group)
|
||||||
|
if ret["ok"] == False:
|
||||||
|
return None
|
||||||
|
return ret["group"]
|
||||||
|
|
||||||
|
def list():
|
||||||
|
ret = api.call("groups.list", token = auth.token())
|
||||||
|
if ret["ok"] == False:
|
||||||
|
return None
|
||||||
|
|
||||||
|
gr_list = []
|
||||||
|
for group in ret["groups"]:
|
||||||
|
gr_list += [ Group(group) ]
|
||||||
|
return gr_list
|
|
@ -3,12 +3,20 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# This class represents a slack "thread", which could be a channel, group,
|
||||||
|
# or chat. Note that child classes need to implement the following methods:
|
||||||
|
#
|
||||||
|
# - do_fetch_info(): Call <whatever>.info to find unread count and timestamp.
|
||||||
|
#
|
||||||
class Thread:
|
class Thread:
|
||||||
def __init__(self, json):
|
def __init__(self, json):
|
||||||
self.__id = json.get("id", 0)
|
self.__id = json.get("id", 0)
|
||||||
self.__name = json.get("name", "").title()
|
self.__name = json.get("name", "").title()
|
||||||
self.__topic = json["topic"].get("value", "").encode("utf-8").decode("latin-1")
|
self.__topic = json["topic"].get("value", "").encode("utf-8").decode("latin-1")
|
||||||
self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode("latin-1")
|
self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode("latin-1")
|
||||||
|
self.__unread = json.get("unread_count", None)
|
||||||
|
self.__last_ts = json.get("last_read", None)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
topic=""
|
topic=""
|
||||||
|
@ -16,5 +24,22 @@ class Thread:
|
||||||
topic = "\nTopic: %s" % self.__topic
|
topic = "\nTopic: %s" % self.__topic
|
||||||
return "%s: %s%s" % (self.__name, self.__purpose, topic)
|
return "%s: %s%s" % (self.__name, self.__purpose, topic)
|
||||||
|
|
||||||
|
def fetch_info(self):
|
||||||
|
json = self.do_fetch_info()
|
||||||
|
if json != None:
|
||||||
|
self.__unread = json.get("unread_count", None)
|
||||||
|
self.__last_ts = json.get("last_read", None)
|
||||||
|
|
||||||
|
def id(self):
|
||||||
|
return self.__id
|
||||||
|
|
||||||
|
def is_member(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.__name
|
return self.__name
|
||||||
|
|
||||||
|
def unread_count(self):
|
||||||
|
if self.__unread == None:
|
||||||
|
self.fetch_info()
|
||||||
|
return self.__unread
|
||||||
|
|
Loading…
Reference in New Issue