@@ -31,7 +31,7 @@ import { store } from 'shared/store/isolatedStore'
3131import { waitForRendererInstance } from 'shared/renderer/sagas-helper'
3232import { getUsedComponentVersions } from 'shared/rolloutVersions'
3333import { SocialAPI } from 'dcl-social-client'
34- import { joinOrCreateChannel , leaveChannel } from 'shared/friends/actions'
34+ import { joinOrCreateChannel , leaveChannel , sendChannelMessage } from 'shared/friends/actions'
3535
3636interface IChatCommand {
3737 name : string
@@ -92,10 +92,12 @@ function* handleReceivedMessage(action: MessageReceived) {
9292}
9393
9494function * handleSendMessage ( action : SendMessage ) {
95- const { body : message } = action . payload
95+ const { body : message , messageType , recipient } = action . payload
9696
9797 let entry : ChatMessage | null = null
9898
99+ // When there is a recipient, it means is a message sent to a channel
100+ const isChannel = messageType === ChatMessageType . PUBLIC && recipient
99101 // Check if message is a command
100102 if ( message [ 0 ] === '/' ) {
101103 entry = handleChatCommand ( message )
@@ -118,17 +120,35 @@ function* handleSendMessage(action: SendMessage) {
118120 } else {
119121 // If the message was not a command ("/cmdname"), then send message through wire
120122 const currentUserId = yield select ( getCurrentUserId )
121- if ( ! currentUserId ) throw new Error ( 'cannotGetCurrentUser' )
122-
123- entry = {
124- messageType : ChatMessageType . PUBLIC ,
125- messageId : uuid ( ) ,
126- timestamp : Date . now ( ) ,
127- sender : currentUserId ,
128- body : message
123+ if ( ! currentUserId ) {
124+ defaultLogger . error ( 'Could not get the current user id.' )
125+ trackEvent ( 'error' , {
126+ message : 'error trying to get the current user id.' ,
127+ context : 'kernel#chatSaga' ,
128+ stack : 'handleSendMessage'
129+ } )
130+ return
131+ }
132+ if ( isChannel ) {
133+ entry = {
134+ messageType : ChatMessageType . PUBLIC ,
135+ messageId : uuid ( ) ,
136+ sender : currentUserId ,
137+ recipient,
138+ body : message ,
139+ timestamp : Date . now ( )
140+ }
141+ yield put ( sendChannelMessage ( recipient , message ) )
142+ } else {
143+ entry = {
144+ messageType : ChatMessageType . PUBLIC ,
145+ messageId : uuid ( ) ,
146+ timestamp : Date . now ( ) ,
147+ sender : currentUserId ,
148+ body : message
149+ }
150+ sendPublicChatMessage ( entry . messageId , entry . body )
129151 }
130-
131- sendPublicChatMessage ( entry . messageId , entry . body )
132152 }
133153
134154 yield call ( waitForRendererInstance )
@@ -473,10 +493,8 @@ function initChatCommands() {
473493 }
474494 }
475495
476- const ownId = client . getUserId ( )
477-
478496 // Join or create channel
479- store . dispatch ( joinOrCreateChannel ( channelId , [ ownId ] ) )
497+ store . dispatch ( joinOrCreateChannel ( channelId , [ ] ) )
480498
481499 return {
482500 messageId : uuid ( ) ,
0 commit comments