From 94a7508996a62b7ace9842b589a1a6b71bf6e111 Mon Sep 17 00:00:00 2001 From: Vincent de Almeida Date: Fri, 22 Nov 2019 12:05:52 +0000 Subject: [PATCH] Pushing FCM token up to a firestore --- .gitignore | 3 ++ package-lock.json | 81 ++++++++++++++++++++++++++++++++++++---- package.json | 1 + src/Firebase/Accounts.js | 45 ++++++++++++++++++++++ src/Firebase/index.js | 12 ++++++ src/components/Chat.vue | 67 ++++++++++++++++++++------------- 6 files changed, 176 insertions(+), 33 deletions(-) create mode 100644 src/Firebase/Accounts.js create mode 100644 src/Firebase/index.js diff --git a/.gitignore b/.gitignore index c06dcc1..462a216 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# General +src/_config/ + .DS_Store node_modules /dist diff --git a/package-lock.json b/package-lock.json index aac4937..ff7b3c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2013,6 +2013,11 @@ "integrity": "sha1-vxEWycdYxRt6kz0pa3LCIe2UKLY=", "dev": true }, + "aes-js": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/aes-js/-/aes-js-3.0.0.tgz", + "integrity": "sha1-4h3xCtbCBTKVvLuNq0Cwnb6ofk0=" + }, "aggregate-error": { "version": "3.0.1", "resolved": "https://registry.npm.taobao.org/aggregate-error/download/aggregate-error-3.0.1.tgz", @@ -2458,8 +2463,7 @@ "bn.js": { "version": "4.11.8", "resolved": "https://registry.npm.taobao.org/bn.js/download/bn.js-4.11.8.tgz", - "integrity": "sha1-LN4J617jQfSEdGuwMJsyU7GxRC8=", - "dev": true + "integrity": "sha1-LN4J617jQfSEdGuwMJsyU7GxRC8=" }, "body-parser": { "version": "1.19.0", @@ -2572,8 +2576,7 @@ "brorand": { "version": "1.1.0", "resolved": "https://registry.npm.taobao.org/brorand/download/brorand-1.1.0.tgz", - "integrity": "sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8=", - "dev": true + "integrity": "sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8=" }, "browserify-aes": { "version": "1.2.0", @@ -4519,6 +4522,60 @@ "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=", "dev": true }, + "ethers": { + "version": "4.0.39", + "resolved": "https://registry.npmjs.org/ethers/-/ethers-4.0.39.tgz", + "integrity": "sha512-QVtC8TTUgTrnlQjQvdFJ7fkSWKwp8HVTbKRmrdbVryrPzJHMTf3WSeRNvLF2enGyAFtyHJyFNnjN0fSshcEr9w==", + "requires": { + "@types/node": "^10.3.2", + "aes-js": "3.0.0", + "bn.js": "^4.4.0", + "elliptic": "6.3.3", + "hash.js": "1.1.3", + "js-sha3": "0.5.7", + "scrypt-js": "2.0.4", + "setimmediate": "1.0.4", + "uuid": "2.0.1", + "xmlhttprequest": "1.8.0" + }, + "dependencies": { + "@types/node": { + "version": "10.17.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.5.tgz", + "integrity": "sha512-RElZIr/7JreF1eY6oD5RF3kpmdcreuQPjg5ri4oQ5g9sq7YWU8HkfB3eH8GwAwxf5OaCh0VPi7r4N/yoTGelrA==" + }, + "elliptic": { + "version": "6.3.3", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.3.3.tgz", + "integrity": "sha1-VILZZG1UvLif19mU/J4ulWiHbj8=", + "requires": { + "bn.js": "^4.4.0", + "brorand": "^1.0.1", + "hash.js": "^1.0.0", + "inherits": "^2.0.1" + } + }, + "hash.js": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.3.tgz", + "integrity": "sha512-/UETyP0W22QILqS+6HowevwhEFJ3MBJnwTf75Qob9Wz9t0DPuisL8kW8YZMK62dHAKE1c1p+gY1TtOLY+USEHA==", + "requires": { + "inherits": "^2.0.3", + "minimalistic-assert": "^1.0.0" + } + }, + "setimmediate": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.4.tgz", + "integrity": "sha1-IOgd5iLUoCWIzgyNqJc8vPHTE48=" + }, + "uuid": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.1.tgz", + "integrity": "sha1-wqMN7bPlNdcsz4LjQ5QaULqFM6w=" + } + } + }, "event-pubsub": { "version": "4.3.0", "resolved": "https://registry.npm.taobao.org/event-pubsub/download/event-pubsub-4.3.0.tgz", @@ -6746,8 +6803,7 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npm.taobao.org/inherits/download/inherits-2.0.4.tgz", - "integrity": "sha1-D6LGT5MpF8NDOg3tVTY6rjdBa3w=", - "dev": true + "integrity": "sha1-D6LGT5MpF8NDOg3tVTY6rjdBa3w=" }, "internal-ip": { "version": "4.3.0", @@ -7157,6 +7213,11 @@ "easy-stack": "^1.0.0" } }, + "js-sha3": { + "version": "0.5.7", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.5.7.tgz", + "integrity": "sha1-DU/9gALVMzqrr0oj7tL2N0yfKOc=" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npm.taobao.org/js-tokens/download/js-tokens-4.0.0.tgz", @@ -7645,8 +7706,7 @@ "minimalistic-assert": { "version": "1.0.1", "resolved": "https://registry.npm.taobao.org/minimalistic-assert/download/minimalistic-assert-1.0.1.tgz", - "integrity": "sha1-LhlN4ERibUoQ5/f7wAznPoPk1cc=", - "dev": true + "integrity": "sha1-LhlN4ERibUoQ5/f7wAznPoPk1cc=" }, "minimalistic-crypto-utils": { "version": "1.0.1", @@ -9739,6 +9799,11 @@ "ajv-keywords": "^3.1.0" } }, + "scrypt-js": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/scrypt-js/-/scrypt-js-2.0.4.tgz", + "integrity": "sha512-4KsaGcPnuhtCZQCxFxN3GVYIhKFPTdLd8PLC552XwbMndtD0cjRFAhDuuydXQ0h08ZfPgzqe6EKHozpuH74iDw==" + }, "select-hose": { "version": "2.0.0", "resolved": "https://registry.npm.taobao.org/select-hose/download/select-hose-2.0.0.tgz", diff --git a/package.json b/package.json index 1a150b3..6c3300b 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "core-js": "^3.3.2", + "ethers": "^4.0.39", "firebase": "^7.5.0", "vue": "^2.6.10", "vue-router": "^3.1.3", diff --git a/src/Firebase/Accounts.js b/src/Firebase/Accounts.js new file mode 100644 index 0000000..4f6b96e --- /dev/null +++ b/src/Firebase/Accounts.js @@ -0,0 +1,45 @@ +import {db} from './index'; + +// todo: check that address is valid before firing any query +export default new class Accounts { + async upsertAccount(account) { + return db + .collection('accounts') + .doc(account.address) + .set(account, { + merge: true + }); + } + + async getAccount(address) { + return db + .collection('accounts') + .doc(address) + .get() + .then(snapshot => { + if(snapshot.exists) { + return snapshot.data(); + } + return null; + }); + } + + async upsertFirebaseMessagingTokenForAccount(address, firebaseMessagingToken) { + return db + .collection('accounts') + .doc(address) + .set({firebaseMessagingToken}, { + merge: true + }); + } + + async getFirebaseMessagingTokenForAccount(address) { + return this.getAccount(address) + .then(account => { + if(account && account.firebaseMessagingToken) { + return account.firebaseMessagingToken; + } + return null; + }); + } +} diff --git a/src/Firebase/index.js b/src/Firebase/index.js new file mode 100644 index 0000000..1b06205 --- /dev/null +++ b/src/Firebase/index.js @@ -0,0 +1,12 @@ +const firebase = require('firebase'); +require("firebase/firestore"); +require("firebase/messaging"); + +const {firebaseConfig} = require('../_config/env'); + +firebase.initializeApp(firebaseConfig); + +module.exports = { + db: firebase.firestore(), + messaging: firebase.messaging() +}; diff --git a/src/components/Chat.vue b/src/components/Chat.vue index 6b45c87..672dd38 100644 --- a/src/components/Chat.vue +++ b/src/components/Chat.vue @@ -1,35 +1,41 @@