mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-06-03 14:57:15 +08:00
added cloud functions for checking if name needs to be changed, validating name and changing name
This commit is contained in:
parent
544b6fb6c4
commit
df2653b225
1 changed files with 141 additions and 3 deletions
|
@ -4,8 +4,7 @@ const admin = require('firebase-admin');
|
|||
var serviceAccount = require("./serviceAccountKey_live.json");
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
databaseURL: "https://monkey-type.firebaseio.com"
|
||||
credential: admin.credential.cert(serviceAccount)
|
||||
});
|
||||
|
||||
|
||||
|
@ -19,7 +18,7 @@ admin.initializeApp({
|
|||
|
||||
exports.moveResults = functions.runWith({timeoutSeconds:540,memory: '2GB'}).https.onCall((request,response) => {
|
||||
|
||||
return admin.firestore().collection('results').orderBy('timestamp','desc').limit(1000).get().then(data => {
|
||||
return admin.firestore().collection('results').orderBy('timestamp','desc').limit(2000).get().then(data => {
|
||||
data.docs.forEach(doc => {
|
||||
let result = doc.data();
|
||||
if(result.moved === undefined || result.moved === false){
|
||||
|
@ -31,4 +30,143 @@ exports.moveResults = functions.runWith({timeoutSeconds:540,memory: '2GB'}).http
|
|||
return
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
function getAllNames(){
|
||||
return admin.auth().listUsers().then(data=>{
|
||||
let names = [];
|
||||
data.users.forEach(user =>{
|
||||
names.push(user.displayName);
|
||||
})
|
||||
return names;
|
||||
})
|
||||
}
|
||||
|
||||
function getAllUsers(){
|
||||
return admin.auth().listUsers().then(data=>{
|
||||
return data.users;
|
||||
})
|
||||
}
|
||||
|
||||
function isUsernameValid(name){
|
||||
if(name == null || name == undefined) return false;
|
||||
if(/miodec/.test(name)) return false;
|
||||
if(name.length > 12) return false;
|
||||
return /^[0-9a-zA-Z_.-]+$/.test(name);
|
||||
}
|
||||
|
||||
exports.checkNameAvailability = functions.https.onCall((request,response) => {
|
||||
try{
|
||||
if(!isUsernameValid(request.name)) return 0;
|
||||
return getAllNames().then(data => {
|
||||
let available = 1;
|
||||
data.forEach(name =>{
|
||||
try{
|
||||
if(name.toLowerCase() == request.name.toLowerCase()) available = 0;
|
||||
}catch(e){
|
||||
//
|
||||
}
|
||||
})
|
||||
return available;
|
||||
});
|
||||
}catch(e){
|
||||
return -1;
|
||||
}
|
||||
})
|
||||
|
||||
exports.changeName = functions.https.onCall((request,response) => {
|
||||
try{
|
||||
if(!isUsernameValid(request.name)){
|
||||
console.warn(`${request.uid} tried to change their name to ${request.name} - not valid`);
|
||||
return 0;
|
||||
}
|
||||
return getAllNames().then(data => {
|
||||
let available = 1;
|
||||
data.forEach(name =>{
|
||||
try{
|
||||
if(name.toLowerCase() == request.name.toLowerCase()) available = 0;
|
||||
}catch(e){
|
||||
//
|
||||
}
|
||||
})
|
||||
if(available === 1){
|
||||
return admin.auth().updateUser(request.uid,{
|
||||
displayName: request.name
|
||||
}).then(d => {
|
||||
console.log(`${request.uid} changed their name to ${request.name} - done`);
|
||||
return 1;
|
||||
}).catch(e => {
|
||||
console.error(`${request.uid} tried to change their name to ${request.name} - ${e}`);
|
||||
return -1;
|
||||
})
|
||||
}else{
|
||||
console.warn(`${request.uid} tried to change their name to ${request.name} - already taken`);
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}catch(e){
|
||||
console.error(`${request.uid} tried to change their name to ${request.name} - ${e}`);
|
||||
return -1;
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
exports.checkIfNeedsToChangeName = functions.https.onCall((request,response) => {
|
||||
try{
|
||||
console.log(`checking if user ${request.uid} needs to change name`);
|
||||
return admin.auth().getUser(request.uid).then(requestUser => {
|
||||
|
||||
if(!isUsernameValid(requestUser.displayName)){
|
||||
//invalid name, needs to change
|
||||
return 1;
|
||||
}else{
|
||||
//valid name, but need to change if not duplicate
|
||||
|
||||
return getAllUsers().then(users => {
|
||||
|
||||
let sameName = [];
|
||||
|
||||
//look for name names
|
||||
users.forEach(user => {
|
||||
if (user.uid != requestUser.uid){
|
||||
try{
|
||||
if(user.displayName.toLowerCase() == requestUser.displayName.toLowerCase()){
|
||||
sameName.push(user);
|
||||
}
|
||||
}catch(e){
|
||||
//
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if(sameName.length == 0){
|
||||
return 0
|
||||
}else{
|
||||
//check when the request user made the account compared to others
|
||||
let earliestTimestamp = 999999999999999;
|
||||
sameName.forEach(sn => {
|
||||
let ts = (new Date(sn.metadata.creationTime).getTime() / 1000);
|
||||
if(ts <= earliestTimestamp){
|
||||
earliestTimestamp = ts;
|
||||
}
|
||||
})
|
||||
|
||||
if((new Date(requestUser.metadata.creationTime).getTime() / 1000) > earliestTimestamp){
|
||||
return 2;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
});
|
||||
}catch(e){
|
||||
return -1;
|
||||
}
|
||||
|
||||
})
|
Loading…
Add table
Reference in a new issue