Merge pull request #112 from ClaytonWWilson/fix_unfollow

Fix unfollow
This commit is contained in:
Leon Liang 2019-12-06 02:28:42 -05:00 committed by GitHub
commit b769ab930a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 53 deletions

View File

@ -565,23 +565,23 @@ exports.unverifyUser = (req, res) => {
// Returns all the DMs that the user is currently participating in // Returns all the DMs that the user is currently participating in
exports.getDirectMessages = (req, res) => { exports.getDirectMessages = (req, res) => {
/* Return value /* Return value
* data: [DMs] * data: [DMs]
* dm : { * dm : {
* dmId: str * dmId: str
* messages: [msgs] * messages: [msgs]
* msg: { * msg: {
* author: str * author: str
* createdAt: ISOString * createdAt: ISOString
* message: str * message: str
* messageId: str * messageId: str
* } * }
* recipient: str * recipient: str
* hasDirectMessagesEnabled: bool * hasDirectMessagesEnabled: bool
* recentMessage: str * recentMessage: str
* recentMessageTimestamp: ISOString * recentMessageTimestamp: ISOString
* } * }
*/ */
// Returns all the messages in a dm documentSnapshot // Returns all the messages in a dm documentSnapshot
function getMessages(dm) { function getMessages(dm) {
@ -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 (
@ -720,16 +716,22 @@ exports.getDirectMessages = (req, res) => {
return 0; return 0;
} }
}); });
dmsData.forEach((dm) => { dmsData.forEach(dm => {
dm.hasDirectMessagesEnabled = userData.find((user) => { dm.hasDirectMessagesEnabled =
if (dm.recipient === user.data().handle) { userData
return true .find(user => {
} else { if (dm.recipient === user.data().handle) {
return false return true;
}}).data().dmEnabled === false ? false : true } else {
}) return false;
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,23 +989,32 @@ 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 (
return res.status(400).json({error: "This user has DMs disabled"}); 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}`)
.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 = [];
dmList.forEach(dmRef => { dmList.forEach(dmRef => {
dmRefPromises.push( dmRefPromises.push(
@ -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,9 +202,8 @@ 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(() => {
@ -254,18 +254,20 @@ class user extends Component {
this.state.myTopics.includes(topic) ? ( this.state.myTopics.includes(topic) ? (
<MyChip <MyChip
label={topic} label={topic}
key={{ topic }.topic.id} // BUG: this value is undefined key={{ topic }.id}
onDelete onDelete
deleteIcon={<DoneIcon />} deleteIcon={<DoneIcon />}
/> />
) : ( ) : this.state.following ? (
<MyChip <MyChip
label={topic} label={topic}
key={{ topic }.topic.id} // BUG: this value is undefined key={{ topic }.id}
color="secondary" color="secondary"
clickable clickable
onClick={key => this.handleAdd(topic)} onClick={key => this.handleAdd(topic)}
/> />
) : (
<MyChip label={topic} key={{ topic }.id} color="secondary" />
) )
) : ( ) : (
<p></p> <p></p>