2022-05-06 07:04:51 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
from cachetools import LRUCache
|
2022-05-06 07:04:51 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2022-11-29 22:41:04 +00:00
|
|
|
from .misc import Message
|
2022-11-26 23:56:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
cache = LRUCache(1024)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
async def handle_relay(request):
|
2022-11-26 23:56:34 +00:00
|
|
|
if request.message.objectid in cache:
|
2022-11-18 21:41:14 +00:00
|
|
|
logging.verbose(f'already relayed {request.message.objectid}')
|
2022-05-06 07:04:51 +00:00
|
|
|
return
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
message = Message.new_announce(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
object = request.message.objectid
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
cache[request.message.objectid] = message.id
|
2022-05-06 07:04:51 +00:00
|
|
|
logging.debug(f'>> relay: {message}')
|
|
|
|
|
2022-12-02 05:11:22 +00:00
|
|
|
inboxes = request.database.distill_inboxes(request.message)
|
2022-11-20 10:50:14 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
for inbox in inboxes:
|
|
|
|
request.app.push_message(inbox, message)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
async def handle_forward(request):
|
2022-11-26 23:56:34 +00:00
|
|
|
if request.message.id in cache:
|
2022-11-18 21:41:14 +00:00
|
|
|
logging.verbose(f'already forwarded {request.message.id}')
|
2022-05-06 07:04:51 +00:00
|
|
|
return
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
message = Message.new_announce(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
object = request.message
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
2022-11-06 06:11:36 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
cache[request.message.id] = message.id
|
|
|
|
logging.debug(f'>> forward: {message}')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-29 22:41:04 +00:00
|
|
|
inboxes = request.database.distill_inboxes(request.message)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
for inbox in inboxes:
|
|
|
|
request.app.push_message(inbox, message)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
async def handle_follow(request):
|
2022-11-26 23:56:34 +00:00
|
|
|
nodeinfo = await request.app.client.fetch_nodeinfo(request.actor.domain)
|
2022-11-18 21:41:14 +00:00
|
|
|
software = nodeinfo.swname if nodeinfo else None
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
## reject if software used by actor is banned
|
|
|
|
if request.config.is_banned_software(software):
|
|
|
|
return logging.verbose(f'Rejected follow from actor for using specific software: actor={request.actor.id}, software={software}')
|
|
|
|
|
|
|
|
request.database.add_inbox(request.actor.shared_inbox, request.message.id, software)
|
|
|
|
request.database.save()
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
await request.app.push_message(
|
2022-11-18 21:41:14 +00:00
|
|
|
request.actor.shared_inbox,
|
2022-11-26 23:56:34 +00:00
|
|
|
Message.new_response(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
actor = request.actor.id,
|
|
|
|
followid = request.message.id,
|
2022-11-07 10:30:13 +00:00
|
|
|
accept = True
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Are Akkoma and Pleroma the only two that expect a follow back?
|
|
|
|
# Ignoring only Mastodon for now
|
2022-11-18 21:41:14 +00:00
|
|
|
if software != 'mastodon':
|
2022-11-26 23:56:34 +00:00
|
|
|
await request.app.push_message(
|
2022-11-18 21:41:14 +00:00
|
|
|
request.actor.shared_inbox,
|
2022-11-26 23:56:34 +00:00
|
|
|
Message.new_follow(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
actor = request.actor.id
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
async def handle_undo(request):
|
2022-11-06 00:15:40 +00:00
|
|
|
## If the object is not a Follow, forward it
|
2022-11-18 21:41:14 +00:00
|
|
|
if request.message.object.type != 'Follow':
|
|
|
|
return await handle_forward(request)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
if not request.database.del_inbox(request.actor.domain, request.message.id):
|
2022-05-06 07:04:51 +00:00
|
|
|
return
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
request.database.save()
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
await request.app.push_message(
|
|
|
|
request.actor.shared_inbox,
|
|
|
|
Message.new_unfollow(
|
|
|
|
host = request.config.host,
|
|
|
|
actor = request.actor.id,
|
|
|
|
follow = request.message
|
|
|
|
)
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
|
|
|
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
processors = {
|
|
|
|
'Announce': handle_relay,
|
|
|
|
'Create': handle_relay,
|
|
|
|
'Delete': handle_forward,
|
|
|
|
'Follow': handle_follow,
|
|
|
|
'Undo': handle_undo,
|
|
|
|
'Update': handle_forward,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
async def run_processor(request):
|
|
|
|
if request.message.type not in processors:
|
2022-08-12 06:36:08 +00:00
|
|
|
return
|
|
|
|
|
2022-11-20 10:22:57 +00:00
|
|
|
if request.instance and not request.instance.get('software'):
|
2022-11-26 23:56:34 +00:00
|
|
|
nodeinfo = await request.app.client.fetch_nodeinfo(request.instance['domain'])
|
2022-11-20 10:22:57 +00:00
|
|
|
|
|
|
|
if nodeinfo:
|
2022-11-25 18:19:29 +00:00
|
|
|
request.instance['software'] = nodeinfo.swname
|
2022-11-20 10:22:57 +00:00
|
|
|
request.database.save()
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
logging.verbose(f'New "{request.message.type}" from actor: {request.actor.id}')
|
|
|
|
return await processors[request.message.type](request)
|