This commit is contained in:
Aditya Sankaran 2019-12-05 19:20:50 -05:00
commit 5e935f3508
3 changed files with 72 additions and 29 deletions

View File

@ -565,29 +565,30 @@ exports.unverifyUser = (req, res) => {
// Returns all the DMs that the user is currently participating in
exports.getDirectMessages = (req, res) => {
/* Return value
* data: [DMs]
* dm : {
* dmId: str
* messages: [msgs]
* msg: {
* author: str
* createdAt: ISOString
* message: str
* messageId: str
* }
* recipient: str
* recentMessage: str
* recentMessageTimestamp: ISOString
* }
*/
/* Return value
* data: [DMs]
* dm : {
* dmId: str
* messages: [msgs]
* msg: {
* author: str
* createdAt: ISOString
* message: str
* messageId: str
* }
* recipient: str
* hasDirectMessagesEnabled: bool
* recentMessage: str
* recentMessageTimestamp: ISOString
* }
*/
// Returns all the messages in a dm documentSnapshot
function getMessages(dm) {
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;
}
@ -625,6 +626,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)
@ -677,9 +679,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 (
@ -705,7 +720,16 @@ exports.getDirectMessages = (req, res) => {
return 0;
}
});
return res.status(200).json({ data: dmsData });
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 => {
return res.status(500).json({
@ -761,7 +785,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);
}
@ -839,7 +863,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;
}
});
@ -957,17 +982,25 @@ exports.sendDirectMessage = (req, res) => {
messageId: null
};
db.doc(`/users/${creator}`)
db.doc(`/users/${recipient}`)
.get()
.then(userDoc => {
.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(

View File

@ -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}
/>
<Fab

View File

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