Skip to content

Commit 6dd538e

Browse files
committed
Redo adapter hierarchy to minimize code
1 parent cde5c9e commit 6dd538e

File tree

6 files changed

+74
-46
lines changed

6 files changed

+74
-46
lines changed

src/lib/adapters/mattermost.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
var env = process.env;
1818
var util = require('util');
1919
var utils = require('./../utils');
20-
var DefaultAdapter = require('./default');
20+
var SlackLikeAdapter = require('./slack-like');
2121

2222

2323
function MattermostAdapter(robot) {
2424
var self = this;
25-
DefaultAdapter.call(self, robot);
25+
SlackLikeAdapter.call(self, robot);
2626
}
2727

28-
util.inherits(MattermostAdapter, DefaultAdapter);
28+
util.inherits(MattermostAdapter, SlackLikeAdapter);
2929

3030
MattermostAdapter.prototype.postData = function(data) {
3131
var self = this;

src/lib/adapters/msteams.js

-7
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,4 @@ MSTeamsAdapter.prototype.normalizeCommand = function (command) {
8989
return command;
9090
}
9191

92-
MSTeamsAdapter.prototype.normalizeAddressee = function(msg) {
93-
return {
94-
name: msg.message.user.name,
95-
room: msg.message.room
96-
};
97-
};
98-
9992
module.exports = MSTeamsAdapter;

src/lib/adapters/rocketchat.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
var env = process.env;
1818
var util = require('util');
1919
var utils = require('./../utils');
20-
var SlackAdapter = require('./slack');
20+
var SlackLikeAdapter = require('./slack-like');
2121

2222

2323
function RocketChatAdapter(robot) {
2424
var self = this;
25-
SlackAdapter.call(self, robot);
25+
SlackLikeAdapter.call(self, robot);
2626
}
2727

28-
util.inherits(RocketChatAdapter, SlackAdapter);
28+
util.inherits(RocketChatAdapter, SlackLikeAdapter);
2929

3030
RocketChatAdapter.prototype.postData = function(data) {
3131
var self = this;

src/lib/adapters/slack-like.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019 Extreme Networks, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
"use strict";
16+
17+
var util = require('util');
18+
var utils = require('./../utils');
19+
var DefaultAdapter = require('./default');
20+
21+
22+
// NOTE: Be careful about making changes to this adapter, because the adapters
23+
// for Mattermost, Cisco Spark, and Rocketchat all inherit from this one
24+
function SlackLikeAdapter(robot) {
25+
var self = this;
26+
DefaultAdapter.call(self, robot);
27+
}
28+
29+
util.inherits(SlackLikeAdapter, DefaultAdapter);
30+
31+
SlackLikeAdapter.prototype.postData = function(data) {
32+
throw Error("Children of SlackLikeAdapter must override postData");
33+
};
34+
35+
SlackLikeAdapter.prototype.formatData = function(data) {
36+
if (utils.isNull(data)) {
37+
return "";
38+
}
39+
// For slack we do not truncate or format the result. This is because
40+
// data is posted to slack as a message attachment.
41+
return data;
42+
};
43+
44+
SlackLikeAdapter.prototype.formatRecipient = function(recipient) {
45+
return recipient;
46+
};
47+
48+
SlackLikeAdapter.prototype.normalizeCommand = function(command) {
49+
var self = this;
50+
command = DefaultAdapter.prototype.normalizeCommand.call(self, command);
51+
// replace left double quote with regular quote
52+
command = command.replace(/\u201c/g, '\u0022');
53+
// replace right double quote with regular quote
54+
command = command.replace(/\u201d/g, '\u0022');
55+
// replace left single quote with regular apostrophe
56+
command = command.replace(/\u2018/g, '\u0027');
57+
// replace right single quote with regular apostrophe
58+
command = command.replace(/\u2019/g, '\u0027');
59+
return command;
60+
};
61+
62+
module.exports = SlackLikeAdapter;

src/lib/adapters/slack.js

+3-30
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ var env = process.env;
1818
var util = require('util');
1919
var utils = require('./../utils');
2020
var messages = require('./../slack-messages');
21-
var DefaultAdapter = require('./default');
21+
var SlackLikeAdapter = require('./slack-like');
2222

2323

2424
// NOTE: Be careful about making changes to this adapter, because the adapters
2525
// for Mattermost, Cisco Spark, and Rocketchat all inherit from this one
2626
function SlackAdapter(robot) {
2727
var self = this;
28-
DefaultAdapter.call(self, robot);
28+
SlackLikeAdapter.call(self, robot);
2929

3030
// We monkey patch sendMessage function to send "parse" argument with the message so the text is not
3131
// formatted and parsed on the server side.
@@ -57,7 +57,7 @@ function SlackAdapter(robot) {
5757
}
5858
};
5959

60-
util.inherits(SlackAdapter, DefaultAdapter);
60+
util.inherits(SlackAdapter, SlackLikeAdapter);
6161

6262
SlackAdapter.prototype.postData = function(data) {
6363
var self = this;
@@ -224,31 +224,4 @@ SlackAdapter.prototype.postData = function(data) {
224224
}
225225
};
226226

227-
SlackAdapter.prototype.formatData = function(data) {
228-
if (utils.isNull(data)) {
229-
return "";
230-
}
231-
// For slack we do not truncate or format the result. This is because
232-
// data is posted to slack as a message attachment.
233-
return data;
234-
};
235-
236-
SlackAdapter.prototype.formatRecipient = function(recipient) {
237-
return recipient;
238-
};
239-
240-
SlackAdapter.prototype.normalizeCommand = function(command) {
241-
var self = this;
242-
command = SlackAdapter.super_.prototype.normalizeCommand.call(self, command);
243-
// replace left double quote with regular quote
244-
command = command.replace(/\u201c/g, '\u0022');
245-
// replace right double quote with regular quote
246-
command = command.replace(/\u201d/g, '\u0022');
247-
// replace left single quote with regular apostrophe
248-
command = command.replace(/\u2018/g, '\u0027');
249-
// replace right single quote with regular apostrophe
250-
command = command.replace(/\u2019/g, '\u0027');
251-
return command;
252-
};
253-
254227
module.exports = SlackAdapter;

src/lib/adapters/spark.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ var env = process.env;
1818
var util = require('util');
1919
var utils = require('./../utils');
2020
var DefaultAdapter = require('./default');
21-
var SlackAdapter = require('./slack');
21+
var SlackLikeAdapter = require('./slack-like');
2222

2323

2424
function SparkAdapter(robot) {
2525
var self = this;
26-
SlackAdapter.call(self, robot);
26+
SlackLikeAdapter.call(self, robot);
2727
}
2828

29-
util.inherits(SparkAdapter, SlackAdapter);
29+
util.inherits(SparkAdapter, SlackLikeAdapter);
3030

3131
SparkAdapter.prototype.postData = function(data) {
3232
var self = this;

0 commit comments

Comments
 (0)