Skip to content

Commit 9f3a090

Browse files
committed
🔖 version 0.5.0
1 parent f039e0c commit 9f3a090

5 files changed

Lines changed: 58 additions & 70 deletions

File tree

nonechat/backend/__init__.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ async def list_users(self) -> list[User]:
7575

7676
async def list_channels(self, list_users: bool = False) -> list[Channel]:
7777
data = list(self.storage.channels.values())
78-
data[0] = await self.create_dm(
79-
self.current_user
80-
) # Ensure the first channel is the DM with current user
78+
data.pop(0)
79+
users = [await self.create_dm(self.current_user)]
8180
if list_users:
82-
data = [
83-
(await self.create_dm(user))
81+
users += [
82+
await self.create_dm(user)
8483
for user in self.storage.users.values()
8584
if user.id != self.current_user.id
86-
] + data
87-
return data
85+
]
86+
users.sort(key=lambda x: x._created_at.timestamp(), reverse=True)
87+
return users + data
8888

8989
async def create_dm(self, user: User):
9090
chl = Channel(f"private:{user.id}", user.nickname, "", user.avatar)
@@ -96,9 +96,7 @@ async def list_bots(self) -> list[User]:
9696

9797
async def get_chat_history(self, channel: Union[Channel, None] = None) -> list[MessageEvent]:
9898
_target = (
99-
Channel(
100-
f"private:{self.current_user.id}", self.current_user.nickname, "", self.current_user.avatar
101-
)
99+
await self.create_dm(self.current_user)
102100
if (channel or self.current_channel).id == DIRECT.id
103101
else (channel or self.current_channel)
104102
)
@@ -157,9 +155,7 @@ async def write_chat(self, message: "MessageEvent", channel: Channel):
157155

158156
async def clear_chat_history(self, channel: Union[Channel, None] = None):
159157
_target = (
160-
Channel(
161-
f"private:{self.current_user.id}", self.current_user.nickname, "", self.current_user.avatar
162-
)
158+
await self.create_dm(self.current_user)
163159
if (channel or self.current_channel).id == DIRECT.id
164160
else (channel or self.current_channel)
165161
)

nonechat/components/channel_selector.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self):
5050
self.channel_items: dict[str, tuple[ListItem, tuple[Channel, Optional[User]]]] = {}
5151
self.check_record: dict[str, float] = {} # 用于记录频道的最后活动时间
5252
self.is_bot_mode = self.app.is_bot_mode
53-
self._current_channel: Optional[Channel] = None
53+
self.current_channel: Optional[Channel] = None
5454

5555
@property
5656
def app(self) -> "Frontend":
@@ -87,7 +87,6 @@ async def update_channel_list(self):
8787
await channel_list.clear()
8888
self.channel_items.clear()
8989

90-
# 普通模式:显示频道列表
9190
channels_times = [
9291
(
9392
channel,
@@ -106,14 +105,12 @@ async def update_channel_list(self):
106105
if time != channel._created_at.timestamp():
107106
color = (
108107
"auto"
109-
if self._current_channel and channel.id == self._current_channel.id
108+
if self.current_channel and channel.id == self.current_channel.id
110109
else "lime blink"
111110
)
112111
elif time > self.check_record[channel.id]:
113112
color = (
114-
"auto"
115-
if self._current_channel and channel.id == self._current_channel.id
116-
else "lime blink"
113+
"auto" if self.current_channel and channel.id == self.current_channel.id else "lime blink"
117114
)
118115

119116
if channel.id.startswith("private:"):
@@ -136,41 +133,36 @@ async def update_channel_list(self):
136133

137134
self.channel_items[channel.id] = (item, (channel, orig_user))
138135
await channel_list.append(item)
139-
if self._current_channel and channel.id == self._current_channel.id:
136+
if self.current_channel and channel.id == self.current_channel.id:
140137
item.highlighted = True
141138

142139
async def on_list_view_selected(self, event: ListView.Selected):
143140
"""处理列表项选择事件"""
144141
if event.item and event.item.id and event.item.id.startswith("channel-"):
145142
# 查找对应的频道
146143
for item, slots in self.channel_items.values():
144+
channel, orig_user = slots
145+
if channel.id == DIRECT.id:
146+
ev = ChannelSelectorPressed(
147+
await self.app.backend.create_dm(self.app.backend.current_user),
148+
self.app.backend.current_user,
149+
)
150+
else:
151+
ev = ChannelSelectorPressed(channel, orig_user)
147152
if item == event.item:
148-
channel, orig_user = slots
149-
self._current_channel = channel # 更新当前频道
150-
self.check_record[channel.id] = datetime.now().timestamp() # 更新最后活动时间
151153
key = (
152154
f"dm-{channel.id[8:]}"
153155
if channel.id.startswith("private:")
154-
else (
155-
f"dm-{self.app.backend.current_user.id}"
156-
if channel.id == DIRECT.id
157-
else channel.id
158-
)
156+
else ("dm-direct" if channel.id == DIRECT.id else channel.id)
159157
)
160158
label = self.query_one(f"#label-{key}", Label)
161159
label.update(label.renderable.replace("lime blink", "auto")) # type: ignore
162-
if channel.id == DIRECT.id:
163-
self.post_message(
164-
ChannelSelectorPressed(
165-
await self.app.backend.create_dm(self.app.backend.current_user),
166-
self.app.backend.current_user,
167-
)
168-
)
169-
else:
170-
self.post_message(ChannelSelectorPressed(channel, orig_user))
160+
self.post_message(ev)
171161
item.highlighted = True
162+
self.current_channel = ev.channel
172163
else:
173164
item.highlighted = False
165+
self.check_record[ev.channel.id] = datetime.now().timestamp() # 更新最后活动时间
174166

175167
async def add_new_channel(self):
176168
"""添加新频道的逻辑"""

nonechat/components/sidebar.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,6 @@ def on_unmount(self):
9292

9393
def compose(self):
9494
with TabbedContent():
95-
with TabPane("🤖 机器人列表" if self.is_bot_mode else "👥 用户列表", id="users"):
96-
with Vertical(classes="selector-container"):
97-
yield self.user_selector
98-
with Vertical(classes="button-container"):
99-
yield Button(
100-
"➕ 添加机器人" if self.is_bot_mode else "➕ 添加用户",
101-
id="add-user",
102-
classes="add-button",
103-
)
10495
with TabPane("📺 频道列表", id="channels"):
10596
with Vertical(classes="selector-container"):
10697
yield self.channel_selector
@@ -111,6 +102,15 @@ def compose(self):
111102
classes="add-button",
112103
disabled=self.is_bot_mode,
113104
)
105+
with TabPane("🤖 机器人列表" if self.is_bot_mode else "👥 用户列表", id="users"):
106+
with Vertical(classes="selector-container"):
107+
yield self.user_selector
108+
with Vertical(classes="button-container"):
109+
yield Button(
110+
"➕ 添加机器人" if self.is_bot_mode else "➕ 添加用户",
111+
id="add-user",
112+
classes="add-button",
113+
)
114114

115115
async def on_bot_mode_changed(self, event: "BotModeChanged"):
116116
"""处理模式切换"""

pdm.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = [
77
dependencies = [
88
"textual<4.0.0,>=3.7.0",
99
]
10-
version = "0.4.3"
10+
version = "0.5.0"
1111
requires-python = ">=3.9, <4.0"
1212
readme = "README.md"
1313
license = {text = "MIT"}

0 commit comments

Comments
 (0)