-
Notifications
You must be signed in to change notification settings - Fork 0
/
dolphin.js
247 lines (208 loc) · 6.65 KB
/
dolphin.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const { v4: uuidv4 } = require('uuid');
import { Subject } from "rxjs";
import { GlobalPubSub as QuestPubSub } from '@questnetwork/quest-pubsub-js';
export class Dolphin {
constructor(ipfsNode) {
this.ipfsNode = ipfsNode;
this.commitNowSub = new Subject();
this.commitSub = new Subject();
this.channelConfig = {};
let uVar;
this.channelNameListSub = new Subject();
QuestPubSub.commitNowSub.subscribe( (value) => {
this.commitNowSub.next(value);
});
QuestPubSub.commitSub.subscribe( (value) => {
this.commitSub.next(value);
});
}
async sayHi(channel){
return QuestPubSub.sayHi(this.ipfsNode.pubsub, channel)
}
getChallengeFlag(ch){
return QuestPubSub.getChallengeFlag(ch);
}
setChallengeFlag(ch, value){
QuestPubSub.setChallengeFlag(ch, value);
}
getChannelConfig(ch = 'all'){
return QuestPubSub.getChannelConfig(ch);
}
setChannelConfig(config, ch = 'all'){
QuestPubSub.setChannelConfig(config, ch);
}
listen(channel){
return QuestPubSub.subs[channel];
}
isOnline(channelPubKey){
return QuestPubSub.isAlive(channelPubKey);
}
getIncomingFavoriteRequests(){
return QuestPubSub.getIncomingFavoriteRequests();
}
setIncomingFavoriteRequests(v){
QuestPubSub.setIncomingFavoriteRequests(v);
}
async createChannel(channelInput, isClean = false){
//generate keypair and register channel
//clean the Input
channelInput = channelInput.toLowerCase().replace(/[^A-Z0-9]+/ig, "-");
let channelName = await QuestPubSub.createChannel(channelInput);
this.getChannelKeyChain(channelName);
this.getChannelParticipantList(channelName);
return channelName;
}
async addChannel(channelName){
//clean the Input
await QuestPubSub.addChannel(channelName);
this.getChannelKeyChain(channelName);
this.getChannelParticipantList(channelName);
return channelName;
}
// async addChannelFromInvite(channelName){
// //clean the Input
// await QuestPubSub.addChannel(channelName);
// let kc = QuestPubSub.getChannelKeyChain(channel);
// this.setChannelKeyChain(kc, channel);
// let plist = QuestPubSub.generateChannelParticipantListFromChannelName();
// this.setChannelParticipantList(plist,channelName);
// return channelName;
// }
getChannelParticipantList(channel = "all"){
let pl = QuestPubSub.getChannelParticipantList(channel);
this.setChannelParticipantList(pl, channel);
return pl;
}
setChannelParticipantList(partList, channel = "all"){
return QuestPubSub.setChannelParticipantList(partList, channel);
}
getChannelNameList(){
return QuestPubSub.getChannelNameList();
}
setChannelNameList(list){
this.channelNameListSub.next(list);
QuestPubSub.setChannelNameList(list);
}
setChannelKeyChain(channelKeyChain, channel = "all"){
return QuestPubSub.setChannelKeyChain(channelKeyChain, channel);
}
getChannelKeyChain(channel = 'all'){
let kc = QuestPubSub.getChannelKeyChain(channel);
this.setChannelKeyChain(kc, channel);
return kc;
}
getIpfsId(){
return QuestPubSub.getIpfsId();
}
setIpfsId(id){
return QuestPubSub.setIpfsId(id);
}
getPubSubPeersSub(){
return QuestPubSub.pubSubPeersSub;
}
async joinChannelProcess(channel){
//TODO: we can retry and all that
await QuestPubSub.joinChannel(this.ipfsNode.pubsub,channel);
}
async completedChallenge(channel, code){
let ownerChannelPubKey = QuestPubSub.getOwnerChannelPubKey(channel);
code = Buffer.from(code,'hex').toString('utf8').split(':')[1];
console.log(code);
let pubObj = {
channel: channel,
type: 'CHALLENGE_RESPONSE',
toChannelPubKey: ownerChannelPubKey,
response: { code: code }
}
QuestPubSub.publish(this.ipfsNode.pubsub,pubObj);
}
async joinChannel(channel){
// try {
// if(this.ipfs.isReady()){
return await this.joinChannelProcess(channel);
// }
// else{
// console.log('Waiting for ipfsNodeReadySub...');
// this.ipfsNodeReadySub.subscribe(async () => {
// return await this.joinChannelProcess(channel);
// });
// }
// }
// catch(error){
// console.log(error);
// }
}
async publishChannelMessage(channel, message, type = 'CHANNEL_MESSAGE'){
let pubObj = { channel: channel, type: type,message }
QuestPubSub.publish(this.ipfsNode.pubsub,pubObj);
}
async publish(channel, pubObj, type = 'CHANNEL_MESSAGE'){
if(typeof pubObj != 'object'){
pubObj = { message: pubObj };
}
pubObj['type'] = type;
pubObj['channel'] = channel;
QuestPubSub.publish(this.ipfsNode.pubsub,pubObj);
}
getChannelHistory(channel){
return QuestPubSub.getChannelHistory(channel);
}
isSubscribed(channel){
return QuestPubSub.isSubscribed(channel);
}
isOwner(channel, pubkey = "none"){
return QuestPubSub.isOwner(channel,pubkey);
}
setInviteCodes(inviteObject, channel = 'all'){
QuestPubSub.setInviteCodes(inviteObject, channel);
return true;
}
getInviteCodes(channel = 'all'){
return QuestPubSub.getInviteCodes(channel);
}
addInviteCode(channel,link,code,newInviteCodeMax){
return QuestPubSub.addInviteCode(channel,link,code,newInviteCodeMax);
}
addInviteToken(channel,token){
return QuestPubSub.addInviteToken(channel,token);
}
removeInviteCode(channel,link){
return QuestPubSub.removeInviteCode(channel, link)
}
setSocialProfiles(v){
QuestPubSub.setSocialProfiles(v);
}
setSocialProfile(profileId, v){
QuestPubSub.setSocialProfile(profileId,v);
}
getSocialProfiles(){
return QuestPubSub.getSocialProfiles();
}
setSocialSharedWith(array){
QuestPubSub.setSocialSharedWith(array);
}
clearSharedWith(){
QuestPubSub.setSocialSharedWith([]);
}
getSocialSharedWith(){
return QuestPubSub.getSocialSharedWith();
}
clearSocialSharedWith(){
QuestPubSub.clearSocialSharedWith();
}
setSocialLinks(v){
QuestPubSub.setSocialLinks(v);
}
getSocialLinks(){
return QuestPubSub.getSocialLinks();
}
commitNow(){
this.commitNowSub.next(true);
}
commit(){
this.commitSub.next(true);
}
removeIncomingFavoriteRequest(pubKey){
QuestPubSub.removeIncomingFavoriteRequest(pubKey);
}
}