Fix httpx session being created on each message

This commit is contained in:
Nexus 2024-09-18 18:51:57 +01:00
parent 9f0ac830f2
commit faed38aafc
3 changed files with 59 additions and 49 deletions

View file

@ -35,53 +35,58 @@ class FediversePreviewModule(niobot.Module):
return return
sent = [] sent = []
async with httpx.AsyncClient( to_get: set[str] = set()
headers={ for item in event.body.split():
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0" if not item.startswith(tuple(supported_prefixes)):
} return
) as client: parsed = urlparse(item)
for item in event.body.split(): post_id = parsed.path.split("/")[-1]
if not item.startswith(tuple(supported_prefixes)): if post_id in sent:
return self.log.info("Already sent post %s", post_id)
parsed = urlparse(item) continue
post_id = parsed.path.split("/")[-1] elif len(to_get) >= 5:
if post_id in sent: self.log.info("Already sent 5 posts, stopping")
self.log.info("Already sent post %s", post_id) break
continue to_get.add("https://%s/api/v1/statuses/%s" % (parsed.netloc, post_id))
elif len(sent) >= 5: if to_get:
self.log.info("Already sent 5 posts, stopping") async with httpx.AsyncClient(
break headers={
resp = await client.get( "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
"https://%s/api/v1/statuses/%s" % (parsed.netloc, post_id) }
) ) as client:
if resp.status_code != 200: for url in to_get:
self.log.error("Got HTTP %d from %s", resp.status_code, resp.url) resp = await client.get(
continue url
data = resp.json()
self.log.info("Got data: %r", data)
username = data["account"]["username"]
if not (text := data.get("content")):
self.log.warning("No text for post %s", post_id)
continue
self.log.info("Detected fediverse post %s: %r", post_id, text)
rendered = await self.bot._markdown_to_html(text)
text_body = "<blockquote>%s</blockquote>" % rendered
body = '<a href="%s">@%s:</a><br>%s' % (
"https://%s/@%s" % (parsed.netloc, username),
username,
text_body,
)
if data.get("media_attachments"):
body += "<p>{:,} attachments</p>".format(
len(data["media_attachments"])
) )
self.log.info("Sending fediverse post %s", post_id) if resp.status_code != 200:
await self.bot.send_message( self.log.error("Got HTTP %d from %s", resp.status_code, resp.url)
room, continue
body, data = resp.json()
reply_to=event, self.log.info("Got data: %r", data)
content_type="html.raw", username = data["account"]["username"]
override={"body": f"@{username}: {data['content']!r}"}, if not (text := data.get("content")):
) self.log.warning("No text for post %s", url)
sent.append(post_id) continue
self.log.info("Detected fediverse post %s: %r", url, text)
rendered = await self.bot._markdown_to_html(text)
parsed = urlparse(url)
text_body = "<blockquote>%s</blockquote>" % rendered
body = '<a href="%s">@%s:</a><br>%s' % (
"https://%s/@%s" % (parsed.netloc, username),
username,
text_body,
)
if data.get("media_attachments"):
body += "<p>{:,} attachments</p>".format(
len(data["media_attachments"])
)
self.log.info("Sending fediverse post %s", url)
await self.bot.send_message(
room,
body,
reply_to=event,
content_type="html.raw",
override={"body": f"@{username}: {data['content']!r}"},
)
sent.append(post_id)

View file

@ -1,4 +1,5 @@
import json import json
import logging
import re import re
import typing import typing
@ -12,6 +13,7 @@ if typing.TYPE_CHECKING:
class MSCGetter(niobot.Module): class MSCGetter(niobot.Module):
bot: "NonsenseBot" bot: "NonsenseBot"
log = logging.getLogger(__name__)
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
@ -34,6 +36,7 @@ class MSCGetter(niobot.Module):
if content: if content:
return content return content
self.log.debug("Requesting MSC: %d", number)
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
response = await client.get( response = await client.get(
"https://api.github.com/repos/matrix-org/matrix-spec-proposals/issues/%d" "https://api.github.com/repos/matrix-org/matrix-spec-proposals/issues/%d"

View file

@ -43,6 +43,8 @@ class TruthSocialTranscode(niobot.Module):
async def on_message_internal( async def on_message_internal(
self, room: niobot.MatrixRoom, event: niobot.RoomMessageVideo self, room: niobot.MatrixRoom, event: niobot.RoomMessageVideo
) -> str: ) -> str:
if self.bot.is_old(event):
return "IGNORED"
self.log.info("Processing event: %r (%r)", event, room) self.log.info("Processing event: %r (%r)", event, room)
# if not isinstance(event, niobot.RoomMessageVideo): # if not isinstance(event, niobot.RoomMessageVideo):
# # We are not interested in non-videos. # # We are not interested in non-videos.