Skip to content

Commit

Permalink
fix(RoomsAdapter): fix return of getActivitiesInRealTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Mar 16, 2022
1 parent f17ca37 commit fb7ba4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/ActivitiesSDKAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function parseSDKAttachments(sdkActivity) {
* @param {object} sdkActivity SDK activity object
* @returns {Activity} Adapter activity object
*/
function fromSDKActivity(sdkActivity) {
export function fromSDKActivity(sdkActivity) {
return {
ID: sdkActivity.id ? constructHydraId('message', sdkActivity.id) : sdkActivity.ID,
roomID: sdkActivity.target ? constructHydraId('room', sdkActivity.target.id) : sdkActivity.roomID,
Expand Down
31 changes: 15 additions & 16 deletions src/RoomsSDKAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
concat,
from,
fromEvent,
BehaviorSubject,
Observable,
throwError,
ReplaySubject,
} from 'rxjs';
import {
filter,
Expand All @@ -17,6 +17,7 @@ import {
} from 'rxjs/operators';
import {RoomsAdapter} from '@webex/component-adapter-interfaces';
import {deconstructHydraId} from '@webex/common';
import {fromSDKActivity} from './ActivitiesSDKAdapter';
import logger from './logger';

// TODO: Figure out how to import JS Doc definitions and remove duplication.
Expand Down Expand Up @@ -294,26 +295,24 @@ export default class RoomsSDKAdapter extends RoomsAdapter {
*/
getActivitiesInRealTime(ID) {
logger.debug('ROOM', ID, 'getActivitiesInRealTime()', ['called with', {ID}]);

if (!ID) {
logger.error('ROOM', ID, 'getPastActivities()', ['Must provide room ID']);

return throwError(new Error('getPastActivities - Must provide room ID'));
}

if (!(ID in this.getActivitiesInRealTimeCache)) {
const getActivitiesInRealTime$ = new BehaviorSubject({});
const getActivitiesInRealTime$ = new ReplaySubject();

this.datasource.internal.mercury.on('event:conversation.activity', (sdkActivity) => {
this.datasource.internal.mercury.on('event:conversation.activity', ({data}) => {
const {activity} = data;
const {id: UUID} = deconstructHydraId(ID);

if (sdkActivity.target && sdkActivity.target.id === UUID) {
logger.debug('ROOM', ID, 'getActivitiesInRealTime()', ['received "event:conversation.activity" event', {sdkActivity}]);

const activity = {
ID: sdkActivity.id,
roomID: sdkActivity.target.id,
content: sdkActivity.object,
contentType: sdkActivity.object.objectType,
personID: sdkActivity.actor.id,
displayAuthor: false,
created: sdkActivity.published,
};
if (activity.target && activity.target.id === UUID) {
logger.debug('ROOM', ID, 'getActivitiesInRealTime()', ['received "event:conversation.activity" event', {activity}]);

getActivitiesInRealTime$.next(activity);
getActivitiesInRealTime$.next(fromSDKActivity(activity).ID);

logger.info('ROOM', ID, 'getActivitiesInRealTime()', ['emitting activity object', {activity}]);
}
Expand Down
16 changes: 5 additions & 11 deletions src/RoomsSDKAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {isObservable} from 'rxjs';
import RoomsSDKAdapter from './RoomsSDKAdapter';
import mockActivities from './mockActivities';
import createMockSDK, {mockSDKActivity, mockSDKRoom} from './mockSdk';
import {fromSDKActivity} from './ActivitiesSDKAdapter';

describe('Rooms SDK Adapter', () => {
let mockSDK;
Expand All @@ -12,6 +13,9 @@ describe('Rooms SDK Adapter', () => {
beforeEach(() => {
mockSDK = createMockSDK();
mockSDK.internal.conversation.list = jest.fn(() => Promise.resolve([]));
mockSDK.internal.mercury.on = jest.fn((event, callback) => callback({
data: {activity: mockSDKActivity},
}));
roomsSDKAdapter = new RoomsSDKAdapter(mockSDK);
});

Expand Down Expand Up @@ -68,18 +72,8 @@ describe('Rooms SDK Adapter', () => {
});

test('returns a activity in a proper shape', (done) => {
mockSDK.internal.mercury.on = jest.fn((event, callback) => callback(mockSDKActivity));

roomsSDKAdapter.getActivitiesInRealTime(mockSDKActivity.target.id).subscribe((activity) => {
expect(activity).toEqual({
ID: mockSDKActivity.id,
roomID: mockSDKActivity.target.id,
content: mockSDKActivity.object,
contentType: mockSDKActivity.object.objectType,
personID: mockSDKActivity.actor.id,
displayAuthor: false,
created: mockSDKActivity.published,
});
expect(activity).toEqual(fromSDKActivity(mockSDKActivity).ID);
done();
});
});
Expand Down

0 comments on commit fb7ba4a

Please sign in to comment.