diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 91190ec..ae839c3 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -61,6 +61,7 @@ exports.getallPostsforUser = (req, res) => { }; exports.getallPosts = (req, res) => { + var post_query = admin.firestore().collection("posts"); post_query .get() @@ -170,6 +171,7 @@ exports.quoteWithPost = (req, res) => { return res.status(400).json({ error: "Post has already been quoted." }); } }) + .catch(err => { return res.status(500).json({ error: err }); }); @@ -281,6 +283,7 @@ exports.likePost = (req, res) => { return res.status(404).json({ error: "Post not found" }); } }) + .then(data => { if (data.empty) { return admin diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 3430fff..7e63f32 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -1,4 +1,5 @@ -/* eslint-disable promise/catch-or-return */ +/* eslint-disable promise/always-return */ + const { admin, db } = require("../util/admin"); const config = require("../util/config"); const { validateUpdateProfileInfo } = require("../util/validator"); @@ -209,7 +210,7 @@ exports.deleteUser = (req, res) => { let errors = {}; function thenFunction(data) { - console.log(`${data} data for ${req.userData.handle} has been deleted.`); + console.log(`${data} for ${req.userData.handle} has been deleted.`); } function catchFunction(data, err) { @@ -217,14 +218,131 @@ exports.deleteUser = (req, res) => { errors[data] = err; } + function deleteDirectMessages() { + return new Promise((resolve, reject) => { + const deleteUsername = req.userData.handle; + db.doc(`/users/${deleteUsername}`) + .get() + .then((deleteUserDocSnap) => { + const dms = deleteUserDocSnap.data().dms; + const dmRecipients = deleteUserDocSnap.data().dmRecipients; + + if (!dms) { + resolve(); + return; + } + + // Iterate over the list of users who this person has DM'd + let otherUsersPromises = []; + + // Resolve if they don't have a dmRecipients list + if (dmRecipients === undefined || dmRecipients === null || dmRecipients.length === 0) { + resolve(); + return; + } + dmRecipients.forEach((dmRecipient) => { + otherUsersPromises.push( + // Get each users data + db.doc(`/users/${dmRecipient}`).get() + .then((otherUserDocSnap) => { + // Get the index of deleteUsername so that we can remove the dangling + // reference to the DM document + let otherUserDMRecipients = otherUserDocSnap.data().dmRecipients; + let otherUserDMs = otherUserDocSnap.data().dms; + let index = -1; + otherUserDMRecipients.forEach((dmRecip, i) => { + if (dmRecip === deleteUsername) { + index = i; + } + }) + + if (index !== -1) { + // Remove deleteUsername from their dmRecipients list + otherUserDMRecipients.splice(index, 1); + + // Remove the DM channel with deleteUsername + otherUserDMs.splice(index, 1); + + // Update the users data + return otherUserDocSnap.ref.update({ + dmRecipients: otherUserDMRecipients, + dms: otherUserDMs + }); + } + + }) + ) + }) + + // Wait for the removal of DM data stored on other users to be deleted + Promise.all(otherUsersPromises) + .then(() => { + // Iterate through DM references and delete them from the dm collection + let dmRefsPromises = []; + dms.forEach((dmRef) => { + // Create a delete queue + let batch = db.batch(); + dmRefsPromises.push( + // Add the messages to the delete queue + db.collection(`/dm/${dmRef.id}/messages`).listDocuments() + .then((docs) => { + console.log("second") + console.log(docs); + docs.map((doc) => { + batch.delete(doc); + }) + + // Add the doc that the DM is stored in to the delete queue + batch.delete(dmRef); + + // Commit the writes + return batch.commit(); + }) + ) + }) + + return Promise.all(dmRefsPromises); + }) + .then(() => { + resolve(); + return; + }) + .catch((err) => { + console.log("error " + err); + reject(err); + return; + }) + }) + .catch((err) => { + console.log(err); + return res.status(500).json({error: err}); + }) + + }) + } + // Deletes user from authentication let auth = admin.auth().deleteUser(userId); // Deletes database data - let data = db - .collection("users") - .doc(`${req.user.handle}`) - .delete(); + let data = new Promise((resolve, reject) => { + deleteDirectMessages() + .then(() => { + return db + .collection("users") + .doc(`${req.user.handle}`) + .delete() + }) + .then(() => { + resolve(); + return; + }) + .catch((err) => { + console.log(err); + reject(err); + return; + }) + }) // Deletes any custom profile image let image; @@ -507,6 +625,7 @@ exports.removeSub = (req, res) => { .catch(err => { return res.status(500).json({ err }); }); + return res.status(200).json({ message: "ok" }); }); }; diff --git a/twistter-frontend/package.json b/twistter-frontend/package.json index 75afcb0..52e50f3 100644 --- a/twistter-frontend/package.json +++ b/twistter-frontend/package.json @@ -43,5 +43,5 @@ "last 1 safari version" ] }, - "proxy": "http://localhost:5006/twistter-e4649/us-central1/api" + "proxy": "http://localhost:5001/twistter-e4649/us-central1/api" } diff --git a/twistter-frontend/src/Userline.js b/twistter-frontend/src/Userline.js index 8c95620..6c61d6d 100644 --- a/twistter-frontend/src/Userline.js +++ b/twistter-frontend/src/Userline.js @@ -1,10 +1,10 @@ import React, { Component } from "react"; -import { BrowserRouter as Router } from 'react-router-dom'; -import Route from 'react-router-dom/Route'; +// import { BrowserRouter as Router } from 'react-router-dom'; +// import Route from 'react-router-dom/Route'; import axios from 'axios'; import Box from '@material-ui/core/Box' -import {borders} from '@material-ui/system'; -import { sizing } from '@material-ui/system'; +// import {borders} from '@material-ui/system'; +// import { sizing } from '@material-ui/system'; // var moment = require('moment'); @@ -41,7 +41,7 @@ class Userline extends Component {
Userline
{sortedPosts.map((microBlog) =>
Microblog Title: {microBlog.microBlogTitle}
@@ -50,7 +50,7 @@ class Userline extends Component {
Number of comments: {microBlog.commentCount}
Number of likes: {microBlog.likeCount}
Body of post: {microBlog.body}
-
Tagged topics: {microBlog.microBlogTopics.join("," + " ")}
+
Tagged topics: {microBlog.microBlogTopics.join(", ")}
Loading post...
); - return authenticated ? ( -