From 1d935dddd4bae3fe7342b6e43f11d5b4861e72e4 Mon Sep 17 00:00:00 2001
From: maxgithubprofile <21055122+maxgithubprofile@users.noreply.github.com>
Date: Tue, 5 Nov 2024 09:31:53 +0300
Subject: [PATCH] fix: 0411 / firebase (#1316)

* v

* Update satolist.js

* firebase multicast

* Update notifications.js

* Update functions.js

* Update functions.js

* Update package.json
---
 js/functions.js               | 73 ++++++++++++++++++++++++++++++++++-
 package.json                  |  4 +-
 proxy16/node/notifications.js |  2 +-
 proxy16/server/firebase.js    | 52 +++++++++++++++++--------
 4 files changed, 110 insertions(+), 21 deletions(-)

diff --git a/js/functions.js b/js/functions.js
index 4d6c3b51c1..1aa3e14fc8 100644
--- a/js/functions.js
+++ b/js/functions.js
@@ -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'
@@ -7036,8 +7105,6 @@ AJAX = function(p) {
 
 		if(_Node) {
 
-			//data.node = "NODE";
-
 			var _d = {
 				method: type,
 				uri: url,
@@ -9548,6 +9615,8 @@ var fkit = {
 		'image/gif': 'gif',
 		'image/webp': 'webp',
 		'image/jfif': 'jfif'
+
+		//Da_Ki
 	},
 	extensionBase64: function (base64) {
 		if (!base64) return ''
diff --git a/package.json b/package.json
index 99d892c56a..a37bda7b7c 100644
--- a/package.json
+++ b/package.json
@@ -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 <support@pocketnet.app>",
diff --git a/proxy16/node/notifications.js b/proxy16/node/notifications.js
index 25a6a9a885..deb3996685 100755
--- a/proxy16/node/notifications.js
+++ b/proxy16/node/notifications.js
@@ -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)
@@ -333,7 +334,6 @@ class Notifications{
             try{
                 this.run()
             }catch(e){
-                console.log('run2', e)
             }
 
             
diff --git a/proxy16/server/firebase.js b/proxy16/server/firebase.js
index e3d7801969..d6c0a1fb77 100644
--- a/proxy16/server/firebase.js
+++ b/proxy16/server/firebase.js
@@ -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))
                     }