Merge branch 'master' into engage_microblog

This commit is contained in:
asankaran35
2019-11-27 21:08:55 -05:00
committed by GitHub
8 changed files with 661 additions and 284 deletions

View File

@@ -1,20 +1,28 @@
const { admin, db } = require("../util/admin");
exports.putTopic = (req, res) => {
const newTopic = {
topic: req.body.topic
};
admin
.firestore()
.collection("topics")
.add(newTopic)
let new_following = [];
let userRef = db.doc(`/users/${req.userData.handle}`);
userRef
.get()
.then(doc => {
const resTopic = newTopic;
return res.status(200).json(resTopic);
new_following = doc.data().followedTopics;
new_following.push(req.body.following);
// add stuff
userRef
.set({ followedTopics: new_following }, { merge: true })
.then(doc => {
return res
.status(201)
.json({ message: `Following ${req.body.following}` });
})
.catch(err => {
return res.status(500).json({ err });
});
return res.status(200).json({ message: "OK" });
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "something is wrong" });
return res.status(500).json({ err });
});
};
@@ -40,21 +48,46 @@ exports.getAllTopics = (req, res) => {
};
exports.deleteTopic = (req, res) => {
const topic = db.doc(`/topics/${req.params.topicId}`);
topic
let new_following = [];
let userRef = db.doc(`/users/${req.userData.handle}`);
userRef
.get()
.then(doc => {
if (!doc.exists) {
return res.status(404).json({ error: "Topic not found" });
} else {
return topic.delete();
}
})
.then(() => {
return res.json({ message: "Topic successfully deleted!" });
new_following = doc.data().followedTopics;
// remove username from array
new_following.forEach(function(follower, index) {
if (follower === `${req.body.unfollow}`) {
new_following.splice(index, 1);
}
});
// update database
userRef
.set({ followedTopics: new_following }, { merge: true })
.then(doc => {
return res
.status(202)
.json({ message: `Successfully unfollow ${req.body.unfollow}` });
})
.catch(err => {
return res.status(500).json({ err });
});
return res.status(200).json({ message: "ok" });
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "Failed to delete topic." });
return res.status(500).json({ err });
});
};
exports.getUserTopics = (req, res) => {
let data = [];
db.doc(`/users/${req.body.handle}`)
.get()
.then(doc => {
data = doc.data().followedTopics;
return res.status(200).json({ data });
})
.catch(err => {
return res.status(500).json({ err });
});
};

View File

