src/windows/room/mod.rs
352 lines · 9927 bytes
1use matrix_sdk::{
2 room::{Invited, Room as MatrixRoom},
3 ruma::RoomId,
4 DisplayName,
5};
6
7use modalkit::tui::{
8 buffer::Buffer,
9 layout::{Alignment, Rect},
10 style::{Modifier as StyleModifier, Style},
11 text::{Span, Spans, Text},
12 widgets::{Paragraph, StatefulWidget, Widget},
13};
14
15use modalkit::{
16 editing::action::{
17 Action,
18 EditInfo,
19 EditResult,
20 Editable,
21 EditorAction,
22 Jumpable,
23 PromptAction,
24 Promptable,
25 Scrollable,
26 },
27 editing::base::{
28 Axis,
29 CloseFlags,
30 Count,
31 MoveDir1D,
32 OpenTarget,
33 PositionList,
34 ScrollStyle,
35 WordStyle,
36 },
37 input::InputContext,
38 widgets::{TermOffset, TerminalCursor, WindowOps},
39};
40
41use crate::base::{
42 IambError,
43 IambId,
44 IambInfo,
45 IambResult,
46 MessageAction,
47 ProgramAction,
48 ProgramContext,
49 ProgramStore,
50 RoomAction,
51 SendAction,
52};
53
54use self::chat::ChatState;
55use self::space::{Space, SpaceState};
56
57mod chat;
58mod scrollback;
59mod space;
60
61macro_rules! delegate {
62 ($s: expr, $id: ident => $e: expr) => {
63 match $s {
64 RoomState::Chat($id) => $e,
65 RoomState::Space($id) => $e,
66 }
67 };
68}
69
70pub enum RoomState {
71 Chat(ChatState),
72 Space(SpaceState),
73}
74
75impl From<ChatState> for RoomState {
76 fn from(chat: ChatState) -> Self {
77 RoomState::Chat(chat)
78 }
79}
80
81impl From<SpaceState> for RoomState {
82 fn from(space: SpaceState) -> Self {
83 RoomState::Space(space)
84 }
85}
86
87impl RoomState {
88 pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
89 let room_id = room.room_id().to_owned();
90 let info = store.application.get_room_info(room_id);
91 info.name = name.to_string().into();
92
93 if room.is_space() {
94 SpaceState::new(room).into()
95 } else {
96 ChatState::new(room, store).into()
97 }
98 }
99
100 pub fn refresh_room(&mut self, store: &mut ProgramStore) {
101 match self {
102 RoomState::Chat(chat) => chat.refresh_room(store),
103 RoomState::Space(space) => space.refresh_room(store),
104 }
105 }
106
107 fn draw_invite(
108 &self,
109 invited: Invited,
110 area: Rect,
111 buf: &mut Buffer,
112 store: &mut ProgramStore,
113 ) {
114 let inviter = store.application.worker.get_inviter(invited.clone());
115
116 let name = match invited.canonical_alias() {
117 Some(alias) => alias.to_string(),
118 None => format!("{:?}", store.application.get_room_title(self.id())),
119 };
120
121 let mut invited = vec![Span::from(format!(
122 "You have been invited to join {}",
123 name
124 ))];
125
126 if let Ok(Some(inviter)) = &inviter {
127 invited.push(Span::from(" by "));
128 invited.push(store.application.settings.get_user_span(inviter.user_id()));
129 }
130
131 let l1 = Spans(invited);
132 let l2 = Spans::from(
133 "You can run `:invite accept` or `:invite reject` to accept or reject this invitation.",
134 );
135 let text = Text { lines: vec![l1, l2] };
136
137 Paragraph::new(text).alignment(Alignment::Center).render(area, buf);
138
139 return;
140 }
141
142 pub async fn message_command(
143 &mut self,
144 act: MessageAction,
145 ctx: ProgramContext,
146 store: &mut ProgramStore,
147 ) -> IambResult<EditInfo> {
148 match self {
149 RoomState::Chat(chat) => chat.message_command(act, ctx, store).await,
150 RoomState::Space(_) => Err(IambError::NoSelectedMessage.into()),
151 }
152 }
153
154 pub async fn send_command(
155 &mut self,
156 act: SendAction,
157 ctx: ProgramContext,
158 store: &mut ProgramStore,
159 ) -> IambResult<EditInfo> {
160 match self {
161 RoomState::Chat(chat) => chat.send_command(act, ctx, store).await,
162 RoomState::Space(_) => Err(IambError::NoSelectedRoom.into()),
163 }
164 }
165
166 pub async fn room_command(
167 &mut self,
168 act: RoomAction,
169 _: ProgramContext,
170 store: &mut ProgramStore,
171 ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
172 match act {
173 RoomAction::InviteAccept => {
174 if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
175 room.accept_invitation().await.map_err(IambError::from)?;
176
177 Ok(vec![])
178 } else {
179 Err(IambError::NotInvited.into())
180 }
181 },
182 RoomAction::InviteReject => {
183 if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
184 room.reject_invitation().await.map_err(IambError::from)?;
185
186 Ok(vec![])
187 } else {
188 Err(IambError::NotInvited.into())
189 }
190 },
191 RoomAction::InviteSend(user) => {
192 if let Some(room) = store.application.worker.client.get_joined_room(self.id()) {
193 room.invite_user_by_id(user.as_ref()).await.map_err(IambError::from)?;
194
195 Ok(vec![])
196 } else {
197 Err(IambError::NotJoined.into())
198 }
199 },
200 RoomAction::Members(mut cmd) => {
201 let width = Count::Exact(30);
202 let act =
203 cmd.default_axis(Axis::Vertical).default_relation(MoveDir1D::Next).window(
204 OpenTarget::Application(IambId::MemberList(self.id().to_owned())),
205 width.into(),
206 );
207
208 Ok(vec![(act, cmd.context.take())])
209 },
210 RoomAction::Set(field) => {
211 store.application.worker.set_room(self.id().to_owned(), field)?;
212
213 Ok(vec![])
214 },
215 }
216 }
217
218 pub fn get_title(&self, store: &mut ProgramStore) -> Spans {
219 let title = store.application.get_room_title(self.id());
220 let style = Style::default().add_modifier(StyleModifier::BOLD);
221 let mut spans = vec![Span::styled(title, style)];
222
223 match self.room().topic() {
224 Some(desc) if !desc.is_empty() => {
225 spans.push(" (".into());
226 spans.push(desc.into());
227 spans.push(")".into());
228 },
229 _ => {},
230 }
231
232 Spans(spans)
233 }
234
235 pub fn focus_toggle(&mut self) {
236 match self {
237 RoomState::Chat(chat) => chat.focus_toggle(),
238 RoomState::Space(_) => return,
239 }
240 }
241
242 pub fn room(&self) -> &MatrixRoom {
243 match self {
244 RoomState::Chat(chat) => chat.room(),
245 RoomState::Space(space) => space.room(),
246 }
247 }
248
249 pub fn id(&self) -> &RoomId {
250 match self {
251 RoomState::Chat(chat) => chat.id(),
252 RoomState::Space(space) => space.id(),
253 }
254 }
255}
256
257impl Editable<ProgramContext, ProgramStore, IambInfo> for RoomState {
258 fn editor_command(
259 &mut self,
260 act: &EditorAction,
261 ctx: &ProgramContext,
262 store: &mut ProgramStore,
263 ) -> EditResult<EditInfo, IambInfo> {
264 delegate!(self, w => w.editor_command(act, ctx, store))
265 }
266}
267
268impl Jumpable<ProgramContext, IambInfo> for RoomState {
269 fn jump(
270 &mut self,
271 list: PositionList,
272 dir: MoveDir1D,
273 count: usize,
274 ctx: &ProgramContext,
275 ) -> IambResult<usize> {
276 delegate!(self, w => w.jump(list, dir, count, ctx))
277 }
278}
279
280impl Scrollable<ProgramContext, ProgramStore, IambInfo> for RoomState {
281 fn scroll(
282 &mut self,
283 style: &ScrollStyle,
284 ctx: &ProgramContext,
285 store: &mut ProgramStore,
286 ) -> EditResult<EditInfo, IambInfo> {
287 delegate!(self, w => w.scroll(style, ctx, store))
288 }
289}
290
291impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomState {
292 fn prompt(
293 &mut self,
294 act: &PromptAction,
295 ctx: &ProgramContext,
296 store: &mut ProgramStore,
297 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
298 delegate!(self, w => w.prompt(act, ctx, store))
299 }
300}
301
302impl TerminalCursor for RoomState {
303 fn get_term_cursor(&self) -> Option<TermOffset> {
304 delegate!(self, w => w.get_term_cursor())
305 }
306}
307
308impl WindowOps<IambInfo> for RoomState {
309 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
310 if let MatrixRoom::Invited(_) = self.room() {
311 self.refresh_room(store);
312 }
313
314 if let MatrixRoom::Invited(invited) = self.room() {
315 self.draw_invite(invited.clone(), area, buf, store);
316 }
317
318 match self {
319 RoomState::Chat(chat) => chat.draw(area, buf, focused, store),
320 RoomState::Space(space) => {
321 Space::new(store).focus(focused).render(area, buf, space);
322 },
323 }
324 }
325
326 fn dup(&self, store: &mut ProgramStore) -> Self {
327 match self {
328 RoomState::Chat(chat) => RoomState::Chat(chat.dup(store)),
329 RoomState::Space(space) => RoomState::Space(space.dup(store)),
330 }
331 }
332
333 fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
334 // XXX: what's the right closing behaviour for a room?
335 // Should write send a message?
336 true
337 }
338
339 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
340 match self {
341 RoomState::Chat(chat) => chat.get_cursor_word(style),
342 RoomState::Space(space) => space.get_cursor_word(style),
343 }
344 }
345
346 fn get_selected_word(&self) -> Option<String> {
347 match self {
348 RoomState::Chat(chat) => chat.get_selected_word(),
349 RoomState::Space(space) => space.get_selected_word(),
350 }
351 }
352}