Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

1.46 #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

1.46 #68

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions it/pingpong.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// @flow

const assert = require('chai').assert;
import {describe, it} from 'mocha';
import Log from 'log';
import Symphony from '../src/symphony';

Expand Down Expand Up @@ -86,7 +85,7 @@ describe('Foundation Open Developer Platform integration tests', () => {
});
})
.then((response) => {
assert.include(response.map((m) => m.message), '<messageML>ping</messageML>');
assert.include(response.map((m) => m.payload.messageSent.message), '<messageML>ping</messageML>');
});
});
});
8 changes: 4 additions & 4 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ class SymphonyAdapter extends Adapter {
if (response) {
self.robot.logger.debug(`Received ${response.length || 0} datafeed messages`);
for (const msg of response) {
if (msg.v2messageType === 'V2Message') {
self._receiveMessage(msg);
if (msg.type === 'MESSAGESENT') {
self._receiveMessage(msg.payload.messageSent);
}
}
}
Expand All @@ -343,8 +343,8 @@ class SymphonyAdapter extends Adapter {
*/
_receiveMessage(message: SymphonyMessageType) {
// ignore anything the bot said
if (message.fromUserId !== this.robot.userId) {
this._userLookup({userId: message.fromUserId}, message.streamId)
if (message.user.userId !== this.robot.userId) {
this._userLookup({userId: message.user.userId}, message.stream.streamId)
.then((response) => {
const v2 = new V2Message(response, message);
this.robot.logger.debug(`Received '${v2.text}' from ${v2.user.name}`);
Expand Down
4 changes: 2 additions & 2 deletions src/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ symphony.createDatafeed()
})
.then((response) => {
for (const msg of response) {
if (msg.v2messageType === 'V2Message') {
logger.info(`Received '${msg.message}'`);
if (msg.type === 'MESSAGESENT') {
logger.info(`Received '${msg.payload.messageSent.message}'`);
}
}
if (argv.runOffline) {
Expand Down
4 changes: 2 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class V2Message extends TextMessage {
* @constructor
*/
constructor(user: Object, symphonyMessage: SymphonyMessageType) {
super(user, V2Message._getMessageText(symphonyMessage), symphonyMessage.id);
super(user, V2Message._getMessageText(symphonyMessage), symphonyMessage.messageId);
this.symphonyMessage = symphonyMessage;
this.room = symphonyMessage.streamId;
this.room = symphonyMessage.stream.streamId;
}

/**
Expand Down
47 changes: 26 additions & 21 deletions src/symphony.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,38 @@ export type SymphonyUserType = {
username: string
};

export type SymphonyDatafeedType = {
id: string,
timestamp: number,
type: string,
initiator: {
user: {
userId: number
}
},
payload: {
messageSent: SymphonyMessageType // this property is actually optional but but as we ignore all message types of than MESSAGESENT this is not currently relevant
}
};

export type SymphonyAttachmentType = {
id: string,
name: string,
size: number
};

export type SymphonyMessageType = {
id: string,
messageId: string,
timestamp: string,
v2messageType: string,
streamId: string,
message: string,
data?: string,
attachments?: Array<SymphonyAttachmentType>,
fromUserId: number
user: {
userId: number
},
stream: {
streamId: string
}
};

export type CreateDatafeedResponseType = {
Expand Down Expand Up @@ -289,20 +307,7 @@ class Symphony {
message: message,
format: format,
};
return this._httpAgentPost(`/agent/v2/stream/${streamId}/message/create`, body);
}

/**
* Get messages from an existing stream (IM, MIM, or chatroom). Additionally returns any attachments associated with
* the message.
*
* See {@link https://rest-api.symphony.com/docs/messages-v2|Messages}
*
* @param {string} streamId
* @return {Promise.<Array.<SymphonyMessageType>>}
*/
getMessages(streamId: string): Promise<Array<SymphonyMessageType>> {
return this._httpAgentGet(`/agent/v2/stream/${streamId}/message`);
return this._httpAgentPost(`/agent/v4/stream/${streamId}/message/create`, body);
}

/**
Expand All @@ -315,7 +320,7 @@ class Symphony {
* @return {Promise.<CreateDatafeedResponseType>}
*/
createDatafeed(): Promise<CreateDatafeedResponseType> {
return this._httpAgentPost('/agent/v1/datafeed/create', undefined);
return this._httpAgentPost('/agent/v4/datafeed/create', undefined);
}

/**
Expand All @@ -327,8 +332,8 @@ class Symphony {
* @param {string} datafeedId
* @return {Promise.<Array.<SymphonyMessageType>>}
*/
readDatafeed(datafeedId: string): Promise<Array<SymphonyMessageType>> {
return this._httpAgentGet(`/agent/v2/datafeed/${datafeedId}/read`);
readDatafeed(datafeedId: string): Promise<Array<SymphonyDatafeedType>> {
return this._httpAgentGet(`/agent/v4/datafeed/${datafeedId}/read`);
}

/**
Expand Down
68 changes: 40 additions & 28 deletions test/nock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class NockServer extends EventEmitter {
this._datafeedCreateHttp400Count = 0;
this._datafeedReadHttp400Count = 0;

let kmHost = args.kmHost || args.host;
let agentHost = args.agentHost || args.host;
let sessionAuthHost = args.sessionAuthHost || agentHost;
const kmHost = args.kmHost || args.host;
const agentHost = args.agentHost || args.host;
const sessionAuthHost = args.sessionAuthHost || agentHost;
logger.info(`Setting up mocks for ${this.host} / ${kmHost} / ${agentHost} / ${sessionAuthHost}`);

let self = this;
const self = this;

this.streamId = 'WLwnGbzxIdU8ZmPUjAs_bn___qulefJUdA';

Expand All @@ -100,7 +100,7 @@ class NockServer extends EventEmitter {
this.realUserName = 'johndoe';
this.realUserEmail = '[email protected]';

let realUserObject = {
const realUserObject = {
id: self.realUserId,
emailAddress: self.realUserEmail,
firstName: 'John',
Expand All @@ -110,10 +110,10 @@ class NockServer extends EventEmitter {
};

this.botUserId = 7696581411197;
let botUserName = 'mozart';
let botUserEmail = '[email protected]';
const botUserName = 'mozart';
const botUserEmail = '[email protected]';

let botUserObject = {
const botUserObject = {
id: self.realUserId,
emailAddress: botUserEmail,
firstName: 'Wolfgang Amadeus',
Expand All @@ -126,18 +126,21 @@ class NockServer extends EventEmitter {

if (args.startWithHelloWorldMessage || args.startWithHelloWorldMessage === undefined) {
this.messages.push({
id: '-sfAvIPTTmyrpORkBuvL_3___qulZoKedA',
messageId: '-sfAvIPTTmyrpORkBuvL_3___qulZoKedA',
timestamp: self.firstMessageTimestamp,
v2messageType: 'V2Message',
streamId: self.streamId,
message: '<messageML>Hello World</messageML>',
fromUserId: self.realUserId,
user: {
userId: self.realUserId,
},
stream: {
streamId: self.streamId,
},
});
}

nock.disableNetConnect();

let checkHeaderMissing = function(val: string): boolean {
const checkHeaderMissing = function(val: string): boolean {
return val === undefined || val === null;
};

Expand Down Expand Up @@ -323,42 +326,51 @@ class NockServer extends EventEmitter {
.reply(200, function(uri: string, requestBody: EchoType): EchoType {
return requestBody;
})
.post(`/agent/v2/stream/${self.streamId}/message/create`)
.post(`/agent/v4/stream/${self.streamId}/message/create`)
.reply(200, function(uri: string, requestBody: SymphonyCreateMessagePayloadType): SymphonyMessageType {
const message = {
id: uuid.v1(),
messageId: uuid.v1(),
timestamp: new Date().valueOf().toString(),
v2messageType: 'V2Message',
streamId: self.streamId,
message: requestBody.message,
attachments: [],
fromUserId: self.botUserId,
user: {
userId: self.botUserId,
},
stream: {
streamId: self.streamId,
},
};
self._receiveMessage(message);
return message;
})
.get(`/agent/v2/stream/${self.streamId}/message`)
.reply(200, function(uri: string, requestBody: mixed) {
return self.messages;
})
.post('/agent/v1/datafeed/create')
.post('/agent/v4/datafeed/create')
.reply(function(uri: string, requestBody: mixed) {
if (self._datafeedCreateHttp400Count-- > 0) {
return [400, null];
}
return [200, {id: self.datafeedId}];
})
.get(`/agent/v2/datafeed/${self.datafeedId}/read`)
.get(`/agent/v4/datafeed/${self.datafeedId}/read`)
.reply(function(uri: string, requestBody: mixed) {
if (self._datafeedReadHttp400Count-- > 0) {
return [400, null];
}
if (self.messages.length == 0) {
if (self.messages.length === 0) {
return [204, null];
}
let copy = self.messages;
const copy = self.messages;
self.messages = [];
return [200, copy];
const wrapped = copy.map(function(msg) {
return {
id: msg.messageId,
timestamp: msg.timestamp,
type: 'MESSAGESENT',
initiator: msg.user,
payload: {
messageSent: msg,
},
}
});
return [200, wrapped];
});
}

Expand Down
60 changes: 24 additions & 36 deletions test/symphony-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,7 @@ describe('REST API test suite', () => {
symphony.sendMessage(nock.streamId, msg, 'MESSAGEML')
.then((response) => {
assert.equal(msg, response.message);
assert.equal(nock.botUserId, response.fromUserId);
done();
})
.catch((error) => {
done(`Failed with error ${error}`);
});
});

it('getMessages should get all messages', (done) => {
let msg = '<messageML>Yo!</messageML>';
symphony.sendMessage(nock.streamId, msg, 'MESSAGEML')
.then((response) => {
assert.equal(msg, response.message);
return symphony.getMessages(nock.streamId);
})
.then((response) => {
assert.isAtLeast(response.length, 2);
assert.include(response.map((m) => m.message), '<messageML>Hello World</messageML>');
assert.include(response.map((m) => m.message), msg);
assert.equal(nock.botUserId, response.user.userId);
done();
})
.catch((error) => {
Expand Down Expand Up @@ -192,7 +174,7 @@ describe('REST API test suite', () => {
})
.then((response) => {
assert.equal(1, response.length);
assert.equal(msg1, response[0].message);
assert.equal(msg1, response[0].payload.messageSent.message);
return symphony.sendMessage(nock.streamId, msg2, 'MESSAGEML');
})
.then((response) => {
Expand All @@ -201,7 +183,7 @@ describe('REST API test suite', () => {
})
.then((response) => {
assert.equal(1, response.length);
assert.equal(msg2, response[0].message);
assert.equal(msg2, response[0].payload.messageSent.message);
done();
});
})
Expand Down Expand Up @@ -395,15 +377,18 @@ describe('REST API test suite', () => {
describe('Object model test suite', () => {
for (const text of ['<messageML>Hello World</messageML>', 'Hello World']) {
it(`parse a V2Message containing '${text}'`, () => {
let msg = {
id: 'foobar',
const msg = {
messageId: 'foobar',
timestamp: '1464629912263',
v2messageType: 'V2Message',
streamId: 'baz',
message: text,
fromUserId: 12345,
user: {
userId: 12345,
},
stream: {
streamId: 'baz',
},
};
let user = {
const user = {
id: 12345,
name: 'johndoe',
displayName: 'John Doe',
Expand All @@ -419,24 +404,27 @@ describe('Object model test suite', () => {
}

it('regex test', (done) => {
let msg = {
id: 'foobar',
const msg = {
messageId: 'foobar',
timestamp: '1464629912263',
v2messageType: 'V2Message',
streamId: 'baz',
message: 'butler ping',
fromUserId: 12345,
user: {
userId: 12345,
},
stream: {
streamId: 'baz',
},
};
let user = {
const user = {
id: 12345,
name: 'johndoe',
displayName: 'John Doe',
};
let robot = new FakeRobot();
let callback = () => {
const robot = new FakeRobot();
const callback = () => {
done();
};
let listener = new TextListener(robot, /^\s*[@]?butler[:,]?\s*(?:PING$)/i, {}, callback);
const listener = new TextListener(robot, /^\s*[@]?butler[:,]?\s*(?:PING$)/i, {}, callback);
listener.call(new V2Message(user, msg));
});
});