fixed unfollow user and topics

This commit is contained in:
Leon Liang 2019-12-06 02:17:52 -05:00
parent a1f9a4bef3
commit 39613584e7
2 changed files with 64 additions and 50 deletions

View File

@ -681,20 +681,16 @@ exports.getDirectMessages = (req, res) => {
// Get all the data from the users to get the data on whether they have DMs enabled or not // Get all the data from the users to get the data on whether they have DMs enabled or not
let userPromises = []; let userPromises = [];
dmRecipients.forEach((recipient) => { dmRecipients.forEach(recipient => {
userPromises.push( userPromises.push(db.doc(`/users/${recipient}`).get());
db.doc(`/users/${recipient}`) });
.get()
)
})
// Wait for all DM document promises to resolve before returning data // Wait for all DM document promises to resolve before returning data
Promise.all(dmPromises) Promise.all(dmPromises)
.then(() => { .then(() => {
return Promise.all(userPromises) return Promise.all(userPromises);
}) })
.then((userData) => { .then(userData => {
// Sort the DMs so that the ones with the newest messages are at the top // Sort the DMs so that the ones with the newest messages are at the top
dmsData.sort((a, b) => { dmsData.sort((a, b) => {
if ( if (
@ -721,15 +717,21 @@ exports.getDirectMessages = (req, res) => {
} }
}); });
dmsData.forEach((dm) => { dmsData.forEach(dm => {
dm.hasDirectMessagesEnabled = userData.find((user) => { dm.hasDirectMessagesEnabled =
userData
.find(user => {
if (dm.recipient === user.data().handle) { if (dm.recipient === user.data().handle) {
return true return true;
} else { } else {
return false return false;
}}).data().dmEnabled === false ? false : true }
}) })
return res.status(200).json({data: dmsData}) .data().dmEnabled === false
? false
: true;
});
return res.status(200).json({ data: dmsData });
}) })
.catch(err => { .catch(err => {
return res.status(500).json({ return res.status(500).json({
@ -864,7 +866,10 @@ oneWayCheck = (userA, userB) => {
if (dmRecipient === userB) { if (dmRecipient === userB) {
console.log(`You already have a DM with ${userB}`); console.log(`You already have a DM with ${userB}`);
// reject(new Error(`You already have a DM with ${userB}`)); // reject(new Error(`You already have a DM with ${userB}`));
reject({code: 400, message: `You already have a DM with that user`}); reject({
code: 400,
message: `You already have a DM with that user`
});
return; return;
} }
}); });
@ -984,21 +989,30 @@ exports.sendDirectMessage = (req, res) => {
db.doc(`/users/${recipient}`) db.doc(`/users/${recipient}`)
.get() .get()
.then((recipDoc) => { .then(recipDoc => {
// Return if the other user has DM's disabled // Return if the other user has DM's disabled
if (recipDoc.data().dmEnabled === false && recipDoc.data().dmEnabled !== null && recipDoc.data().dmEnabled !== undefined) { if (
recipDoc.data().dmEnabled === false &&
recipDoc.data().dmEnabled !== null &&
recipDoc.data().dmEnabled !== undefined
) {
return res.status(400).json({ error: "This user has DMs disabled" }); return res.status(400).json({ error: "This user has DMs disabled" });
} }
}) });
db.doc(`/users/${creator}`).get() db.doc(`/users/${creator}`)
.then((userDoc) => { .get()
.then(userDoc => {
let dmList = userDoc.data().dms; let dmList = userDoc.data().dms;
// Return if the creator doesn't have any DMs. // Return if the creator doesn't have any DMs.
// This means they have not created a DM's channel yet // This means they have not created a DM's channel yet
if (dmList === null || dmList === undefined) { if (dmList === null || dmList === undefined) {
return res.status(400).json({error: `There is no DM channel between ${creator} and ${recipient}. Use /api/dms/new.`}) return res
.status(400)
.json({
error: `There is no DM channel between ${creator} and ${recipient}. Use /api/dms/new.`
});
} }
let dmRefPromises = []; let dmRefPromises = [];
@ -1181,7 +1195,7 @@ exports.addSubscription = (req, res) => {
const struct = { const struct = {
handle: req.body.following, handle: req.body.following,
topics: ["Admin"] topics: ["Admin"]
} };
new_following new_following
? new_following.push(struct) ? new_following.push(struct)
: (new_following = req.body.following); : (new_following = req.body.following);
@ -1357,7 +1371,7 @@ exports.removeSub = (req, res) => {
new_following = doc.data().following; new_following = doc.data().following;
// remove username from array // remove username from array
new_following.forEach(function(follower, index) { new_following.forEach(function(follower, index) {
if (follower === `${req.body.unfollow}`) { if (follower.handle === `${req.body.unfollow}`) {
new_following.splice(index, 1); new_following.splice(index, 1);
} }
}); });

View File

@ -93,7 +93,8 @@ class user extends Component {
.then(res => { .then(res => {
console.log("removed sub"); console.log("removed sub");
this.setState({ this.setState({
following: false following: false,
myTopics: []
}); });
}) })
.catch(function(err) { .catch(function(err) {
@ -201,10 +202,9 @@ class user extends Component {
} else { } else {
alertPromise = new Promise((resolve, reject) => { alertPromise = new Promise((resolve, reject) => {
resolve(); resolve();
}) });
} }
Promise.all([otherUserPromise, userPromise, posts, alertPromise]) Promise.all([otherUserPromise, userPromise, posts, alertPromise])
.then(() => { .then(() => {
this.setState({ loading: false }); this.setState({ loading: false });