From 643751c48c6449cf42f9954939fac5c450660c49 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 8 Mar 2021 17:43:46 +0100 Subject: [PATCH] mattermost_awards/main.py --- mattermost_awards/main.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/mattermost_awards/main.py b/mattermost_awards/main.py index f95cfae..3f22c01 100755 --- a/mattermost_awards/main.py +++ b/mattermost_awards/main.py @@ -20,7 +20,7 @@ import config -def award_infcoins(user_id, amount, reason_part): +def award_infcoins(user_id, amount, reason_part, reason2=None): infcoin_wallet = json.loads(_load_wallet(config.infcoin_wallet_uid_mappingsdir+"wallet-"+user_id+".json")) if infcoin_wallet["wallet"] != "": r = requests.get(config.infcoin_req_url, {"auth_name":config.infcoin_req_user, "auth_secret":config.infcoin_req_pw, "addr":infcoin_wallet["wallet"], "amount":amount, "reason_uniq":"mm-"+reason_part+", uid="+user_id}) @@ -42,19 +42,38 @@ def main(): dbconn = psycopg2.connect(config.dbconnstring) dbconn.set_session(autocommit=False) - # TODO: grant coins - - # process awarded coins - TODO. + # grant coins cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor) - cur.execute(""" SELECT id AS postid, userid FROM posts WHERE deleteat = 0 AND props::jsonb->>'infcoin' = '1' """) + cur.execute(""" + UPDATE posts SET props='{"attachments":[{"color":"#38ad69", "text":"User was awarded 1 [Inf-coin](https://mattermost.fsinf.at/fsinf/channels/inf-coin) for this post."}], "infcoin":1}' + WHERE (props='' OR props='{}' OR props='{"disable_group_highlight":true}') AND posts.id in (SELECT postid FROM ( + SELECT 'https://mattermost.fsinf.at/'||teams.name||'/pl/'||postid AS url, teams.name||'::'||channels.name AS channelname, count(*) AS cnt, postid, channels.id, posts.userid + FROM reactions JOIN posts ON (postid=posts.id) JOIN channels ON (posts.channelid=channels.id) left JOIN teams ON (teams.id=channels.teamid) + WHERE posts.deleteat = '0' AND channels.type='O' AND channels.id NOT IN ('4rxjday69jbj8pe5pdcx4ezesr') AND channels.id IN ('9twh9scp47g57dgjat61skni5w') AND posts.createat > extract(epoch FROM (NOW() - INTERVAL '1 day'))*1000 + GROUP BY teams.name, channels.name, postid, channels.id, posts.userid + ) AS a WHERE cnt >10) RETURNING * + """) for post in cur.fetchall(): - print("awarding: "+post["userid"]+"for post: "+post["postid"]) - award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["postid"]) + if award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["id"], "First Time"): + print("awarded: "+post["userid"]+"for post: "+post["id"]) + else: + print("failed to award: "+post["userid"]+". wallet not set?") + + # process awarded coins - TODO? + cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor) + cur.execute("""SELECT id, userid FROM posts WHERE deleteat = 0 AND type='' AND props::jsonb->>'infcoin' = '1'""") + + for post in cur.fetchall(): + if award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["id"], "Later attempt"): + print("awarded: "+post["userid"]+"for post: "+post["id"]) + else: + print("failed to award: "+post["userid"]+". wallet not set?") - # TODO: remove old tags? + dbconn.commit() + print("*** committed ***\n") main() -- 2.43.0