Commit 7403e900 authored by axet's avatar axet 🍄

fix firebase functions

parent b179009d
Pipeline #34147348 passed with stage
in 28 seconds
...@@ -24,30 +24,24 @@ exports.deleteOldUsers = functions.database.ref('users/{userId}').onCreate((snap ...@@ -24,30 +24,24 @@ exports.deleteOldUsers = functions.database.ref('users/{userId}').onCreate((snap
}); });
exports.topics = functions.https.onRequest((req, res) => { exports.topics = functions.https.onRequest((req, res) => {
let message; res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, PUT');
res.set('Access-Control-Allow-Headers', 'Content-Type');
switch (req.get('content-type')) { if (req.method === 'OPTIONS') {
case 'application/json': return res.status(204).send('');
message = req.body;
break;
case 'application/octet-stream':
message = JSON.parse(req.body.toString()) ; // Convert buffer to a string
break;
case 'text/plain':
message = JSON.parse(req.body);
break;
case 'application/x-www-form-urlencoded':
message = JSON.parse(req.body);
break;
} }
let message = req.body;
if (message) { if (!message) {
let to = message.to.replace(/^(\/topics\/)/,''); return res.status(500).send("no message");
res = admin.messaging().sendToTopic(to, message);
} }
res.send(`OK`); return admin.messaging().send(message).then((response) => {
return res.status(200).send(response);
})
.catch((error) => {
return res.status(500).send(error);
});
}); });
...@@ -5,8 +5,5 @@ ...@@ -5,8 +5,5 @@
"firebase-admin": "^6.0.0", "firebase-admin": "^6.0.0",
"firebase-functions": "^2.0.5" "firebase-functions": "^2.0.5"
}, },
"engines": {
"node": "8"
},
"private": true "private": true
} }
...@@ -99,10 +99,9 @@ global.deleteall = function deleteall(thread) { ...@@ -99,10 +99,9 @@ global.deleteall = function deleteall(thread) {
function command(cmd) { function command(cmd) {
var url = "https://us-central1-android-sms-gate.cloudfunctions.net/topics"; var url = "https://us-central1-android-sms-gate.cloudfunctions.net/topics";
var ref = "/topics/" + CRYPTO.address(current.key); var ref = CRYPTO.address(current.key).toString();
var json = { var json = {
"to" : ref, "topic" : ref,
"priority" : "high",
"data": { "data": {
"text": CRYPTO.encrypt(current.key, new Buffer(JSON.stringify(cmd))).toString('base64') "text": CRYPTO.encrypt(current.key, new Buffer(JSON.stringify(cmd))).toString('base64')
} }
......
...@@ -8,7 +8,7 @@ which minify || npm install minifier ...@@ -8,7 +8,7 @@ which minify || npm install minifier
which browserify || npm install browserify which browserify || npm install browserify
npm install [ ! -e node_modules ] && npm install
browserify index.js -o smsgate.js browserify index.js -o smsgate.js
......
...@@ -144,10 +144,9 @@ global.deleteall = function deleteall(thread) { ...@@ -144,10 +144,9 @@ global.deleteall = function deleteall(thread) {
function command(cmd) { function command(cmd) {
var url = "https://us-central1-android-sms-gate.cloudfunctions.net/topics"; var url = "https://us-central1-android-sms-gate.cloudfunctions.net/topics";
var ref = "/topics/" + CRYPTO.address(current.key); var ref = CRYPTO.address(current.key).toString();
var json = { var json = {
"to" : ref, "topic" : ref,
"priority" : "high",
"data": { "data": {
"text": CRYPTO.encrypt(current.key, new Buffer(JSON.stringify(cmd))).toString('base64') "text": CRYPTO.encrypt(current.key, new Buffer(JSON.stringify(cmd))).toString('base64')
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment