Commit 428bbcb434
Unsigned
Layout: unified · split
src/render.rs +48 −13
| @@ -1,3 +1,5 @@ | |||
| 1 | use url::Url; | ||
| 2 | |||
| 1 | use matrix_sdk::{ | 3 | use matrix_sdk::{ |
| 2 | events::{ | 4 | events::{ |
| 3 | room::{ | 5 | room::{ |
| @@ -26,11 +28,11 @@ pub struct RenderedEvent { | |||
| 26 | /// The tags of the event, | 28 | /// The tags of the event, |
| 27 | pub tags: Vec<String>, | 29 | pub tags: Vec<String>, |
| 28 | // TODO we need to allow multiple message/tags pairs here. | 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 | 31 | // This will be useful if a message has a part that we should be able to |
| 30 | // using a filter on the tags. | 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 | 34 | // One such example would be a code snippet, where we want to show that |
| 33 | // posted a code snippet but we might want to filter out the body. | 35 | // someone posted a code snippet but we might want to filter out the body. |
| 34 | /// The event rendered as a string. | 36 | /// The event rendered as a string. |
| 35 | pub message: String, | 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 | /// Rendering function for room messages. | 258 | /// Rendering function for room messages. |
| 232 | // FIXME: Pass room member | 259 | // FIXME: Pass room member |
| 233 | // TODO: We should not return raw strings here but something like [Block] | 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 | 391 | /// Trait for message event types that contain an optional formatted body. |
| 365 | /// return the formatted body if present, else fallback to the regular body. | 392 | /// `resolve_body` will return the formatted body if present, else fallback to |
| 393 | /// the regular body. | ||
| 366 | trait HasFormattedBody { | 394 | trait HasFormattedBody { |
| 367 | fn body(&self) -> &str; | 395 | fn body(&self) -> &str; |
| 368 | fn formatted_body(&self) -> Option<&str>; | 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 | 403 | // Repeating this for each event type would get boring fast so lets use a simple |
| 376 | // the trait for a struct that has a `body` and `formatted_body` field | 404 | // macro to implement the trait for a struct that has a `body` and |
| 405 | // `formatted_body` field | ||
| 377 | macro_rules! has_formatted_body { | 406 | macro_rules! has_formatted_body { |
| 378 | ($content: ident) => { | 407 | ($content: ident) => { |
| 379 | impl HasFormattedBody for $content { | 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 | 422 | /// This trait is implemented for message types that can contain either an URL |
| 394 | /// file. One of these _must_ be present. | 423 | /// or an encrypted file. One of these _must_ be present. |
| 395 | trait HasUrlOrFile { | 424 | trait HasUrlOrFile { |
| 396 | fn url(&self) -> Option<&str>; | 425 | fn url(&self) -> Option<&str>; |
| 397 | fn file(&self) -> Option<&str>; | 426 | fn file(&self) -> Option<&str>; |
| 427 | fn body(&self) -> &str; | ||
| 398 | #[inline] | 428 | #[inline] |
| 399 | fn resolve_url(&self) -> &str { | 429 | fn resolve_url(&self) -> &str { |
| 400 | // the file is either encrypted or not encrypted so either `url` or `file` must | 430 | // the file is either encrypted or not encrypted so either `url` or |
| 401 | // exist and unwrapping will never panic | 431 | // `file` must exist and unwrapping will never panic |
| 402 | self.url().or_else(|| self.file()).unwrap() | 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 | macro_rules! has_url_or_file { | 438 | macro_rules! has_url_or_file { |
| 408 | ($content: ident) => { | 439 | ($content: ident) => { |
| 409 | impl HasUrlOrFile for $content { | 440 | impl HasUrlOrFile for $content { |
| 441 | fn body(&self) -> &str { | ||
| 442 | &self.body | ||
| 443 | } | ||
| 444 | |||
| 410 | #[inline] | 445 | #[inline] |
| 411 | fn url(&self) -> Option<&str> { | 446 | fn url(&self) -> Option<&str> { |
| 412 | self.url.as_deref() | 447 | self.url.as_deref() |