Commit b7ae01499b

b7ae01499b1dd84044b837ccedabc99ef0acfb31

parent: 88af9bfec3

Unregistered key

FormindVER <valentin.eudeline-remy@formind.fr> · 2024-02-28 02:36 UTC
committer: <git@ulyssa.dev>

Add a new `:chats` window that lists both DMs and Rooms (#184)

Fixes #172

Layout: unified · split

src/base.rs +17 −2
@@ -1172,7 +1172,7 @@ pub enum IambId {
11721172 /// A Matrix room.
11731173 Room(OwnedRoomId),
11741174
1175 /// The `:rooms` window.
1175 /// The `:dms` window.
11761176 DirectList,
11771177
11781178 /// The `:members` window for a given Matrix room.
@@ -1189,6 +1189,9 @@ pub enum IambId {
11891189
11901190 /// The `:welcome` window.
11911191 Welcome,
1192
1193 /// The `:chats` window.
1194 ChatList,
11921195}
11931196
11941197impl Display for IambId {
@@ -1205,6 +1208,7 @@ impl Display for IambId {
12051208 IambId::SpaceList => f.write_str("iamb://spaces"),
12061209 IambId::VerifyList => f.write_str("iamb://verify"),
12071210 IambId::Welcome => f.write_str("iamb://welcome"),
1211 IambId::ChatList => f.write_str("iamb://chats"),
12081212 }
12091213 }
12101214}
@@ -1317,6 +1321,13 @@ impl<'de> Visitor<'de> for IambIdVisitor {
13171321
13181322 Ok(IambId::Welcome)
13191323 },
1324 Some("chats") => {
1325 if url.path() != "" {
1326 return Err(E::custom("iamb://chats takes no path"));
1327 }
1328
1329 Ok(IambId::ChatList)
1330 },
13201331 Some(s) => Err(E::custom(format!("{s:?} is not a valid window"))),
13211332 None => Err(E::custom("Invalid iamb window URL")),
13221333 }
@@ -1374,6 +1385,9 @@ pub enum IambBufferId {
13741385
13751386 /// The buffer for the `:rooms` window.
13761387 Welcome,
1388
1389 /// The `:chats` window.
1390 ChatList,
13771391}
13781392
13791393impl IambBufferId {
@@ -1388,6 +1402,7 @@ impl IambBufferId {
13881402 IambBufferId::SpaceList => Some(IambId::SpaceList),
13891403 IambBufferId::VerifyList => Some(IambId::VerifyList),
13901404 IambBufferId::Welcome => Some(IambId::Welcome),
1405 IambBufferId::ChatList => Some(IambId::ChatList),
13911406 }
13921407 }
13931408}
@@ -1410,7 +1425,6 @@ impl ApplicationInfo for IambInfo {
14101425 match content {
14111426 IambBufferId::Command(CommandType::Command) => complete_cmdbar(text, cursor, store),
14121427 IambBufferId::Command(CommandType::Search) => vec![],
1413
14141428 IambBufferId::Room(_, RoomFocus::MessageBar) => complete_msgbar(text, cursor, store),
14151429 IambBufferId::Room(_, RoomFocus::Scrollback) => vec![],
14161430
@@ -1420,6 +1434,7 @@ impl ApplicationInfo for IambInfo {
14201434 IambBufferId::SpaceList => vec![],
14211435 IambBufferId::VerifyList => vec![],
14221436 IambBufferId::Welcome => vec![],
1437 IambBufferId::ChatList => vec![],
14231438 }
14241439 }
14251440
src/commands.rs +16
@@ -292,6 +292,17 @@ fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
292292 return Ok(step);
293293}
294294
295fn iamb_chats(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
296 if !desc.arg.text.is_empty() {
297 return Result::Err(CommandError::InvalidArgument);
298 }
299
300 let open = ctx.switch(OpenTarget::Application(IambId::ChatList));
301 let step = CommandStep::Continue(open, ctx.context.take());
302
303 return Ok(step);
304}
305
295306fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
296307 if !desc.arg.text.is_empty() {
297308 return Result::Err(CommandError::InvalidArgument);
@@ -495,6 +506,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
495506 aliases: vec![],
496507 f: iamb_create,
497508 });
509 cmds.add_command(ProgramCommand {
510 name: "chats".into(),
511 aliases: vec![],
512 f: iamb_chats,
513 });
498514 cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
499515 cmds.add_command(ProgramCommand {
500516 name: "download".into(),
src/config.rs +5 −1
@@ -227,6 +227,7 @@ pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>;
227227
228228fn merge_sorts(a: SortOverrides, b: SortOverrides) -> SortOverrides {
229229 SortOverrides {
230 chats: b.chats.or(a.chats),
230231 dms: b.dms.or(a.dms),
231232 rooms: b.rooms.or(a.rooms),
232233 spaces: b.spaces.or(a.spaces),
@@ -308,6 +309,7 @@ pub struct ImagePreviewProtocolValues {
308309
309310#[derive(Clone)]
310311pub struct SortValues {
312 pub chats: Vec<SortColumn<SortFieldRoom>>,
311313 pub dms: Vec<SortColumn<SortFieldRoom>>,
312314 pub rooms: Vec<SortColumn<SortFieldRoom>>,
313315 pub spaces: Vec<SortColumn<SortFieldRoom>>,
@@ -316,6 +318,7 @@ pub struct SortValues {
316318
317319#[derive(Clone, Default, Deserialize)]
318320pub struct SortOverrides {
321 pub chats: Option<Vec<SortColumn<SortFieldRoom>>>,
319322 pub dms: Option<Vec<SortColumn<SortFieldRoom>>>,
320323 pub rooms: Option<Vec<SortColumn<SortFieldRoom>>>,
321324 pub spaces: Option<Vec<SortColumn<SortFieldRoom>>>,
@@ -325,11 +328,12 @@ pub struct SortOverrides {
325328impl SortOverrides {
326329 pub fn values(self) -> SortValues {
327330 let rooms = self.rooms.unwrap_or_else(|| Vec::from(DEFAULT_ROOM_SORT));
331 let chats = self.chats.unwrap_or_else(|| rooms.clone());
328332 let dms = self.dms.unwrap_or_else(|| rooms.clone());
329333 let spaces = self.spaces.unwrap_or_else(|| rooms.clone());
330334 let members = self.members.unwrap_or_else(|| Vec::from(DEFAULT_MEMBERS_SORT));
331335
332 SortValues { rooms, members, dms, spaces }
336 SortValues { rooms, members, chats, dms, spaces }
333337 }
334338}
335339
src/message/mod.rs +2 −2
@@ -196,7 +196,7 @@ impl Ord for MessageTimeStamp {
196196
197197impl PartialOrd for MessageTimeStamp {
198198 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
199 self.cmp(other).into()
199 Some(self.cmp(other))
200200 }
201201}
202202
@@ -340,7 +340,7 @@ impl Ord for MessageCursor {
340340
341341impl PartialOrd for MessageCursor {
342342 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
343 self.cmp(other).into()
343 Some(self.cmp(other))
344344 }
345345}
346346
src/windows/mod.rs +169 −13
@@ -215,28 +215,34 @@ fn user_fields_cmp(
215215 user_cmp(a, b, &SortFieldUser::UserId)
216216}
217217
218fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) {
218fn tag_to_span(tag: &TagName, style: Style) -> Vec<Span<'_>> {
219 match tag {
220 TagName::Favorite => vec![Span::styled("Favorite", style)],
221 TagName::LowPriority => vec![Span::styled("Low Priority", style)],
222 TagName::ServerNotice => vec![Span::styled("Server Notice", style)],
223 TagName::User(tag) => {
224 vec![
225 Span::styled("User Tag: ", style),
226 Span::styled(tag.as_ref(), style),
227 ]
228 },
229 tag => vec![Span::styled(format!("{tag:?}"), style)],
230 }
231}
232
233fn append_tags<'a>(tags: Vec<Vec<Span<'a>>>, spans: &mut Vec<Span<'a>>, style: Style) {
219234 if tags.is_empty() {
220235 return;
221236 }
222237
223238 spans.push(Span::styled(" (", style));
224239
225 for (i, tag) in tags.keys().enumerate() {
240 for (i, tag) in tags.into_iter().enumerate() {
226241 if i > 0 {
227242 spans.push(Span::styled(", ", style));
228243 }
229244
230 match tag {
231 TagName::Favorite => spans.push(Span::styled("Favorite", style)),
232 TagName::LowPriority => spans.push(Span::styled("Low Priority", style)),
233 TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)),
234 TagName::User(tag) => {
235 spans.push(Span::styled("User Tag: ", style));
236 spans.push(Span::styled(tag.as_ref(), style));
237 },
238 tag => spans.push(Span::styled(format!("{tag:?}"), style)),
239 }
245 spans.extend(tag);
240246 }
241247
242248 spans.push(Span::styled(")", style));
@@ -289,6 +295,7 @@ macro_rules! delegate {
289295 IambWindow::SpaceList($id) => $e,
290296 IambWindow::VerifyList($id) => $e,
291297 IambWindow::Welcome($id) => $e,
298 IambWindow::ChatList($id) => $e,
292299 }
293300 };
294301}
@@ -301,6 +308,7 @@ pub enum IambWindow {
301308 RoomList(RoomListState),
302309 SpaceList(SpaceListState),
303310 Welcome(WelcomeState),
311 ChatList(ChatListState),
304312}
305313
306314impl IambWindow {
@@ -355,9 +363,16 @@ impl IambWindow {
355363pub type DirectListState = ListState<DirectItem, IambInfo>;
356364pub type MemberListState = ListState<MemberItem, IambInfo>;
357365pub type RoomListState = ListState<RoomItem, IambInfo>;
366pub type ChatListState = ListState<GenericChatItem, IambInfo>;
358367pub type SpaceListState = ListState<SpaceItem, IambInfo>;
359368pub type VerifyListState = ListState<VerifyItem, IambInfo>;
360369
370impl From<ChatListState> for IambWindow {
371 fn from(list: ChatListState) -> Self {
372 IambWindow::ChatList(list)
373 }
374}
375
361376impl From<RoomState> for IambWindow {
362377 fn from(room: RoomState) -> Self {
363378 IambWindow::Room(room)
@@ -514,6 +529,37 @@ impl WindowOps<IambInfo> for IambWindow {
514529 .focus(focused)
515530 .render(area, buf, state);
516531 },
532 IambWindow::ChatList(state) => {
533 let mut items = store
534 .application
535 .sync_info
536 .rooms
537 .clone()
538 .into_iter()
539 .map(|room_info| GenericChatItem::new(room_info, store, false))
540 .collect::<Vec<_>>();
541
542 let dms = store
543 .application
544 .sync_info
545 .dms
546 .clone()
547 .into_iter()
548 .map(|room_info| GenericChatItem::new(room_info, store, true));
549
550 items.extend(dms);
551
552 let fields = &store.application.settings.tunables.sort.chats;
553 items.sort_by(|a, b| room_fields_cmp(a, b, fields));
554
555 state.set(items);
556
557 List::new(store)
558 .empty_message("You do not have rooms or dms yet")
559 .empty_alignment(Alignment::Center)
560 .focus(focused)
561 .render(area, buf, state);
562 },
517563 IambWindow::SpaceList(state) => {
518564 let mut items = store
519565 .application
@@ -564,6 +610,7 @@ impl WindowOps<IambInfo> for IambWindow {
564610 IambWindow::SpaceList(w) => w.dup(store).into(),
565611 IambWindow::VerifyList(w) => w.dup(store).into(),
566612 IambWindow::Welcome(w) => w.dup(store).into(),
613 IambWindow::ChatList(w) => w.dup(store).into(),
567614 }
568615 }
569616
@@ -603,6 +650,7 @@ impl Window<IambInfo> for IambWindow {
603650 IambWindow::SpaceList(_) => IambId::SpaceList,
604651 IambWindow::VerifyList(_) => IambId::VerifyList,
605652 IambWindow::Welcome(_) => IambId::Welcome,
653 IambWindow::ChatList(_) => IambId::ChatList,
606654 }
607655 }
608656
@@ -613,6 +661,7 @@ impl Window<IambInfo> for IambWindow {
613661 IambWindow::SpaceList(_) => bold_spans("Spaces"),
614662 IambWindow::VerifyList(_) => bold_spans("Verifications"),
615663 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
664 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
616665
617666 IambWindow::Room(w) => {
618667 let title = store.application.get_room_title(w.id());
@@ -639,6 +688,7 @@ impl Window<IambInfo> for IambWindow {
639688 IambWindow::SpaceList(_) => bold_spans("Spaces"),
640689 IambWindow::VerifyList(_) => bold_spans("Verifications"),
641690 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
691 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
642692
643693 IambWindow::Room(w) => w.get_title(store),
644694 IambWindow::MemberList(state, room_id, _) => {
@@ -695,6 +745,11 @@ impl Window<IambInfo> for IambWindow {
695745
696746 return Ok(win.into());
697747 },
748 IambId::ChatList => {
749 let list = ChatListState::new(IambBufferId::ChatList, vec![]);
750
751 Ok(list.into())
752 },
698753 }
699754 }
700755
@@ -729,6 +784,105 @@ impl Window<IambInfo> for IambWindow {
729784 }
730785}
731786
787#[derive(Clone)]
788pub struct GenericChatItem {
789 room_info: MatrixRoomInfo,
790 name: String,
791 alias: Option<OwnedRoomAliasId>,
792 is_dm: bool,
793}
794
795impl GenericChatItem {
796 fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore, is_dm: bool) -> Self {
797 let room = &room_info.deref().0;
798 let room_id = room.room_id();
799
800 let info = store.application.get_room_info(room_id.to_owned());
801
802 let name = info.name.clone().unwrap_or_default();
803 let alias = room.canonical_alias();
804 info.tags = room_info.deref().1.clone();
805
806 if let Some(alias) = &alias {
807 store.application.names.insert(alias.to_string(), room_id.to_owned());
808 }
809
810 GenericChatItem { room_info, name, alias, is_dm }
811 }
812
813 #[inline]
814 fn room(&self) -> &MatrixRoom {
815 &self.room_info.deref().0
816 }
817
818 #[inline]
819 fn tags(&self) -> &Option<Tags> {
820 &self.room_info.deref().1
821 }
822}
823
824impl RoomLikeItem for GenericChatItem {
825 fn name(&self) -> &str {
826 self.name.as_str()
827 }
828
829 fn alias(&self) -> Option<&RoomAliasId> {
830 self.alias.as_deref()
831 }
832
833 fn room_id(&self) -> &RoomId {
834 self.room().room_id()
835 }
836
837 fn has_tag(&self, tag: TagName) -> bool {
838 if let Some(tags) = &self.room_info.deref().1 {
839 tags.contains_key(&tag)
840 } else {
841 false
842 }
843 }
844}
845
846impl ToString for GenericChatItem {
847 fn to_string(&self) -> String {
848 return self.name.clone();
849 }
850}
851
852impl ListItem<IambInfo> for GenericChatItem {
853 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
854 let style = selected_style(selected);
855 let mut spans = vec![Span::styled(self.name.as_str(), style)];
856 let mut labels = if self.is_dm {
857 vec![vec![Span::styled("DM", style)]]
858 } else {
859 vec![vec![Span::styled("Room", style)]]
860 };
861
862 if let Some(tags) = &self.tags() {
863 labels.extend(tags.keys().map(|t| tag_to_span(t, style)));
864 }
865
866 append_tags(labels, &mut spans, style);
867 Text::from(Line::from(spans))
868 }
869
870 fn get_word(&self) -> Option<String> {
871 self.room_id().to_string().into()
872 }
873}
874
875impl Promptable<ProgramContext, ProgramStore, IambInfo> for GenericChatItem {
876 fn prompt(
877 &mut self,
878 act: &PromptAction,
879 ctx: &ProgramContext,
880 _: &mut ProgramStore,
881 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
882 room_prompt(self.room_id(), act, ctx)
883 }
884}
885
732886#[derive(Clone)]
733887pub struct RoomItem {
734888 room_info: MatrixRoomInfo,
@@ -797,6 +951,7 @@ impl ListItem<IambInfo> for RoomItem {
797951 if let Some(tags) = &self.tags() {
798952 let style = selected_style(selected);
799953 let mut spans = vec![Span::styled(self.name.as_str(), style)];
954 let tags = tags.keys().map(|t| tag_to_span(t, style)).collect();
800955
801956 append_tags(tags, &mut spans, style);
802957
@@ -882,6 +1037,7 @@ impl ListItem<IambInfo> for DirectItem {
8821037 if let Some(tags) = &self.tags() {
8831038 let style = selected_style(selected);
8841039 let mut spans = vec![Span::styled(self.name.as_str(), style)];
1040 let tags = tags.keys().map(|t| tag_to_span(t, style)).collect();
8851041
8861042 append_tags(tags, &mut spans, style);
8871043
@@ -1069,7 +1225,7 @@ impl Ord for VerifyItem {
10691225
10701226impl PartialOrd for VerifyItem {
10711227 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1072 self.cmp(other).into()
1228 Some(self.cmp(other))
10731229 }
10741230}
10751231
src/windows/welcome.md +1
@@ -12,6 +12,7 @@
1212
1313- `:dms` will open a list of direct messages
1414- `:rooms` will open a list of joined rooms
15- `:chats` will open a list containing both direct messages and rooms
1516- `:members` will open a list of members for the currently focused room or space
1617- `:spaces` will open a list of joined spaces
1718- `:join` can be used to switch to join a new room or start a direct message
src/worker.rs +1 −1
@@ -832,7 +832,7 @@ impl ClientWorker {
832832 let room_id = room.room_id().to_owned();
833833 let room_name = Some(room_name.to_string());
834834 let mut locked = store.lock().await;
835 let mut info = locked.application.rooms.get_or_default(room_id.clone());
835 let info = locked.application.rooms.get_or_default(room_id.clone());
836836 info.name = room_name;
837837 }
838838 }