Commit 0ac54e78bc
Unsigned
Layout: unified · split
src/render.rs +189 −142
| @@ -1,4 +1,3 @@ | ||
| 1 | use crate::room_buffer::WeechatRoomMember; | |
| 2 | 1 | use matrix_sdk::{ |
| 3 | 2 | events::{ |
| 4 | 3 | room::{ |
| @@ -15,169 +14,217 @@ use matrix_sdk::{ | ||
| 15 | 14 | }, |
| 16 | 15 | PossiblyRedactedExt, |
| 17 | 16 | }; |
| 17 | ||
| 18 | 18 | use weechat::Weechat; |
| 19 | 19 | |
| 20 | /// Rendering function for membership events (joins, leaves, bans, profile | |
| 21 | /// changes, etc). | |
| 22 | // TODO: We should not return raw strings here but something like [Block] | |
| 23 | // where Block is (String, [Tags]). Each Block represents one or several lines | |
| 24 | // which have the same tags. | |
| 25 | pub fn render_membership( | |
| 26 | member: &SyncStateEvent<MemberEventContent>, | |
| 27 | sender: &WeechatRoomMember, | |
| 28 | target: &WeechatRoomMember, | |
| 29 | ) -> String { | |
| 30 | use MembershipChange::*; | |
| 31 | let change_op = member.membership_change(); | |
| 32 | ||
| 33 | let operation = match change_op { | |
| 34 | None => "did nothing", | |
| 35 | Error => "caused an error", // must never happen | |
| 36 | Joined => "has joined the room", | |
| 37 | Left => "has left the room", | |
| 38 | Banned => "was banned by", | |
| 39 | Unbanned => "was unbanned by", | |
| 40 | Kicked => "was kicked from the room by", | |
| 41 | Invited => "was invited to the room by", | |
| 42 | KickedAndBanned => "was kicked and banned by", | |
| 43 | InvitationRejected => "rejected the invitation", | |
| 44 | InvitationRevoked => "had the invitation revoked by", | |
| 45 | ProfileChanged { .. } => "_", | |
| 46 | NotImplemented => "performed an unimplemented operation", | |
| 47 | _ => "caused an unhandled membership change", | |
| 48 | }; | |
| 20 | use crate::room_buffer::WeechatRoomMember; | |
| 49 | 21 | |
| 50 | fn formatted_name(member: &WeechatRoomMember) -> String { | |
| 51 | match &member.display_name { | |
| 52 | Some(display_name) => { | |
| 53 | format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", | |
| 54 | name = display_name, | |
| 55 | user_id = &member.user_id, | |
| 56 | color_delim = Weechat::color("chat_delimiters"), | |
| 57 | color_reset = Weechat::color("reset")) | |
| 58 | } | |
| 22 | /// The rendered version of an event. | |
| 23 | pub struct RenderedEvent { | |
| 24 | /// The UNIX timestamp of the event. | |
| 25 | pub message_timestamp: u64, | |
| 26 | /// The tags of the event, | |
| 27 | pub tags: Vec<String>, | |
| 28 | // TODO we need to allow multiple message/tags pairs here. | |
| 29 | // This will be useful if a message has a part that we should be able to hide | |
| 30 | // using a filter on the tags. | |
| 31 | // | |
| 32 | // One such example would be a code snippet, where we want to show that someone | |
| 33 | // posted a code snippet but we might want to filter out the body. | |
| 34 | /// The event rendered as a string. | |
| 35 | pub message: String, | |
| 36 | } | |
| 37 | ||
| 38 | /// Trait allowing events to be rendered for Weechat. | |
| 39 | pub trait Render { | |
| 40 | /// The event specific tags that should be attached to the rendered event. | |
| 41 | const TAGS: &'static [&'static str]; | |
| 42 | ||
| 43 | /// Some events might need additional context to be rendered, for example if | |
| 44 | /// we want to display the sender, we don't want to display the mxid, | |
| 45 | /// instead we want to display the disambiguated display name. | |
| 46 | /// | |
| 47 | /// This allows the render implementation to be passed some additional data | |
| 48 | /// when rendering. | |
| 49 | type RenderContext; | |
| 59 | 50 | |
| 60 | std::option::Option::None => { | |
| 61 | member.user_id.as_ref().to_string() | |
| 51 | /// Render the event. | |
| 52 | fn render(&self, context: &Self::RenderContext) -> RenderedEvent; | |
| 53 | } | |
| 54 | ||
| 55 | pub struct MembershipContext<'a> { | |
| 56 | /// The room member object of the event sender. | |
| 57 | pub sender: &'a WeechatRoomMember, | |
| 58 | /// The room member object of the target of the membership change. | |
| 59 | pub target: &'a WeechatRoomMember, | |
| 60 | } | |
| 61 | ||
| 62 | impl<'a> Render for &'a SyncStateEvent<MemberEventContent> { | |
| 63 | const TAGS: &'static [&'static str] = &["matrix_membership"]; | |
| 64 | ||
| 65 | type RenderContext = MembershipContext<'a>; | |
| 66 | ||
| 67 | /// Rendering implementation for membership events (joins, leaves, bans, profile | |
| 68 | /// changes, etc). | |
| 69 | fn render(&self, context: &MembershipContext<'a>) -> RenderedEvent { | |
| 70 | use MembershipChange::*; | |
| 71 | let change_op = self.membership_change(); | |
| 72 | ||
| 73 | let operation = match change_op { | |
| 74 | None => "did nothing", | |
| 75 | Error => "caused an error", // must never happen | |
| 76 | Joined => "has joined the room", | |
| 77 | Left => "has left the room", | |
| 78 | Banned => "was banned by", | |
| 79 | Unbanned => "was unbanned by", | |
| 80 | Kicked => "was kicked from the room by", | |
| 81 | Invited => "was invited to the room by", | |
| 82 | KickedAndBanned => "was kicked and banned by", | |
| 83 | InvitationRejected => "rejected the invitation", | |
| 84 | InvitationRevoked => "had the invitation revoked by", | |
| 85 | ProfileChanged { .. } => "_", | |
| 86 | NotImplemented => "performed an unimplemented operation", | |
| 87 | _ => "caused an unhandled membership change", | |
| 88 | }; | |
| 89 | ||
| 90 | fn formatted_name(member: &WeechatRoomMember) -> String { | |
| 91 | match &member.display_name { | |
| 92 | Some(display_name) => { | |
| 93 | format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", | |
| 94 | name = display_name, | |
| 95 | user_id = &member.user_id, | |
| 96 | color_delim = Weechat::color("chat_delimiters"), | |
| 97 | color_reset = Weechat::color("reset")) | |
| 98 | } | |
| 99 | ||
| 100 | std::option::Option::None => { | |
| 101 | member.user_id.as_ref().to_string() | |
| 102 | } | |
| 62 | 103 | } |
| 63 | 104 | } |
| 64 | } | |
| 65 | 105 | |
| 66 | let (prefix, color_action) = match change_op { | |
| 67 | Joined => ("join", "green"), | |
| 68 | Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), | |
| 69 | _ => ("quit", "red"), | |
| 70 | }; | |
| 106 | let (prefix, color_action) = match change_op { | |
| 107 | Joined => ("join", "green"), | |
| 108 | Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), | |
| 109 | _ => ("quit", "red"), | |
| 110 | }; | |
| 71 | 111 | |
| 72 | let color_action = Weechat::color(color_action); | |
| 73 | let color_reset = Weechat::color("reset"); | |
| 112 | let color_action = Weechat::color(color_action); | |
| 113 | let color_reset = Weechat::color("reset"); | |
| 74 | 114 | |
| 75 | let operation = format!( | |
| 76 | "{color_action}{op}{color_reset}", | |
| 77 | color_action = color_action, | |
| 78 | op = operation, | |
| 79 | color_reset = color_reset | |
| 80 | ); | |
| 81 | ||
| 82 | let target_name = format!( | |
| 83 | "{color_user}{target_name}{color_reset}", | |
| 84 | target_name = formatted_name(target), | |
| 85 | color_user = Weechat::color("reset"), // TODO | |
| 86 | color_reset = Weechat::color("reset") | |
| 87 | ); | |
| 88 | ||
| 89 | let sender_name = format!( | |
| 90 | "{color_user}{sender_name}{color_reset}", | |
| 91 | sender_name = formatted_name(sender), | |
| 92 | color_user = Weechat::color("reset"), // TODO | |
| 93 | color_reset = Weechat::color("reset") | |
| 94 | ); | |
| 95 | ||
| 96 | match change_op { | |
| 97 | ProfileChanged { | |
| 98 | displayname_changed, | |
| 99 | avatar_url_changed, | |
| 100 | } => { | |
| 101 | let new_display_name = &member.content.displayname; | |
| 102 | ||
| 103 | // TODO: Should we display the new avatar URL? | |
| 104 | // let new_avatar = member.content.avatar_url.as_ref(); | |
| 105 | ||
| 106 | match (displayname_changed, avatar_url_changed) { | |
| 107 | (false, true) => | |
| 108 | format!( | |
| 109 | "{prefix} {target} {color_action}changed their avatar{color_reset}", | |
| 110 | prefix = Weechat::prefix(prefix), | |
| 111 | target = target_name, | |
| 112 | color_action = color_action, | |
| 113 | color_reset = color_reset | |
| 114 | ), | |
| 115 | (true, false) => { | |
| 116 | match new_display_name { | |
| 117 | Some(name) => format!( | |
| 118 | "{prefix} {target} {color_action}changed their display name to{color_reset} {new}", | |
| 119 | prefix = Weechat::prefix(prefix), | |
| 120 | target = target_name, | |
| 121 | new = name, | |
| 122 | color_action = color_action, | |
| 123 | color_reset = color_reset | |
| 124 | ), | |
| 125 | std::option::Option::None => format!( | |
| 126 | "{prefix} {target} {color_action}removed their display name{color_reset}", | |
| 115 | let operation = format!( | |
| 116 | "{color_action}{op}{color_reset}", | |
| 117 | color_action = color_action, | |
| 118 | op = operation, | |
| 119 | color_reset = color_reset | |
| 120 | ); | |
| 121 | ||
| 122 | let target_name = format!( | |
| 123 | "{color_user}{target_name}{color_reset}", | |
| 124 | target_name = formatted_name(context.target), | |
| 125 | color_user = Weechat::color("reset"), // TODO | |
| 126 | color_reset = Weechat::color("reset") | |
| 127 | ); | |
| 128 | ||
| 129 | let sender_name = format!( | |
| 130 | "{color_user}{sender_name}{color_reset}", | |
| 131 | sender_name = formatted_name(context.sender), | |
| 132 | color_user = Weechat::color("reset"), // TODO | |
| 133 | color_reset = Weechat::color("reset") | |
| 134 | ); | |
| 135 | ||
| 136 | let message = match change_op { | |
| 137 | ProfileChanged { | |
| 138 | displayname_changed, | |
| 139 | avatar_url_changed, | |
| 140 | } => { | |
| 141 | let new_display_name = &self.content.displayname; | |
| 142 | ||
| 143 | // TODO: Should we display the new avatar URL? | |
| 144 | // let new_avatar = self.content.avatar_url.as_ref(); | |
| 145 | ||
| 146 | match (displayname_changed, avatar_url_changed) { | |
| 147 | (false, true) => | |
| 148 | format!( | |
| 149 | "{prefix} {target} {color_action}changed their avatar{color_reset}", | |
| 127 | 150 | prefix = Weechat::prefix(prefix), |
| 128 | 151 | target = target_name, |
| 129 | 152 | color_action = color_action, |
| 130 | 153 | color_reset = color_reset |
| 131 | 154 | ), |
| 155 | (true, false) => { | |
| 156 | match new_display_name { | |
| 157 | Some(name) => format!( | |
| 158 | "{prefix} {target} {color_action}changed their display name to{color_reset} {new}", | |
| 159 | prefix = Weechat::prefix(prefix), | |
| 160 | target = target_name, | |
| 161 | new = name, | |
| 162 | color_action = color_action, | |
| 163 | color_reset = color_reset | |
| 164 | ), | |
| 165 | std::option::Option::None => format!( | |
| 166 | "{prefix} {target} {color_action}removed their display name{color_reset}", | |
| 167 | prefix = Weechat::prefix(prefix), | |
| 168 | target = target_name, | |
| 169 | color_action = color_action, | |
| 170 | color_reset = color_reset | |
| 171 | ), | |
| 172 | } | |
| 132 | 173 | } |
| 174 | (true, true) => | |
| 175 | match new_display_name { | |
| 176 | Some(name) => format!( | |
| 177 | "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}", | |
| 178 | prefix = Weechat::prefix(prefix), | |
| 179 | target = target_name, | |
| 180 | new = name, | |
| 181 | color_action = color_action, | |
| 182 | color_reset = color_reset | |
| 183 | ), | |
| 184 | std::option::Option::None => format!( | |
| 185 | "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}", | |
| 186 | prefix = Weechat::prefix(prefix), | |
| 187 | target = target_name, | |
| 188 | color_action = color_action, | |
| 189 | color_reset = color_reset | |
| 190 | ), | |
| 191 | } | |
| 192 | (false, false) => | |
| 193 | "Cannot happen: got profile changed but nothing really changed".to_string() | |
| 133 | 194 | } |
| 134 | (true, true) => | |
| 135 | match new_display_name { | |
| 136 | Some(name) => format!( | |
| 137 | "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}", | |
| 138 | prefix = Weechat::prefix(prefix), | |
| 139 | target = target_name, | |
| 140 | new = name, | |
| 141 | color_action = color_action, | |
| 142 | color_reset = color_reset | |
| 143 | ), | |
| 144 | std::option::Option::None => format!( | |
| 145 | "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}", | |
| 146 | prefix = Weechat::prefix(prefix), | |
| 147 | target = target_name, | |
| 148 | color_action = color_action, | |
| 149 | color_reset = color_reset | |
| 150 | ), | |
| 151 | } | |
| 152 | (false, false) => | |
| 153 | "Cannot happen: got profile changed but nothing really changed".to_string() | |
| 154 | 195 | } |
| 155 | } | |
| 156 | None | Error | Joined | Left | InvitationRejected | NotImplemented => { | |
| 157 | format!( | |
| 196 | None | Error | Joined | Left | InvitationRejected | |
| 197 | | NotImplemented => format!( | |
| 158 | 198 | "{prefix} {target} {op}", |
| 159 | 199 | prefix = Weechat::prefix(prefix), |
| 160 | 200 | target = target_name, |
| 161 | 201 | op = operation |
| 162 | ) | |
| 202 | ), | |
| 203 | Banned | Unbanned | Kicked | Invited | InvitationRevoked | |
| 204 | | KickedAndBanned => format!( | |
| 205 | "{prefix} {target} {op} {sender}", | |
| 206 | prefix = Weechat::prefix(prefix), | |
| 207 | target = target_name, | |
| 208 | op = operation, | |
| 209 | sender = sender_name | |
| 210 | ), | |
| 211 | ||
| 212 | // This means an unsupported membership change happened so we just | |
| 213 | // print a generic message to indicate this. | |
| 214 | _ => format!( | |
| 215 | "{prefix} {sender} {op}", | |
| 216 | prefix = Weechat::prefix(prefix), | |
| 217 | sender = sender_name, | |
| 218 | op = operation, | |
| 219 | ), | |
| 220 | }; | |
| 221 | ||
| 222 | RenderedEvent { | |
| 223 | message, | |
| 224 | tags: Self::TAGS.iter().map(|t| t.to_string()).collect(), | |
| 225 | // FIXME do the conversion here. | |
| 226 | message_timestamp: 0, | |
| 163 | 227 | } |
| 164 | Banned | Unbanned | Kicked | Invited | InvitationRevoked | |
| 165 | | KickedAndBanned => format!( | |
| 166 | "{prefix} {target} {op} {sender}", | |
| 167 | prefix = Weechat::prefix(prefix), | |
| 168 | target = target_name, | |
| 169 | op = operation, | |
| 170 | sender = sender_name | |
| 171 | ), | |
| 172 | ||
| 173 | // This means an unsupported membership change happened so we just print a generic | |
| 174 | // message to indicate this. | |
| 175 | _ => format!( | |
| 176 | "{prefix} {sender} {op}", | |
| 177 | prefix = Weechat::prefix(prefix), | |
| 178 | sender = sender_name, | |
| 179 | op = operation, | |
| 180 | ), | |
| 181 | 228 | } |
| 182 | 229 | } |
| 183 | 230 | |
src/room_buffer.rs +3 −2
| @@ -50,7 +50,7 @@ use tracing::{debug, error, trace}; | ||
| 50 | 50 | |
| 51 | 51 | use crate::{ |
| 52 | 52 | connection::{Connection, TYPING_NOTICE_TIMEOUT}, |
| 53 | render::{render_membership, render_message}, | |
| 53 | render::{render_message, MembershipContext, Render}, | |
| 54 | 54 | }; |
| 55 | 55 | use std::{ |
| 56 | 56 | cell::{Ref, RefCell, RefMut}, |
| @@ -709,7 +709,8 @@ impl RoomBuffer { | ||
| 709 | 709 | // Display the event message |
| 710 | 710 | let message = match (&sender, &target) { |
| 711 | 711 | (Some(sender), Some(target)) => { |
| 712 | render_membership(event, sender, target) | |
| 712 | let context = MembershipContext { sender, target }; | |
| 713 | event.render(&context).message | |
| 713 | 714 | } |
| 714 | 715 | |
| 715 | 716 | _ => { |