-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmdtoslack.js
125 lines (115 loc) · 3.23 KB
/
mdtoslack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
const config = require('./config.json');
class mdtoslack {
constructor() {
}
async _getUser(u) {
const isMe = u === config.puppet.id;
const userid = isMe ? this.app.client.getSelfUserId() : this.app.getThirdPartyUserIdFromMatrixGhostId(u);
if (!await this.app.client.getUserById(userid)) {
return u;
}
return `<@${userid}>`;
}
async _getChannel(c) {
let chanid;
try {
chanid = this.app.getThirdPartyRoomIdFromMatrixRoomId(c);
} catch (e) {
// fallback copy & modify from getThirdPartyUserIdFromMatrixGhostId of matrix-puppet-bridge
const svcPrefix = this.app.getServicePrefix();
const domain = this.app.domain;
const patt = new RegExp(`^#${svcPrefix}_(.+)$`);
const localpart = c.replace(':'+domain, '');
const matches = localpart.match(patt);
if (!matches || !matches[1]) {
return c;
}
chanid = matches[1];
}
const chan = await this.app.client.getChannelById(chanid);
if (!chan) {
return c;
}
return `<#${chan.id}|${chan.name}>`;
}
async _matchMention(match) {
// const name = match[1];
// const mxid = match[2];
const action = match[2].substr(0,1);
switch(action) {
case "!":
//return this._payloads(match[1]);
return match[1];
case "#":
return await this._getChannel(match[2]);
case "@":
return await this._getUser(match[2]);
default:
return match[1];
}
}
async _publicParse(text) {
if (typeof text !== 'string') {
return text;
}
const patterns = [
{p: /\[([^\]]+)\]\(https:\/\/matrix.to\/\#\/([^)]+)\)/g, cb: 'mention'},
];
for (let p = 0; p < patterns.length; p++) {
let pattern = patterns[p],
original = text,
result, replace;
while ((result = pattern.p.exec(original)) !== null) {
switch(pattern.cb) {
case "mention":
replace = await this._matchMention(result);
break;
default:
return text;
}
if (replace) {
text = text.replace(result[0], replace);
}
}
}
return text;
}
async parse(app, text) {
this.app = app;
return await this._publicParse(text);
}
}
if (!module.parent) {
const MX_TP = {
'@slack_FOO_USLACKBOT:matrix': 'USLACKBOT',
};
const USERS = {
'USLACKBOT': {
id: 'USLACKBOT',
name: 'slackbot',
},
};
const CHANS = {
'test': {
id: 'CTEST0000',
name: 'test',
},
}
const app = {
client: {
getChannelById: async(id) => CHANS[id],
getUserById: async(id) => USERS[id],
getSelfUserId: (id) => {},
},
domain: 'matrix',
getServicePrefix: () => 'slack_FOO',
getThirdPartyUserIdFromMatrixGhostId: (id) => MX_TP[id],
}
const parser = new mdtoslack();
parser.parse(app, '[slackbot](https://matrix.to/#/@slack_FOO_USLACKBOT:matrix)').then(console.log);
parser.parse(app, '[slackbot](https://matrix.to/#/@slack_FOO_USLACKBOT:matrix): [slackbot](https://matrix.to/#/@slack_FOO_USLACKBOT:matrix)').then(console.log);
parser.parse(app, '[#test](https://matrix.to/#/#slack_FOO_test:matrix)').then(console.log);
} else {
module.exports = mdtoslack;
}