Commit 0ac54e78bc

0ac54e78bc05d789bf53ec5a52762a665a90eeba

parent: bcad95a38f

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-06 16:38 UTC

render: Create a Render trait using associated types and constants.

This adds a render trait back, the trait now allows adding custom
additional context that might be needed to render the event to be passed
to the render method.

It also naturally scopes the event specific tags in the associated
constant of the trait.

Layout: unified · split

src/render.rs +189 −142
@@ -1,4 +1,3 @@
1use crate::room_buffer::WeechatRoomMember;
2use matrix_sdk::{ 1use matrix_sdk::{
3 events::{ 2 events::{
4 room::{ 3 room::{
@@ -15,169 +14,217 @@ use matrix_sdk::{
15 }, 14 },
16 PossiblyRedactedExt, 15 PossiblyRedactedExt,
17}; 16};
17
18use weechat::Weechat; 18use weechat::Weechat;
19 19
20/// Rendering function for membership events (joins, leaves, bans, profile 20use crate::room_buffer::WeechatRoomMember;
21/// changes, etc).
22// TODO: We should not return raw strings here but something like [Block]
23// where Block is (String, [Tags]). Each Block represents one or several lines
24// which have the same tags.
25pub fn render_membership(
26 member: &SyncStateEvent<MemberEventContent>,
27 sender: &WeechatRoomMember,
28 target: &WeechatRoomMember,
29) -> String {
30 use MembershipChange::*;
31 let change_op = member.membership_change();
32
33 let operation = match change_op {
34 None => "did nothing",
35 Error => "caused an error", // must never happen
36 Joined => "has joined the room",
37 Left => "has left the room",
38 Banned => "was banned by",
39 Unbanned => "was unbanned by",
40 Kicked => "was kicked from the room by",
41 Invited => "was invited to the room by",
42 KickedAndBanned => "was kicked and banned by",
43 InvitationRejected => "rejected the invitation",
44 InvitationRevoked => "had the invitation revoked by",
45 ProfileChanged { .. } => "_",
46 NotImplemented => "performed an unimplemented operation",
47 _ => "caused an unhandled membership change",
48 };
49 21
50 fn formatted_name(member: &WeechatRoomMember) -> String { 22/// The rendered version of an event.
51 match &member.display_name { 23pub struct RenderedEvent {
52 Some(display_name) => { 24 /// The UNIX timestamp of the event.
53 format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}", 25 pub message_timestamp: u64,
54 name = display_name, 26 /// The tags of the event,
55 user_id = &member.user_id, 27 pub tags: Vec<String>,
56 color_delim = Weechat::color("chat_delimiters"), 28 // TODO we need to allow multiple message/tags pairs here.
57 color_reset = Weechat::color("reset")) 29 // This will be useful if a message has a part that we should be able to hide
58 } 30 // using a filter on the tags.
31 //
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 /// The event rendered as a string.
35 pub message: String,
36}
37
38/// Trait allowing events to be rendered for Weechat.
39pub trait Render {
40 /// The event specific tags that should be attached to the rendered event.
41 const TAGS: &'static [&'static str];
42
43 /// Some events might need additional context to be rendered, for example if
44 /// we want to display the sender, we don't want to display the mxid,
45 /// instead we want to display the disambiguated display name.
46 ///
47 /// This allows the render implementation to be passed some additional data
48 /// when rendering.
49 type RenderContext;
59 50
60 std::option::Option::None => { 51 /// Render the event.
61 member.user_id.as_ref().to_string() 52 fn render(&self, context: &Self::RenderContext) -> RenderedEvent;
53}
54
55pub struct MembershipContext<'a> {
56 /// The room member object of the event sender.
57 pub sender: &'a WeechatRoomMember,
58 /// The room member object of the target of the membership change.
59 pub target: &'a WeechatRoomMember,
60}
61
62impl<'a> Render for &'a SyncStateEvent<MemberEventContent> {
63 const TAGS: &'static [&'static str] = &["matrix_membership"];
64
65 type RenderContext = MembershipContext<'a>;
66
67 /// Rendering implementation for membership events (joins, leaves, bans, profile
68 /// changes, etc).
69 fn render(&self, context: &MembershipContext<'a>) -> RenderedEvent {
70 use MembershipChange::*;
71 let change_op = self.membership_change();
72
73 let operation = match change_op {
74 None => "did nothing",
75 Error => "caused an error", // must never happen
76 Joined => "has joined the room",
77 Left => "has left the room",
78 Banned => "was banned by",
79 Unbanned => "was unbanned by",
80 Kicked => "was kicked from the room by",
81 Invited => "was invited to the room by",
82 KickedAndBanned => "was kicked and banned by",
83 InvitationRejected => "rejected the invitation",
84 InvitationRevoked => "had the invitation revoked by",
85 ProfileChanged { .. } => "_",
86 NotImplemented => "performed an unimplemented operation",
87 _ => "caused an unhandled membership change",
88 };
89
90 fn formatted_name(member: &WeechatRoomMember) -> String {
91 match &member.display_name {
92 Some(display_name) => {
93 format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}",
94 name = display_name,
95 user_id = &member.user_id,
96 color_delim = Weechat::color("chat_delimiters"),
97 color_reset = Weechat::color("reset"))
98 }
99
100 std::option::Option::None => {
101 member.user_id.as_ref().to_string()
102 }
62 } 103 }
63 } 104 }
64 }
65 105
66 let (prefix, color_action) = match change_op { 106 let (prefix, color_action) = match change_op {
67 Joined => ("join", "green"), 107 Joined => ("join", "green"),
68 Banned | ProfileChanged { .. } | Invited => ("network", "magenta"), 108 Banned | ProfileChanged { .. } | Invited => ("network", "magenta"),
69 _ => ("quit", "red"), 109 _ => ("quit", "red"),
70 }; 110 };
71 111
72 let color_action = Weechat::color(color_action); 112 let color_action = Weechat::color(color_action);
73 let color_reset = Weechat::color("reset"); 113 let color_reset = Weechat::color("reset");
74 114
75 let operation = format!( 115 let operation = format!(
76 "{color_action}{op}{color_reset}", 116 "{color_action}{op}{color_reset}",
77 color_action = color_action, 117 color_action = color_action,
78 op = operation, 118 op = operation,
79 color_reset = color_reset 119 color_reset = color_reset
80 ); 120 );
81 121
82 let target_name = format!( 122 let target_name = format!(
83 "{color_user}{target_name}{color_reset}", 123 "{color_user}{target_name}{color_reset}",
84 target_name = formatted_name(target), 124 target_name = formatted_name(context.target),
85 color_user = Weechat::color("reset"), // TODO 125 color_user = Weechat::color("reset"), // TODO
86 color_reset = Weechat::color("reset") 126 color_reset = Weechat::color("reset")
87 ); 127 );
88 128
89 let sender_name = format!( 129 let sender_name = format!(
90 "{color_user}{sender_name}{color_reset}", 130 "{color_user}{sender_name}{color_reset}",
91 sender_name = formatted_name(sender), 131 sender_name = formatted_name(context.sender),
92 color_user = Weechat::color("reset"), // TODO 132 color_user = Weechat::color("reset"), // TODO
93 color_reset = Weechat::color("reset") 133 color_reset = Weechat::color("reset")
94 ); 134 );
95 135
96 match change_op { 136 let message = match change_op {
97 ProfileChanged { 137 ProfileChanged {
98 displayname_changed, 138 displayname_changed,
99 avatar_url_changed, 139 avatar_url_changed,
100 } => { 140 } => {
101 let new_display_name = &member.content.displayname; 141 let new_display_name = &self.content.displayname;
102 142
103 // TODO: Should we display the new avatar URL? 143 // TODO: Should we display the new avatar URL?
104 // let new_avatar = member.content.avatar_url.as_ref(); 144 // let new_avatar = self.content.avatar_url.as_ref();
105 145
106 match (displayname_changed, avatar_url_changed) { 146 match (displayname_changed, avatar_url_changed) {
107 (false, true) => 147 (false, true) =>
108 format!( 148 format!(
109 "{prefix} {target} {color_action}changed their avatar{color_reset}", 149 "{prefix} {target} {color_action}changed their avatar{color_reset}",
110 prefix = Weechat::prefix(prefix),
111 target = target_name,
112 color_action = color_action,
113 color_reset = color_reset
114 ),
115 (true, false) => {
116 match new_display_name {
117 Some(name) => format!(
118 "{prefix} {target} {color_action}changed their display name to{color_reset} {new}",
119 prefix = Weechat::prefix(prefix),
120 target = target_name,
121 new = name,
122 color_action = color_action,
123 color_reset = color_reset
124 ),
125 std::option::Option::None => format!(
126 "{prefix} {target} {color_action}removed their display name{color_reset}",
127 prefix = Weechat::prefix(prefix), 150 prefix = Weechat::prefix(prefix),
128 target = target_name, 151 target = target_name,
129 color_action = color_action, 152 color_action = color_action,
130 color_reset = color_reset 153 color_reset = color_reset
131 ), 154 ),
155 (true, false) => {
156 match new_display_name {
157 Some(name) => format!(
158 "{prefix} {target} {color_action}changed their display name to{color_reset} {new}",
159 prefix = Weechat::prefix(prefix),
160 target = target_name,
161 new = name,
162 color_action = color_action,
163 color_reset = color_reset
164 ),
165 std::option::Option::None => format!(
166 "{prefix} {target} {color_action}removed their display name{color_reset}",
167 prefix = Weechat::prefix(prefix),
168 target = target_name,
169 color_action = color_action,
170 color_reset = color_reset
171 ),
172 }
132 } 173 }
174 (true, true) =>
175 match new_display_name {
176 Some(name) => format!(
177 "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}",
178 prefix = Weechat::prefix(prefix),
179 target = target_name,
180 new = name,
181 color_action = color_action,
182 color_reset = color_reset
183 ),
184 std::option::Option::None => format!(
185 "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}",
186 prefix = Weechat::prefix(prefix),
187 target = target_name,
188 color_action = color_action,
189 color_reset = color_reset
190 ),
191 }
192 (false, false) =>
193 "Cannot happen: got profile changed but nothing really changed".to_string()
133 } 194 }
134 (true, true) =>
135 match new_display_name {
136 Some(name) => format!(
137 "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}",
138 prefix = Weechat::prefix(prefix),
139 target = target_name,
140 new = name,
141 color_action = color_action,
142 color_reset = color_reset
143 ),
144 std::option::Option::None => format!(
145 "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}",
146 prefix = Weechat::prefix(prefix),
147 target = target_name,
148 color_action = color_action,
149 color_reset = color_reset
150 ),
151 }
152 (false, false) =>
153 "Cannot happen: got profile changed but nothing really changed".to_string()
154 } 195 }
155 } 196 None | Error | Joined | Left | InvitationRejected
156 None | Error | Joined | Left | InvitationRejected | NotImplemented => { 197 | NotImplemented => format!(
157 format!(
158 "{prefix} {target} {op}", 198 "{prefix} {target} {op}",
159 prefix = Weechat::prefix(prefix), 199 prefix = Weechat::prefix(prefix),
160 target = target_name, 200 target = target_name,
161 op = operation 201 op = operation
162 ) 202 ),
203 Banned | Unbanned | Kicked | Invited | InvitationRevoked
204 | KickedAndBanned => format!(
205 "{prefix} {target} {op} {sender}",
206 prefix = Weechat::prefix(prefix),
207 target = target_name,
208 op = operation,
209 sender = sender_name
210 ),
211
212 // This means an unsupported membership change happened so we just
213 // print a generic message to indicate this.
214 _ => format!(
215 "{prefix} {sender} {op}",
216 prefix = Weechat::prefix(prefix),
217 sender = sender_name,
218 op = operation,
219 ),
220 };
221
222 RenderedEvent {
223 message,
224 tags: Self::TAGS.iter().map(|t| t.to_string()).collect(),
225 // FIXME do the conversion here.
226 message_timestamp: 0,
163 } 227 }
164 Banned | Unbanned | Kicked | Invited | InvitationRevoked
165 | KickedAndBanned => format!(
166 "{prefix} {target} {op} {sender}",
167 prefix = Weechat::prefix(prefix),
168 target = target_name,
169 op = operation,
170 sender = sender_name
171 ),
172
173 // This means an unsupported membership change happened so we just print a generic
174 // message to indicate this.
175 _ => format!(
176 "{prefix} {sender} {op}",
177 prefix = Weechat::prefix(prefix),
178 sender = sender_name,
179 op = operation,
180 ),
181 } 228 }
182} 229}
183 230
src/room_buffer.rs +3 −2
@@ -50,7 +50,7 @@ use tracing::{debug, error, trace};
50 50
51use crate::{ 51use crate::{
52 connection::{Connection, TYPING_NOTICE_TIMEOUT}, 52 connection::{Connection, TYPING_NOTICE_TIMEOUT},
53 render::{render_membership, render_message}, 53 render::{render_message, MembershipContext, Render},
54}; 54};
55use std::{ 55use std::{
56 cell::{Ref, RefCell, RefMut}, 56 cell::{Ref, RefCell, RefMut},
@@ -709,7 +709,8 @@ impl RoomBuffer {
709 // Display the event message 709 // Display the event message
710 let message = match (&sender, &target) { 710 let message = match (&sender, &target) {
711 (Some(sender), Some(target)) => { 711 (Some(sender), Some(target)) => {
712 render_membership(event, sender, target) 712 let context = MembershipContext { sender, target };
713 event.render(&context).message
713 } 714 }
714 715
715 _ => { 716 _ => {