This commit is contained in:
retoor 2025-03-27 17:10:02 +01:00
parent af4a70e894
commit 5390b8bdc3
6 changed files with 58 additions and 31 deletions

View File

@ -160,6 +160,11 @@ class Application(BaseApplication):
other_user = await self.services.channel_member.get_other_dm_user(subscribed_channel["channel_uid"], request.session.get("uid")) 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() parent_object = await subscribed_channel.get_channel()
last_message =await parent_object.get_last_message() last_message =await parent_object.get_last_message()
color = None
if last_message:
last_message_user = await last_message.get_user()
color = last_message_user["color"]
item['color'] = color
item["last_message_on"] = parent_object["last_message_on"] item["last_message_on"] = parent_object["last_message_on"]
item["is_private"] = parent_object["tag"] == "dm" item["is_private"] = parent_object["tag"] == "dm"
if other_user: if other_user:
@ -168,6 +173,9 @@ class Application(BaseApplication):
else: else:
item["name"] = subscribed_channel["label"] item["name"] = subscribed_channel["label"]
item["uid"] = subscribed_channel["channel_uid"] item["uid"] = subscribed_channel["channel_uid"]
item['new_count'] = subscribed_channel['new_count']
print(item)
channels.append(item) channels.append(item)
channels.sort(key=lambda x: x['last_message_on'] or '', reverse=True) channels.sort(key=lambda x: x['last_message_on'] or '', reverse=True)

View File

@ -13,9 +13,10 @@ class ChannelModel(BaseModel):
last_message_on = ModelField(name="last_message_on", required=False, kind=str) last_message_on = ModelField(name="last_message_on", required=False, kind=str)
async def get_last_message(self)->ChannelMessageModel: async def get_last_message(self)->ChannelMessageModel:
async for model in self.app.services.channel_message.query("SELECT * FROM channel_message WHERE channel_uid=:channel_uid ORDER BY created_at DESC LIMIT 1",dict(channel_uid=self['uid'])): async for model in self.app.services.channel_message.query("SELECT uid FROM channel_message WHERE channel_uid=:channel_uid ORDER BY created_at DESC LIMIT 1",dict(channel_uid=self['uid'])):
return model
return await self.app.services.channel_message.get(uid=model['uid'])
return None return None
async def get_members(self): async def get_members(self):
return await self.app.services.channel_member.find(channel_uid=self['uid'],deleted_at=None,is_banned=False) return await self.app.services.channel_member.find(channel_uid=self['uid'],deleted_at=None,is_banned=False)

View File

@ -51,6 +51,7 @@ class NotificationService(BaseService):
model["message"] = ( model["message"] = (
f"New message from {user['nick']} in {channel_member['label']}." f"New message from {user['nick']} in {channel_member['label']}."
) )
if await self.save(model): try:
return model await self.save(model)
raise Exception(f"Failed to create notification: {model.errors}.") except Exception as ex:
raise Exception(f"Failed to create notification: {model.errors}.")

View File

@ -12,13 +12,13 @@
<h2 class="no-select">Channels</h2> <h2 class="no-select">Channels</h2>
<ul> <ul>
{% for channel in channels if not channel['is_private'] %} {% for channel in channels if not channel['is_private'] %}
<li id="channel-list-item-{{channel['uid']}}"><a class="no-select" href="/channel/{{channel['uid']}}.html">{{channel['name']}} <span class="message-count"></span></a></li> <li id="channel-list-item-{{channel['uid']}}"><a class="no-select" {% if channel['color'] %}style="color: {{channel['color']}}"{% endif %} href="/channel/{{channel['uid']}}.html">{{channel['name']}} <span class="message-count">{% if channel['new_count'] %}({{ channel['new_count'] }}){% endif %}</span></a></li>
{% endfor %} {% endfor %}
</ul> </ul>
<h2 class="no-select">Private</h2> <h2 class="no-select">Private</h2>
<ul> <ul>
{% for channel in channels if channel['is_private'] %} {% for channel in channels if channel['is_private'] %}
<li id="channel-list-item-{{channel['uid']}}"><a class="no-select" href="/channel/{{channel['uid']}}.html">{{channel['name']}} <span class="message-count"></span></a></li> <li id="channel-list-item-{{channel['uid']}}"><a {% if channel['color'] %}style="color: {{channel['color']}}"{% endif %} class="no-select" href="/channel/{{channel['uid']}}.html">{{channel['name']}} <span class="message-count">{% if channel['new_count'] %}({{ channel['new_count'] }}){% endif %}</span></a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
@ -28,12 +28,21 @@
constructor(el){ constructor(el){
this.el = el this.el = el
} }
async init(){
const channels = await window.app.rpc.getChannels()
channels.forEach(channel => {
if(channel.color){
this.setMessageCount(channel.uid, channel.new_count)
this.setColor(channel.uid, channel.color)
}
})
}
get channelNodes() { get channelNodes() {
return this.el.querySelectorAll("li") return this.el.querySelectorAll("li")
} }
channelListItemByUid(channelUid){ channelListItemByUid(channelUid){
const id = "channel-list-item-" + channelUid; const id = "channel-list-item-" + channelUid;
console.error(id)
return document.getElementById(id) return document.getElementById(id)
} }
incrementMessageCount(channelUid){ incrementMessageCount(channelUid){
@ -52,11 +61,22 @@
} }
} }
setMessageCount(channelUid, count){ setMessageCount(channelUid, count){
const li = this.channelListItemByUid(channelUid);
const li = this.channelListItemByUid(channelUid);
if(li){ if(li){
li.dataset.messageCount = new String(count) li.dataset.messageCount = new String(count)
li.dataset['messageCount'] = count li.dataset['messageCount'] = count
li.querySelector(".message-count").textContent = '(' + count + ')' if(!count){
li.querySelector(".message-count").textContent = ''
}else{
li.querySelector(".message-count").textContent = '(' + count + ')'
}
}
}
setColor(channelUid, color){
const li = this.channelListItemByUid(channelUid);
if(li){
li.querySelector("a").style.color = color
} }
} }
notify(message){ notify(message){
@ -73,4 +93,7 @@
} }
const channelSidebar = new ChannelSidebar(document.getElementById("channelSidebar")) const channelSidebar = new ChannelSidebar(document.getElementById("channelSidebar"))
document.addEventListener("DOMContentLoaded", () => {
channelSidebar.init()
})
</script> </script>

