Commit 01d19c68e8
Unsigned
Layout: unified · split
src/render.rs +220 −202
| @@ -4,6 +4,7 @@ use url::Url; | ||
| 4 | 4 | use matrix_sdk::{ |
| 5 | 5 | events::{ |
| 6 | 6 | room::{ |
| 7 | encrypted::EncryptedEventContent, | |
| 7 | 8 | member::{MemberEventContent, MembershipChange}, |
| 8 | 9 | message::{ |
| 9 | 10 | AudioMessageEventContent, EmoteMessageEventContent, |
| @@ -98,202 +99,6 @@ pub trait Render { | ||
| 98 | 99 | fn render(&self, context: &Self::RenderContext) -> RenderedContent; |
| 99 | 100 | } |
| 100 | 101 | |
| 101 | /// Rendering implementation for membership events (joins, leaves, bans, profile | |
| 102 | /// changes, etc). | |
| 103 | pub fn render_membership<'a>( | |
| 104 | event: &SyncStateEvent<MemberEventContent>, | |
| 105 | sender: &WeechatRoomMember, | |
| 106 | target: &WeechatRoomMember, | |
| 107 | ) -> String { | |
| 108 | const TAGS: &'static [&'static str] = &["matrix_membership"]; | |
| 109 | use MembershipChange::*; | |
| 110 | let change_op = event.membership_change(); | |
| 111 | ||
| 112 | let operation = match change_op { | |
| 113 | None => "did nothing", | |
| 114 | Error => "caused an error", // must never happen | |
| 115 | Joined => "has joined the room", | |
| 116 | Left => "has left the room", | |
| 117 | Banned => "was banned by", | |
| 118 | Unbanned => "was unbanned by", | |
| 119 | Kicked => "was kicked from the room by", | |
| 120 | Invited => "was invited to the room by", | |
| 121 | KickedAndBanned => "was kicked and banned by", | |
| 122 | InvitationRejected => "rejected the invitation", | |
| 123 | InvitationRevoked => "had the invitation revoked by", | |
| 124 | ProfileChanged { .. } => "_", | |
| 125 | NotImplemented => "performed an unimplemented operation", | |
| 126 | _ => "caused an unhandled membership change", | |
| 127 | }; | |
| 128 | ||
| 129 | fn formatted_name(member: &WeechatRoomMember) -> String { | |
| 130 | match &member.display_name { | |
| 131 | Some(display_name) => { | |
| 132 | format!( | |
| 133 | "{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", | |
| 134 | name = display_name, | |
| 135 | user_id = &member.user_id, | |
| 136 | color_delim = Weechat::color("chat_delimiters"), | |
| 137 | color_reset = Weechat::color("reset")) | |
| 138 | } | |
| 139 | ||
| 140 | Option::None => { | |
| 141 | member.user_id.as_ref().to_string() | |
| 142 | } | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | let (prefix, color_action) = match change_op { | |
| 147 | Joined => ("join", "green"), | |
| 148 | Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), | |
| 149 | _ => ("quit", "red"), | |
| 150 | }; | |
| 151 | ||
| 152 | let color_action = Weechat::color(color_action); | |
| 153 | let color_reset = Weechat::color("reset"); | |
| 154 | ||
| 155 | let operation = format!( | |
| 156 | "{color_action}{op}{color_reset}", | |
| 157 | color_action = color_action, | |
| 158 | op = operation, | |
| 159 | color_reset = color_reset | |
| 160 | ); | |
| 161 | ||
| 162 | let target_name = format!( | |
| 163 | "{color_user}{target_name}{color_reset}", | |
| 164 | target_name = formatted_name(target), | |
| 165 | color_user = Weechat::color("reset"), // TODO | |
| 166 | color_reset = Weechat::color("reset") | |
| 167 | ); | |
| 168 | ||
| 169 | let sender_name = format!( | |
| 170 | "{color_user}{sender_name}{color_reset}", | |
| 171 | sender_name = formatted_name(sender), | |
| 172 | color_user = Weechat::color("reset"), // TODO | |
| 173 | color_reset = Weechat::color("reset") | |
| 174 | ); | |
| 175 | ||
| 176 | let message = match change_op { | |
| 177 | ProfileChanged { | |
| 178 | displayname_changed, | |
| 179 | avatar_url_changed, | |
| 180 | } => { | |
| 181 | let new_display_name = &event.content.displayname; | |
| 182 | ||
| 183 | // TODO: Should we display the new avatar URL? | |
| 184 | // let new_avatar = self.content.avatar_url.as_ref(); | |
| 185 | ||
| 186 | match (displayname_changed, avatar_url_changed) { | |
| 187 | (false, true) => | |
| 188 | format!( | |
| 189 | "{prefix} {target} {color_action}changed their avatar{color_reset}", | |
| 190 | prefix = Weechat::prefix(prefix), | |
| 191 | target = target_name, | |
| 192 | color_action = color_action, | |
| 193 | color_reset = color_reset | |
| 194 | ), | |
| 195 | (true, false) => { | |
| 196 | match new_display_name { | |
| 197 | Some(name) => format!( | |
| 198 | "{prefix} {target} {color_action}changed their display name to{color_reset} {new}", | |
| 199 | prefix = Weechat::prefix(prefix), | |
| 200 | target = target_name, | |
| 201 | new = name, | |
| 202 | color_action = color_action, | |
| 203 | color_reset = color_reset | |
| 204 | ), | |
| 205 | Option::None => format!( | |
| 206 | "{prefix} {target} {color_action}removed their display name{color_reset}", | |
| 207 | prefix = Weechat::prefix(prefix), | |
| 208 | target = target_name, | |
| 209 | color_action = color_action, | |
| 210 | color_reset = color_reset | |
| 211 | ), | |
| 212 | } | |
| 213 | } | |
| 214 | (true, true) => | |
| 215 | match new_display_name { | |
| 216 | Some(name) => format!( | |
| 217 | "{prefix} {target} {color_action}changed their avatar \ | |
| 218 | and changed their display name to{color_reset} {new}", | |
| 219 | prefix = Weechat::prefix(prefix), | |
| 220 | target = target_name, | |
| 221 | new = name, | |
| 222 | color_action = color_action, | |
| 223 | color_reset = color_reset | |
| 224 | ), | |
| 225 | Option::None => format!( | |
| 226 | "{prefix} {target} {color_action}changed their \ | |
| 227 | avatar and removed display name{color_reset}", | |
| 228 | prefix = Weechat::prefix(prefix), | |
| 229 | target = target_name, | |
| 230 | color_action = color_action, | |
| 231 | color_reset = color_reset | |
| 232 | ), | |
| 233 | } | |
| 234 | (false, false) => | |
| 235 | "Cannot happen: got profile changed but nothing really changed".to_string() | |
| 236 | } | |
| 237 | } | |
| 238 | None | Error | Joined | Left | InvitationRejected | NotImplemented => { | |
| 239 | format!( | |
| 240 | "{prefix} {target} {op}", | |
| 241 | prefix = Weechat::prefix(prefix), | |
| 242 | target = target_name, | |
| 243 | op = operation | |
| 244 | ) | |
| 245 | } | |
| 246 | Banned | Unbanned | Kicked | Invited | InvitationRevoked | |
| 247 | | KickedAndBanned => format!( | |
| 248 | "{prefix} {target} {op} {sender}", | |
| 249 | prefix = Weechat::prefix(prefix), | |
| 250 | target = target_name, | |
| 251 | op = operation, | |
| 252 | sender = sender_name | |
| 253 | ), | |
| 254 | ||
| 255 | // This means an unsupported membership change happened so we just | |
| 256 | // print a generic message to indicate this. | |
| 257 | _ => format!( | |
| 258 | "{prefix} {sender} {op}", | |
| 259 | prefix = Weechat::prefix(prefix), | |
| 260 | sender = sender_name, | |
| 261 | op = operation, | |
| 262 | ), | |
| 263 | }; | |
| 264 | ||
| 265 | // TODO we should return the tags as well. | |
| 266 | message | |
| 267 | } | |
| 268 | ||
| 269 | impl Render for dyn HasUrlOrFile { | |
| 270 | type RenderContext = Url; | |
| 271 | const TAGS: &'static [&'static str] = &["matrix_media"]; | |
| 272 | ||
| 273 | fn render(&self, _homeserver: &Self::RenderContext) -> RenderedContent { | |
| 274 | let message = format!( | |
| 275 | "{color_delimiter}<{color_reset}{}{color_delimiter}>\ | |
| 276 | [{color_reset}{}{color_delimiter}]{color_reset}", | |
| 277 | self.body(), | |
| 278 | // FIXME this isn't right, the MXID -> URL transformation depends on | |
| 279 | // your homeserver URL. | |
| 280 | self.resolve_url(), | |
| 281 | color_delimiter = Weechat::color("color_delimiter"), | |
| 282 | color_reset = Weechat::color("reset") | |
| 283 | ); | |
| 284 | ||
| 285 | let line = RenderedLine { | |
| 286 | message, | |
| 287 | tags: self.tags(), | |
| 288 | }; | |
| 289 | ||
| 290 | ||
| 291 | RenderedContent { | |
| 292 | lines: vec![line], | |
| 293 | } | |
| 294 | } | |
| 295 | } | |
| 296 | ||
| 297 | 102 | impl Render for TextMessageEventContent { |
| 298 | 103 | const TAGS: &'static [&'static str] = &["matrix_media"]; |
| 299 | 104 | type RenderContext = (); |
| @@ -388,9 +193,7 @@ impl Render for NoticeMessageEventContent { | ||
| 388 | 193 | tags: self.tags(), |
| 389 | 194 | }; |
| 390 | 195 | |
| 391 | RenderedContent { | |
| 392 | lines: vec![line], | |
| 393 | } | |
| 196 | RenderedContent { lines: vec![line] } | |
| 394 | 197 | } |
| 395 | 198 | } |
| 396 | 199 | |
| @@ -419,9 +222,56 @@ impl Render for ServerNoticeMessageEventContent { | ||
| 419 | 222 | tags: self.tags(), |
| 420 | 223 | }; |
| 421 | 224 | |
| 422 | RenderedContent { | |
| 423 | lines: vec![line], | |
| 424 | } | |
| 225 | RenderedContent { lines: vec![line] } | |
| 226 | } | |
| 227 | } | |
| 228 | ||
| 229 | impl Render for dyn HasUrlOrFile { | |
| 230 | type RenderContext = Url; | |
| 231 | const TAGS: &'static [&'static str] = &["matrix_media"]; | |
| 232 | ||
| 233 | fn render(&self, _homeserver: &Self::RenderContext) -> RenderedContent { | |
| 234 | let message = format!( | |
| 235 | "{color_delimiter}<{color_reset}{}{color_delimiter}>\ | |
| 236 | [{color_reset}{}{color_delimiter}]{color_reset}", | |
| 237 | self.body(), | |
| 238 | // FIXME this isn't right, the MXID -> URL transformation depends on | |
| 239 | // your homeserver URL. | |
| 240 | self.resolve_url(), | |
| 241 | color_delimiter = Weechat::color("color_delimiter"), | |
| 242 | color_reset = Weechat::color("reset") | |
| 243 | ); | |
| 244 | ||
| 245 | let line = RenderedLine { | |
| 246 | message, | |
| 247 | tags: self.tags(), | |
| 248 | }; | |
| 249 | ||
| 250 | RenderedContent { lines: vec![line] } | |
| 251 | } | |
| 252 | } | |
| 253 | ||
| 254 | impl Render for EncryptedEventContent { | |
| 255 | const TAGS: &'static [&'static str] = &["matrix_encrypted"]; | |
| 256 | type RenderContext = (); | |
| 257 | ||
| 258 | fn render(&self, _: &Self::RenderContext) -> RenderedContent { | |
| 259 | let message = format!( | |
| 260 | "{}<{}Unable to decrypt message{}>{}", | |
| 261 | Weechat::color("chat_delimiters"), | |
| 262 | Weechat::color("logger.color.backlog_line"), | |
| 263 | Weechat::color("chat_delimiters"), | |
| 264 | Weechat::color("reset"), | |
| 265 | ); | |
| 266 | ||
| 267 | let line = RenderedLine { | |
| 268 | message, | |
| 269 | // TODO add tags that allow us decrypt the event at a later point in | |
| 270 | // time, sender key, algorithm, session id. | |
| 271 | tags: self.tags(), | |
| 272 | }; | |
| 273 | ||
| 274 | RenderedContent { lines: vec![line] } | |
| 425 | 275 | } |
| 426 | 276 | } |
| 427 | 277 | |
| @@ -634,3 +484,171 @@ has_url_or_file!(AudioMessageEventContent); | ||
| 634 | 484 | has_url_or_file!(FileMessageEventContent); |
| 635 | 485 | has_url_or_file!(ImageMessageEventContent); |
| 636 | 486 | has_url_or_file!(VideoMessageEventContent); |
| 487 | ||
| 488 | /// Rendering implementation for membership events (joins, leaves, bans, profile | |
| 489 | /// changes, etc). | |
| 490 | pub fn render_membership<'a>( | |
| 491 | event: &SyncStateEvent<MemberEventContent>, | |
| 492 | sender: &WeechatRoomMember, | |
| 493 | target: &WeechatRoomMember, | |
| 494 | ) -> String { | |
| 495 | const TAGS: &'static [&'static str] = &["matrix_membership"]; | |
| 496 | use MembershipChange::*; | |
| 497 | let change_op = event.membership_change(); | |
| 498 | ||
| 499 | let operation = match change_op { | |
| 500 | None => "did nothing", | |
| 501 | Error => "caused an error", // must never happen | |
| 502 | Joined => "has joined the room", | |
| 503 | Left => "has left the room", | |
| 504 | Banned => "was banned by", | |
| 505 | Unbanned => "was unbanned by", | |
| 506 | Kicked => "was kicked from the room by", | |
| 507 | Invited => "was invited to the room by", | |
| 508 | KickedAndBanned => "was kicked and banned by", | |
| 509 | InvitationRejected => "rejected the invitation", | |
| 510 | InvitationRevoked => "had the invitation revoked by", | |
| 511 | ProfileChanged { .. } => "_", | |
| 512 | NotImplemented => "performed an unimplemented operation", | |
| 513 | _ => "caused an unhandled membership change", | |
| 514 | }; | |
| 515 | ||
| 516 | fn formatted_name(member: &WeechatRoomMember) -> String { | |
| 517 | match &member.display_name { | |
| 518 | Some(display_name) => { | |
| 519 | format!( | |
| 520 | "{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", | |
| 521 | name = display_name, | |
| 522 | user_id = &member.user_id, | |
| 523 | color_delim = Weechat::color("chat_delimiters"), | |
| 524 | color_reset = Weechat::color("reset")) | |
| 525 | } | |
| 526 | ||
| 527 | Option::None => { | |
| 528 | member.user_id.as_ref().to_string() | |
| 529 | } | |
| 530 | } | |
| 531 | } | |
| 532 | ||
| 533 | let (prefix, color_action) = match change_op { | |
| 534 | Joined => ("join", "green"), | |
| 535 | Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), | |
| 536 | _ => ("quit", "red"), | |
| 537 | }; | |
| 538 | ||
| 539 | let color_action = Weechat::color(color_action); | |
| 540 | let color_reset = Weechat::color("reset"); | |
| 541 | ||
| 542 | let operation = format!( | |
| 543 | "{color_action}{op}{color_reset}", | |
| 544 | color_action = color_action, | |
| 545 | op = operation, | |
| 546 | color_reset = color_reset | |
| 547 | ); | |
| 548 | ||
| 549 | let target_name = format!( | |
| 550 | "{color_user}{target_name}{color_reset}", | |
| 551 | target_name = formatted_name(target), | |
| 552 | color_user = Weechat::color("reset"), // TODO | |
| 553 | color_reset = Weechat::color("reset") | |
| 554 | ); | |
| 555 | ||
| 556 | let sender_name = format!( | |
| 557 | "{color_user}{sender_name}{color_reset}", | |
| 558 | sender_name = formatted_name(sender), | |
| 559 | color_user = Weechat::color("reset"), // TODO | |
| 560 | color_reset = Weechat::color("reset") | |
| 561 | ); | |
| 562 | ||
| 563 | let message = match change_op { | |
| 564 | ProfileChanged { | |
| 565 | displayname_changed, | |
| 566 | avatar_url_changed, | |
| 567 | } => { | |
| 568 | let new_display_name = &event.content.displayname; | |
| 569 | ||
| 570 | // TODO: Should we display the new avatar URL? | |
| 571 | // let new_avatar = self.content.avatar_url.as_ref(); | |
| 572 | ||
| 573 | match (displayname_changed, avatar_url_changed) { | |
| 574 | (false, true) => | |
| 575 | format!( | |
| 576 | "{prefix} {target} {color_action}changed their avatar{color_reset}", | |
| 577 | prefix = Weechat::prefix(prefix), | |
| 578 | target = target_name, | |
| 579 | color_action = color_action, | |
| 580 | color_reset = color_reset | |
| 581 | ), | |
| 582 | (true, false) => { | |
| 583 | match new_display_name { | |
| 584 | Some(name) => format!( | |
| 585 | "{prefix} {target} {color_action}changed their display name to{color_reset} {new}", | |
| 586 | prefix = Weechat::prefix(prefix), | |
| 587 | target = target_name, | |
| 588 | new = name, | |
| 589 | color_action = color_action, | |
| 590 | color_reset = color_reset | |
| 591 | ), | |
| 592 | Option::None => format!( | |
| 593 | "{prefix} {target} {color_action}removed their display name{color_reset}", | |
| 594 | prefix = Weechat::prefix(prefix), | |
| 595 | target = target_name, | |
| 596 | color_action = color_action, | |
| 597 | color_reset = color_reset | |
| 598 | ), | |
| 599 | } | |
| 600 | } | |
| 601 | (true, true) => | |
| 602 | match new_display_name { | |
| 603 | Some(name) => format!( | |
| 604 | "{prefix} {target} {color_action}changed their avatar \ | |
| 605 | and changed their display name to{color_reset} {new}", | |
| 606 | prefix = Weechat::prefix(prefix), | |
| 607 | target = target_name, | |
| 608 | new = name, | |
| 609 | color_action = color_action, | |
| 610 | color_reset = color_reset | |
| 611 | ), | |
| 612 | Option::None => format!( | |
| 613 | "{prefix} {target} {color_action}changed their \ | |
| 614 | avatar and removed display name{color_reset}", | |
| 615 | prefix = Weechat::prefix(prefix), | |
| 616 | target = target_name, | |
| 617 | color_action = color_action, | |
| 618 | color_reset = color_reset | |
| 619 | ), | |
| 620 | } | |
| 621 | (false, false) => | |
| 622 | "Cannot happen: got profile changed but nothing really changed".to_string() | |
| 623 | } | |
| 624 | } | |
| 625 | None | Error | Joined | Left | InvitationRejected | NotImplemented => { | |
| 626 | format!( | |
| 627 | "{prefix} {target} {op}", | |
| 628 | prefix = Weechat::prefix(prefix), | |
| 629 | target = target_name, | |
| 630 | op = operation | |
| 631 | ) | |
| 632 | } | |
| 633 | Banned | Unbanned | Kicked | Invited | InvitationRevoked | |
| 634 | | KickedAndBanned => format!( | |
| 635 | "{prefix} {target} {op} {sender}", | |
| 636 | prefix = Weechat::prefix(prefix), | |
| 637 | target = target_name, | |
| 638 | op = operation, | |
| 639 | sender = sender_name | |
| 640 | ), | |
| 641 | ||
| 642 | // This means an unsupported membership change happened so we just | |
| 643 | // print a generic message to indicate this. | |
| 644 | _ => format!( | |
| 645 | "{prefix} {sender} {op}", | |
| 646 | prefix = Weechat::prefix(prefix), | |
| 647 | sender = sender_name, | |
| 648 | op = operation, | |
| 649 | ), | |
| 650 | }; | |
| 651 | ||
| 652 | // TODO we should return the tags as well. | |
| 653 | message | |
| 654 | } | |