Commit f99447ca82

f99447ca828a818149e5c2ddd30c490bce69c370

parent: 428bbcb434

Unsigned

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

render: Change the Render trait up.

This removes the Render implementation for the member event but adds it
to a bunch of contents.

Turns out we need to render the contents without a prefix for local
echo, so we need to change the trait up a bit.

At the same time we'll never need to render member events without a
prefix, so it makes sense to special case them.

Layout: unified · split

src/render.rs +217 −154
@@ -37,6 +37,19 @@ pub struct RenderedEvent {
3737 pub message: String,
3838}
3939
40pub struct RenderedContent {
41 /// The tags of the event,
42 pub tags: Vec<String>,
43 // TODO we need to allow multiple message/tags pairs here.
44 // This will be useful if a message has a part that we should be able to
45 // hide using a filter on the tags.
46 //
47 // One such example would be a code snippet, where we want to show that
48 // someone posted a code snippet but we might want to filter out the body.
49 /// The event rendered as a string.
50 pub message: String,
51}
52
4053/// Trait allowing events to be rendered for Weechat.
4154pub trait Render {
4255 /// The event specific tags that should be attached to the rendered event.
@@ -50,8 +63,26 @@ pub trait Render {
5063 /// when rendering.
5164 type RenderContext;
5265
66 fn tags(&self) -> Vec<String> {
67 Self::TAGS.iter().map(|t| t.to_string()).collect()
68 }
69
5370 /// Render the event.
54 fn render(&self, context: &Self::RenderContext) -> RenderedEvent;
71 fn render_with_prefix(&self, sender: &WeechatRoomMember, context: &Self::RenderContext) -> RenderedEvent {
72 // TODO the sender should have a color attribute and we should use it
73 // here.
74 let colorname_user = Weechat::info_get("nick_color_name", sender.user_id.as_str()).unwrap_or_else(|| String::from("default"));
75 let prefix = format!("{}{}{}", Weechat::color(&colorname_user), sender.nick, Weechat::color("reset"));
76
77 let content = self.render(context);
78
79 RenderedEvent {
80 tags: self.tags(),
81 message: content.message,
82 message_timestamp: 0,
83 }
84 }
85 fn render(&self, context: &Self::RenderContext) -> RenderedContent;
5586}
5687
5788pub struct MembershipContext<'a> {
@@ -61,172 +92,167 @@ pub struct MembershipContext<'a> {
6192 pub target: &'a WeechatRoomMember,
6293}
6394
64impl<'a> Render for &'a SyncStateEvent<MemberEventContent> {
95/// Rendering implementation for membership events (joins, leaves, bans, profile
96/// changes, etc).
97pub fn render_membership<'a>(event: &SyncStateEvent<MemberEventContent>, context: &MembershipContext<'a>) -> RenderedEvent {
6598 const TAGS: &'static [&'static str] = &["matrix_membership"];
99 use MembershipChange::*;
100 let change_op = event.membership_change();
101
102 let operation = match change_op {
103 None => "did nothing",
104 Error => "caused an error", // must never happen
105 Joined => "has joined the room",
106 Left => "has left the room",
107 Banned => "was banned by",
108 Unbanned => "was unbanned by",
109 Kicked => "was kicked from the room by",
110 Invited => "was invited to the room by",
111 KickedAndBanned => "was kicked and banned by",
112 InvitationRejected => "rejected the invitation",
113 InvitationRevoked => "had the invitation revoked by",
114 ProfileChanged { .. } => "_",
115 NotImplemented => "performed an unimplemented operation",
116 _ => "caused an unhandled membership change",
117 };
66118
67 type RenderContext = MembershipContext<'a>;
68
69 /// Rendering implementation for membership events (joins, leaves, bans, profile
70 /// changes, etc).
71 fn render(&self, context: &MembershipContext<'a>) -> RenderedEvent {
72 use MembershipChange::*;
73 let change_op = self.membership_change();
74
75 let operation = match change_op {
76 None => "did nothing",
77 Error => "caused an error", // must never happen
78 Joined => "has joined the room",
79 Left => "has left the room",
80 Banned => "was banned by",
81 Unbanned => "was unbanned by",
82 Kicked => "was kicked from the room by",
83 Invited => "was invited to the room by",
84 KickedAndBanned => "was kicked and banned by",
85 InvitationRejected => "rejected the invitation",
86 InvitationRevoked => "had the invitation revoked by",
87 ProfileChanged { .. } => "_",
88 NotImplemented => "performed an unimplemented operation",
89 _ => "caused an unhandled membership change",
90 };
91
92 fn formatted_name(member: &WeechatRoomMember) -> String {
93 match &member.display_name {
94 Some(display_name) => {
95 format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}",
96 name = display_name,
97 user_id = &member.user_id,
98 color_delim = Weechat::color("chat_delimiters"),
99 color_reset = Weechat::color("reset"))
100 }
119 fn formatted_name(member: &WeechatRoomMember) -> String {
120 match &member.display_name {
121 Some(display_name) => {
122 format!("{name} {color_delim}({color_reset}{user_id}{color_delim}){color_reset}",
123 name = display_name,
124 user_id = &member.user_id,
125 color_delim = Weechat::color("chat_delimiters"),
126 color_reset = Weechat::color("reset"))
127 }
101128
102 Option::None => {
103 member.user_id.as_ref().to_string()
104 }
129 Option::None => {
130 member.user_id.as_ref().to_string()
105131 }
106132 }
133 }
107134
108 let (prefix, color_action) = match change_op {
109 Joined => ("join", "green"),
110 Banned | ProfileChanged { .. } | Invited => ("network", "magenta"),
111 _ => ("quit", "red"),
112 };
113
114 let color_action = Weechat::color(color_action);
115 let color_reset = Weechat::color("reset");
116
117 let operation = format!(
118 "{color_action}{op}{color_reset}",
119 color_action = color_action,
120 op = operation,
121 color_reset = color_reset
122 );
123
124 let target_name = format!(
125 "{color_user}{target_name}{color_reset}",
126 target_name = formatted_name(context.target),
127 color_user = Weechat::color("reset"), // TODO
128 color_reset = Weechat::color("reset")
129 );
130
131 let sender_name = format!(
132 "{color_user}{sender_name}{color_reset}",
133 sender_name = formatted_name(context.sender),
134 color_user = Weechat::color("reset"), // TODO
135 color_reset = Weechat::color("reset")
136 );
137
138 let message = match change_op {
139 ProfileChanged {
140 displayname_changed,
141 avatar_url_changed,
142 } => {
143 let new_display_name = &self.content.displayname;
135 let (prefix, color_action) = match change_op {
136 Joined => ("join", "green"),
137 Banned | ProfileChanged { .. } | Invited => ("network", "magenta"),
138 _ => ("quit", "red"),
139 };
144140
145 // TODO: Should we display the new avatar URL?
146 // let new_avatar = self.content.avatar_url.as_ref();
141 let color_action = Weechat::color(color_action);
142 let color_reset = Weechat::color("reset");
147143
148 match (displayname_changed, avatar_url_changed) {
149 (false, true) =>
150 format!(
151 "{prefix} {target} {color_action}changed their avatar{color_reset}",
144 let operation = format!(
145 "{color_action}{op}{color_reset}",
146 color_action = color_action,
147 op = operation,
148 color_reset = color_reset
149 );
150
151 let target_name = format!(
152 "{color_user}{target_name}{color_reset}",
153 target_name = formatted_name(context.target),
154 color_user = Weechat::color("reset"), // TODO
155 color_reset = Weechat::color("reset")
156 );
157
158 let sender_name = format!(
159 "{color_user}{sender_name}{color_reset}",
160 sender_name = formatted_name(context.sender),
161 color_user = Weechat::color("reset"), // TODO
162 color_reset = Weechat::color("reset")
163 );
164
165 let message = match change_op {
166 ProfileChanged {
167 displayname_changed,
168 avatar_url_changed,
169 } => {
170 let new_display_name = &event.content.displayname;
171
172 // TODO: Should we display the new avatar URL?
173 // let new_avatar = self.content.avatar_url.as_ref();
174
175 match (displayname_changed, avatar_url_changed) {
176 (false, true) =>
177 format!(
178 "{prefix} {target} {color_action}changed their avatar{color_reset}",
179 prefix = Weechat::prefix(prefix),
180 target = target_name,
181 color_action = color_action,
182 color_reset = color_reset
183 ),
184 (true, false) => {
185 match new_display_name {
186 Some(name) => format!(
187 "{prefix} {target} {color_action}changed their display name to{color_reset} {new}",
188 prefix = Weechat::prefix(prefix),
189 target = target_name,
190 new = name,
191 color_action = color_action,
192 color_reset = color_reset
193 ),
194 Option::None => format!(
195 "{prefix} {target} {color_action}removed their display name{color_reset}",
152196 prefix = Weechat::prefix(prefix),
153197 target = target_name,
154198 color_action = color_action,
155199 color_reset = color_reset
156200 ),
157 (true, false) => {
158 match new_display_name {
159 Some(name) => format!(
160 "{prefix} {target} {color_action}changed their display name to{color_reset} {new}",
161 prefix = Weechat::prefix(prefix),
162 target = target_name,
163 new = name,
164 color_action = color_action,
165 color_reset = color_reset
166 ),
167 Option::None => format!(
168 "{prefix} {target} {color_action}removed their display name{color_reset}",
169 prefix = Weechat::prefix(prefix),
170 target = target_name,
171 color_action = color_action,
172 color_reset = color_reset
173 ),
174 }
175201 }
176 (true, true) =>
177 match new_display_name {
178 Some(name) => format!(
179 "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}",
180 prefix = Weechat::prefix(prefix),
181 target = target_name,
182 new = name,
183 color_action = color_action,
184 color_reset = color_reset
185 ),
186 Option::None => format!(
187 "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}",
188 prefix = Weechat::prefix(prefix),
189 target = target_name,
190 color_action = color_action,
191 color_reset = color_reset
192 ),
193 }
194 (false, false) =>
195 "Cannot happen: got profile changed but nothing really changed".to_string()
196202 }
203 (true, true) =>
204 match new_display_name {
205 Some(name) => format!(
206 "{prefix} {target} {color_action}changed their avatar and changed their display name to{color_reset} {new}",
207 prefix = Weechat::prefix(prefix),
208 target = target_name,
209 new = name,
210 color_action = color_action,
211 color_reset = color_reset
212 ),
213 Option::None => format!(
214 "{prefix} {target} {color_action}changed their avatar and removed display name{color_reset}",
215 prefix = Weechat::prefix(prefix),
216 target = target_name,
217 color_action = color_action,
218 color_reset = color_reset
219 ),
220 }
221 (false, false) =>
222 "Cannot happen: got profile changed but nothing really changed".to_string()
197223 }
198 None | Error | Joined | Left | InvitationRejected
199 | NotImplemented => format!(
200 "{prefix} {target} {op}",
201 prefix = Weechat::prefix(prefix),
202 target = target_name,
203 op = operation
204 ),
205 Banned | Unbanned | Kicked | Invited | InvitationRevoked
206 | KickedAndBanned => format!(
207 "{prefix} {target} {op} {sender}",
208 prefix = Weechat::prefix(prefix),
209 target = target_name,
210 op = operation,
211 sender = sender_name
212 ),
213
214 // This means an unsupported membership change happened so we just
215 // print a generic message to indicate this.
216 _ => format!(
217 "{prefix} {sender} {op}",
218 prefix = Weechat::prefix(prefix),
219 sender = sender_name,
220 op = operation,
221 ),
222 };
223
224 RenderedEvent {
225 message,
226 tags: Self::TAGS.iter().map(|t| t.to_string()).collect(),
227 // FIXME do the conversion here.
228 message_timestamp: 0,
229224 }
225 None | Error | Joined | Left | InvitationRejected
226 | NotImplemented => format!(
227 "{prefix} {target} {op}",
228 prefix = Weechat::prefix(prefix),
229 target = target_name,
230 op = operation
231 ),
232 Banned | Unbanned | Kicked | Invited | InvitationRevoked
233 | KickedAndBanned => format!(
234 "{prefix} {target} {op} {sender}",
235 prefix = Weechat::prefix(prefix),
236 target = target_name,
237 op = operation,
238 sender = sender_name
239 ),
240
241 // This means an unsupported membership change happened so we just
242 // print a generic message to indicate this.
243 _ => format!(
244 "{prefix} {sender} {op}",
245 prefix = Weechat::prefix(prefix),
246 sender = sender_name,
247 op = operation,
248 ),
249 };
250
251 RenderedEvent {
252 message,
253 tags: TAGS.iter().map(|t| t.to_string()).collect(),
254 // FIXME do the conversion here.
255 message_timestamp: 0,
230256 }
231257}
232258
@@ -234,7 +260,7 @@ impl Render for dyn HasUrlOrFile {
234260 type RenderContext = Url;
235261 const TAGS: &'static [&'static str] = &["matrix_media"];
236262
237 fn render(&self, _homeserver: &Self::RenderContext) -> RenderedEvent {
263 fn render(&self, _homeserver: &Self::RenderContext) -> RenderedContent {
238264 let message = format!(
239265 "{color_delimiter}<{color_reset}{}{color_delimiter}>\
240266 [{color_reset}{}{color_delimiter}]{color_reset}",
@@ -246,13 +272,50 @@ impl Render for dyn HasUrlOrFile {
246272 color_reset = Weechat::color("reset")
247273 );
248274
249 RenderedEvent {
275 RenderedContent {
250276 message,
251 tags: Self::TAGS.iter().map(|t| t.to_string()).collect(),
252 // FIXME do the conversion here.
277 tags: self.tags(),
278 }
279 }
280}
281
282impl Render for TextMessageEventContent {
283 const TAGS: &'static [&'static str] = &["matrix_media"];
284 type RenderContext = ();
285
286 fn render(&self, _: &Self::RenderContext) -> RenderedContent {
287 RenderedContent {
288 message: self.body.clone(),
289 tags: self.tags(),
290 }
291 }
292}
293
294impl Render for EmoteMessageEventContent {
295 const TAGS: &'static [&'static str] = &["matrix_media"];
296 type RenderContext = WeechatRoomMember;
297
298 fn render_with_prefix(&self, _sender: &WeechatRoomMember, context: &Self::RenderContext) -> RenderedEvent {
299 let content = self.render(context);
300 let _prefix = Weechat::prefix("action");
301
302 // TODO the rendered event should have a prefix field
303 RenderedEvent {
304 message: content.message,
305 tags: content.tags,
253306 message_timestamp: 0,
254307 }
255308 }
309
310 fn render(&self, sender: &Self::RenderContext) -> RenderedContent {
311 let message = format!("{} {}", sender.nick, self.body);
312
313 RenderedContent {
314 message,
315 tags: self.tags(),
316 }
317 }
318
256319}
257320
258321/// Rendering function for room messages.
src/room_buffer.rs +2 −2
@@ -50,7 +50,7 @@ use tracing::{debug, error, trace};
5050
5151use crate::{
5252 connection::{Connection, TYPING_NOTICE_TIMEOUT},
53 render::{render_message, MembershipContext, Render},
53 render::{render_message, MembershipContext, render_membership},
5454};
5555use std::{
5656 cell::{Ref, RefCell, RefMut},
@@ -710,7 +710,7 @@ impl RoomBuffer {
710710 let message = match (&sender, &target) {
711711 (Some(sender), Some(target)) => {
712712 let context = MembershipContext { sender, target };
713 event.render(&context).message
713 render_membership(event, &context).message
714714 }
715715
716716 _ => {