1060e1179c3befec3e7bd12b3716ecb12c3e7471
[pub/jan/mattermost-privileged.git] / profile_badges / main.py
1 #!/usr/bin/env -S python3 -Bu
2 # Someone's Mattermost scripts.
3 #   Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
4 #   published under MIT-License
5 #
6 # Custom profile badges.
7 #
8
9 import psycopg2
10 import psycopg2.extras
11 from inspect import cleandoc
12
13 import mattermost
14
15 import config
16
17 mm = mattermost.MMApi(config.mm_api_url)
18 mm.login(config.mm_user, config.mm_user_pw)
19
20 cfg = mm._get("/v4/config")
21 cfg["PluginSettings"]["Enable"] = True
22 cfg["PluginSettings"]["PluginStates"]["com.mattermost.custom-attributes"]["Enable"] = True
23 cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"] = []
24
25 # Im a bot!
26 bot_ids = [mm._my_user_id]
27 bot_ids.extend(config.bot_ids)
28 cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].append({"Name": ":robot:[**``Bot (software agent)``**](https://en.wikipedia.org/wiki/Software_agent)", "UserIDs": bot_ids})
29
30
31 #####################################
32 # post-based badges + 2k+posts club #
33 #####################################
34 if hasattr(config, "dbconnstring") and hasattr(config, "club_team_id") and hasattr(config, "club_id") :
35     dbconn = psycopg2.connect(config.dbconnstring)
36     dbconn.set_session(autocommit=False)
37
38     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
39     cur.execute("""SELECT users.id AS id, username, count(*) AS posts, SUM(LENGTH(posts.message)) AS chars FROM posts JOIN users ON(users.id = userid)
40                 WHERE posts.userid != ALL(%(club_banned_uids)s) AND posts.deleteat = 0 AND NOT posts.props::jsonb @> '{"from_webhook":"true"}'::jsonb AND posts.createat > EXTRACT(EPOCH FROM (NOW() - INTERVAL '7 day'))*1000
41                 GROUP BY users.id, username ORDER BY posts DESC LIMIT 1
42                 """, {"club_banned_uids":config.club_banned_uids})
43     sotw = cur.fetchall()[0]
44     cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].append({"Name": ":crown: **``Spammer of the week ("+str(sotw["posts"])+")``**", "UserIDs": [sotw["id"]]})
45
46     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
47     cur.execute("""SELECT users.id AS id, username, count(*) AS posts, SUM(LENGTH(posts.message)) AS chars FROM posts JOIN users ON(users.id = userid)
48                 WHERE posts.userid != ALL(%(club_banned_uids)s) AND posts.deleteat = 0 AND NOT posts.props::jsonb @> '{"from_webhook":"true"}'::jsonb AND posts.createat > EXTRACT(EPOCH FROM (NOW() - INTERVAL '7 day'))*1000
49                 GROUP BY users.id, username ORDER BY chars DESC LIMIT 1
50                 """, {"club_banned_uids":config.club_banned_uids})
51     sotwc = cur.fetchall()[0]
52     cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].append({"Name": ":crown: **``Spammer of the week ("+str(sotwc["chars"])+")``**", "UserIDs": [sotwc["id"]]})
53
54
55     pbb = [
56         {"Name": "placeholder1", "UserIDs": []},
57         {"Name": "placeholder2", "UserIDs": []},
58         {"Name": "placeholder3", "UserIDs": []},
59         {"Name": ":envelope::medal_sports:``50000+ posts``", "UserIDs": []},
60         {"Name": ":envelope::medal_sports:``20000+ posts``", "UserIDs": []},
61         {"Name": ":envelope::medal_sports:``10000+ posts``", "UserIDs": []},
62         {"Name": ":envelope::medal_sports:``5000+ posts``", "UserIDs": []},
63         {"Name": ":envelope::medal_sports:``2000+ posts``", "UserIDs": []},
64         {"Name": ":envelope:``1000+ posts``", "UserIDs": []},
65         {"Name": ":envelope:``500+ posts``", "UserIDs": []},
66         {"Name": ":envelope:``200+ posts``", "UserIDs": []},
67         {"Name": ":envelope:``100+ posts``", "UserIDs": []},
68         {"Name": ":envelope:``50+ posts``", "UserIDs": []},
69         {"Name": ":envelope:``10+ posts``", "UserIDs": []},
70         {"Name": ":envelope::medal_military:``Less than 10 posts``", "UserIDs": []},
71         ]
72
73     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
74     cur.execute("""SELECT users.id AS id, username, count(*) AS posts, SUM(LENGTH(posts.message)) AS chars FROM posts JOIN users ON(users.id = userid)
75                 WHERE posts.deleteat = 0 AND NOT posts.props::jsonb @> '{"from_webhook":"true"}'::jsonb AND posts.createat < EXTRACT(EPOCH FROM (NOW() - INTERVAL '7 day'))*1000
76                 GROUP BY users.id, username ORDER BY posts DESC
77                 """)
78     last_week_post_pos = [[r["id"], r["posts"], r["chars"]] for r in cur.fetchall()]
79     last_week_post_statsres = {x[0]:[x[1], x[2]] for x in last_week_post_pos}
80     last_week_post_pos = [x[0] for x in last_week_post_pos if x[0] not in config.club_banned_uids]
81
82
83     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
84     cur.execute("""SELECT users.id AS id, username, count(*) AS posts, SUM(LENGTH(posts.message)) AS chars FROM posts JOIN users ON (users.id = userid)
85                 WHERE posts.deleteat = 0 AND NOT posts.props::jsonb @> '{"from_webhook":"true"}'::jsonb
86                 GROUP BY users.id, username ORDER BY posts DESC
87                 """)
88
89     msg = cleandoc("""Post-Stats. #mmstats
90     :crown: SOTW (Spammer-of-the-week): **@"""+sotw["username"]+"""** ("""+str(sotw["posts"])+""")
91
92     :crown: SOTWbC (Spammer-of-the-week-by-chars): **@"""+sotwc["username"]+"""** ("""+str(sotwc["chars"])+""")
93
94     |Rank|Username|Posts|Chars|Chars/post|Posts:7-day-diff|Chars:7-day-diff|
95     |---:|----|---:|---:|---:|---:|---:|
96     """)+"\n"
97     rank = 0
98     for record in cur.fetchall():
99         # add/remove users to/from 2k+ posts club. The post count can decline e.g. if channels are deleted.
100         if record["posts"] >= 2000 and record["id"] not in config.club_banned_uids:
101             rank += 1
102             mm.add_user_to_team(config.club_team_id, record["id"])
103             mm.add_user_to_channel(config.club_id, record["id"])
104             mm.update_channel_members_scheme_roles(config.club_id, record["id"], {"scheme_user": True, "scheme_admin": True})
105         else:
106             mm.remove_user_from_channel(config.club_id, record["id"], exc=False)
107
108         # setrank based on post count
109         if rank == 1 and record["id"] not in config.club_banned_uids:
110             pbb[0] = {"Name": ":envelope::1st_place_medal: **``Top-user! ("+str(record["posts"])+" posts)``**", "UserIDs": [record["id"]]}
111             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
112
113         elif rank == 2 and record["id"] not in config.club_banned_uids:
114             pbb[1] = {"Name": ":envelope::2nd_place_medal: **``Top-user! ("+str(record["posts"])+" posts)``**", "UserIDs": [record["id"]]}
115             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
116
117         elif rank == 3 and record["id"] not in config.club_banned_uids:
118             pbb[2] = {"Name": ":envelope::3rd_place_medal: **``Top-user! ("+str(record["posts"])+" posts)``**", "UserIDs": [record["id"]]}
119             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
120
121         elif record["posts"] >= 50000 and record["id"] not in config.club_banned_uids:
122             pbb[3]["UserIDs"].append(record["id"])
123             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
124
125         elif record["posts"] >= 20000 and record["id"] not in config.club_banned_uids:
126             pbb[4]["UserIDs"].append(record["id"])
127             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
128
129         elif record["posts"] >= 10000 and record["id"] not in config.club_banned_uids:
130             pbb[5]["UserIDs"].append(record["id"])
131             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
132
133         elif record["posts"] >= 5000 and record["id"] not in config.club_banned_uids:
134             pbb[6]["UserIDs"].append(record["id"])
135             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
136
137         elif record["posts"] >= 2000 and record["id"] not in config.club_banned_uids:
138             pbb[7]["UserIDs"].append(record["id"])
139             msg += "|"+str(rank)+" ("+str(last_week_post_pos.index(record["id"])+1)+")|**``"+record["username"]+"``**|"+str(record["posts"])+"|"+str(record["chars"])+"|"+str(round(record["chars"]/record["posts"]))+"|"+str(record["posts"]-last_week_post_statsres[record["id"]][0])+"|"+str(record["chars"]-last_week_post_statsres[record["id"]][1])+"|\n"
140
141         elif record["posts"] >= 1000:
142             pbb[8]["UserIDs"].append(record["id"])
143         elif record["posts"] >= 500:
144             pbb[9]["UserIDs"].append(record["id"])
145         elif record["posts"] >= 200:
146             pbb[10]["UserIDs"].append(record["id"])
147         elif record["posts"] >= 100:
148             pbb[11]["UserIDs"].append(record["id"])
149         elif record["posts"] >= 50:
150             pbb[12]["UserIDs"].append(record["id"])
151         elif record["posts"] >= 10:
152             pbb[13]["UserIDs"].append(record["id"])
153         elif record["posts"] >= 1:
154             pbb[14]["UserIDs"].append(record["id"])
155
156     cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].extend(pbb)
157
158
159 ###############################
160 # uid based badges (pre cmbb) #
161 ###############################
162 for ubb_def in config.uid_based_bagdes_pre:
163     cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].append({"Name": ubb_def[0], "UserIDs": ubb_def[1]})
164
165
166 ###################################
167 # channel-membership based badges #
168 ###################################
169 for cmbb_def in config.channel_membership_based_bagdes:
170     uids_in_channel = {u["user_id"] for u in mm.get_channel_members(cmbb_def[1])}
171     uids_not_in_channel = set()
172     if cmbb_def[2]:
173         uids_not_in_channel = {u["user_id"] for u in mm.get_channel_members(cmbb_def[2])}
174
175     uids = uids_in_channel - uids_not_in_channel - set(cmbb_def[3])
176     cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].append({"Name": cmbb_def[0], "UserIDs": list(uids)})
177
178
179 ################################
180 # uid based badges (post cmbb) #
181 ################################
182 for ubb_def in config.uid_based_bagdes_post:
183     cfg["PluginSettings"]["Plugins"]["com.mattermost.custom-attributes"]["customattributes"].append({"Name": ubb_def[0], "UserIDs": ubb_def[1]})
184
185
186 ########################
187 # set mm-server-config #
188 ########################
189 mm._put("/v4/config", data=cfg)
190
191 # post 2k+posts stats to 2k+posts club channel.
192 if hasattr(config, "dbconnstring") and hasattr(config, "club_team_id") and hasattr(config, "club_id") :
193     mm.create_post(config.club_id, msg)
194
195 mm.logout()