-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
208 lines (179 loc) · 5 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict'
const cote = require('cote')({statusLogsEnabled:false})
const u = require('@elife/utils')
/* understand/
* This is the main entry point where we start.
*
* outcome/
* Start our microservice and register with the communication manager
* and SSB.
*/
function main() {
startMicroservice()
registerWithCommMgr()
registerWithSSB()
getAvatarID()
}
const commMgrClient = new cote.Requester({
name: 'direct-message -> CommMgr',
key: 'everlife-communication-svc',
})
function sendReply(msg, req) {
req.type = 'reply'
req.msg = msg
commMgrClient.send(req, (err) => {
if(err) u.showErr(err)
})
}
function sendMsgOnLastChannel(req) {
req.type = 'reply-on-last-channel'
commMgrClient.send(req, (err) => {
if(err) u.showErr(err)
})
}
let msKey = 'everlife-dir-msg-svc'
/* outcome/
* Register ourselves as a message handler with the communication
* manager.
*/
function registerWithCommMgr() {
commMgrClient.send({
type: 'register-msg-handler',
mskey: msKey,
mstype: 'msg',
mshelp: [ { cmd: '/send_msg', txt: 'send a direct message to another avatar' } ],
}, (err) => {
if(err) u.showErr(err)
})
}
const ssbClient = new cote.Requester({
name: 'direct-message -> SSB',
key: 'everlife-ssb-svc',
})
/* outcome/
* Register ourselves as a feed consumer with the SSB subsystem
*/
function registerWithSSB() {
ssbClient.send({
type: 'register-feed-handler',
mskey: msKey,
mstype: 'ssb-msg',
}, (err) => {
if(err) u.showErr(err)
})
}
/* outcome/
* Get the avatar id
*/
let avatarid
function getAvatarID() {
ssbClient.send({ type: 'avatar-id' }, (err, id) => {
if(err) u.showErr(err)
else avatarid = id
})
}
let directMsgHandlerRegistry = []
function startMicroservice() {
/* understand/
* The microservice (partitioned by key to prevent
* conflicting with other services.
*/
const svc = new cote.Responder({
name: 'Direct Msg Service',
key: msKey,
})
svc.on('msg', (req, cb) => {
if(!req.msg) return cb()
let msg = req.msg
if(!msg.startsWith('/send_msg ')) return cb()
msg = msg.substr('/send_msg '.length)
msg = msg.trim()
let p = msg.indexOf(" ")
if(p < 1) return cb()
let userID = msg.substr(0, p)
let userMsg = msg.substr(p+1)
if(!(userID.startsWith("@") &&
userID.endsWith(".ed25519") &&
userMsg.length > 0)) return cb()
directMessage(req, userID, userMsg, (err) => {
if(err) cb(err)
else {
cb(null, true)
sendReply(`Message posted for ${userID}`, req)
}
})
})
svc.on('ssb-msg', (req, cb) => {
cb()
processMsg(req.msg)
})
svc.on('register-direct-msg-handler', (req, cb) => {
if(!req.mskey || !req.mstype) cb(`mskey & mstype needed to register feed handler`)
else {
if(isRegistered(req.mskey)) return cb()
let client = new cote.Requester({
name: `Direct Msg -> ${req.mskey}`,
key: req.mskey,
})
directMsgHandlerRegistry.push({client: client, mstype: req.mstype})
cb()
}
})
}
function isHandling(skill, msg, cb) {
skill.client.send({type: 'direct-msg', msg: msg }, cb)
}
function isRegistered(mskey) {
for (let i=0; i<directMsgHandlerRegistry.length; i++) {
if (directMsgHandlerRegistry[i].mskey === mskey) return true
}
return false
}
let CURRENT_HANDLER
function handleDirectMsg(msg, cb) {
check_handler_ndx_1(0)
function check_handler_ndx_1(ndx) {
if(ndx < directMsgHandlerRegistry.length) {
isHandling(directMsgHandlerRegistry[ndx], msg, (err, handling) => {
if(err) u.showErr(err)
else {
if(handling) {
CURRENT_HANDLER = directMsgHandlerRegistry[ndx]
cb(null, true)
} else check_handler_ndx_1(ndx+1)
}
})
} else {
sendMsgOnLastChannel({
msg: msg.value.author + ' says:\n' + msg.value.content.text,
})
cb(null, true)
}
}
}
/* outcome/
* If this is a message directed to me, relay it to my owner over the
* last used channel
*/
function processMsg(msg) {
if(msg.value.content.type == 'direct-msg' && msg.value.content.to == avatarid) {
handleDirectMsg(msg, (err, success) => {
if(err) u.showErr(err)
})
}
}
/* outcome/
* Post a 'direct message' to someone on my feed and let the network
* replicate it to the recipient
*/
function directMessage(req, userID, userMsg, cb) {
ssbClient.send({
type: 'new-msg',
msg: {
type: "direct-msg",
to: userID,
text: userMsg
},
}, cb)
}
main()