feat: add dm channel sidebar with last message sorting and user context to template rendering

Add channel subscription loading in render_template to populate sidebar with
private DM channels showing other user's nick, sort channels by last_message_on
descending, and inject user object into template context for authenticated sessions.
This commit is contained in:
2025-03-22 19:42:38 +00:00
parent cb2c72ce1d
commit 99cefc6b5f
3 changed files with 31 additions and 6 deletions
+25
View File
@@ -143,6 +143,31 @@ class Application(BaseApplication):
# @time_cache_async(60)
async def render_template(self, template, request, context=None):
channels = []
if not context:
context = {}
if request.session.get("uid"):
async for subscribed_channel in self.services.channel_member.find(user_uid=request.session.get("uid"), deleted_at=None, is_banned=False):
item = {}
other_user = await self.services.channel_member.get_other_dm_user(subscribed_channel["channel_uid"], request.session.get("uid"))
parent_object = await subscribed_channel.get_channel()
last_message =await parent_object.get_last_message()
item["last_message_on"] = parent_object["last_message_on"]
item["is_private"] = parent_object["tag"] == "dm"
if other_user:
item["name"] = other_user["nick"]
item["uid"] = subscribed_channel["channel_uid"]
else:
item["name"] = subscribed_channel["label"]
item["uid"] = subscribed_channel["channel_uid"]
channels.append(item)
channels.sort(key=lambda x: x['last_message_on'] or '', reverse=True)
if not 'channels' in context:
context['channels'] = channels
if not 'user' in context:
context['user'] = await self.services.user.get(uid=request.session.get("uid"))
return await super().render_template(template, request, context)
+2 -3
View File
@@ -17,9 +17,8 @@ class TerminalSocketView(BaseView):
terminal_folder = pathlib.Path("terminal")
for path in terminal_folder.iterdir():
destination_path = root.joinpath(path.name)
if not destination_path.exists():
if not path.is_dir():
destination_path.write_bytes(path.read_bytes())
if not path.is_dir():
destination_path.write_bytes(path.read_bytes())
return root
async def get(self):