Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #201 from broidHQ/fix/slack_thread
Browse files Browse the repository at this point in the history
Fix/slack thread
  • Loading branch information
broidy committed Nov 7, 2017
2 parents 4cc91c1 + 275af1a commit bdc026f
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 98 deletions.
4 changes: 1 addition & 3 deletions _tools/tsconfig.json
Expand Up @@ -6,8 +6,6 @@
"target": "es2015",
"module": "commonjs",
"outDir": "lib",
"removeComments": true,
"preserveConstEnums": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
Expand All @@ -23,4 +21,4 @@
"exclude": [
"node_modules"
]
}
}
4 changes: 3 additions & 1 deletion broid-slack/lib/core/Adapter.js
Expand Up @@ -202,9 +202,11 @@ class Adapter {
const opts = {
as_user: this.asUser,
attachments: msg.attachments || [],
thread_ts: msg.messageID,
unfurl_links: true,
};
if (R.path(['object', 'context', 'name'], data) === 'thread') {
opts.thread_ts = R.path(['object', 'context', 'content'], data);
}
const confirm = () => {
if (msg.callbackID) {
return { callbackID: msg.callbackID, serviceID: this.serviceId(), type: 'sent' };
Expand Down
16 changes: 13 additions & 3 deletions broid-slack/lib/core/Parser.js
Expand Up @@ -77,7 +77,7 @@ class Parser {
if (attachment) {
as2.object = {
content: attachment.content,
id: normalized.thread_ts || normalized.ts || this.createIdentifier(),
id: normalized.ts || this.createIdentifier(),
mediaType: attachment.mediaType,
name: attachment.name,
type: attachment.type,
Expand All @@ -94,7 +94,7 @@ class Parser {
if (!as2.object && !R.isEmpty(normalized.content)) {
as2.object = {
content: normalized.text,
id: normalized.thread_ts || normalized.ts || this.createIdentifier(),
id: normalized.ts || this.createIdentifier(),
type: 'Note',
};
}
Expand All @@ -106,13 +106,23 @@ class Parser {
};
}
return as2;
})
.then((as2) => {
if (normalized.thread_ts) {
as2.object.context = {
content: normalized.thread_ts.toString(),
name: 'thread',
type: 'Object',
};
}
return as2;
});
}
createIdentifier() {
return uuid.v4();
}
createActivityStream(normalized) {
const ts = normalized.thread_ts || normalized.ts;
const ts = normalized.ts;
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'generator': {
Expand Down
4 changes: 2 additions & 2 deletions broid-slack/package.json
Expand Up @@ -50,7 +50,7 @@
"ramda": "^0.23.0",
"request": "^2.79.0",
"request-promise": "^4.1.1",
"rxjs": "^5.0.2",
"rxjs": "^5.5.2",
"sinon": "^2.1.0",
"uuid": "^3.1.0"
},
Expand Down Expand Up @@ -90,4 +90,4 @@
"directories": {
"test": "test"
}
}
}
158 changes: 81 additions & 77 deletions broid-slack/src/core/Adapter.ts
Expand Up @@ -18,7 +18,7 @@ import {
IWebHookEvent,
} from './interfaces';
import { Parser } from './Parser';
import { WebHookServer} from './WebHookServer';
import { WebHookServer } from './WebHookServer';

export class Adapter {
private asUser: boolean;
Expand Down Expand Up @@ -145,76 +145,76 @@ export class Adapter {
events.push(webHookEvent);

return Observable.merge(...events)
.switchMap((value) => {
return Observable.of(value)
.mergeMap((event: ISlackMessage) => {
if (!R.contains(event.type, [
'message',
'event_callback',
'slash_command',
'interactive_message',
])) { return Promise.resolve(null); }

if (event.type === 'message' && R.contains(event.subtype, [
'channel_join',
'message_changed',
])) { return Promise.resolve(null); }

return Promise.resolve(event)
.then((evt) => {
// if user id exist, we get information about the user
if (evt.user) {
return this.user(evt.user)
.then((userInfo) => {
if (userInfo) {
evt.user = userInfo;
}
return evt;
});
}
return evt;
})
.then((evt) => {
// if channel id exist, we get information about the channel
if (evt.channel) {
return this.channel(evt.channel)
.then((channelInfo) => {
if (channelInfo) {
evt.channel = channelInfo;
}
return evt;
});
}
})
.then((evt) => {
if (evt.subtype === 'bot_message') {
evt.user = {
id: evt.bot_id,
is_bot: true,
name: evt.username,
};
}
return evt;
});
})
.mergeMap((normalized: IMessage) => this.parser.parse(normalized))
.mergeMap((parsed) => this.parser.validate(parsed))
.mergeMap((validated) => {
if (!validated) { return Observable.empty(); }
return Promise.resolve(validated);
})
.catch((err) => {
this.logger.error('Caught Error, continuing', err);
// Return an empty Observable which gets collapsed in the output
return Observable.of(err);
});
})
.mergeMap((value) => {
if (value instanceof Error) {
return Observable.empty();
}
return Promise.resolve(value);
});
.switchMap((value) => {
return Observable.of(value)
.mergeMap((event: ISlackMessage) => {
if (!R.contains(event.type, [
'message',
'event_callback',
'slash_command',
'interactive_message',
])) { return Promise.resolve(null); }

if (event.type === 'message' && R.contains(event.subtype, [
'channel_join',
'message_changed',
])) { return Promise.resolve(null); }

return Promise.resolve(event)
.then((evt) => {
// if user id exist, we get information about the user
if (evt.user) {
return this.user(evt.user)
.then((userInfo) => {
if (userInfo) {
evt.user = userInfo;
}
return evt;
});
}
return evt;
})
.then((evt) => {
// if channel id exist, we get information about the channel
if (evt.channel) {
return this.channel(evt.channel)
.then((channelInfo) => {
if (channelInfo) {
evt.channel = channelInfo;
}
return evt;
});
}
})
.then((evt) => {
if (evt.subtype === 'bot_message') {
evt.user = {
id: evt.bot_id,
is_bot: true,
name: evt.username,
};
}
return evt;
});
})
.mergeMap((normalized: IMessage) => this.parser.parse(normalized))
.mergeMap((parsed) => this.parser.validate(parsed))
.mergeMap((validated) => {
if (!validated) { return Observable.empty(); }
return Promise.resolve(validated);
})
.catch((err) => {
this.logger.error('Caught Error, continuing', err);
// Return an empty Observable which gets collapsed in the output
return Observable.of(err);
});
})
.mergeMap((value) => {
if (value instanceof Error) {
return Observable.empty();
}
return Promise.resolve(value);
});
}

public send(data: ISendParameters): Promise<object | Error> {
Expand Down Expand Up @@ -261,13 +261,17 @@ export class Adapter {
.spread((message, actions, attachments, responseURL) =>
createSendMessage(data, message, actions, attachments, responseURL))
.then((msg) => {
const opts = {
const opts: any = {
as_user: this.asUser,
attachments: msg.attachments || [],
thread_ts: msg.messageID,
unfurl_links: true,
};

// In case this is a reply
if (R.path(['object', 'context', 'name'], data) === 'thread') {
opts.thread_ts = R.path(['object', 'context', 'content'], data);
}

const confirm = () => {
if (msg.callbackID) {
return { callbackID: msg.callbackID, serviceID: this.serviceId(), type: 'sent' };
Expand Down Expand Up @@ -348,10 +352,10 @@ export class Adapter {

throw chan.error || grp.error;
})
.then((info) => {
this.storeChannels.set(key, info);
return info;
});
.then((info) => {
this.storeChannels.set(key, info);
return info;
});
}

// Return user information
Expand Down
29 changes: 20 additions & 9 deletions broid-slack/src/core/Parser.ts
@@ -1,8 +1,8 @@
import {
default as schemas,
IActivityStream,
IASMedia,
} from '@broid/schemas';
import {
default as schemas,
IActivityStream,
IASMedia,
} from '@broid/schemas';
import { cleanNulls, fileInfo, isUrl, Logger } from '@broid/utils';

import * as Promise from 'bluebird';
Expand Down Expand Up @@ -95,7 +95,7 @@ export class Parser {
if (attachment) {
as2.object = {
content: attachment.content,
id: normalized.thread_ts || normalized.ts || this.createIdentifier(),
id: normalized.ts || this.createIdentifier(),
mediaType: attachment.mediaType,
name: attachment.name,
type: attachment.type,
Expand All @@ -110,11 +110,11 @@ export class Parser {

return as2;
})
.then((as2) => {
.then((as2: IActivityStream) => {
if (!as2.object && !R.isEmpty(normalized.content)) {
as2.object = {
content: normalized.text,
id: normalized.thread_ts || normalized.ts || this.createIdentifier(),
id: normalized.ts || this.createIdentifier(),
type: 'Note',
};
}
Expand All @@ -127,6 +127,17 @@ export class Parser {
};
}

return as2;
})
.then((as2: IActivityStream) => { // Thread
if (normalized.thread_ts) {
as2.object.context = {
content: normalized.thread_ts.toString(),
name: 'thread',
type: 'Object',
};
}

return as2;
});
}
Expand All @@ -136,7 +147,7 @@ export class Parser {
}

private createActivityStream(normalized: any): IActivityStream {
const ts: string = normalized.thread_ts || normalized.ts;
const ts: string = normalized.ts;

return {
'@context': 'https://www.w3.org/ns/activitystreams',
Expand Down
6 changes: 3 additions & 3 deletions broid-slack/yarn.lock
Expand Up @@ -3311,9 +3311,9 @@ rx@2.3.24:
version "2.3.24"
resolved "https://registry.yarnpkg.com/rx/-/rx-2.3.24.tgz#14f950a4217d7e35daa71bbcbe58eff68ea4b2b7"

rxjs@^5.0.2:
version "5.3.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.3.0.tgz#d88ccbdd46af290cbdb97d5d8055e52453fabe2d"
rxjs@^5.5.2:
version "5.5.2"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.2.tgz#28d403f0071121967f18ad665563255d54236ac3"
dependencies:
symbol-observable "^1.0.1"

Expand Down

0 comments on commit bdc026f

Please sign in to comment.