Commit f99447ca82
Unsigned
Layout: unified · split
src/render.rs +217 −154
| @@ -37,6 +37,19 @@ pub struct RenderedEvent { | ||
| 37 | 37 | pub message: String, |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | pub struct RenderedContent { | |
| 41 | /// The tags of the event, | |
| 42 | pub tags: Vec<String>, | |
| 43 | // TODO we need to allow multiple message/tags pairs here. | |
| 44 | // This will be useful if a message has a part that we should be able to | |
| 45 | // hide using a filter on the tags. | |
| 46 | // | |
| 47 | // One such example would be a code snippet, where we want to show that | |
| 48 | // someone posted a code snippet but we might want to filter out the body. | |
| 49 | /// The event rendered as a string. | |
| 50 | pub message: String, | |
| 51 | } | |
| 52 | ||
| 40 | 53 | /// Trait allowing events to be rendered for Weechat. |
| 41 | 54 | pub trait Render { |
| 42 | 55 | /// The event specific tags that should be attached to the rendered event. |
| @@ -50,8 +63,26 @@ pub trait Render { | ||
| 50 | 63 | /// when rendering. |
| 51 | 64 | type RenderContext; |
| 52 | 65 | |
| 66 | fn tags(&self) -> Vec<String> { | |
| 67 | Self::TAGS.iter().map(|t| t.to_string()).collect() | |
| 68 | } | |
| 69 | ||
| 53 | 70 | /// Render the event. |
| 54 | fn render(&self, context: &Self::RenderContext) -> RenderedEvent; | |
| 71 | fn render_with_prefix(&self, sender: &WeechatRoomMember, context: &Self::RenderContext) -> RenderedEvent { | |
| 72 | // TODO the sender should have a color attribute and we should use it | |
| 73 | // here. | |
| 74 | let colorname_user = Weechat::info_get("nick_color_name", sender.user_id.as_str()).unwrap_or_else(|| String::from("default")); | |
| 75 | let prefix = format!("{}{}{}", Weechat::color(&colorname_user), sender.nick, Weechat::color("reset")); | |
| 76 | ||
| 77 | let content = self.render(context); | |
| 78 | ||
| 79 | RenderedEvent { | |
| 80 | tags: self.tags(), | |
| 81 | message: content.message, | |
| 82 | message_timestamp: 0, | |
| 83 | } | |
| 84 | } | |
| 85 | fn render(&self, context: &Self::RenderContext) -> RenderedContent; | |
| 55 | 86 | } |
| 56 | 87 | |
| 57 | 88 | pub struct MembershipContext<'a> { |
| @@ -61,172 +92,167 @@ pub struct MembershipContext<'a> { | ||
| 61 | 92 | pub target: &'a WeechatRoomMember, |
| 62 | 93 | } |
| 63 | 94 | |
| 64 | impl<'a> Render for &'a SyncStateEvent<MemberEventContent> { | |
| 95 | /// Rendering implementation for membership events (joins, leaves, bans, profile | |
| 96 | /// changes, etc). | |
| 97 | pub fn render_membership<'a>(event: &SyncStateEvent<MemberEventContent>, context: &MembershipContext<'a>) -> RenderedEvent { | |
| 65 | 98 | const TAGS: &'static [&'static str] = &["matrix_membership"]; |
| 99 | use MembershipChange::*; | |
| 100 | let change_op = event.membership_change(); | |
| 101 | ||
| 102 | let operation = match change_op { | |
| 103 | None => "did nothing", | |
| 104 | Error => "caused an error", // must never happen | |
| 105 | Joined => "has joined the room", | |
| 106 | Left => "has left the room", | |
| 107 | Banned => "was banned by", | |
| 108 | Unbanned => "was unbanned by", | |
| 109 | Kicked => "was kicked from the room by", | |
| 110 | Invited => "was invited to the room by", | |
| 111 | KickedAndBanned => "was kicked and banned by", | |
| 112 | InvitationRejected => "rejected the invitation", | |
| 113 | InvitationRevoked => "had the invitation revoked by", | |
| 114 | ProfileChanged { .. } => "_", | |
| 115 | NotImplemented => "performed an unimplemented operation", | |
| 116 | _ => "caused an unhandled membership change", | |
| 117 | }; | |
| 66 | 118 | |
| 67 | type RenderContext = MembershipContext<'a>; | |
| 68 | ||
| 69 | /// Rendering implementation for membership events (joins, leaves, bans, profile | |
| 70 | /// changes, etc). | |
| 71 | fn render(&self, context: &MembershipContext<'a>) -> RenderedEvent { | |
| 72 | use MembershipChange::*; | |
| 73 | let change_op = self.membership_change(); | |
| 74 | ||
| 75 | let operation = match change_op { | |
| 76 | None => "did nothing", | |
| 77 | Error => "caused an error", // must never happen | |
| 78 | Joined => "has joined the room", | |
| 79 | Left => "has left the room", | |
| 80 | Banned => "was banned by", | |
| 81 | Unbanned => "was unbanned by", | |
| 82 | Kicked => "was kicked from the room by", | |
| 83 | Invited => "was invited to the room by", | |
| 84 | KickedAndBanned => "was kicked and banned by", | |
| 85 | InvitationRejected => "rejected the invitation", | |
| 86 | InvitationRevoked => "had the invitation revoked by", | |
| 87 | ProfileChanged { .. } => "_", | |
| 88 | NotImplemented => "performed an unimplemented operation", | |
| 89 | _ => "caused an unhandled membership change", | |
| 90 | }; | |
| 91 | ||
| 92 | fn formatted_name(member: &WeechatRoomMember) -> String { | |
| 93 | match &member.display_name { | |
| 94 | Some(display_name) => { | |
| 95 | format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", | |
| 96 | name = display_name, | |
| 97 | user_id = &member.user_id, | |
| 98 | color_delim = Weechat::color("chat_delimiters"), | |
| 99 | color_reset = Weechat::color("reset")) | |
| 100 | } | |
| 119 | fn formatted_name(member: &WeechatRoomMember) -> String { | |
| 120 | match &member.display_name { | |
| 121 | Some(display_name) => { | |
| 122 | format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", | |
| 123 | name = display_name, | |
| 124 | user_id = &member.user_id, | |
| 125 | color_delim = Weechat::color("chat_delimiters"), | |
| 126 | color_reset = Weechat::color("reset")) | |
| 127 | } | |
| 101 | 128 | |
| 102 | Option::None => { | |
| 103 | member.user_id.as_ref().to_string() | |
| 104 | } | |
| 129 | Option::None => { | |
| 130 | member.user_id.as_ref().to_string() | |
| 105 | 131 | } |
| 106 | 132 | } |
| 133 | } | |
| 107 | 134 | |
| 108 | let (prefix, color_action) = match change_op { | |
| 109 | Joined => ("join", "green"), | |
| 110 | Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), | |
| 111 | _ => ("quit", "red"), | |
| 112 | }; | |
| 113 | ||
| 114 | let color_action = Weechat::color(color_action); | |
| 115 | let color_reset = Weechat::color("reset"); | |
| 116 | ||
| 117 | let operation = format!( | |
| 118 | "{color_action}{op}{color_reset}", | |
| 119 | color_action = color_action, | |
| 120 | op = operation, | |
| 121 | color_reset = color_reset | |
| 122 | ); | |
| 123 | ||
| 124 | let target_name = format!( | |
| 125 | "{color_user}{target_name}{color_reset}", | |
| 126 | target_name = formatted_name(context.target), | |
| 127 | color_user = Weechat::color("reset"), // TODO | |
| 128 | color_reset = Weechat::color("reset") | |
| 129 | ); | |
| 130 | ||
| 131 | let sender_name = format!( | |
| 132 | "{color_user}{sender_name}{color_reset}", | |
| 133 | sender_name = formatted_name(context.sender), | |
| 134 | color_user = Weechat::color("reset"), // TODO | |
| 135 | color_reset = Weechat::color("reset") | |
| 136 | ); | |
| 137 | ||
| 138 | let message = match change_op { | |
| 139 | ProfileChanged { | |
| 140 | displayname_changed, | |
| 141 | avatar_url_changed, | |
| 142 | } => { | |
| 143 | let new_display_name = &self.content.displayname; | |
| 135 | let (prefix, color_action) = match change_op { | |
| 136 | Joined => ("join", "green"), | |
| 137 | Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), | |
| 138 | _ => ("quit", "red"), | |
| 139 | }; | |
| 144 | 140 | |
| 145 | // TODO: Should we display the new avatar URL? | |
| 146 | // let new_avatar = self.content.avatar_url.as_ref(); | |
| 141 | let color_action = Weechat::color(color_action); | |
| 142 | let color_reset = Weechat::color("reset"); | |
| 147 | 143 | |
| 148 | match (displayname_changed, avatar_url_changed) { | |
| 149 | (false, true) => | |
| 150 | format!( | |
| 151 | "{prefix} {target} {color_action}changed their avatar{color_reset}", | |
| 144 | let operation = format!( | |
| 145 | "{color_action}{op}{color_reset}", | |
| 146 | color_action = color_action, | |
| 147 | op = operation, | |
| 148 | color_reset = color_reset | |
| 149 | ); | |
| 150 | ||
| 151 | let target_name = format!( | |
| 152 | "{color_user}{target_name}{color_reset}", | |
| 153 | target_name = formatted_name(context.target), | |
| 154 | color_user = Weechat::color("reset"), // TODO | |
| 155 | color_reset = Weechat::color("reset") | |
| 156 | ); | |
| 157 | ||
| 158 | let sender_name = format!( | |
| 159 | "{color_user}{sender_name}{color_reset}", | |
| 160 | sender_name = formatted_name(context.sender), | |
| 161 | color_user = Weechat::color("reset"), // TODO | |
| 162 | color_reset = Weechat::color("reset") | |
| 163 | ); | |
| 164 | ||
| 165 | let message = match change_op { | |
| 166 | ProfileChanged { | |
| 167 | displayname_changed, | |
| 168 | avatar_url_changed, | |
| 169 | } => { | |
| 170 | let new_display_name = &event.content.displayname; | |
| 171 | ||
| 172 | // TODO: Should we display the new avatar URL? | |
| 173 | // let new_avatar = self.content.avatar_url.as_ref(); | |
| 174 | ||
| 175 | match (displayname_changed, avatar_url_changed) { | |
| 176 | (false, true) => | |
| 177 | format!( | |
| 178 | "{prefix} {target} {color_action}changed their avatar{color_reset}", | |
| 179 | prefix = Weechat::prefix(prefix), | |
| 180 | target = target_name, | |
| 181 | color_action = color_action, | |
| 182 | color_reset = color_reset | |
| 183 | ), | |
| 184 | (true, false) => { | |
| 185 | match new_display_name { | |
| 186 | Some(name) => format!( | |
| 187 | "{prefix} {target} {color_action}changed their display name to{color_reset} {new}", | |
| 188 | prefix = Weechat::prefix(prefix), | |
| 189 | target = target_name, | |
| 190 | new = name, | |
| 191 | color_action = color_action, | |
| 192 | color_reset = color_reset | |
| 193 | ), | |
| 194 | Option::None => format!( | |
| 195 | "{prefix} {target} {color_action}removed their display name{color_reset}", | |
| 152 | 196 | prefix = Weechat::prefix(prefix), |
| 153 | 197 | target = target_name, |
| 154 | 198 | color_action = color_action, |
| 155 | 199 | color_reset = color_reset |
| 156 | 200 | ), |
| 157 | (true, false) => { | |
| 158 | match new_display_name { | |
| 159 | Some(name) => format!( | |
| 160 | "{prefix} {target} {color_action}changed their display name to{color_reset} {new}", | |
| 161 | prefix = Weechat::prefix(prefix), | |
| 162 | target = target_name, | |
| 163 | new = name, | |
| 164 | color_action = color_action, | |
| 165 | color_reset = color_reset | |
| 166 | ), | |
| 167 | Option::None => format!( | |
| 168 | "{prefix} {target} {color_action}removed their display name{color_reset}", | |
| 169 | prefix = Weechat::prefix(prefix), | |
| 170 | target = target_name, | |
| 171 | color_action = color_action, | |
| 172 | color_reset = color_reset | |
| 173 | ), | |
| 174 | } | |
| 175 | 201 | } |
| 176 | (true, true) => | |
| 177 | match new_display_name { | |
| 178 | Some(name) => format!( | |
| 179 | "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}", | |
| 180 | prefix = Weechat::prefix(prefix), | |
| 181 | target = target_name, | |
| 182 | new = name, | |
| 183 | color_action = color_action, | |
| 184 | color_reset = color_reset | |
| 185 | ), | |
| 186 | Option::None => format!( | |
| 187 | "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}", | |
| 188 | prefix = Weechat::prefix(prefix), | |
| 189 | target = target_name, | |
| 190 | color_action = color_action, | |
| 191 | color_reset = color_reset | |
| 192 | ), | |
| 193 | } | |
| 194 | (false, false) => | |
| 195 | "Cannot happen: got profile changed but nothing really changed".to_string() | |
| 196 | 202 | } |
| 203 | (true, true) => | |
| 204 | match new_display_name { | |
| 205 | Some(name) => format!( | |
| 206 | "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}", | |
| 207 | prefix = Weechat::prefix(prefix), | |
| 208 | target = target_name, | |
| 209 | new = name, | |
| 210 | color_action = color_action, | |
| 211 | color_reset = color_reset | |
| 212 | ), | |
| 213 | Option::None => format!( | |
| 214 | "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}", | |
| 215 | prefix = Weechat::prefix(prefix), | |
| 216 | target = target_name, | |
| 217 | color_action = color_action, | |
| 218 | color_reset = color_reset | |
| 219 | ), | |
| 220 | } | |
| 221 | (false, false) => | |
| 222 | "Cannot happen: got profile changed but nothing really changed".to_string() | |
| 197 | 223 | } |
| 198 | None | Error | Joined | Left | InvitationRejected | |
| 199 | | NotImplemented => format!( | |
| 200 | "{prefix} {target} {op}", | |
| 201 | prefix = Weechat::prefix(prefix), | |
| 202 | target = target_name, | |
| 203 | op = operation | |
| 204 | ), | |
| 205 | Banned | Unbanned | Kicked | Invited | InvitationRevoked | |
| 206 | | KickedAndBanned => format!( | |
| 207 | "{prefix} {target} {op} {sender}", | |
| 208 | prefix = Weechat::prefix(prefix), | |
| 209 | target = target_name, | |
| 210 | op = operation, | |
| 211 | sender = sender_name | |
| 212 | ), | |
| 213 | ||
| 214 | // This means an unsupported membership change happened so we just | |
| 215 | // print a generic message to indicate this. | |
| 216 | _ => format!( | |
| 217 | "{prefix} {sender} {op}", | |
| 218 | prefix = Weechat::prefix(prefix), | |
| 219 | sender = sender_name, | |
| 220 | op = operation, | |
| 221 | ), | |
| 222 | }; | |
| 223 | ||
| 224 | RenderedEvent { | |
| 225 | message, | |
| 226 | tags: Self::TAGS.iter().map(|t| t.to_string()).collect(), | |
| 227 | // FIXME do the conversion here. | |
| 228 | message_timestamp: 0, | |
| 229 | 224 | } |
| 225 | None | Error | Joined | Left | InvitationRejected | |
| 226 | | NotImplemented => format!( | |
| 227 | "{prefix} {target} {op}", | |
| 228 | prefix = Weechat::prefix(prefix), | |
| 229 | target = target_name, | |
| 230 | op = operation | |
| 231 | ), | |
| 232 | Banned | Unbanned | Kicked | Invited | InvitationRevoked | |
| 233 | | KickedAndBanned => format!( | |
| 234 | "{prefix} {target} {op} {sender}", | |
| 235 | prefix = Weechat::prefix(prefix), | |
| 236 | target = target_name, | |
| 237 | op = operation, | |
| 238 | sender = sender_name | |
| 239 | ), | |
| 240 | ||
| 241 | // This means an unsupported membership change happened so we just | |
| 242 | // print a generic message to indicate this. | |
| 243 | _ => format!( | |
| 244 | "{prefix} {sender} {op}", | |
| 245 | prefix = Weechat::prefix(prefix), | |
| 246 | sender = sender_name, | |
| 247 | op = operation, | |
| 248 | ), | |
| 249 | }; | |
| 250 | ||
| 251 | RenderedEvent { | |
| 252 | message, | |
| 253 | tags: TAGS.iter().map(|t| t.to_string()).collect(), | |
| 254 | // FIXME do the conversion here. | |
| 255 | message_timestamp: 0, | |
| 230 | 256 | } |
| 231 | 257 | } |
| 232 | 258 | |
| @@ -234,7 +260,7 @@ impl Render for dyn HasUrlOrFile { | ||
| 234 | 260 | type RenderContext = Url; |
| 235 | 261 | const TAGS: &'static [&'static str] = &["matrix_media"]; |
| 236 | 262 | |
| 237 | fn render(&self, _homeserver: &Self::RenderContext) -> RenderedEvent { | |
| 263 | fn render(&self, _homeserver: &Self::RenderContext) -> RenderedContent { | |
| 238 | 264 | let message = format!( |
| 239 | 265 | "{color_delimiter}<{color_reset}{}{color_delimiter}>\ |
| 240 | 266 | [{color_reset}{}{color_delimiter}]{color_reset}", |
| @@ -246,13 +272,50 @@ impl Render for dyn HasUrlOrFile { | ||
| 246 | 272 | color_reset = Weechat::color("reset") |
| 247 | 273 | ); |
| 248 | 274 | |
| 249 | RenderedEvent { | |
| 275 | RenderedContent { | |
| 250 | 276 | message, |
| 251 | tags: Self::TAGS.iter().map(|t| t.to_string()).collect(), | |
| 252 | // FIXME do the conversion here. | |
| 277 | tags: self.tags(), | |
| 278 | } | |
| 279 | } | |
| 280 | } | |
| 281 | ||
| 282 | impl Render for TextMessageEventContent { | |
| 283 | const TAGS: &'static [&'static str] = &["matrix_media"]; | |
| 284 | type RenderContext = (); | |
| 285 | ||
| 286 | fn render(&self, _: &Self::RenderContext) -> RenderedContent { | |
| 287 | RenderedContent { | |
| 288 | message: self.body.clone(), | |
| 289 | tags: self.tags(), | |
| 290 | } | |
| 291 | } | |
| 292 | } | |
| 293 | ||
| 294 | impl Render for EmoteMessageEventContent { | |
| 295 | const TAGS: &'static [&'static str] = &["matrix_media"]; | |
| 296 | type RenderContext = WeechatRoomMember; | |
| 297 | ||
| 298 | fn render_with_prefix(&self, _sender: &WeechatRoomMember, context: &Self::RenderContext) -> RenderedEvent { | |
| 299 | let content = self.render(context); | |
| 300 | let _prefix = Weechat::prefix("action"); | |
| 301 | ||
| 302 | // TODO the rendered event should have a prefix field | |
| 303 | RenderedEvent { | |
| 304 | message: content.message, | |
| 305 | tags: content.tags, | |
| 253 | 306 | message_timestamp: 0, |
| 254 | 307 | } |
| 255 | 308 | } |
| 309 | ||
| 310 | fn render(&self, sender: &Self::RenderContext) -> RenderedContent { | |
| 311 | let message = format!("{} {}", sender.nick, self.body); | |
| 312 | ||
| 313 | RenderedContent { | |
| 314 | message, | |
| 315 | tags: self.tags(), | |
| 316 | } | |
| 317 | } | |
| 318 | ||
| 256 | 319 | } |
| 257 | 320 | |
| 258 | 321 | /// Rendering function for room messages. |
src/room_buffer.rs +2 −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_message, MembershipContext, Render}, | |
| 53 | render::{render_message, MembershipContext, render_membership}, | |
| 54 | 54 | }; |
| 55 | 55 | use std::{ |
| 56 | 56 | cell::{Ref, RefCell, RefMut}, |
| @@ -710,7 +710,7 @@ impl RoomBuffer { | ||
| 710 | 710 | let message = match (&sender, &target) { |
| 711 | 711 | (Some(sender), Some(target)) => { |
| 712 | 712 | let context = MembershipContext { sender, target }; |
| 713 | event.render(&context).message | |
| 713 | render_membership(event, &context).message | |
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | _ => { |