Skip to content

Commit

Permalink
feat(webchat): send conversation.started event (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmasse committed Feb 16, 2022
1 parent ff00118 commit fcf5d8c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/server/src/conversations/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const Api = {
const Socket = {
Create: Joi.object({}).required(),

Start: Joi.object({ id: Joi.string().guid().required() }).required(),

Get: Joi.object({
id: Joi.string().guid().required()
}).required(),
Expand Down
14 changes: 14 additions & 0 deletions packages/server/src/conversations/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class ConversationSocket {

setup() {
this.sockets.handle('conversations.create', Schema.Socket.Create, this.create.bind(this))
this.sockets.handle('conversations.start', Schema.Socket.Start, this.start.bind(this))
this.sockets.handle('conversations.get', Schema.Socket.Get, this.get.bind(this))
this.sockets.handle('conversations.list', Schema.Socket.List, this.list.bind(this))
this.sockets.handle('conversations.delete', Schema.Socket.Delete, this.delete.bind(this))
Expand All @@ -20,6 +21,19 @@ export class ConversationSocket {
socket.reply(conversation)
}

async start(socket: SocketRequest) {
const { id } = socket.data
const conversation = await this.conversations.fetch(id)

if (!conversation || conversation.userId !== socket.userId) {
return socket.notFound('Conversation does not exist')
}

await this.conversations.start(id)

socket.reply(true)
}

async get(socket: SocketRequest) {
const { id } = socket.data
const conversation = await this.conversations.fetch(id)
Expand Down
4 changes: 4 additions & 0 deletions packages/socket/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class MessagingSocket extends SocketEmitter<{
return conversation
}

async startConversation(id?: uuid) {
await this.request<Conversation>('conversations.start', { id: id || this._conversationId })
}

async getConversation(id?: uuid): Promise<Conversation | undefined> {
return this.request('conversations.get', {
id: id || this._conversationId
Expand Down
9 changes: 9 additions & 0 deletions packages/webchat/src/core/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ export default class WebchatApi {
}
}

async startConversation(): Promise<void> {
try {
await this.socket.socket.startConversation()
} catch (err) {
console.error('Error in start conversation', err)
}
}

async downloadConversation(conversationId: uuid): Promise<any> {
try {
const { data } = await this.axios.post(
Expand Down Expand Up @@ -196,6 +204,7 @@ export default class WebchatApi {
if (data && typeof data === 'string' && data.includes('BP_CONV_NOT_FOUND')) {
console.error('Conversation not found, starting a new one...')
await this.createConversation()
await this.startConversation()
}
}
}
1 change: 1 addition & 0 deletions packages/webchat/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class RootStore {
@action.bound
async createConversation(): Promise<uuid> {
const newId = await this.api.createConversation()
await this.api.startConversation()
await this.fetchConversations()
await this.fetchConversation(newId)
return newId!
Expand Down

0 comments on commit fcf5d8c

Please sign in to comment.