1- use crate :: {
2- ChatwootWebhookPayload ,
3- constants:: { EVENT_CONVERSATION_STATUS_CHANGED , EVENT_CONVERSATION_UPDATED , EVENT_MESSAGE_CREATED } ,
4- markdown_plain_text,
5- } ;
1+ use crate :: { ChatwootWebhookPayload , constants:: EVENT_MESSAGE_CREATED , markdown_plain_text} ;
62use cacher:: CacherClient ;
73use localizer:: LanguageLocalizer ;
84use primitives:: {
9- Device , GorushNotification , PushNotification , PushNotificationTypes , StreamEvent , SupportMessage , SupportStreamEvent , device_stream_channel,
10- push_notification:: PushNotificationSupport ,
5+ Device , GorushNotification , PushNotification , PushNotificationTypes , StreamEvent , SupportMessage , device_stream_channel, push_notification:: PushNotificationSupport ,
116} ;
127use std:: error:: Error ;
138use storage:: database:: devices:: DevicesStore ;
@@ -34,14 +29,10 @@ impl SupportClient {
3429 }
3530
3631 pub async fn process_webhook ( & self , device : & Device , payload : & ChatwootWebhookPayload ) -> Result < ( usize , usize ) , Box < dyn Error + Send + Sync > > {
37- match payload. event . as_str ( ) {
38- EVENT_MESSAGE_CREATED => self . process_message_created ( device, payload) . await ,
39- EVENT_CONVERSATION_UPDATED | EVENT_CONVERSATION_STATUS_CHANGED => self . process_conversation_updated ( device, payload) . await ,
40- _ => Ok ( ( 0 , 0 ) ) ,
32+ if payload. event . as_str ( ) != EVENT_MESSAGE_CREATED {
33+ return Ok ( ( 0 , 0 ) ) ;
4134 }
42- }
4335
44- async fn process_message_created ( & self , device : & Device , payload : & ChatwootWebhookPayload ) -> Result < ( usize , usize ) , Box < dyn Error + Send + Sync > > {
4536 let notifications_count = if let Some ( notification) = Self :: build_notification ( device, payload) {
4637 self . stream_producer . publish_notifications_support ( NotificationsPayload :: new ( vec ! [ notification] ) ) . await ?;
4738 1
@@ -54,15 +45,6 @@ impl SupportClient {
5445 Ok ( ( notifications_count, stream_events_count) )
5546 }
5647
57- async fn process_conversation_updated ( & self , device : & Device , payload : & ChatwootWebhookPayload ) -> Result < ( usize , usize ) , Box < dyn Error + Send + Sync > > {
58- if let Some ( conversation) = payload. support_conversation ( ) {
59- self . publish_stream_event ( device, SupportStreamEvent :: Conversation ( conversation) ) . await ?;
60- Ok ( ( 0 , 1 ) )
61- } else {
62- Ok ( ( 0 , 0 ) )
63- }
64- }
65-
6648 fn build_notification ( device : & Device , payload : & ChatwootWebhookPayload ) -> Option < GorushNotification > {
6749 if !payload. is_public_outgoing_message ( ) {
6850 return None ;
@@ -86,18 +68,14 @@ impl SupportClient {
8668 return Ok ( 0 ) ;
8769 }
8870
89- self . publish_stream_event ( device, SupportStreamEvent :: Message ( message) ) . await ?;
71+ let channel = device_stream_channel ( & device. id ) ;
72+ self . cacher . publish ( & channel, & StreamEvent :: Support ( message) ) . await ?;
9073 Ok ( 1 )
9174 }
9275
9376 fn should_publish_stream_message ( message : & SupportMessage ) -> bool {
9477 message. sender . is_agent ( )
9578 }
96-
97- async fn publish_stream_event ( & self , device : & Device , event : SupportStreamEvent ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
98- let channel = device_stream_channel ( & device. id ) ;
99- self . cacher . publish ( & channel, & StreamEvent :: Support ( event) ) . await
100- }
10179}
10280
10381#[ cfg( test) ]
0 commit comments