-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
396 lines (365 loc) · 18.4 KB
/
server.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// BITS Server-JavaScript Document
try {
bitsVersion = 656;
// importScripts('https://bitsoko.co.ke/bitsAssets/js/lightwallet/lightwallet.min.js');
// importScripts('https://bitsoko.co.ke/bitsAssets/js/web3/web3.js');
// importScripts('https://bitsoko.co.ke/bitsAssets/js/hooked-web3-provider/build/hooked-web3-provider.js');
// importScripts('https://bitsoko.co.ke/bitsAssets/js/async/lib/async.js');//
bitsInstall = function (event) {}
bitsNotificationClick = function (dat) {
// event.notification.close();
// event.waitUntil(
console.log(dat);
clients.matchAll({
includeUncontrolled: true,
type: "all"
}).then(function (clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
console.log(client);
client.postMessage('notification clicked');
var requestURL = new URL(client.url);
if (requestURL.pathname.split("/")[1] == opserv) {
//if (client.url == 'https://bitsoko.co.ke/app/index.html?web=1' && 'focus' in client)
client.focus();
return client.postMessage('notification clicked');
}
}
if (clients.openWindow) {
clients.openWindow(opurl);
return client.postMessage('notification clicked');
}
})
switch (dat.req) {
// This command returns a list of the URLs corresponding to the Request objects
// that serve as keys for the current cache.
// This command adds a new request/response pair to the cache.
case 'createBackup':
//do backup
bitsNotification('Wallet Backed Up', 'Thank you for backing up your wallet you are one step closer to saving the world', '', 'bits/images/no.png', [{
action: '',
title: "Backed up"
}], true, true);
break;
case 'buyoffer':
startPay(actvServ().storeAddress, 'servID-' + getBitsWinOpt('s') + '-' + getBitsWinOpt('a'), currentTransaction.amount, currentTransaction.action, 'qwerty').then(function (result) {
console.log(result);
// "Stuff worked!"
bitsNotification('Sent', result, '', 'bits/images/icon-ok.png', [{
action: '',
title: "Sent"
}], true, true);
}, function (err) {
console.log(err);
// Error: "It broke"
bitsNotification('Not Sent', err, '', 'bits/images/icon-bad.png', [{
action: '',
title: "Not Sent"
}], true, true);
});
break;
}
}
bitsMessage = function (dat) {
id = dat.mid ? dat.mid : randomString(20);
msDat = dat;
console.log(dat);
return new Promise(function (resolve, reject) {
switch (dat.req) {
// This command returns a list of the URLs corresponding to the Request objects
// that serve as keys for the current cache.
// This command adds a new request/response pair to the cache.
case 'appVersion':
data = dat;
console.log(data);
resolve(bitsVersion)
break;
}
});
}
//.............................................................................................
bitsPush = function (dat) {
return new Promise(function (resolve, reject) {
// bitsNotification('Incoming Transaction',dat.name+' has sent'+dat.amt,'tag','icon',[],true,true);
switch (dat.req) {
// This command returns a list of the URLs corresponding to the Request objects
// that serve as keys for the current cache.
// This command adds a new request/response pair to the cache.
case 'gotMoney':
bitsNotification('You have recevied ' + dat.amt, 'sam has sent you ' + dat.amt + ' Click to view your balance', 'AnonMsg', 'bits/images/no.png', [{
action: '',
title: "Backed up"
}], true, true);
break;
case 'deliverOrder':
var tg = "soko-order-" + dat.oid + "-" + dat.store;
bitsNotification('Manage Order', dat.msg, tg, 'bits/images/yes.png', 'bits/images/sokoNewOrder.png', [{
action: 'manage',
title: "Manage"
}], true, true);
// bitsNotification('Deliver Order',+dat.msg,'Delivery','bits/images/yes.png','bits/images/Delivered.png',[{action: '', title: "Backed up"}],true,true);
break;
// get notifications when you request for services eg delivery
case 'marketTrade':
var tg = "tokenMarket-order-" + dat.oid;
bitsNotification('Complete Trade', dat.msg, tg, 'bits/images/yes.png', 'bits/images/sokoNewOrder.png', [{
action: 'tOrder',
title: "tOrder"
}], true, true);
// bitsNotification('Deliver Order',+dat.msg,'Delivery','bits/images/yes.png','bits/images/Delivered.png',[{action: '', title: "Backed up"}],true,true);
break;
case 'userOrder':
var tg = "bits-order-" + dat.oid + "-" + dat.store;
var state = dat.state;
if (state == 'pending') {
bitsNotification('Order ' + dat.oid + ' Pending', 'You have requested for this service ', tg, 'bits/images/pending.png', dat.orderImg, [{
action: 'open',
title: "Open"
}, {
action: 'rem-pend',
title: "Cancel"
}], true, true);
} else if (state == 'delivering') {
bitsNotification('Order ' + dat.oid + ' Delivery', 'Your order is being delivered', tg, 'bits/images/deliveryM.png', dat.orderImg, [{
action: 'cancel',
title: "Cancel"
}, {
action: 'complete',
title: "Complete"
}], true, true);
}
break;
case 'delOrder':
var tg = "bits-order-" + dat.oid + "-(" + dat.toLoc + ")";
var state = dat.state;
bitsNotification('New ' + dat.deliveryPrice + '/= Delivery', 'order number ' + dat.oid, tg, 'bits/images/Delivered.png', dat.orderImg, [{
action: 'locate',
title: "Location"
}, {
action: 'reject',
title: "Reject"
}], true, true);
break;
case 'delCheck':
var tg = "bits-order-checker-" + dat.store;
bitsNotification('Confirm Availability?', dat.msg, tg, 'bits/images/deliveryM.png', dat.storeImg, [{
action: 'yes',
title: "Im Available"
}, {
action: 'no',
title: "Not Available"
}], true, true);
break;
case 'gotPoints':
bitsNotification('You have points ', 'Click to view your balance', 'PointsMsg', 'bits/images/yes.png', 'bits/images/Delivered.png', [{
action: '',
title: "Points"
}], true, true);
break;
//end of get notifications when you request for services eg delivery
case 'merchantMessage':
console.log(dat);
new Promise(function (resolve, reject) {
//reject(event.data.error);
console.log(dat);
getObjectStore('data', 'readwrite').get('bits-mypromos').onsuccess = function (event) {
console.log(dat);
resolve({
myp: JSON.parse(event.target.result),
d: dat
});
}
}).then(function (pd) {
var pdata = pd.myp;
var dat = pd.d;
console.log(dat);
for (var i = 0, dat = dat, data = data; i < pdata.length; i++) {
if (parseInt(dat.pid) == parseInt(pdata[i].id)) {
ddat = pdata[i];
getObjectStore('data', 'readwrite').get('bits-merchant-id-' + pdata[i].promoOwner).onsuccess = function (event) {
var data = event.target.result;
butArr = [];
if (data.payments == "true") {
butArr.push({
action: 'bits-redeem-' + dat.pid,
title: "Buy Offer"
});
}
butArr.push({
action: 'bits-unsubscribe-' + dat.pid,
title: "Unsubscribe"
});
if (ddat.discount == null || ddat.discount == 'null') {
var disc = ''
} else {
var disc = ddat.discount + "% off "
}
//console.log(data.discount+"% off" +data.name,dat.msg,"bits-promo-"+dat.pid,'bits/images/no.png',data.imagePath,[{action: 'createBackup', title: "Back up"}],true,true);
bitsNotification(disc + ddat.promoName + " @ " + dat.sNm, dat.msg, "bits-promo-" + dat.pid + "-" + data.id, dat.sImg, dat.pImg, butArr, true, false);
}
}
}
});
/*
var store = getObjectStore('data', 'readwrite').get("bits-promo-"+dat.pid);
store.onsuccess = function (event) {
var data = event.target.result;
data= JSON.parse(data);
getObjectStore('data', 'readwrite').get('bits-merchant-id-'+e.data.id).onsuccess = function (event) {
var data = event.target.result;
data= JSON.parse(data);
butArr=[];
if(p){
butArr.push({action: 'bits-redeem-'+dat.pid, title: "Buy Offer"});
}
butArr.push({action: 'bits-unsubscribe-'+dat.pid, title: "Unsubscribe"});
//console.log(data.discount+"% off" +data.name,dat.msg,"bits-promo-"+dat.pid,'bits/images/no.png',data.imagePath,[{action: 'createBackup', title: "Back up"}],true,true);
bitsNotification(data.discount+"% off " +data.name+" @ "+dat.sNm,dat.msg,"bits-promo-"+dat.pid,dat.sImg,dat.pImg,butArr,true,false);
}
*/
//console.log(data.discount+"% off" +data.name,dat.msg,"bits-promo-"+dat.pid,'bits/images/no.png',data.imagePath,[{action: 'createBackup', title: "Back up"}],true,true);
//bitsNotification(data.discount+"% off " +data.name+" @ "+dat.sNm,dat.msg,"bits-promo-"+dat.pid,dat.sImg,dat.pImg,[{action: 'bits-redeem-'+dat.pid, title: "Buy Offer"},{action: 'bits-unsubscribe-'+dat.pid, title: "Unsubscribe"}],true,false);
//}
break;
}
});
// var config = {
// apiKey: "AIzaSyAsqSLYO7ZDulCM7QX4-SjYSAWMbySCY8M",
// authDomain: "bitsoko-server.firebaseapp.com",
// databaseURL: "https://bitsoko-server.firebaseio.com",
// storageBucket: "bitsoko-server.appspot.com",
// messagingSenderId: "476194103258"
// };
// firebase.initializeApp(config);
// const messaging = firebase.messaging();
// firebase.messaging().RequestPermission()
// .then (function()
// {
// console.log('we got permission baby!')
// return messaging.getTocken();
// })
// .then (function(token)
// {
// console.log(token);
// })
// .catch(function(err){
// console.log('damn we missed it!');
// })
//.................................................................................................
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
// messaging.onMessage(function(payload) {
// console.log("Message received. ", payload);
// });
// //..................................................................................................
// messaging.setBackgroundMessageHandler(function(payload) {
// console.log('[firebase-messaging-sw.js] Received background message ', payload);
// // Customize notification here dummy data for testng
// const notificationTitle = 'Message Title';
// const notificationOptions = {
// body: ' Message body.',
// icon: '/logo.png'
// };
// return self.registration.showNotification(notificationTitle,
// notificationOptions);
//});
//....................................................................................................
}
bitsNotification = function (title, body, tag, icon, image, actions, sticky, silent) {
var note = self.registration.showNotification(title, {
body: body,
icon: icon,
image: image,
tag: tag,
actions: actions,
sticky: sticky,
silent: silent
}).then(function(notificationEvent){
setTimeout(function(){
notificationEvent.notification.close();
}, 40000);
});
}
bitsFetch = function (event, cache) {
//console.log(resolve);
//var orReq=event.request.clone();
var url = new URL(event.request.clone().url);
/*
if (url.pathname.substring(0, 2) == '/p' && url.hostname == location.hostname) {
console.log('physical web url!!');
var bid=url.pathname.substring(2, 5);
//resolve(Response.redirect('http://bitsoko.co.ke/bits/#m='+bid));
//orReq.url='https://bitsoko.co.ke/p'+bid+'?f=j';
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < 60; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return cache.match(event.request.clone()).then(function (response) {
console.log(response);
var sendUrl='https://bitsoko.co.ke/p'+bid+'?f=j&p='+text;
return response || fetch(sendUrl).then(function(response) {
//response=Response.redirect('https://bitsoko.co.ke/p='+bid+'?format=json');
console.log(response.clone().url);
console.log(event.request.clone().url);
console.log(sendUrl);
if(response.clone().url==sendUrl ){
console.log(response.clone().text());
return response.clone().text().then(function(d){
var respJ = JSON.parse(d);
console.log(respJ);
if(respJ.a=='0'&&respJ.s=='3'){
//This is a sokopos Default url so redirect to homepage
url = location.origin+'/soko/';
var trResp=Response.redirect(url);
return trResp;
}else if(respJ.s=='3'){
url = location.origin+'/bits/?s='+respJ.a+'&p='+respJ.p;
var trResp=Response.redirect(url);
cache.put(event.request.clone(), trResp.clone());
return trResp;
}else{
url = location.origin+'/bits/?s='+respJ.s+'&p='+respJ.p;
var trResp=Response.redirect(url);
cache.put(event.request.clone(), trResp.clone());
return trResp;
}
});
}
}).catch(function(err){
//url = 'https://bitsoko.co.ke/bits/index.html#s=3&a=404';
return Response.redirect('https://bitsoko.co.ke/bits/index.html?s=3&a=404');
});
//cache.put(orReq, response.clone());
// console.log(resp.clone());
});
}
else if (url.pathname.substring(0, 2) == '/w' && url.hostname == location.hostname) {
*/
if (url.pathname.substring(0, 2) == '/w' && url.hostname == location.hostname) {
console.log('wallet web url!!');
var bid = url.pathname.substring(2, 5);
//resolve(Response.redirect('http://bitsoko.co.ke/bits/#m='+bid));
//orReq.url='https://bitsoko.co.ke/p'+bid+'?f=j';
return cache.match(event.request.clone()).then(function (response) {
console.log(response);
return response || fetch('https://bitsoko.co.ke/w' + bid + '?f=j').then(function (response) {
//response=Response.redirect('https://bitsoko.co.ke/p='+bid+'?format=json');
//console.log(response);
if (response.clone().url == 'https://bitsoko.co.ke/w' + bid + '?f=j' && event.request.clone().url == 'https://bitsoko.co.ke/w' + bid) {
return response.clone().text().then(function (d) {
var respJ = JSON.parse(d);
// console.log(respJ);
url = 'http://' + respJ.a;
var trResp = Response.redirect(url);
cache.put(event.request.clone(), trResp.clone());
return trResp;
});
}
});
});
}
}
} catch (err) {
console.log(err)
}