-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (147 loc) · 4.94 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
'use strict';
const RTM = require('satori-sdk-js');
class Channel {
constructor(cfg) {
this.queue = [];
this.ready = false;
Object.assign(this, cfg);
const {endpoint, appkey, role, roleSecretKey, channel} = cfg;
this.listeners = [];
this.rtm = new RTM(endpoint, appkey, {
authProvider: RTM.roleSecretAuthProvider(role, roleSecretKey)
});
this.rtm.on('enter-connected', () => {
console.log('Connected to RTM. Powered by Satori SDK...');
this.ready = true;
this.check();
});
this.rtm.on("leave-connected", function() {
console.log("Disconnected from RTM.");
});
/* set callback for PDU with specific action */
this.subscription = this.rtm.subscribe(channel, RTM.SubscriptionMode.SIMPLE);
this.subscription.on('rtm/subscription/data', (pdu) => {
pdu.body.messages.forEach((msg) => {
console.log('MSG', msg);
this.listeners.forEach(l => l(msg));
});
});
/* set callback for all subscription PDUs */
// this.rtm.on('data', function (pdu) {
// console.log('rtm data', pdu);
// if (pdu.action.endsWith('/error')) {
// console.log('Subscription is failed: ', pdu.body);
// }
// });
this.rtm.start();
// todo: onerror this.ready=false
this.publish = this.publish.bind(this);
this.subscribe = this.subscribe.bind(this);
this.unsubscribe = this.unsubscribe.bind(this);
this.check = this.check.bind(this);
}
check() {
if (this.queue.length && this.ready) {
this.rtm.publish(this.channel, this.queue.shift(), this.check);
}
}
publish(data) {
this.queue.push(data);
this.check();
}
subscribe(callback) {
const index = this.listeners.indexOf(callback);
if (!~index) {
this.listeners.push(callback);
}
}
unsubscribe(callback) {
const index = this.listeners.indexOf(callback);
if (~index) {
this.listeners.splice(index, 1);
}
return ~index;
}
}
class FeedbackLoop {
constructor(cfg) {
if (cfg.listen !== undefined && parseInt(cfg.listen) === Math.abs(cfg.listen) ) {
server(cfg.listen);
}
const { endpoint, appkey, role, roleSecretKey } = cfg;
this.output = new Channel({ endpoint, appkey, role, roleSecretKey, channel: cfg.output });
this.input = new Channel({ endpoint, appkey, role, roleSecretKey, channel: cfg.input });
this.ask = this.ask.bind(this);
this.listen = this.listen.bind(this);
this.unlisten = this.unlisten.bind(this);
this.publish = this.publish.bind(this);
this.subscribe = this.subscribe.bind(this);
this.unsubscribe = this.unsubscribe.bind(this);
}
// users: ask, listen and unlisten
ask(data) {
this.input.publish(data);
}
listen(callback) {
this.output.subscribe(callback);
}
unlisten(callback) {
this.output.unsubscribe(callback);
}
// providers: publish, subscribe and unsubscribe,
// just remember, providers can be users too!
publish(data) {
this.output.publish(data);
}
subscribe(callback) {
this.input.subscribe(callback);
}
unsubscribe(callback) {
this.input.unsubscribe(callback);
}
}
function server(port = 0, handler=defaultHandler) {
const http = require('http');
http.createServer(handler)
.listen(port, (err) => {
return err ? console.log('something bad happened', err) : console.log(`server is listening on ${port}`);
});
}
function defaultHandler(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
}
/**
* Exports:
* 1. Provider class so you can make more providers if you need them (Satori SDK doesn't seem to handle more than a handful)
* 2. An instance of provider with its publish, subscribe and unsubscribe functions
* @type {{Provider: Provider, publish: Provider.publish, subscribe: Provider.subscribe, unsubscribe: Provider.unsubscribe}}
*/
/*
* override search channel defaults (change the out channel for instance to
* offer monthly subscription to premium results and push that channel id each
* month to paid users or access a private channel)
*/
// todo: think through the publish model. Can we use BOTS on an open-data channel?
const DEFAULT_CONFIG = {
input:'search-i',
output:'search-o',
// todo: should this be an open-data channel?
appkey: 'ecff36cdd85309C0bCc7da30ebcB9226',
endpoint: 'wss://s55smyuu.api.satori.com',
// todo: do we need roles? should be public w/o credentials
roleSecretKey: '69aB3Ff3cdaC0c5ED8e4Bd72Ed88e88E',
role: 'search-all',
// todo: filter satori channels so users and providers can just subscribe to what they want
filter: '',
// todo: turn listen into a configurable object to pass to http
listen: 0
};
const provider = new FeedbackLoop(DEFAULT_CONFIG);
const publish = provider.publish;
const subscribe = provider.subscribe;
const unsubscribe = provider.unsubscribe;
const ask = provider.ask;
const listen = provider.listen;
const unlisten = provider.unlisten;
module.exports = { ask, listen, unlisten, publish, subscribe, unsubscribe };