Commit 428bbcb434

428bbcb43453e1bd0b12fe568f8bd7b3b4c229b7

parent: c3d4ac0bbf

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-07 19:47 UTC

render: Implement render for the media event contents.

Layout: unified · split

src/render.rs +48 −13
@@ -1,3 +1,5 @@
1use url::Url;
2
1use matrix_sdk::{ 3use 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
233impl 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.
366trait HasFormattedBody { 394trait 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
377macro_rules! has_formatted_body { 406macro_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.
395trait HasUrlOrFile { 424trait 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.
407macro_rules! has_url_or_file { 438macro_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()