Skip to content

Commit

Permalink
fix: 0411 / firebase (#1316)
Browse files Browse the repository at this point in the history
* v

* Update satolist.js

* firebase multicast

* Update notifications.js

* Update functions.js

* Update functions.js

* Update package.json
  • Loading branch information
maxgithubprofile authored Nov 5, 2024
1 parent 52584a1 commit 1d935dd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 21 deletions.
73 changes: 71 additions & 2 deletions js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6095,12 +6095,81 @@ p_saveAsWithCordova = function (file, name, clbk, todownloads) {

}

//Da_Ki

saveBase64File = function(name, base64, type){

return new Promise((resolve, reject) => {

var format = fkit.extensionBase64(base64)

if (window.cordova) {

var fl = b64toBlob(base64.split(',')[1], type);

p_saveAsWithCordova(fl, name + '.' + format, function (d, e) {

if(e) return reject(e)

return resolve(d)

}, true)

}

else {
p_saveAs({
file: base64,
format: format,
name: name
})

return resolve({name})
}
})

}

downloadFileByUrl = function(url){

return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();

xhr.onload = function () {

var type = xhr.getResponseHeader("content-type");
var blob = new Blob([xhr.response], { type: type, name: "file" })

getBase64(blob).then((base64) => {

resolve({
base64,
type
})

})

};

xhr.onerror = function (e) {
console.error(e, url);
reject(new TypeError("Request failed"));
};

xhr.open("GET", url);
xhr.responseType = "arraybuffer";
xhr.send(null);
});

}

/* ______________________________ */

/* NAVIGATION */




_scrollTop = function (scrollTop, el, time, direction) {

if (!direction) direction = 'Top'
Expand Down Expand Up @@ -7036,8 +7105,6 @@ AJAX = function(p) {

if(_Node) {

//data.node = "NODE";

var _d = {
method: type,
uri: url,
Expand Down Expand Up @@ -9548,6 +9615,8 @@ var fkit = {
'image/gif': 'gif',
'image/webp': 'webp',
'image/jfif': 'jfif'

//Da_Ki
},
extensionBase64: function (base64) {
if (!base64) return ''
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

"version": "0.9.102",

"versionsuffix": "3",
"versionsuffix": "4",
"cordovaversion": "1.8.102",
"cordovaversioncode": "1801023",
"cordovaversioncode": "1801024",

"description": "Bastyon desktop application",
"author": "Pocketnet Community <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion proxy16/node/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Notifications{
const ts = Date.now();
try {


this.logger.w('system', 'info', `Notification: Generate Events: ${item.height}`)

const {events, block} = await this.generateEvents(item)
Expand Down Expand Up @@ -333,7 +334,6 @@ class Notifications{
try{
this.run()
}catch(e){
console.log('run2', e)
}


Expand Down
52 changes: 36 additions & 16 deletions proxy16/server/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,29 +405,49 @@ var Firebase = function(p){
const sendPush = async (message, tokens, users)=>{
const resendTokens = [];
for(let i = 0; i < tokens.length; i += 499) {
message.tokens = tokens.slice(i, 499);

var messages = []
var tokensForEach = tokens.slice(i, 499)

_.each(tokensForEach, (token) => {
messages.push({
...message,
token
})
})

//message.tokens = tokens.slice(i, 499);
try {
const response = await admin.messaging().sendMulticast(message)
const response = await admin.messaging().sendEach(messages)

for (const responseIndex in response.responses) {
const addresses = users.filter(el=>el.token === message?.tokens[responseIndex]).map(el=>el.address)
for(const address of addresses) {
if (!response.responses[responseIndex]?.success) {
block.pushStatus.unshift(addStatus(message?.tokens[responseIndex], address, true, response.responses[responseIndex]?.error?.errorInfo?.message, response.responses[responseIndex]?.error?.errorInfo?.code))
if (message?.tokens[responseIndex] && errorCodeList.includes(response.responses[responseIndex]?.error?.errorInfo?.code)) {
this.logger.w('system', 'error', `Firebase: Token is inactive, delete token - Message:${response.responses[responseIndex]?.error?.errorInfo?.message} Token: ${message?.tokens[responseIndex]}`)
self.kit.revokeToken({token: message?.tokens[responseIndex]})
} else if (message?.tokens[responseIndex]) {

self.logger.w('system', 'error', `Firebase: Send push (resend after 35s): Message:${response.responses[responseIndex]?.error?.errorInfo?.message} Token: ${message?.tokens[responseIndex]}`)
resendTokens.push(message?.tokens[responseIndex])

var msg = messages[responseIndex]

if (msg){
const addresses = users.filter(el=>el.token === msg.token).map(el=>el.address)

for(const address of addresses) {
if (!response.responses[responseIndex]?.success) {
block.pushStatus.unshift(addStatus(msg.token, address, true, response.responses[responseIndex]?.error?.errorInfo?.message, response.responses[responseIndex]?.error?.errorInfo?.code))
if (msg.token && errorCodeList.includes(response.responses[responseIndex]?.error?.errorInfo?.code)) {
this.logger.w('system', 'error', `Firebase: Token is inactive, delete token - Message:${response.responses[responseIndex]?.error?.errorInfo?.message} Token: ${msg.token}`)
self.kit.revokeToken({token: msg.token})
} else if (msg.token) {

self.logger.w('system', 'error', `Firebase: Send push (resend after 35s): Message:${response.responses[responseIndex]?.error?.errorInfo?.message} Token: ${msg.token}`)
resendTokens.push(msg.token)
}
}else {
block.pushStatus.unshift(addStatus(msg.token, address, false, "Success", 0))
}
}else {
block.pushStatus.unshift(addStatus(message?.tokens[responseIndex], address, false, "Success", 0))
}
}


}
}catch (e) {
self.logger.w('system', 'error', `Firebase: Multicast response error (resend after 35s): ${e?.message || e}`)
self.logger.w('system', 'error', `Firebase: SendEach response error (resend after 35s): ${e?.message || e}`)
await new Promise(resolve => setTimeout(resolve, 35000))
resendTokens.push(tokens.slice(i, 499))
}
Expand Down

0 comments on commit 1d935dd

Please sign in to comment.