Unverified Commit 11696f5e authored by Mikael Mello's avatar Mikael Mello
Browse files

Multiple changes on ClientCommands

Change how customClientData is reassigned and sent to RC

Fix lint errors

Add tests to verify customClientData
parent 3893f80d
......@@ -115,6 +115,24 @@ describe('driver', () => {
expect(result.user.status).to.equal('online')
})
})
describe('.clientCommands', () => {
it('sets customClientData from the SDK with no customizations', async () => {
await driver.connect()
await driver.login()
await utils
const result = await utils.userInfo(botUser.username)
expect(result.user.customClientData).to.deep.include(driver.customClientData)
})
it('sets customClientData from the SDK with customizations', async () => {
driver.setCustomClientData({ framework: 'Testing' })
await driver.connect()
await driver.login()
await utils
const result = await utils.userInfo(botUser.username)
expect(result.user.customClientData).to.deep.include(driver.customClientData)
expect(result.user.customClientData.framework).to.equal('Testing')
})
})
describe('.subscribeToMessages', () => {
it('resolves with subscription object', async () => {
await driver.connect()
......
......@@ -99,7 +99,11 @@ export let commandHandlers: IClientCommandHandlerMap = {}
/**
* Custom Data set by the client that is using the SDK
*/
export let customClientData: object = {}
export let customClientData: object = {
framework: 'Rocket.Chat JS SDK',
canPauseResumeMsgStream: true,
canListenToHeartbeat: true
}
/**
* Allow override of default logging with adapter's log instance
......@@ -484,24 +488,13 @@ export function respondToMessages (callback: ICallback, options: IRespondOptions
* Begin subscription to clientCommands for user and returns the collection
*/
async function subscribeToCommands (): Promise<ICollection> {
const subscription = await subscribe(_clientCommandsSubscriptionName)
await subscribe(_clientCommandsSubscriptionName)
clientCommands = asteroid.getCollection(_clientCommandsCollectionName)
// v2
// clientCommands = asteroid.collections.get(_clientCommandsCollectionName) || Map()
return clientCommands
}
/**
* Data set by the SDK to indicate which operations the server can execute on the client
*/
function getSDKData(): object {
return {
framework: 'Rocket.Chat JS SDK',
canPauseResumeMsgStream: true,
canListenToHeartbeat: true
}
}
/**
* Once a subscription is created, using `subscribeToCommands` this method
* can be used to attach a callback to changes in the clientCommands stream.
......@@ -513,8 +506,7 @@ function getSDKData(): object {
async function reactToCommands (callback: ICallback): Promise<void> {
const clientCommands = await subscribeToCommands()
const clientData = Object.assign(getSDKData(), customClientData);
await asyncCall('setCustomClientData', clientData);
await asyncCall('setCustomClientData', customClientData)
logger.info(`[reactive] Listening for change events in collection ${clientCommands.name}`)
clientCommands.reactiveQuery({}).on('change', (_id: string) => {
......@@ -531,7 +523,7 @@ async function reactToCommands (callback: ICallback): Promise<void> {
*/
async function respondToCommands (): Promise<void | void[]> {
commandLastReadTime = new Date() // init before any message read
reactToCommands(async (err, command) => {
await reactToCommands(async (err, command) => {
if (err) {
logger.error(`Unable to receive commands ${JSON.stringify(err)}`)
throw err
......@@ -610,7 +602,7 @@ export function registerCommandHandler (key: string, callback: IClientCommandHan
* @param clientData Object containing additional data about the client using the SDK
*/
export function setCustomClientData (clientData: object) {
customClientData = clientData;
Object.assign(customClientData, clientData)
}
/**
......
......@@ -86,18 +86,19 @@ export interface INewUserAPI {
joinDefaultChannels?: boolean // Auto join channels marked as default
requirePasswordChange?: boolean // Direct to password form on next login
sendWelcomeEmail?: boolean // Send new credentials in email
verified?: true // Email address verification status
verified?: true // Email address verification status
}
/** User object structure for queries (not including admin access level) */
export interface IUserAPI {
_id: string // MongoDB user doc ID
type: string // user / bot ?
status: string // online | offline
active: boolean // Subscription is active
name: string // Full name
utcOffset: number // Hours off UTC/GMT
username: string // Username
_id: string // MongoDB user doc ID
type: string // user / bot ?
status: string // online | offline
active: boolean // Subscription is active
name: string // Full name
utcOffset: number // Hours off UTC/GMT
username: string // Username
customClientData: any // Custom Client Data
}
/** Result structure for user data request (by non-admin) */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment