added cloud functions, moving to a new db design

This commit is contained in:
Jack 2020-06-05 18:17:59 +01:00
parent 34d081898e
commit 249dae1161
12 changed files with 6706 additions and 31 deletions

View file

@ -3,4 +3,4 @@
"default": "monkey-type-dev-67af4",
"live": "monkey-type"
}
}
}

1
.gitignore vendored
View file

@ -70,3 +70,4 @@ node_modules/
#css
public/css/style.css
public/css/style.css.map
functions/serviceAccountKey.json

View file

@ -7,18 +7,23 @@
"**/node_modules/**"
],
"redirects": [
{
{
"source": "/soon",
"destination": "/"
}
],
"rewrites": [
{
{
"source": "**",
"destination": "/index.html"
}
],
"cleanUrls": true,
"trailingSlash": false
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}

123
functions/.eslintrc.json Normal file
View file

@ -0,0 +1,123 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 2017
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",
// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",
// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",
// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",
// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",
// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",
// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,
// Warn if return statements do not either always or never specify values
"consistent-return": 1,
// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,
// Require the use of === and !==
"eqeqeq": 2,
// Disallow the use of alert, confirm, and prompt
"no-alert": 2,
// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,
// Disallow null comparisons without type-checking operators
"no-eq-null": 2,
// Disallow the use of eval()
"no-eval": 2,
// Warn against extending native types
"no-extend-native": 1,
// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,
// Warn against unnecessary labels
"no-extra-label": 1,
// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,
// Warn against shorthand type conversions
"no-implicit-coercion": 1,
// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,
// Disallow new operators with the Function object
"no-new-func": 2,
// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,
// Disallow throwing literals as exceptions
"no-throw-literal": 2,
// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,
// Enforce for loop update clause moving the counter in the right direction
"for-direction": 2,
// Enforce return statements in getters
"getter-return": 2,
// Disallow await inside of loops
"no-await-in-loop": 2,
// Disallow comparing against -0
"no-compare-neg-zero": 2,
// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,
// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,
// Enforce return statements in callbacks of array methods
"callback-return": 2,
// Require error handling in callbacks
"handle-callback-err": 2,
// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,
// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,
// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,
//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,
// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}

1
functions/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules/

35
functions/index.js Normal file
View file

@ -0,0 +1,35 @@
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://monkey-type-dev-67af4.firebaseio.com"
});
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
exports.moveResults = functions.https.onCall((request,response) => {
admin.firestore().collection('results').get().then(data => {
data.docs.forEach(doc => {
let result = doc.data();
if(result.moved == undefined || result.moved == false){
admin.firestore().collection(`results`).doc(doc.id).update({moved:true});
admin.firestore().collection(`users/${result.uid}/results`).add(result);
console.log(`moving doc ${doc.id}`);
}else{
console.log(`doc already moved`);
}
})
})
})

2963
functions/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

25
functions/package.json Normal file
View file

@ -0,0 +1,25 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}

3523
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Monkey Type</title>
<link rel="stylesheet" href="css/fa.css">
<link rel="stylesheet" href="css/style.css?v=3">
<link rel="stylesheet" href="css/style.css?v=4">
<link rel="stylesheet" href="themes/serika_dark.css" id="currentTheme">
<link id="favicon" rel="shortcut icon" href="favicon.png">
<link rel="shortcut icon" href="favicon.png">
@ -411,6 +411,9 @@
<div class="page pageAccount hidden">
<div class="preloader"><i class="fas fa-fw fa-spin fa-circle-notch"></i></div>
<div class="content hidden">
<div style="margin: 2rem 0">
Hey, im currently moving to a new database design. You might not see your previous data for now, but don't worry - its all safe and will be moved over soon.
</div>
<div class="group filterButtons">
<div class="buttonsAndTitle">
<div class="title">filters</div>
@ -605,6 +608,7 @@
<script src="/__/firebase/7.14.3/firebase-analytics.js"></script>
<script src="/__/firebase/7.14.3/firebase-auth.js"></script>
<script src="/__/firebase/7.14.2/firebase-firestore.js"></script>
<script src="/__/firebase/7.14.2/firebase-functions.js"></script>
<!-- Initialize Firebase -->
@ -618,14 +622,14 @@
<script src="js/chartjs-plugin-trendline.js"></script>
<script src="js/chartjs-plugin-annotation.js"></script>
<script src="js/html2canvas.js"></script>
<script src="js/words.js?v=3"></script>
<script src="js/layouts.js?v=3"></script>
<script src="js/db.js?v=3"></script>
<script src="js/userconfig.js?v=3"></script>
<script src="js/commandline.js?v=3"></script>
<script src="js/settings.js?v=3"></script>
<script src="js/account.js?v=3"></script>
<script src="js/script.js?v=3"></script>
<script src="js/words.js?v=4"></script>
<script src="js/layouts.js?v=4"></script>
<script src="js/db.js?v=4"></script>
<script src="js/userconfig.js?v=4"></script>
<script src="js/commandline.js?v=4"></script>
<script src="js/settings.js?v=4"></script>
<script src="js/account.js?v=4"></script>
<script src="js/script.js?v=4"></script>
</html>

View file

@ -12,7 +12,8 @@ function db_testCompleted(obj) {
uid = user.uid;
}
try{
db.collection('results').add(obj);
db.collection(`users/${uid}/results`).add(obj);
// db.collection(`results`).add(obj);
}catch(e){
showNotification("Error saving result! Please contact Miodec on Discord.",5000);
}
@ -22,9 +23,18 @@ async function db_getUserResults() {
let user = firebase.auth().currentUser;
if (user == null) return false;
let ret = [];
await db.collection('results')
// await db.collection('results')
// .orderBy('timestamp', 'desc')
// .where('uid', '==', user.uid)
// .get()
// .then(data => {
// // console.log('getting data from db!');
// data.docs.forEach(doc => {
// ret.push(doc.data());
// })
// })
await db.collection(`users/${user.uid}/results/`)
.orderBy('timestamp', 'desc')
.where('uid', '==', user.uid)
.get()
.then(data => {
// console.log('getting data from db!');
@ -81,21 +91,6 @@ function db_addEmailToQueue(type, body) {
from = firebase.auth().currentUser.email + ' (' + firebase.auth().currentUser.uid + ')';
}
// $.get("https://us-central1-monkey-type.cloudfunctions.net/sendEmailNotification",
// {
// subject: "New " + subject,
// body: body
// })
// .done(data => {
// if (data == 'Email queued') {
// showNotification("Message sent. Thanks!", 3000);
// } else {
// showNotification("Unknown error", 3000);
// }
// }).fail(error => {
// showNotification("Unexpected error", 3000);
// });
db.collection('mail').add({
to: "bartnikjack@gmail.com",
message: {

View file

@ -1469,7 +1469,7 @@ if (window.location.hostname === "localhost") {
};
$("#top .logo .top").text("localhost");
$("head title").text($("head title").text() + " (localhost)");
firebase.functions().useFunctionsEmulator("http://localhost:5001");
}
$(document).on('mouseenter','#words .word',e =>{