-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuser.py
More file actions
135 lines (116 loc) · 4.19 KB
/
user.py
File metadata and controls
135 lines (116 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import re
import time
import ddb
import user_created
import utils
import config
import configuration
class User(object):
table_name = "User"
fake_table_name = "FakeUser"
def __init__(self, fake=False):
self.fake = fake
if fake:
self.table_name = self.fake_table_name
self.ddb = ddb.DDB(self.table_name, [('slack_uid', 'S')])
self.table = self.ddb.get_table()
self.users = {}
self.configuration = configuration.Configuration(fake=fake)
self.modified = {}
self.uc = user_created.UserCreated()
def find(self, username):
"""
Return a list of matches to the given username, case-sensitive
"""
if username[0] == "@":
username = username[1:]
matches = []
attrs = "user_name display_name real_name".split()
for item in self.ddb.items(self.table):
for i in attrs:
if username == item.get(i):
matches.append(item)
break
return matches
def batch_get_user(self, userids):
return self.ddb.batch_hash_get(userids)
def pick_name(self, user):
"""
given a user structure from user.get(), return the name we should
show people -- this should ideally be the name they see people
interact as in slack
"""
dn = user.get('display_name')
rn = user.get("real_name")
un = user.get("user_name")
return dn or rn or un
def get_users(self, list_of_userids):
"""
Given a list of userIDs, returns a dictionary indexed by userID
where the value is another dictionary with
'label': The actual label to show for the user ID
'hover': The text to show when hovering over the label
'url': The URL to link to for more information about the user
"""
dummy = {
'slack_uid': 'USLACKBOT',
'tz_offset': -25200,
'insert_ts': 1567210676,
'user_name': 'dummy',
'tz': 'America/Los_Angeles',
'real_name': 'Dummy User',
'display_name': 'Dummy User'}
ret = {}
start = time.time()
entries = self.batch_get_user(list_of_userids)
for uid in list_of_userids:
entry = entries.get(uid, dummy)
ret[uid] = self.make_pretty(entry)
return ret
def get_pretty(self, uid):
entry = self.get(uid)
return self.make_pretty(entry)
def make_pretty(self, user_structure):
url = "https://{}.slack.com/team/{}"
url = url.format(config.slack_name, user_structure['slack_uid'])
ret = {
'label': '@' + self.pick_name(user_structure),
'hover': user_structure.get("real_name", ""),
'url': url
}
return ret
def get(self, key):
if key in self.users:
return self.users[key]
response = self.table.get_item(Key={'slack_uid': key})
item = response.get("Item")
self.users[key] = item
return item
def batch_upload(self, users):
self.uc.load()
active_users = [x for x in users if x['deleted'] is False]
self.configuration.set_count("active_users", len(active_users))
self.configuration.set_count("all_users", len(users))
insert_users = []
now = int(time.time())
for user in users:
uid = user['id']
self.uc.set(uid)
Row = {
'slack_uid': user['id'],
'real_name': user.get("real_name"),
'deleted': user.get("deleted"),
'admin': user.get("is_admin"),
'user_name': user.get("name"),
'display_name': user.get('profile', {}).get('display_name'),
'tz': user.get("tz"),
'tz_offset': user.get("tz_offset"),
}
Row = utils.prune_empty(Row)
insert_users.append(Row)
with self.table.batch_writer() as batch:
for row in insert_users:
batch.put_item(row)
self.uc.save()
def f(self, row):
return "{} ({})".format(row['slack_uid'], row['display_name'])