Fixing DMs with users who have them disabled

This commit is contained in:
Clayton Wilson 2019-12-05 19:13:00 -05:00
parent 719294f0ed
commit b402c96864
3 changed files with 55 additions and 7 deletions

View File

@ -569,6 +569,7 @@ exports.getDirectMessages = (req, res) => {
* messageId: str * messageId: str
* } * }
* recipient: str * recipient: str
* hasDirectMessagesEnabled: bool
* recentMessage: str * recentMessage: str
* recentMessageTimestamp: ISOString * recentMessageTimestamp: ISOString
* } * }
@ -579,7 +580,7 @@ exports.getDirectMessages = (req, res) => {
let promise = new Promise((resolve, reject) => { let promise = new Promise((resolve, reject) => {
let messagesCollection = dm.collection('messages'); 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) { if (messagesCollection === null || messagesCollection === undefined) {
return; return;
} }
@ -616,6 +617,7 @@ exports.getDirectMessages = (req, res) => {
const dms = req.userData.dms; const dms = req.userData.dms;
const dmRecipients = req.userData.dmRecipients;
// Return null if this user has no DMs // Return null if this user has no DMs
if (dms === undefined || dms === null || dms.length === 0) return res.status(200).json({data: null}); 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 // Wait for all DM document promises to resolve before returning data
dmWaitPromise = Promise.all(dmPromises) Promise.all(dmPromises)
.then(() => { .then(() => {
return Promise.all(userPromises)
})
.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 (a.recentMessageTimestamp === null && b.recentMessageTimestamp === null) { if (a.recentMessageTimestamp === null && b.recentMessageTimestamp === null) {
@ -691,6 +706,15 @@ exports.getDirectMessages = (req, res) => {
return 0; 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}) return res.status(200).json({data: dmsData})
}) })
.catch((err) => { .catch((err) => {
@ -739,7 +763,7 @@ isDirectMessageEnabled = (username) => {
// Assume DMs are enabled if they don't have a dmEnabled key // Assume DMs are enabled if they don't have a dmEnabled key
resolve(result); resolve(result);
} else { } else {
result.code = 200; result.code = 400;
result.message = `${username} has DMs disabled`; result.message = `${username} has DMs disabled`;
reject(result); reject(result);
} }
@ -813,7 +837,8 @@ oneWayCheck = (userA, userB) => {
dmRecipients.forEach((dmRecipient) => { dmRecipients.forEach((dmRecipient) => {
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`});
return; return;
} }
}) })
@ -934,13 +959,25 @@ exports.sendDirectMessage = (req, res) => {
messageId: null 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() db.doc(`/users/${creator}`).get()
.then((userDoc) => { .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) 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 = []; let dmRefPromises = [];
dmList.forEach((dmRef) => { dmList.forEach((dmRef) => {
dmRefPromises.push( dmRefPromises.push(
@ -1050,6 +1087,7 @@ exports.createDirectMessage = (req, res) => {
return res.status(201).json({message: "Success!"}); return res.status(201).json({message: "Success!"});
}) })
.catch((err) => { .catch((err) => {
console.log("yep")
console.log(err); console.log(err);
if (err.code && err.message && err.code > 0) { if (err.code && err.message && err.code > 0) {

View File

@ -432,7 +432,7 @@ export class directMessages extends Component {
) )
} }
> >
{ {!channel.hasDirectMessagesEnabled ? "This user has DMs disabled" :
!channel.recentMessage ? !channel.recentMessage ?
'No messages' 'No messages'
: :
@ -597,7 +597,16 @@ export class directMessages extends Component {
multiline multiline
rows={2} rows={2}
margin="dense" 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} onChange={this.handleChangeMessage}
/> />
<Fab <Fab

View File

@ -142,5 +142,6 @@ export const sendDirectMessage = (user, message) => (dispatch) => {
sendDirectMessage: err.response.data sendDirectMessage: err.response.data
} }
}) })
dispatch({type: SET_NOT_LOADING_UI_4});
}) })
} }