]> git.somenet.org - pub/jan/neo-infcoin-utils.git/blob - mattermost_awards/main.py
mattermost_awards/main.py
[pub/jan/neo-infcoin-utils.git] / mattermost_awards / main.py
1 #!/usr/bin/env python3
2 # Someone's FSInf-coin utils.
3 #   Copyright (c) 2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
4 #   published under MIT-License
5 #
6 # Run as cronjob:
7 #   40 12 * * * (cd ~/neo-infcoin-utils/mattermost_awards; ./main.py) &> /tmp/infcoin_awards.log
8 #
9 # awards infcoins to mm-posts.
10 #
11
12 import json
13 import os
14 import psycopg2
15 import psycopg2.extras
16 import requests
17 from inspect import cleandoc
18
19 import config
20
21
22
23 def award_infcoins(user_id, amount, reason_part, reason2=None):
24     infcoin_wallet = json.loads(_load_wallet(config.infcoin_wallet_uid_mappingsdir+"wallet-"+user_id+".json"))
25     if infcoin_wallet["wallet"] != "":
26         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})
27         return True
28     return False
29
30
31
32 def _load_wallet(path, default='{"wallet":""}'):
33     ret = default
34     if os.path.isfile(path):
35         with open(path, "r") as f:
36             ret = f.read()
37     return ret
38
39
40
41 def main():
42     dbconn = psycopg2.connect(config.dbconnstring)
43     dbconn.set_session(autocommit=False)
44
45     # grant coins
46     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
47     cur.execute("""
48         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}' 
49         WHERE (props='' OR props='{}' OR props='{"disable_group_highlight":true}') AND posts.id in (SELECT postid FROM (
50             SELECT count(*) AS cnt, postid, channels.id, posts.userid FROM reactions
51             JOIN posts ON (postid=posts.id) JOIN channels ON (posts.channelid=channels.id) left JOIN teams ON (teams.id=channels.teamid)
52             WHERE posts.deleteat = '0' AND channels.type='O' AND channels.id NOT IN ('4rxjday69jbj8pe5pdcx4ezesr') AND posts.createat > extract(epoch FROM (NOW() - INTERVAL '1 day'))*1000
53             GROUP BY postid, channels.id, posts.userid
54           ) AS a WHERE cnt >10) RETURNING *
55         """)
56
57     for post in cur.fetchall():
58         if award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["id"], "First Time"):
59             print("awarded: "+post["userid"]+"for post: "+post["id"])
60         else:
61             print("failed to award: "+post["userid"]+". wallet not set?")
62
63
64     # process awarded coins - TODO?
65     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
66     cur.execute("""SELECT id, userid FROM posts WHERE deleteat = 0 AND type='' AND props::jsonb->>'infcoin' = '1'""")
67
68     for post in cur.fetchall():
69         if award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["id"], "Later attempt"):
70             print("awarded: "+post["userid"]+"for post: "+post["id"])
71         else:
72             print("failed to award: "+post["userid"]+". wallet not set?")
73
74
75     dbconn.commit()
76     print("*** committed ***\n")
77
78
79 main()