From 93462d4c4b93c4f7eb81702801df9159cbb64e8e Mon Sep 17 00:00:00 2001 From: retoor Date: Fri, 16 May 2025 00:32:54 +0200 Subject: [PATCH] Update. --- src/snek/service/socket.py | 5 +++-- src/snek/templates/web.html | 4 +++- src/snek/view/channel.py | 25 ++++++++++++++----------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/snek/service/socket.py b/src/snek/service/socket.py index ecc7bf9..d0ff024 100644 --- a/src/snek/service/socket.py +++ b/src/snek/service/socket.py @@ -61,6 +61,8 @@ class SocketService(BaseService): async def add(self, ws, user_uid): s = self.Socket(ws, await self.app.services.user.get(uid=user_uid)) self.sockets.add(s) + s.user["last_ping"] = now() + await self.app.services.user.save(s.user) logger.info(f"Added socket for user {s.user['username']}") if not self.users.get(user_uid): self.users[user_uid] = set() @@ -89,8 +91,7 @@ class SocketService(BaseService): async for user_uid in self.services.channel_member.get_user_uids( channel_uid ): - await self.send_to_user(user_uid, message) - sent += 1 + sent += await self.send_to_user(user_uid, message) except Exception as ex: print(ex, flush=True) logger.info(f"Broadcasted a message to {sent} users.") diff --git a/src/snek/templates/web.html b/src/snek/templates/web.html index db91188..02e60df 100644 --- a/src/snek/templates/web.html +++ b/src/snek/templates/web.html @@ -144,9 +144,11 @@ getInputField().focus(); }) document.querySelector("upload-button").addEventListener("uploaded",function(e){ + let message = "" e.detail.files.forEach((file)=>{ - app.rpc.sendMessage(channelUid,`[${file.name}](/channel/attachment/${file.relative_url})`) + message += `[${file.name}](/channel/attachment/${file.relative_url})` }) + app.rpc.sendMessage(channelUid,message) }) textBox.addEventListener("paste", async (e) => { try { diff --git a/src/snek/view/channel.py b/src/snek/view/channel.py index 9d36d8f..7580642 100644 --- a/src/snek/view/channel.py +++ b/src/snek/view/channel.py @@ -17,20 +17,21 @@ class ChannelAttachmentView(BaseView): relative_url=relative_path ) - current_format = mimetypes.guess_type(channel_attachment["path"])[0] - - format = self.request.query.get("format") + original_format = mimetypes.guess_type(channel_attachment["path"])[0] + format_ = self.request.query.get("format") width = self.request.query.get("width") height = self.request.query.get("height") - if any([format, width, height]) and current_format.startswith("image/"): + if any([format_, width, height]) and original_format.startswith("image/"): + if not format_: + format_ = original_format.split("/")[1] with Image.open(channel_attachment["path"]) as image: response = web.StreamResponse( status=200, reason="OK", headers={ "Cache-Control": f"public, max-age={1337 * 420}", - "Content-Type": f"image/{format}" if format else current_format, + "Content-Type": f"image/{format_}", "Content-Disposition": f'attachment; filename="{channel_attachment["name"]}"', }, ) @@ -65,19 +66,21 @@ class ChannelAttachmentView(BaseView): ) await response.prepare(self.request) - # response.write is async but image.save is not naughty_steal = response.write - loop = asyncio.get_event_loop() - + tasks = [] def sync_writer(*args, **kwargs): - return loop.run_until_complete(naughty_steal(*args, **kwargs)) - + tasks.append(naughty_steal(*args, **kwargs)) + return True + setattr(response, "write", sync_writer) - image.save(response, format=self.request.query["format"]) + image.save(response, format=format_) setattr(response, "write", naughty_steal) + + await asyncio.gather(*tasks) + return response else: response = web.FileResponse(channel_attachment["path"])