From b402c96864c07fdd9d7be0a1c8ca13f8bfa45818 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Thu, 5 Dec 2019 19:13:00 -0500 Subject: [PATCH] Fixing DMs with users who have them disabled --- functions/handlers/users.js | 48 +++++++++++++++++-- twistter-frontend/src/pages/directMessages.js | 13 ++++- .../src/redux/actions/dataActions.js | 1 + 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index f6dc0fc..85f21c5 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -569,6 +569,7 @@ exports.getDirectMessages = (req, res) => { * messageId: str * } * recipient: str + * hasDirectMessagesEnabled: bool * recentMessage: str * recentMessageTimestamp: ISOString * } @@ -579,7 +580,7 @@ exports.getDirectMessages = (req, res) => { let promise = new Promise((resolve, reject) => { let messagesCollection = dm.collection('messages'); - // If the messagesCollection is missing, that mean that there aren't any messages + // If the messagesCollection is missing, that means that there aren't any messages if (messagesCollection === null || messagesCollection === undefined) { return; } @@ -616,6 +617,7 @@ exports.getDirectMessages = (req, res) => { const dms = req.userData.dms; + const dmRecipients = req.userData.dmRecipients; // Return null if this user has no DMs if (dms === undefined || dms === null || dms.length === 0) return res.status(200).json({data: null}); @@ -666,9 +668,22 @@ exports.getDirectMessages = (req, res) => { }) + // Get all the data from the users to get the data on whether they have DMs enabled or not + let userPromises = []; + dmRecipients.forEach((recipient) => { + userPromises.push( + db.doc(`/users/${recipient}`) + .get() + ) + }) + + // Wait for all DM document promises to resolve before returning data - dmWaitPromise = Promise.all(dmPromises) + Promise.all(dmPromises) .then(() => { + return Promise.all(userPromises) + }) + .then((userData) => { // Sort the DMs so that the ones with the newest messages are at the top dmsData.sort((a, b) => { if (a.recentMessageTimestamp === null && b.recentMessageTimestamp === null) { @@ -691,6 +706,15 @@ exports.getDirectMessages = (req, res) => { return 0; } }); + + dmsData.forEach((dm) => { + dm.hasDirectMessagesEnabled = userData.find((user) => { + if (dm.recipient === user.data().handle) { + return true + } else { + return false + }}).data().dmEnabled === false ? false : true + }) return res.status(200).json({data: dmsData}) }) .catch((err) => { @@ -739,7 +763,7 @@ isDirectMessageEnabled = (username) => { // Assume DMs are enabled if they don't have a dmEnabled key resolve(result); } else { - result.code = 200; + result.code = 400; result.message = `${username} has DMs disabled`; reject(result); } @@ -813,7 +837,8 @@ oneWayCheck = (userA, userB) => { dmRecipients.forEach((dmRecipient) => { if (dmRecipient === 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`}); return; } }) @@ -934,13 +959,25 @@ exports.sendDirectMessage = (req, res) => { messageId: null } + db.doc(`/users/${recipient}`) + .get() + .then((recipDoc) => { + // Return if the other user has DM's disabled + if (recipDoc.data().dmEnabled === false && recipDoc.data().dmEnabled !== null && recipDoc.data().dmEnabled !== undefined) { + return res.status(400).json({error: "This user has DMs disabled"}); + } + }) + db.doc(`/users/${creator}`).get() .then((userDoc) => { let dmList = userDoc.data().dms; // Return if the creator doesn't have any DMs. // This means they have not created a DM's channel yet - if (dmList === null || dmList === undefined) return res.status(400).json({error: `There is no DM channel between ${creator} and ${recipient}. Use /api/dms/new.`}) + if (dmList === null || dmList === undefined) { + return res.status(400).json({error: `There is no DM channel between ${creator} and ${recipient}. Use /api/dms/new.`}) + } + let dmRefPromises = []; dmList.forEach((dmRef) => { dmRefPromises.push( @@ -1050,6 +1087,7 @@ exports.createDirectMessage = (req, res) => { return res.status(201).json({message: "Success!"}); }) .catch((err) => { + console.log("yep") console.log(err); if (err.code && err.message && err.code > 0) { diff --git a/twistter-frontend/src/pages/directMessages.js b/twistter-frontend/src/pages/directMessages.js index 2201ac6..68f23b9 100644 --- a/twistter-frontend/src/pages/directMessages.js +++ b/twistter-frontend/src/pages/directMessages.js @@ -432,7 +432,7 @@ export class directMessages extends Component { ) } > - { + {!channel.hasDirectMessagesEnabled ? "This user has DMs disabled" : !channel.recentMessage ? 'No messages' : @@ -597,7 +597,16 @@ export class directMessages extends Component { multiline rows={2} margin="dense" - value={this.state.drafts[this.state.selectedChannel.dmId] ? this.state.drafts[this.state.selectedChannel.dmId] : ""} + disabled={!this.state.selectedChannel.hasDirectMessagesEnabled} + value={ + !this.state.selectedChannel.hasDirectMessagesEnabled ? + "This user has DMs disabled" + : + this.state.drafts[this.state.selectedChannel.dmId] ? + this.state.drafts[this.state.selectedChannel.dmId] + : + "" + } onChange={this.handleChangeMessage} /> (dispatch) => { sendDirectMessage: err.response.data } }) + dispatch({type: SET_NOT_LOADING_UI_4}); }) } \ No newline at end of file