File tree Expand file tree Collapse file tree 6 files changed +25
-2
lines changed
Expand file tree Collapse file tree 6 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ pub trait Message: Clone {
55 fn get_content ( & self ) -> impl Element ;
66 fn get_identifier ( & self ) -> String ;
77 fn get_nonce ( & self ) -> Option < & String > ;
8+ fn should_group ( & self , previous : & Self ) -> bool ;
89}
910
1011pub trait MessageAuthor : PartialEq + Eq {
Original file line number Diff line number Diff line change 11use std:: sync:: Arc ;
22
33use scope_chat:: channel:: Channel ;
4+ use serenity:: all:: Timestamp ;
45use tokio:: sync:: broadcast;
56
67use crate :: {
@@ -51,6 +52,7 @@ impl Channel for DiscordChannel {
5152 author : self . client . user ( ) . clone ( ) ,
5253 id : Snowflake { content : 0 } ,
5354 nonce : Some ( nonce) ,
55+ creation_time : Timestamp :: now ( ) ,
5456 }
5557 }
5658}
Original file line number Diff line number Diff line change @@ -114,6 +114,7 @@ impl EventHandler for DiscordClient {
114114 Nonce :: Number ( n) => n. to_string ( ) ,
115115 Nonce :: String ( s) => s,
116116 } ) ,
117+ creation_time : msg. timestamp ,
117118 } ) ;
118119 }
119120 }
Original file line number Diff line number Diff line change 1+ use std:: time:: Instant ;
2+
13use author:: DiscordMessageAuthor ;
24use content:: DiscordMessageContent ;
35use gpui:: { Element , IntoElement } ;
@@ -14,6 +16,7 @@ pub struct DiscordMessage {
1416 pub author : DiscordMessageAuthor ,
1517 pub id : Snowflake ,
1618 pub nonce : Option < String > ,
19+ pub creation_time : serenity:: model:: Timestamp ,
1720}
1821
1922impl Message for DiscordMessage {
@@ -32,4 +35,10 @@ impl Message for DiscordMessage {
3235 fn get_nonce ( & self ) -> Option < & String > {
3336 self . nonce . as_ref ( )
3437 }
38+
39+ fn should_group ( & self , previous : & Self ) -> bool {
40+ const MAX_DISCORD_MESSAGE_GAP_SECS_FOR_GROUP : i64 = 5 * 60 ;
41+
42+ self . creation_time . signed_duration_since ( & * previous. creation_time ) . num_seconds ( ) <= MAX_DISCORD_MESSAGE_GAP_SECS_FOR_GROUP
43+ }
3544}
Original file line number Diff line number Diff line change @@ -49,6 +49,10 @@ impl<M: Message> MessageGroup<M> {
4949
5050 self . contents . remove ( index) ;
5151 }
52+
53+ pub fn last ( & self ) -> & M {
54+ self . contents . last ( ) . unwrap ( )
55+ }
5256}
5357
5458pub fn message < M : Message > ( message : MessageGroup < M > ) -> impl IntoElement {
Original file line number Diff line number Diff line change @@ -37,7 +37,10 @@ impl<M: Message> MessageList<M> {
3737
3838 let last = self . messages . last_mut ( ) ;
3939
40- if last. is_some ( ) && last. as_ref ( ) . unwrap ( ) . get_author ( ) . get_id ( ) == message. get_author ( ) . get_id ( ) {
40+ if last. is_some ( )
41+ && last. as_ref ( ) . unwrap ( ) . get_author ( ) . get_id ( ) == message. get_author ( ) . get_id ( )
42+ && message. should_group ( last. as_ref ( ) . unwrap ( ) . last ( ) )
43+ {
4144 last. unwrap ( ) . add ( message) ;
4245 } else {
4346 self . messages . push ( MessageGroup :: new ( message) ) ;
@@ -47,7 +50,10 @@ impl<M: Message> MessageList<M> {
4750 pub fn add_pending_message ( & mut self , pending_message : M ) {
4851 let last = self . messages . last_mut ( ) ;
4952
50- if last. is_some ( ) && last. as_ref ( ) . unwrap ( ) . get_author ( ) . get_id ( ) == pending_message. get_author ( ) . get_id ( ) {
53+ if last. is_some ( )
54+ && last. as_ref ( ) . unwrap ( ) . get_author ( ) . get_id ( ) == pending_message. get_author ( ) . get_id ( )
55+ && pending_message. should_group ( last. as_ref ( ) . unwrap ( ) . last ( ) )
56+ {
5157 last. unwrap ( ) . add ( pending_message) ;
5258 } else {
5359 self . messages . push ( MessageGroup :: new ( pending_message) ) ;
You can’t perform that action at this time.
0 commit comments