Skip to content

Commit

Permalink
CF SDK: Subscriber for conversation API (#956)
Browse files Browse the repository at this point in the history
* Subscriber for conversation API

* fix git diff

* expect unused var

* setup conversation worker

* refactor get conversations api

* converstation messages fetch and post request

* worker folders merged

* import links fix

* hanlde events via worker

* refactor subsribe callbacks

* include changeset

* unit tests for conversation api

* create generic pagination utility

* fix tests

* funcation renamed

* address api usage fix

* do not expose create message method

* integrate messages and conversation messages api

* rename ts type

* include more test cases

---------

Co-authored-by: Joao Santos <[email protected]>
  • Loading branch information
iAmmar7 and jpsantosbh authored Feb 15, 2024
1 parent 45dbb9d commit e16ec47
Show file tree
Hide file tree
Showing 22 changed files with 699 additions and 108 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-lies-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@signalwire/core': minor
'@signalwire/js': minor
---

Introduce Conversation API with Conversation Subscriber
25 changes: 23 additions & 2 deletions internal/playground-js/src/fabric-http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ window.connect = async () => {
window.__client = client

await fetchAddresses()

await fetchConverstations()
}

window.saveInLocalStorage = (e) => {
Expand Down Expand Up @@ -78,7 +80,7 @@ const escapeHTML = (str) => {
function updateAddressUI() {
const addressesDiv = document.getElementById('addresses')
addressesDiv.innerHTML = ''
const { addresses } = window.__addressData
const { data: addresses } = window.__addressData

const createListItem = (address) => {
const displayName = escapeHTML(address.display_name)
Expand Down Expand Up @@ -117,7 +119,7 @@ async function fetchAddresses() {
const searchText = document.getElementById('searchInput').value
const selectedType = document.getElementById('searchType').value

const addressData = await client.getAddresses({
const addressData = await client.address.getAddresses({
type: selectedType === 'all' ? undefined : selectedType,
displayName: !searchText.length ? undefined : searchText,
})
Expand Down Expand Up @@ -158,3 +160,22 @@ searchInput.addEventListener('input', () => {
})

searchType.addEventListener('change', fetchAddresses)

async function fetchConverstations() {
const conversations = await client.conversation.getConversations()
console.log('>> conversations', conversations)

const conversationMessages = await client.conversation.getMessages()
console.log('>> conversationMessages', conversationMessages)

const conversationMessagesById =
await client.conversation.getConversationMessages({
addressId: conversations.data[0]?.id,
})
console.log('>> conversationMessagesById', conversationMessagesById)

// Subscribe to updates
client.conversation.subscribe((newConversation) => {
console.log('>> newConversation', newConversation)
})
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
LOCAL_EVENT_PREFIX,
stripNamespacePrefix,
isJSONRPCRequest,
isJSONRPCResponse
isJSONRPCResponse,
} from './utils'
import { WEBRTC_EVENT_TYPES, isWebrtcEventType } from './utils/common'
import { BaseSession } from './BaseSession'
Expand Down Expand Up @@ -115,3 +115,4 @@ export const selectors = {
export { ChatMember, ChatMessage } from './chat'
export { PubSubMessage } from './pubSub'
export * as testUtils from './testUtils'
export * from './utils/mapObject'
83 changes: 71 additions & 12 deletions packages/core/src/types/callfabric.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
export interface PaginatedResponse<T> {
data: Array<T> | []
links: {
first: string
self: string
first?: string
self?: string
next?: string
prev?: string
}
}

/**
* Addresses
*/
export interface GetAddressesOptions {
type?: string
displayName?: string
pageSize?: number
}

export interface Address {
display_name: string
name: string
Expand All @@ -21,24 +30,74 @@ export interface Address {
video?: string
}
}

export interface FetchAddressResponse extends PaginatedResponse<Address> {}

export interface GetAddressesOptions {
type?: string
displayName?: string
pageSize?: number
/**
* Conversations
*/
export interface GetConversationsOptions {
limit?: number
since?: number
until?: number
cursor?: string
}

export interface Conversation {
created_at: number
id: string
last_message_at: number
metadata: Record<string, any>
name: string
}

export interface ConversationHistory {
export interface FetchConversationsResponse
extends PaginatedResponse<Conversation> {}

/**
* Messages
*/

export interface GetMessagesOptions {
limit?: number
since?: number
until?: number
cursor?: string
}

export interface ConversationMessage {
id: string
conversation_id: string
user_id: string
ts: number
details: Record<string, any>
type: string
// FIXME needs to be completed
subtype: string
kind: string
}

export interface FetchConversationHistoryResponse
extends PaginatedResponse<ConversationHistory> {}
export interface FetchConversationMessagesResponse
extends PaginatedResponse<ConversationMessage> {}

export interface GetConversationHistoriOption {
subscriberId: string
export interface GetConversationMessagesOptions {
addressId: string
limit?: number
since?: number
until?: number
cursor?: string
}

export interface SubscriberInfoResponse {
app_settings?: string
company_name?: string
country?: string
display_name?: string
email: string
first_name?: string
id: string
job_title?: string
last_name?: string
push_notification_key: string
region?: string
time_zone?: number
}
28 changes: 28 additions & 0 deletions packages/core/src/types/conversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { SwEvent } from '..'

export type ConversationMessageEventName = 'conversation.message'

export interface ConversationMessageEventParams {
conversation_id: string
conversation_name: string
details: Record<string, unknown>
hidden: boolean
id: string
kind: string
metadata: Record<string, unknown>
subtype: string
text?: string
ts: number
type: string
user_id: string
user_name: string
}

export interface ConversationMessageEvent extends SwEvent {
event_type: ConversationMessageEventName
params: ConversationMessageEventParams
}

export type ConversationEvent = ConversationMessageEvent

export type ConversationEventParams = ConversationMessageEventParams
3 changes: 3 additions & 0 deletions packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ChatEvent } from './chat'
import type { TaskEvent } from './task'
import type { MessagingEvent } from './messaging'
import type { VoiceCallEvent } from './voice'
import { ConversationEvent } from '..'

export interface SwEvent {
event_channel: string
Expand Down Expand Up @@ -222,6 +223,7 @@ export type SwEventParams =
| MessagingEvent
| VoiceCallEvent
| SwAuthorizationStateParams
| ConversationEvent

// prettier-ignore
export type PubSubChannelEvents =
Expand All @@ -238,3 +240,4 @@ export * from './task'
export * from './messaging'
export * from './voice'
export * from './callfabric'
export * from './conversation'
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export * from './extendComponent'
export * from './debounce'
export * from './SWCloseEvent'
export * from './eventUtils'
export * from './mapObject'
export { LOCAL_EVENT_PREFIX }

export const mutateStorageKey = (key: string) => `${STORAGE_PREFIX}${key}`
Expand Down
Loading

0 comments on commit e16ec47

Please sign in to comment.