View File

@ -90,13 +90,20 @@ class RPCView(BaseView):
channels = [] channels = []
async for subscription in self.services.channel_member.find(user_uid=self.user_uid, is_banned=False): async for subscription in self.services.channel_member.find(user_uid=self.user_uid, is_banned=False):
channel = await self.services.channel.get(uid=subscription['channel_uid']) channel = await self.services.channel.get(uid=subscription['channel_uid'])
last_message = await channel.get_last_message()
color = None
if last_message:
last_message_user = await last_message.get_user()
color = last_message_user['color']
channels.append({ channels.append({
"name": subscription["label"], "name": subscription["label"],
"uid": subscription["channel_uid"], "uid": subscription["channel_uid"],
"tag": channel["tag"], "tag": channel["tag"],
"new_count": subscription["new_count"], "new_count": subscription["new_count"],
"is_moderator": subscription["is_moderator"], "is_moderator": subscription["is_moderator"],
"is_read_only": subscription["is_read_only"] "is_read_only": subscription["is_read_only"],
'new_count': subscription['new_count'],
'color': color
}) })
return channels return channels
@ -156,9 +163,11 @@ class RPCView(BaseView):
if result != "noresponse": if result != "noresponse":
await self._send_json({"callId": call_id, "success": success, "data": result}) await self._send_json({"callId": call_id, "success": success, "data": result})
except Exception as ex: except Exception as ex:
print(str(ex), flush=True)
await self._send_json({"callId": call_id, "success": False, "data": str(ex)}) await self._send_json({"callId": call_id, "success": False, "data": str(ex)})
async def _send_json(self, obj): async def _send_json(self, obj):
print(obj, flush=True)
await self.ws.send_str(json.dumps(obj, default=str)) await self.ws.send_str(json.dumps(obj, default=str))

View File

@ -46,6 +46,9 @@ class WebView(BaseView):
channel_member = await self.app.services.channel_member.get(user_uid=self.session.get("uid"), channel_uid=channel["uid"]) channel_member = await self.app.services.channel_member.get(user_uid=self.session.get("uid"), channel_uid=channel["uid"])
if not channel_member: if not channel_member:
return web.HTTPNotFound() return web.HTTPNotFound()
channel_member['new_count'] = 0
await self.app.services.channel_member.save(channel_member)
user = await self.services.user.get(uid=self.session.get("uid")) user = await self.services.user.get(uid=self.session.get("uid"))
messages = [await self.app.services.channel_message.to_extended_dict(message) for message in await self.app.services.channel_message.offset( messages = [await self.app.services.channel_message.to_extended_dict(message) for message in await self.app.services.channel_message.offset(
@ -54,23 +57,5 @@ class WebView(BaseView):
for message in messages: for message in messages:
await self.app.services.notification.mark_as_read(self.session.get("uid"),message["uid"]) await self.app.services.notification.mark_as_read(self.session.get("uid"),message["uid"])
channels = []
async for subscribed_channel in self.app.services.channel_member.find(user_uid=self.session.get("uid"), deleted_at=None, is_banned=False):
item = {}
other_user = await self.app.services.channel_member.get_other_dm_user(subscribed_channel["channel_uid"], self.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)
name = await channel_member.get_name() name = await channel_member.get_name()
return await self.render_template("web.html", {"name": name, "channel": channel,"user": user,"messages": messages , "channels": channels}) return await self.render_template("web.html", {"name": name, "channel": channel,"user": user,"messages": messages})