push inbox POSTs on to a background thread

This commit is contained in:
jesopo 2019-09-16 14:18:58 +01:00
parent cc65892586
commit 69032ea476
2 changed files with 33 additions and 8 deletions

View file

@ -37,6 +37,9 @@ class Module(ModuleManager.BaseModule):
self.server.ap_inbox, authenticated=False)
self.events.on("api.get.ap-outbox").hook(
self.server.ap_outbox, authenticated=False)
def unload(self):
if not self.server == None:
self.server.unload()
@utils.hook("received.command.fediverse")
@utils.hook("received.command.fedi", alias_of="fediverse")

View file

@ -1,4 +1,4 @@
import base64, binascii, json, os, urllib.parse, uuid
import base64, binascii, json, os, queue, theading, urllib.parse, uuid
from src import ModuleManager, utils
from . import ap_activities, ap_actor, ap_security, ap_utils
@ -11,6 +11,33 @@ class Server(object):
self.username = username
self.instance = instance
self._request_queue = queue.Queue()
self._request_thread = threading.Thread(target=self._request_loop)
self._request_thread.daemon = True
self._request_thread.start()
def _request_loop(self):
url_for = self.exports.get_one("url-for")
key_id = self._ap_keyid_url(url_for)
private_key = ap_security.PrivateKey(self.bot.config["tls-key"], key_id)
self_id = self._ap_self_url(url_for)
our_actor = ap_actor.Actor(self_id)
del url_for
while True:
obj = self._request_queue.get()
if obj == "kill":
break
else:
actor, activity = obj
actor.inbox.send(our_actor, activity, private_key)
def unload(self):
self._request_queue.put("kill")
def _random_id(self):
return binascii.hexlify(os.urandom(3)).decode("ascii")
@ -185,18 +212,13 @@ class Server(object):
if not new_follower in followers:
followers.add(new_follower)
key_id = self._ap_keyid_url(event["url_for"])
private_key = self._private_key(key_id)
our_actor = ap_actor.Actor(self_id)
actor = ap_actor.Actor(new_follower)
actor.load()
accept = ap_activities.Accept(data["id"], data)
actor.inbox.send(our_actor, accept, private_key)
self._request_queue.put([actor, accept])
follow_id = "data:%s" % str(uuid.uuid4())
follow = ap_activities.Follow(follow_id, actor.url)
actor.inbox.send(our_actor, follow, private_key)
self._request_queue.put([actor, follow])
else:
event["response"].code = 404