aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2022-10-19 03:34:10 +0100
committerubq323 <ubq323@ubq323.website>2022-10-19 03:34:10 +0100
commit2f8c207a18c23236d8c7c76e39157526b138745e (patch)
tree793a443693393cb74619c64ba902f28a09ec6e9d
parent7fc0f942cbc26c6f306a1df1344edc4c2c6905ec (diff)
add support for apionet 'web'hooks
it can now send information about new posts and threads to apiobot's socket thing. the code is not very good.
-rw-r--r--apioforum/webhooks.py81
1 files changed, 71 insertions, 10 deletions
diff --git a/apioforum/webhooks.py b/apioforum/webhooks.py
index a9b3a72..5543687 100644
--- a/apioforum/webhooks.py
+++ b/apioforum/webhooks.py
@@ -4,10 +4,10 @@ import json
from .db import get_db
from flask import url_for, flash
-def abridge_post(text):
- MAXLEN = 20
- if len(text) > MAXLEN+3:
- return text[:MAXLEN]+"..."
+
+def abridge(text,maxlen=20):
+ if len(text) > maxlen+3:
+ return text[:maxlen]+"..."
else:
return text
@@ -48,17 +48,19 @@ def do_webhooks_thread(forum_id,thread):
for wh in get_webhooks(forum_id):
try:
wh.on_new_thread(thread)
- except:
- # should probably log the error somewhere
+ except Exception as e:
+ #raise e
flash(f"error executing webhook with id {wh.wh_id}")
def do_webhooks_post(forum_id,post):
for wh in get_webhooks(forum_id):
try:
wh.on_new_post(post)
- except:
+ except Exception as e:
+ #raise e
flash(f"error executing webhook with id {wh.wh_id}")
+
@webhook_type("fake")
class FakeWebhook(WebhookType):
def on_new_post(self, post):
@@ -108,7 +110,7 @@ class DiscordWebhook(WebhookType):
"embeds":[
{
"title":"new thread: "+thread['title'],
- "description":abridge_post(post['content']),
+ "description":abridge(post['content']),
"url": url_for('thread.view_thread',thread_id=thread['id'],_external=True),
"color": 0xff00ff,
"fields":[
@@ -143,7 +145,7 @@ class DiscordWebhook(WebhookType):
"embeds":[
{
"title":"re: "+thread['title'],
- "description":abridge_post(post['content']),
+ "description":abridge(post['content']),
"url": post_jump(post['id'],external=True),
"color": 0x00ffff,
"fields":[
@@ -159,4 +161,63 @@ class DiscordWebhook(WebhookType):
}
self.send(payload)
-
+
+@webhook_type("apionet")
+class ApionetWebhook(WebhookType):
+ # this is generally quite awful
+
+ sockpath = "/srv/apiobot/bees.sock"
+ #sockpath = "/home/rebecca/programming/apiobot/bees.sock"
+ MAXMSGLEN = 420
+
+ # doesn't use url or anything
+ def send(self,payload):
+ # this is possibly terrible
+ import socket
+ s = socket.socket(socket.AF_UNIX,socket.SOCK_DGRAM)
+ s.sendto(payload.encode("utf-8"),self.sockpath)
+ s.close()
+
+ def append_url(self,rest,url):
+ ub = url.encode("utf-8")
+ max_rlen = self.MAXMSGLEN - len(ub) - 1
+
+ while len(rest.encode("utf-8")) > max_rlen:
+ # chop off characters until short enough
+ rest = rest[:-1]
+
+ return rest + " " + url
+
+ def on_new_thread(self,thread):
+ # copy paste from above. the great refactor will fix this all
+ db = get_db()
+ forum = db.execute("select * from forums where id = ?",(thread['forum'],)).fetchone()
+ username = thread['creator'][:30]
+ post = db.execute("select * from posts where thread = ? order by id asc limit 1",(thread['id'],)).fetchone()
+ url = url_for('thread.view_thread',thread_id=thread['id'],_external=True)
+
+
+ p = ""
+ if forum['id'] != 1:
+ p = f" in {forum['name']}"
+ payload = f"new thread{p}: \"{abridge(thread['title'],100)}\" - \"{abridge(post['content'],100)}\" (author: {username}) ->"
+ self.send(self.append_url(payload,url))
+
+ def on_new_post(self,post):
+ from .thread import post_jump
+ db = get_db()
+
+ thread = db.execute("select * from threads where id = ?",(post['thread'],)).fetchone()
+ forum = db.execute("select * from forums where id = ?",(thread['forum'],)).fetchone()
+ username = post['author'][:30]
+
+ url = post_jump(post['id'],external=True)
+
+
+ payload = f"re: \"{abridge(thread['title'],100)}\" - \"{abridge(post['content'],100)}\" (author: {username}) ->"
+ self.send(self.append_url(payload,url))
+
+
+
+
+