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
13use matrix_sdk::{
24 events::{
35 room::{
@@ -26,11 +28,11 @@ pub struct RenderedEvent {
2628 /// The tags of the event,
2729 pub tags: Vec<String>,
2830 // 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.
3133 //
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.
3436 /// The event rendered as a string.
3537 pub message: String,
3638}
@@ -228,6 +230,31 @@ impl<'a> Render for &'a SyncStateEvent<MemberEventContent> {
228230 }
229231}
230232
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
231258/// Rendering function for room messages.
232259// FIXME: Pass room member
233260// TODO: We should not return raw strings here but something like [Block]
@@ -361,8 +388,9 @@ pub fn render_message(
361388 }
362389}
363390
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.
366394trait HasFormattedBody {
367395 fn body(&self) -> &str;
368396 fn formatted_body(&self) -> Option<&str>;
@@ -372,8 +400,9 @@ trait HasFormattedBody {
372400 }
373401}
374402
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
377406macro_rules! has_formatted_body {
378407 ($content: ident) => {
379408 impl HasFormattedBody for $content {
@@ -390,23 +419,29 @@ macro_rules! has_formatted_body {
390419 };
391420}
392421
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.
395424trait HasUrlOrFile {
396425 fn url(&self) -> Option<&str>;
397426 fn file(&self) -> Option<&str>;
427 fn body(&self) -> &str;
398428 #[inline]
399429 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
402432 self.url().or_else(|| self.file()).unwrap()
403433 }
404434}
405435
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.
407438macro_rules! has_url_or_file {
408439 ($content: ident) => {
409440 impl HasUrlOrFile for $content {
441 fn body(&self) -> &str {
442 &self.body
443 }
444
410445 #[inline]
411446 fn url(&self) -> Option<&str> {
412447 self.url.as_deref()