munar-adapter-slack
Version:
Slack.com adapter for Munar
97 lines (71 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _munarCore = require("munar-core");
var _utils = require("./utils");
var _renameProp = _interopRequireDefault(require("rename-prop"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const defaultMessageOptions = {
as_user: true
};
function normalizeMessageOptions(options) {
if (options.titleLink) {
(0, _renameProp.default)(options, 'titleLink', 'title_link');
}
return options;
}
class SlackChannel {
constructor(slack, channel) {
this.slack = slack;
this.client = slack.client;
this.webClient = slack.webClient;
this.channel = channel;
}
getUsers() {
return this.slack.getUsers();
}
getUser(id) {
return this.slack.getUser(id);
}
getUserByName(name) {
return this.slack.getUserByName(name);
}
getChannels() {
return this.slack.getChannels();
}
getChannel(id) {
return this.slack.getChannel(id);
}
getChannelByName(name) {
return this.slack.getChannelByName(name);
}
reply(message, text, opts = undefined) {
this.send(`@${message.username} ${text}`, opts);
}
send(text, opts = undefined) {
if (typeof opts === 'object' && Object.keys(opts).length > 0) {
const chatClient = this.slack.web.chat;
chatClient.postMessage(this.channel.id, (0, _utils.linkNames)(this.slack, text), normalizeMessageOptions(_objectSpread({}, defaultMessageOptions, opts)));
} else {
this.client.sendMessage((0, _utils.linkNames)(this.slack, text), this.channel.id);
}
}
canExecute(message, command) {
const user = message.user.sourceUser;
if (user.is_admin) {
return true;
}
return command.role <= _munarCore.permissions.NONE;
}
getAdapterName() {
return this.slack.getAdapterName();
}
toString() {
return `slack:${this.channel.name}`;
}
}
exports.default = SlackChannel;