nodeinfo: add commit hash to version field to prevent scams
[relay.git/.git] / relay / remote_actor.py
1 import logging
2 import aiohttp
3 from . import CONFIG
4 from .http_debug import http_debug
5
6 from cachetools import TTLCache
7
8
9 CACHE_SIZE = CONFIG.get('cache-size', 16384)
10 CACHE_TTL = CONFIG.get('cache-ttl', 3600)
11
12 ACTORS = TTLCache(CACHE_SIZE, CACHE_TTL)
13
14
15 async def fetch_actor(uri, force=False):
16     if uri in ACTORS and not force:
17         return ACTORS[uri]
18
19     try:
20         async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session:
21             async with session.get(uri, headers={'Accept': 'application/activity+json'}) as resp:
22                 if resp.status != 200:
23                     return None
24                 ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None))
25                 return ACTORS[uri]
26     except Exception as e:
27         logging.info('Caught %r while fetching actor %r.', e, uri)
28         return None