@@ -201,10 +201,10 @@ exports.deleteUser = (req, res) => {
// Get the profile image filename
// `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`
let imageFileName;
req.userData.imageUrl ?
imageFileName = req.userData.imageUrl.split('/o/')[1].split('?alt=')[0] :
imageFileName = 'no-img.png'
req.userData.imageUrl
? (imageFileName = req.userData.imageUrl.split("/o/")[1].split("?alt=")[0])
: (imageFileName = "no-img.png");
const userId = req.userData.userId;
let errors = {};
@@ -221,56 +221,58 @@ exports.deleteUser = (req, res) => {
let auth = admin.auth().deleteUser(userId);
// Deletes database data
let data = db.collection("users").doc(`${req.user.handle}`).delete();
let data = db
.collection("users")
.doc(`${req.user.handle}`)
.delete();
// Deletes any custom profile image
let image;
if (imageFileName !== 'no-img.png') {
image = admin.storage().bucket().file(imageFileName).delete()
if (imageFileName !== "no-img.png") {
image = admin
.storage()
.bucket()
.file(imageFileName)
.delete();
} else {
image = Promise.resolve();
}
// Deletes all users posts
let posts = db.collection("posts")
let posts = db
.collection("posts")
.where("userHandle", "==", req.user.handle)
.get()
.then((query) => {
query.forEach((snap) => {
.then(query => {
query.forEach(snap => {
snap.ref.delete();
});
return;
})
});
let promises = [
auth
.then(thenFunction('auth'))
.catch((err) => catchFunction('auth', err)),
data
.then(thenFunction('data'))
.catch((err) => catchFunction('data', err)),
image
.then(thenFunction('image'))
.catch((err) => catchFunction('image', err)),
posts
.then(thenFunction('posts'))
.catch((err) => catchFunction('image', err))
auth.then(thenFunction("auth")).catch(err => catchFunction("auth", err)),
data.then(thenFunction("data")).catch(err => catchFunction("data", err)),
image.then(thenFunction("image")).catch(err => catchFunction("image", err)),
posts.then(thenFunction("posts")).catch(err => catchFunction("image", err))
];
// Wait for all promises to resolve
let waitPromise = Promise.all(promises);
waitPromise.then(() => {
if (Object.keys(errors) > 0) {
return res.status(500).json(errors);
} else {
return res.status(200).json({message: `All data for ${req.userData.handle} has been deleted.`});
}
})
.catch((err) => {
return res.status(500).json({error: err});
})
waitPromise
.then(() => {
if (Object.keys(errors) > 0) {
return res.status(500).json(errors);
} else {
return res.status(200).json({
message: `All data for ${req.userData.handle} has been deleted.`
});
}
})
.catch(err => {
return res.status(500).json({ error: err });
});
};
// Returns all data in the database for the user who is currently signed in
@@ -351,69 +353,142 @@ exports.getAuthenticatedUser = (req, res) => {
// Must be run by the Admin user
exports.verifyUser = (req, res) => {
if (req.userData.handle !== "Admin") {
return res.status(403).json({error: "This must be done as Admin"});
return res.status(403).json({ error: "This must be done as Admin" });
}
db.doc(`/users/${req.body.user}`)
.get()
.then((doc) => {
.then(doc => {
if (doc.exists) {
let verifiedUser = doc.data();
verifiedUser.verified = true;
return db.doc(`/users/${req.body.user}`).set(verifiedUser, {merge: true});
return db
.doc(`/users/${req.body.user}`)
.set(verifiedUser, { merge: true });
} else {
return res.status(400).json({error: `User ${req.body.user} was not found`});
return res
.status(400)
.json({ error: `User ${req.body.user} was not found` });
}
})
.then(() => {
return res.status(201).json({message: `${req.body.user} is now verified`});
return res
.status(201)
.json({ message: `${req.body.user} is now verified` });
})
.catch((err) => {
.catch(err => {
console.error(err);
return res.status(500).json({error: err.code});
return res.status(500).json({ error: err.code });
});
}
};
// Unverifies the user sent to the request
// Must be run by admin
exports.unverifyUser = (req, res) => {
if (req.userData.handle !== "Admin") {
return res.status(403).json({error: "This must be done as Admin"});
return res.status(403).json({ error: "This must be done as Admin" });
}
db.doc(`/users/${req.body.user}`)
.get()
.then((doc) => {
.then(doc => {
if (doc.exists) {
let unverifiedUser = doc.data();
unverifiedUser.verified = false;
return db.doc(`/users/${req.body.user}`).set(unverifiedUser, {merge: true});
return db
.doc(`/users/${req.body.user}`)
.set(unverifiedUser, { merge: true });
} else {
return res.status(400).json({error: `User ${req.body.user} was not found`});
return res
.status(400)
.json({ error: `User ${req.body.user} was not found` });
}
})
.then(() => {
return res.status(201).json({message: `${req.body.user} is no longer verified`});
return res
.status(201)
.json({ message: `${req.body.user} is no longer verified` });
})
.catch((err) => {
.catch(err => {
console.error(err);
return res.status(500).json({error: err.code});
return res.status(500).json({ error: err.code });
});
}
};
exports.getUserHandles = (req, res) => {
admin
.firestore()
.collection("users")
db.doc(`/users/${req.body.userHandle}`)
.get()
.then(data => {
let users = [];
data.forEach(function(doc) {
users.push(doc.data().handle);
});
return res.status(200).json(users);
.then(doc => {
if (doc.exists) {
let userHandle = doc.data().handle;
return res.status(200).json(userHandle);
} else {
return res.status(404).json({ error: "user not found" });
}
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "Failed to get all user handles." });
});
};
exports.addSubscription = (req, res) => {
let new_following = [];
let userRef = db.doc(`/users/${req.userData.handle}`);
userRef.get().then(doc => {
new_following = doc.data().following;
new_following.push(req.body.following);
// add stuff
userRef
.set({ following: new_following }, { merge: true })
.then(doc => {
return res
.status(201)
.json({ message: `Following ${req.body.following}` });
})
.catch(err => {
return res.status(500).json({ err });
});
return res.status(500).json({ error: "shouldn't execute" });
});
};
exports.getSubs = (req, res) => {
let data = [];
db.doc(`/users/${req.userData.handle}`)
.get()
.then(doc => {
data = doc.data().following;
return res.status(200).json({ data });
})
.catch(err => {
return res.status(500).json({ err });
});
};
exports.removeSub = (req, res) => {
let new_following = [];
let userRef = db.doc(`/users/${req.userData.handle}`);
userRef.get().then(doc => {
new_following = doc.data().following;
// remove username from array
new_following.forEach(function(follower, index) {
if (follower === `${req.body.unfollow}`) {
new_following.splice(index, 1);
}
});
// update database
userRef
.set({ following: new_following }, { merge: true })
.then(doc => {
return res
.status(202)
.json({ message: `Successfully unfollow ${req.body.unfollow}` });
})
.catch(err => {
return res.status(500).json({ err });
});
return res.status(500).json({ error: "shouldn't execute" });
});
};