add tars slash command

This commit is contained in:
Morgan McMillian 2022-11-25 14:58:09 -08:00
parent 0a526a84f1
commit 8872168510
2 changed files with 44 additions and 1 deletions

View file

@ -3,3 +3,4 @@ Flask
gunicorn gunicorn
requests requests
cryptography cryptography
redis

View file

@ -5,6 +5,7 @@ import hashlib
import requests import requests
import yaml import yaml
import os import os
import redis
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
from flask import Flask, jsonify, request, abort from flask import Flask, jsonify, request, abort
@ -15,6 +16,8 @@ app = Flask(__name__)
SNAP_USER_DATA = os.environ.get('SNAP_USER_DATA') SNAP_USER_DATA = os.environ.get('SNAP_USER_DATA')
CONFIG_FILE = os.environ.get('CONFIG_FILE') CONFIG_FILE = os.environ.get('CONFIG_FILE')
LOG_FILE = os.environ.get('LOG_FILE') LOG_FILE = os.environ.get('LOG_FILE')
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
REDIS_PORT = os.environ.get('REDIS_PORT', 6379)
if SNAP_USER_DATA is None: if SNAP_USER_DATA is None:
if CONFIG_FILE is None: if CONFIG_FILE is None:
filename = 'config.yaml' filename = 'config.yaml'
@ -34,6 +37,8 @@ logging.basicConfig(level=logging.DEBUG, filename=log_file)
with open(filename, "rb") as config_file: with open(filename, "rb") as config_file:
config = yaml.load(config_file, Loader=yaml.SafeLoader) config = yaml.load(config_file, Loader=yaml.SafeLoader)
rs = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
srht_public_key = Ed25519PublicKey.from_public_bytes( srht_public_key = Ed25519PublicKey.from_public_bytes(
base64.b64decode(config['srht_payload_sig'])) base64.b64decode(config['srht_payload_sig']))
# GitHub webhooks # GitHub webhooks
@ -186,7 +191,7 @@ def gl_push_event(gateway, data):
# logging.debug(data) # logging.debug(data)
project = data["project"]["path_with_namespace"] project = data["project"]["path_with_namespace"]
username = data["user_username"] username = data["user_username"]
branch = data['ref'][len('refs/heads/'):] branch = data['ref'][len('refs/heads/'):]
count = data["total_commits_count"] count = data["total_commits_count"]
homepage = data["repository"]["homepage"] homepage = data["repository"]["homepage"]
s = "s" if count > 1 else "" s = "s" if count > 1 else ""
@ -335,3 +340,40 @@ def srht_event(user, repo, gateway):
logging.exception('SRHT') logging.exception('SRHT')
return '', 200 return '', 200
@app.route('/tars/', methods=['POST'])
def feedbot_cmd():
token = request.headers['Authorization']
user_id = request.form['user_id']
reply = {"username": "tars"}
cmd_text = request.form['text']
cmd_help = '''
The following commands are available:
xpost-on: turn on cross posts from mastodon to pnut
xpost-off: turn off cross posts from mastodon to pnut
'''
if token != "Token " + config['mm_tars_token']:
logging.debug("INVALID TOKEN")
abort(401)
if user_id != config['mm_user_id']:
logging.debug("UNAUTHORIZED USER")
abort(403)
if cmd_text == "xpost-on":
logging.debug("enable crossposts")
rs.rpush('TARS', "feedbot_enable")
reply['text'] = "crossposts will be enabled"
elif cmd_text == "xpost-off":
logging.debug("disable crossposts")
rs.rpush('TARS', "feedbot_disable")
reply['text'] = "crossposts will be disabled"
else:
logging.debug("SHOW HELP?")
reply['text'] = cmd_help
return reply, 200