Commit 428bbcb434
Unsigned
Layout: unified · split
src/render.rs +48 −13
| @@ -1,3 +1,5 @@ | ||
| 1 | use url::Url; | |
| 2 | ||
| 1 | 3 | use matrix_sdk::{ |
| 2 | 4 | events::{ |
| 3 | 5 | room::{ |
| @@ -26,11 +28,11 @@ pub struct RenderedEvent { | ||
| 26 | 28 | /// The tags of the event, |
| 27 | 29 | pub tags: Vec<String>, |
| 28 | 30 | // 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 | // This will be useful if a message has a part that we should be able to | |
| 32 | // hide using a filter on the tags. | |
| 31 | 33 | // |
| 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 | // One such example would be a code snippet, where we want to show that | |
| 35 | // someone posted a code snippet but we might want to filter out the body. | |
| 34 | 36 | /// The event rendered as a string. |
| 35 | 37 | pub message: String, |
| 36 | 38 | } |
| @@ -228,6 +230,31 @@ impl<'a> Render for &'a SyncStateEvent<MemberEventContent> { | ||
| 228 | 230 | } |
| 229 | 231 | } |
| 230 | 232 | |
| 233 | impl Render for dyn HasUrlOrFile { | |
| 234 | type RenderContext = Url; | |
| 235 | const TAGS: &'static [&'static str] = &["matrix_media"]; | |
| 236 | ||
| 237 | fn render(&self, _homeserver: &Self::RenderContext) -> RenderedEvent { | |
| 238 | let message = format!( | |
| 239 | "{color_delimiter}<{color_reset}{}{color_delimiter}>\ | |
| 240 | [{color_reset}{}{color_delimiter}]{color_reset}", | |
| 241 | self.body(), | |
| 242 | // FIXME this isn't right, the MXID -> URL transformation depends on | |
| 243 | // your homeserver URL. | |
| 244 | self.resolve_url(), | |
| 245 | color_delimiter = Weechat::color("color_delimiter"), | |
| 246 | color_reset = Weechat::color("reset") | |
| 247 | ); | |
| 248 | ||
| 249 | RenderedEvent { | |
| 250 | message, | |
| 251 | tags: Self::TAGS.iter().map(|t| t.to_string()).collect(), | |
| 252 | // FIXME do the conversion here. | |
| 253 | message_timestamp: 0, | |
| 254 | } | |
| 255 | } | |
| 256 | } | |
| 257 | ||
| 231 | 258 | /// Rendering function for room messages. |
| 232 | 259 | // FIXME: Pass room member |
| 233 | 260 | // TODO: We should not return raw strings here but something like [Block] |
| @@ -361,8 +388,9 @@ pub fn render_message( | ||
| 361 | 388 | } |
| 362 | 389 | } |
| 363 | 390 | |
| 364 | /// Trait for message event types that contain an optional formatted body. `resolve_body` will | |
| 365 | /// return the formatted body if present, else fallback to the regular body. | |
| 391 | /// Trait for message event types that contain an optional formatted body. | |
| 392 | /// `resolve_body` will return the formatted body if present, else fallback to | |
| 393 | /// the regular body. | |
| 366 | 394 | trait HasFormattedBody { |
| 367 | 395 | fn body(&self) -> &str; |
| 368 | 396 | fn formatted_body(&self) -> Option<&str>; |
| @@ -372,8 +400,9 @@ trait HasFormattedBody { | ||
| 372 | 400 | } |
| 373 | 401 | } |
| 374 | 402 | |
| 375 | // Repeating this for each event type would get boring fast so lets use a simple macro to implement | |
| 376 | // the trait for a struct that has a `body` and `formatted_body` field | |
| 403 | // Repeating this for each event type would get boring fast so lets use a simple | |
| 404 | // macro to implement the trait for a struct that has a `body` and | |
| 405 | // `formatted_body` field | |
| 377 | 406 | macro_rules! has_formatted_body { |
| 378 | 407 | ($content: ident) => { |
| 379 | 408 | impl HasFormattedBody for $content { |
| @@ -390,23 +419,29 @@ macro_rules! has_formatted_body { | ||
| 390 | 419 | }; |
| 391 | 420 | } |
| 392 | 421 | |
| 393 | /// This trait is implemented for message types that can contain either an URL or an encrypted | |
| 394 | /// file. One of these _must_ be present. | |
| 422 | /// This trait is implemented for message types that can contain either an URL | |
| 423 | /// or an encrypted file. One of these _must_ be present. | |
| 395 | 424 | trait HasUrlOrFile { |
| 396 | 425 | fn url(&self) -> Option<&str>; |
| 397 | 426 | fn file(&self) -> Option<&str>; |
| 427 | fn body(&self) -> &str; | |
| 398 | 428 | #[inline] |
| 399 | 429 | fn resolve_url(&self) -> &str { |
| 400 | // the file is either encrypted or not encrypted so either `url` or `file` must | |
| 401 | // exist and unwrapping will never panic | |
| 430 | // the file is either encrypted or not encrypted so either `url` or | |
| 431 | // `file` must exist and unwrapping will never panic | |
| 402 | 432 | self.url().or_else(|| self.file()).unwrap() |
| 403 | 433 | } |
| 404 | 434 | } |
| 405 | 435 | |
| 406 | // Same as above: a simple macro to implement the trait for structs with `url` and `file` fields. | |
| 436 | // Same as above: a simple macro to implement the trait for structs with `url` | |
| 437 | // and `file` fields. | |
| 407 | 438 | macro_rules! has_url_or_file { |
| 408 | 439 | ($content: ident) => { |
| 409 | 440 | impl HasUrlOrFile for $content { |
| 441 | fn body(&self) -> &str { | |
| 442 | &self.body | |
| 443 | } | |
| 444 | ||
| 410 | 445 | #[inline] |
| 411 | 446 | fn url(&self) -> Option<&str> { |
| 412 | 447 | self.url.as_deref() |