mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 21:25:04 +00:00
Compare commits
130 Commits
branch-692
...
chronologi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3522876d7 | ||
| fc9994d42e | |||
| b85bee7cba | |||
| 76330fd234 | |||
| b007666317 | |||
| a0d2532c22 | |||
| 739b1cc92a | |||
| 57087a5ea3 | |||
|
|
3424a7d34f | ||
| 1aff5ba99b | |||
|
|
2bcf6bfcb3 | ||
| f9a45ffe07 | |||
|
|
17c7e989ef | ||
|
|
efecf3b02a | ||
|
|
8b4c32b81f | ||
|
|
323cb5bcee | ||
|
|
4a4733f593 | ||
|
|
35cec7abf3 | ||
|
|
6989091fb1 | ||
|
|
699ea23750 | ||
| 0bcfdcdd6c | |||
| 4e5b100968 | |||
| 374c4d45b0 | |||
| 7efed257f2 | |||
| 99ffbe52a4 | |||
|
|
856cb8630e | ||
| 0776728e73 | |||
| 77a3ba9f69 | |||
| 7a0a5725b7 | |||
| 79051f2243 | |||
| 94db3f451d | |||
| fd1718a1f4 | |||
| 7fd976d4cb | |||
| 72d099e05e | |||
|
|
f3fcd5ee8d | ||
|
|
7141bf9000 | ||
| ebca5192e0 | |||
|
|
acecf704dc | ||
|
|
56e836451d | ||
| 1747a29c2e | |||
| 041e53c36f | |||
| 2d426840c0 | |||
| 5b5e785142 | |||
| a4cce0b75e | |||
| 825c126c6c | |||
| 49a4d55f1e | |||
| b4f4dec9ca | |||
| f0c4a2974f | |||
| c26ff21b20 | |||
| 3092fabfd2 | |||
| 1a6aae6545 | |||
| 4abaa13b09 | |||
| 09a3ccd12d | |||
| f7058a520d | |||
| 1a02b53d0f | |||
| e4ea325fb8 | |||
|
|
6e32b25861 | ||
|
|
684ccd1a85 | ||
|
|
f0ddaa30b1 | ||
| 8244631d4d | |||
| 4d97afcb09 | |||
| 88064c55a3 | |||
| 7c7256acbd | |||
| cff0fd4e05 | |||
|
|
e218708e0a | ||
|
|
445c8b39de | ||
|
|
6e8b7f410a | ||
|
|
07a8f0b7bb | ||
|
|
54cd1c44a7 | ||
|
|
ca7f793d4c | ||
|
|
a36212c23d | ||
|
|
b83eb0c897 | ||
| fee2225745 | |||
| 0d8850c45d | |||
| f7ef0cb333 | |||
| 731e01c552 | |||
|
|
0c9778949a | ||
|
|
1a2af4e9bb | ||
|
|
c34f06f130 | ||
| 3814377817 | |||
|
|
9fff6ed81c | ||
| 445687cc54 | |||
| 331509da7f | |||
|
|
64cc9bd156 | ||
|
|
6a78d74930 | ||
|
|
4e817c9647 | ||
|
|
7976110e2b | ||
|
|
3da2449050 | ||
|
|
607ea3fd55 | ||
|
|
e8110d643f | ||
| e06cc614cb | |||
| 709df1492d | |||
|
|
c3091cc10a | ||
|
|
81749c19ce | ||
|
|
1ccc195036 | ||
|
|
0aaa9014b9 | ||
| 3d9266778d | |||
|
|
2de3da928a | ||
| 297619cacb | |||
|
|
42c53fdbc4 | ||
|
|
c8aa1fd050 | ||
| a59c2feb4a | |||
| 0f1cfe0393 | |||
| 63e917f54c | |||
| 2f5ec1c5ac | |||
| 19f8f70bf2 | |||
| 02197ee940 | |||
| 5507247a7f | |||
| 5560b6e13c | |||
| 19d78596b5 | |||
| 9372a092ad | |||
| 7a6ac8499c | |||
| 6149e15b35 | |||
| 80670d054e | |||
| 947e5b01a4 | |||
| 7cc8a3f11f | |||
| 7476833f0a | |||
| a2ed627f2b | |||
| bbbfc4b750 | |||
| 38df4ef087 | |||
| 2bd1c08c2c | |||
| 3fa4cd7033 | |||
| ab07d45205 | |||
| 3c31db9bf5 | |||
| c96c77cd9f | |||
| fb6c1e3d98 | |||
| 3d875e2bde | |||
| 3001ec0b38 | |||
| e8a403f575 | |||
| 44d3450b10 |
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable prefer-arrow-callback */
|
||||
/* eslint-disable promise/always-return */
|
||||
const { admin, db } = require("../util/admin");
|
||||
|
||||
const admin = require("firebase-admin");
|
||||
const { db } = require("../util/admin");
|
||||
|
||||
exports.putPost = (req, res) => {
|
||||
const newPost = {
|
||||
@@ -33,18 +33,6 @@ exports.putPost = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
exports.deletePost = (req, res) => {
|
||||
let posts = db.collection("posts")
|
||||
.where("userHandle", "==", req.user.handle)
|
||||
.get()
|
||||
.then((query) => {
|
||||
query.forEach((snap) => {
|
||||
snap.ref.delete();
|
||||
});
|
||||
return;
|
||||
})
|
||||
};
|
||||
|
||||
exports.getallPostsforUser = (req, res) => {
|
||||
var post_query = admin
|
||||
.firestore()
|
||||
@@ -74,102 +62,52 @@ exports.getallPostsforUser = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
exports.hidePost = (req, res) => {
|
||||
/* db
|
||||
.collection("posts")
|
||||
.doc(${req.params.postId}) */
|
||||
const postId = req.body.postId;
|
||||
db.doc(`/posts/${postId}`)
|
||||
.update({
|
||||
hidden: true
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(200).json({message: "ok"});
|
||||
})
|
||||
.catch((error) => {
|
||||
return res.status(500).json(error);
|
||||
})
|
||||
};
|
||||
|
||||
exports.getallPosts = (req, res) => {
|
||||
let posts = [];
|
||||
let users = {};
|
||||
|
||||
// Get all the posts
|
||||
var postsPromise = new Promise((resolve, reject) => {
|
||||
db.collection("posts")
|
||||
.get()
|
||||
.then(allPosts => {
|
||||
allPosts.forEach(post => {
|
||||
db.collection("posts").get()
|
||||
.then((allPosts) => {
|
||||
allPosts.forEach((post) => {
|
||||
posts.push(post.data());
|
||||
});
|
||||
posts.sort((a, b) => -a.createdAt.localeCompare(b.createdAt));
|
||||
resolve();
|
||||
})
|
||||
.catch(error => {
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
// Get all users
|
||||
var usersPromise = new Promise((resolve, reject) => {
|
||||
db.collection("users")
|
||||
.get()
|
||||
.then(allUsers => {
|
||||
allUsers.forEach(user => {
|
||||
db.collection("users").get()
|
||||
.then((allUsers) => {
|
||||
allUsers.forEach((user) => {
|
||||
users[user.data().handle] = user.data();
|
||||
});
|
||||
})
|
||||
resolve();
|
||||
})
|
||||
.catch(error => {
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
// Wait for the two promises
|
||||
Promise.all([postsPromise, usersPromise])
|
||||
.then(() => {
|
||||
let newPosts = [];
|
||||
let newPosts = []
|
||||
// Add the image url of the person who made the post to all of the post objects
|
||||
posts.forEach(post => {
|
||||
post.profileImage = users[post.userHandle].imageUrl
|
||||
? users[post.userHandle].imageUrl
|
||||
: null;
|
||||
posts.forEach((post) => {
|
||||
post.profileImage = users[post.userHandle].imageUrl ? users[post.userHandle].imageUrl : null;
|
||||
newPosts.push(post);
|
||||
});
|
||||
return res.status(200).json(newPosts);
|
||||
})
|
||||
.catch(error => {
|
||||
return res.status(500).json({ error });
|
||||
});
|
||||
};
|
||||
|
||||
exports.getAlert = (req, res) => {
|
||||
var post_query = admin
|
||||
.firestore()
|
||||
.collection("posts")
|
||||
.where("microBlogTitle", "==", "Alert");
|
||||
|
||||
post_query
|
||||
.get()
|
||||
.then(function(myPosts) {
|
||||
let posts = [];
|
||||
myPosts.forEach(function(doc) {
|
||||
posts.push(doc.data());
|
||||
});
|
||||
posts.sort((a, b) => -a.createdAt.localeCompare(b.createdAt));
|
||||
return res.status(200).json(posts);
|
||||
.catch((error) => {
|
||||
return res.status(500).json({error});
|
||||
})
|
||||
.then(function() {
|
||||
return res
|
||||
.status(200)
|
||||
.json("Successfully retrieved all user's posts from database.");
|
||||
})
|
||||
.catch(function(err) {
|
||||
return res
|
||||
.status(500)
|
||||
.json("Failed to retrieve user's posts from database.", err);
|
||||
});
|
||||
};
|
||||
|
||||
exports.getOtherUsersPosts = (req, res) => {
|
||||
@@ -178,11 +116,6 @@ exports.getOtherUsersPosts = (req, res) => {
|
||||
.collection("posts")
|
||||
.where("userHandle", "==", req.body.handle);
|
||||
|
||||
// post_query += admin
|
||||
// .firestore()
|
||||
// .collection("posts")
|
||||
// .where("microBlogTitle", "==", "Alert").where("userHandle", "==", "Admin");
|
||||
|
||||
post_query
|
||||
.get()
|
||||
.then(function(myPosts) {
|
||||
@@ -190,7 +123,6 @@ exports.getOtherUsersPosts = (req, res) => {
|
||||
myPosts.forEach(function(doc) {
|
||||
posts.push(doc.data());
|
||||
});
|
||||
posts.sort((a, b) => -a.createdAt.localeCompare(b.createdAt));
|
||||
return res.status(200).json(posts);
|
||||
})
|
||||
.then(function() {
|
||||
@@ -207,23 +139,21 @@ exports.getOtherUsersPosts = (req, res) => {
|
||||
|
||||
exports.quoteWithPost = (req, res) => {
|
||||
let quoteData;
|
||||
const quoteDoc = admin
|
||||
.firestore()
|
||||
.collection("quote")
|
||||
.where("userHandle", "==", req.user.handle)
|
||||
.where("quoteId", "==", req.params.postId)
|
||||
.limit(1);
|
||||
const quoteDoc = admin.firestore().collection('quote').
|
||||
where('userHandle', '==', req.user.handle).
|
||||
where('quoteId', '==', req.params.postId).limit(1);
|
||||
|
||||
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
|
||||
postDoc
|
||||
.get()
|
||||
.then(doc => {
|
||||
if (doc.exists) {
|
||||
postDoc.get()
|
||||
.then((doc) => {
|
||||
if(doc.exists) {
|
||||
quoteData = doc.data();
|
||||
return quoteDoc.get();
|
||||
} else {
|
||||
return res.status(404).json({ error: "Post not found" });
|
||||
}
|
||||
else
|
||||
{
|
||||
return res.status(404).json({error: 'Post not found'});
|
||||
}
|
||||
})
|
||||
.then(data => {
|
||||
@@ -273,23 +203,21 @@ exports.quoteWithPost = (req, res) => {
|
||||
|
||||
exports.quoteWithoutPost = (req, res) => {
|
||||
let quoteData;
|
||||
const quoteDoc = admin
|
||||
.firestore()
|
||||
.collection("quote")
|
||||
.where("userHandle", "==", req.user.handle)
|
||||
.where("quoteId", "==", req.params.postId)
|
||||
.limit(1);
|
||||
const quoteDoc = admin.firestore().collection('quote').
|
||||
where('userHandle', '==', req.user.handle).
|
||||
where('quoteId', '==', req.params.postId).limit(1);
|
||||
|
||||
const postDoc = db.doc(`/posts/${req.params.postId}`);
|
||||
|
||||
postDoc
|
||||
.get()
|
||||
.then(doc => {
|
||||
if (doc.exists) {
|
||||
postDoc.get()
|
||||
.then((doc) => {
|
||||
if(doc.exists) {
|
||||
quoteData = doc.data();
|
||||
return quoteDoc.get();
|
||||
} else {
|
||||
return res.status(404).json({ error: "Post not found" });
|
||||
}
|
||||
else
|
||||
{
|
||||
return res.status(404).json({error: 'Post not found'});
|
||||
}
|
||||
})
|
||||
.then(data => {
|
||||
@@ -346,9 +274,7 @@ exports.checkforLikePost = (req, res) => {
|
||||
.limit(1);
|
||||
let result;
|
||||
|
||||
likedPostDoc
|
||||
.get()
|
||||
.then(data => {
|
||||
likedPostDoc.get().then(data => {
|
||||
if (data.empty) {
|
||||
result = false;
|
||||
return res.status(200).json(result);
|
||||
@@ -357,49 +283,49 @@ exports.checkforLikePost = (req, res) => {
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
};
|
||||
|
||||
exports.likePost = (req, res) => {
|
||||
|
||||
const postId = req.params.postId;
|
||||
let likedPostDoc;
|
||||
db.doc(`/users/${req.userData.handle}`)
|
||||
.get()
|
||||
.then(userDoc => {
|
||||
.then((userDoc) => {
|
||||
let likes = userDoc.data().likes;
|
||||
if (likes === undefined || likes === null) {
|
||||
likes = [];
|
||||
}
|
||||
|
||||
if (likes.includes(postId)) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "This user has already liked this post" });
|
||||
return res.status(400).json({error: "This user has already liked this post"});
|
||||
}
|
||||
|
||||
likes.push(postId);
|
||||
|
||||
return userDoc.ref.update({ likes });
|
||||
return userDoc.ref.update({likes})
|
||||
})
|
||||
.then(() => {
|
||||
return db.doc(`/posts/${postId}`).get();
|
||||
return db.doc(`/posts/${postId}`).get()
|
||||
|
||||
})
|
||||
.then(postDoc => {
|
||||
.then((postDoc) => {
|
||||
let postData = postDoc.data();
|
||||
postData.likeCount++;
|
||||
likedPostDoc = postData;
|
||||
return postDoc.ref.update({ likeCount: postData.likeCount });
|
||||
return postDoc.ref.update({likeCount : postData.likeCount})
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json(likedPostDoc);
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
|
||||
// let postData;
|
||||
// const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||
@@ -437,23 +363,24 @@ exports.likePost = (req, res) => {
|
||||
// .catch((err) => {
|
||||
// return res.status(500).json({error: 'Something is wrong'});
|
||||
// })
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
exports.unlikePost = (req, res) => {
|
||||
|
||||
const postId = req.params.postId;
|
||||
let likedPostDoc;
|
||||
db.doc(`/users/${req.userData.handle}`)
|
||||
.get()
|
||||
.then(userDoc => {
|
||||
.then((userDoc) => {
|
||||
let likes = userDoc.data().likes;
|
||||
if (likes === undefined || likes === null) {
|
||||
likes = [];
|
||||
}
|
||||
|
||||
if (!likes.includes(postId)) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "This user hasn't liked this post yet" });
|
||||
return res.status(400).json({error: "This user hasn't liked this post yet"});
|
||||
}
|
||||
|
||||
let i;
|
||||
@@ -463,24 +390,25 @@ exports.unlikePost = (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
return userDoc.ref.update({ likes });
|
||||
return userDoc.ref.update({likes})
|
||||
})
|
||||
.then(() => {
|
||||
return db.doc(`/posts/${postId}`).get();
|
||||
return db.doc(`/posts/${postId}`).get()
|
||||
|
||||
})
|
||||
.then(postDoc => {
|
||||
.then((postDoc) => {
|
||||
let postData = postDoc.data();
|
||||
postData.likeCount--;
|
||||
likedPostDoc = postData;
|
||||
return postDoc.ref.update({ likeCount: postData.likeCount });
|
||||
return postDoc.ref.update({likeCount : postData.likeCount})
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json(likedPostDoc);
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
|
||||
// let postData;
|
||||
// const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
|
||||
@@ -516,28 +444,32 @@ exports.unlikePost = (req, res) => {
|
||||
// console.error(err);
|
||||
// return res.status(500).json({error: 'Something is wrong'});
|
||||
// })
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
exports.getLikes = (req, res) => {
|
||||
db.doc(`/users/${req.userData.handle}`)
|
||||
.get()
|
||||
.then(doc => {
|
||||
.then((doc) => {
|
||||
let likes = doc.data().likes;
|
||||
if (likes === undefined || likes === null) {
|
||||
likes = [];
|
||||
}
|
||||
return res.status(200).json({ likes });
|
||||
return res.status(200).json({likes});
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
};
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
}
|
||||
|
||||
exports.getFilteredPosts = (req, res) => {
|
||||
|
||||
admin
|
||||
.firestore()
|
||||
.collection("posts")
|
||||
.where("userHandle", "==", "new user")
|
||||
.where("microBlogTopics", "==");
|
||||
};
|
||||
|
||||
|
||||
@@ -26,41 +26,6 @@ exports.putTopic = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
exports.putNewTopic = (req, res) => {
|
||||
let new_following = [];
|
||||
let userRef = db.doc(`/users/${req.userData.handle}`);
|
||||
userRef
|
||||
.get()
|
||||
.then(doc => {
|
||||
let topics = [];
|
||||
new_following = doc.data().following;
|
||||
// new_following.push(req.body.following);
|
||||
new_following.forEach(follow => {
|
||||
if (follow.handle === req.body.handle) {
|
||||
// topics = follow.topics;
|
||||
follow.topics.push(req.body.topic);
|
||||
}
|
||||
});
|
||||
// return res.status(201).json({ new_following });
|
||||
|
||||
// add stuff
|
||||
userRef
|
||||
.set({ following: new_following }, { merge: true })
|
||||
.then(doc => {
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: `Following ${req.body.topic}` });
|
||||
})
|
||||
.catch(err => {
|
||||
return res.status(500).json({ err });
|
||||
});
|
||||
return res.status(200).json({ message: "OK" });
|
||||
})
|
||||
.catch(err => {
|
||||
return res.status(500).json({ err });
|
||||
});
|
||||
};
|
||||
|
||||
exports.getAllTopics = (req, res) => {
|
||||
admin
|
||||
.firestore()
|
||||
|
||||
@@ -226,7 +226,7 @@ exports.deleteUser = (req, res) => {
|
||||
const deleteUsername = req.userData.handle;
|
||||
db.doc(`/users/${deleteUsername}`)
|
||||
.get()
|
||||
.then(deleteUserDocSnap => {
|
||||
.then((deleteUserDocSnap) => {
|
||||
const dms = deleteUserDocSnap.data().dms;
|
||||
const dmRecipients = deleteUserDocSnap.data().dmRecipients;
|
||||
|
||||
@@ -239,32 +239,25 @@ exports.deleteUser = (req, res) => {
|
||||
let otherUsersPromises = [];
|
||||
|
||||
// Resolve if they don't have a dmRecipients list
|
||||
if (
|
||||
dmRecipients === undefined ||
|
||||
dmRecipients === null ||
|
||||
dmRecipients.length === 0
|
||||
) {
|
||||
if (dmRecipients === undefined || dmRecipients === null || dmRecipients.length === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
dmRecipients.forEach(dmRecipient => {
|
||||
dmRecipients.forEach((dmRecipient) => {
|
||||
otherUsersPromises.push(
|
||||
// Get each users data
|
||||
db
|
||||
.doc(`/users/${dmRecipient}`)
|
||||
.get()
|
||||
.then(otherUserDocSnap => {
|
||||
db.doc(`/users/${dmRecipient}`).get()
|
||||
.then((otherUserDocSnap) => {
|
||||
// Get the index of deleteUsername so that we can remove the dangling
|
||||
// reference to the DM document
|
||||
let otherUserDMRecipients = otherUserDocSnap.data()
|
||||
.dmRecipients;
|
||||
let otherUserDMRecipients = otherUserDocSnap.data().dmRecipients;
|
||||
let otherUserDMs = otherUserDocSnap.data().dms;
|
||||
let index = -1;
|
||||
otherUserDMRecipients.forEach((dmRecip, i) => {
|
||||
if (dmRecip === deleteUsername) {
|
||||
index = i;
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
if (index !== -1) {
|
||||
// Remove deleteUsername from their dmRecipients list
|
||||
@@ -279,29 +272,28 @@ exports.deleteUser = (req, res) => {
|
||||
dms: otherUserDMs
|
||||
});
|
||||
}
|
||||
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Wait for the removal of DM data stored on other users to be deleted
|
||||
Promise.all(otherUsersPromises)
|
||||
.then(() => {
|
||||
// Iterate through DM references and delete them from the dm collection
|
||||
let dmRefsPromises = [];
|
||||
dms.forEach(dmRef => {
|
||||
dms.forEach((dmRef) => {
|
||||
// Create a delete queue
|
||||
let batch = db.batch();
|
||||
dmRefsPromises.push(
|
||||
// Add the messages to the delete queue
|
||||
db
|
||||
.collection(`/dm/${dmRef.id}/messages`)
|
||||
.listDocuments()
|
||||
.then(docs => {
|
||||
console.log("second");
|
||||
db.collection(`/dm/${dmRef.id}/messages`).listDocuments()
|
||||
.then((docs) => {
|
||||
console.log("second")
|
||||
console.log(docs);
|
||||
docs.map(doc => {
|
||||
docs.map((doc) => {
|
||||
batch.delete(doc);
|
||||
});
|
||||
})
|
||||
|
||||
// Add the doc that the DM is stored in to the delete queue
|
||||
batch.delete(dmRef);
|
||||
@@ -309,8 +301,8 @@ exports.deleteUser = (req, res) => {
|
||||
// Commit the writes
|
||||
return batch.commit();
|
||||
})
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
return Promise.all(dmRefsPromises);
|
||||
})
|
||||
@@ -318,17 +310,18 @@ exports.deleteUser = (req, res) => {
|
||||
resolve();
|
||||
return;
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log("error " + err);
|
||||
reject(err);
|
||||
return;
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
});
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// Deletes user from authentication
|
||||
@@ -341,18 +334,18 @@ exports.deleteUser = (req, res) => {
|
||||
return db
|
||||
.collection("users")
|
||||
.doc(`${req.user.handle}`)
|
||||
.delete();
|
||||
.delete()
|
||||
})
|
||||
.then(() => {
|
||||
resolve();
|
||||
return;
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
return;
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
// Deletes any custom profile image
|
||||
let image;
|
||||
@@ -461,18 +454,17 @@ exports.getUserDetails = (req, res) => {
|
||||
|
||||
exports.getAllHandles = (req, res) => {
|
||||
var user_query = admin.firestore().collection("users");
|
||||
user_query
|
||||
.get()
|
||||
.then(allUsers => {
|
||||
user_query.get()
|
||||
.then((allUsers) => {
|
||||
let users = [];
|
||||
allUsers.forEach(user => {
|
||||
allUsers.forEach((user) => {
|
||||
users.push(user.data().handle);
|
||||
});
|
||||
return res.status(200).json(users);
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
return res.status(500).json({
|
||||
message: "Failed to retrieve posts from database.",
|
||||
message:"Failed to retrieve posts from database.",
|
||||
error: err
|
||||
});
|
||||
});
|
||||
@@ -565,7 +557,7 @@ exports.unverifyUser = (req, res) => {
|
||||
|
||||
// Returns all the DMs that the user is currently participating in
|
||||
exports.getDirectMessages = (req, res) => {
|
||||
/* Return value
|
||||
/* Return value
|
||||
* data: [DMs]
|
||||
* dm : {
|
||||
* dmId: str
|
||||
@@ -577,7 +569,6 @@ exports.getDirectMessages = (req, res) => {
|
||||
* messageId: str
|
||||
* }
|
||||
* recipient: str
|
||||
* hasDirectMessagesEnabled: bool
|
||||
* recentMessage: str
|
||||
* recentMessageTimestamp: ISOString
|
||||
* }
|
||||
@@ -586,9 +577,9 @@ exports.getDirectMessages = (req, res) => {
|
||||
// Returns all the messages in a dm documentSnapshot
|
||||
function getMessages(dm) {
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
let messagesCollection = dm.collection("messages");
|
||||
let messagesCollection = dm.collection('messages');
|
||||
|
||||
// If the messagesCollection is missing, that means that there aren't any messages
|
||||
// If the messagesCollection is missing, that mean that there aren't any messages
|
||||
if (messagesCollection === null || messagesCollection === undefined) {
|
||||
return;
|
||||
}
|
||||
@@ -597,106 +588,90 @@ exports.getDirectMessages = (req, res) => {
|
||||
let promises = [];
|
||||
|
||||
// Get all of the messages in the DM
|
||||
messagesCollection.get().then(dmQuerySnap => {
|
||||
dmQuerySnap.forEach(dmQueryDocSnap => {
|
||||
messagesCollection.get()
|
||||
.then((dmQuerySnap) => {
|
||||
dmQuerySnap.forEach((dmQueryDocSnap) => {
|
||||
promises.push(
|
||||
dmQueryDocSnap.ref.get().then(messageData => {
|
||||
dmQueryDocSnap.ref.get()
|
||||
.then((messageData) => {
|
||||
msgs.push(messageData.data());
|
||||
return;
|
||||
})
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
let waitPromise = Promise.all(promises);
|
||||
waitPromise.then(() => {
|
||||
// Sort the messages in reverse order by date
|
||||
// Newest should be at the bottom, because that's how they will be displayed on the front-end
|
||||
msgs.sort((a, b) => {
|
||||
return b.createdAt > a.createdAt
|
||||
? -1
|
||||
: b.createdAt < a.createdAt
|
||||
? 1
|
||||
: 0;
|
||||
});
|
||||
return (b.createdAt > a.createdAt) ? -1 : ((b.createdAt < a.createdAt) ? 1 : 0);
|
||||
})
|
||||
resolve(msgs);
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
||||
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 });
|
||||
if (dms === undefined || dms === null || dms.length === 0) return res.status(200).json({data: null});
|
||||
|
||||
let dmsData = [];
|
||||
let dmPromises = [];
|
||||
|
||||
dms.forEach(dm => {
|
||||
dms.forEach((dm) => {
|
||||
let dmData = {};
|
||||
// Make a new promise for each DM document
|
||||
dmPromises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
dm.get() // DM document reference
|
||||
.then(doc => {
|
||||
dmPromises.push(new Promise((resolve, reject) => {
|
||||
dm // DM document reference
|
||||
.get()
|
||||
.then((doc) => {
|
||||
let docData = doc.data();
|
||||
|
||||
// Recipient is the person you are messaging
|
||||
docData.authors[0] === req.userData.handle
|
||||
? (dmData.recipient = docData.authors[1])
|
||||
: (dmData.recipient = docData.authors[0]);
|
||||
docData.authors[0] === req.userData.handle ?
|
||||
dmData.recipient = docData.authors[1] :
|
||||
dmData.recipient = docData.authors[0]
|
||||
|
||||
// Save the createdAt time
|
||||
dmData.createdAt = docData.createdAt;
|
||||
|
||||
// Get all the messages from this dm document
|
||||
getMessages(dm).then(msgs => {
|
||||
getMessages(dm)
|
||||
.then((msgs) => {
|
||||
dmData.messages = msgs;
|
||||
dmData.recentMessage =
|
||||
msgs.length !== 0 ? msgs[msgs.length - 1].message : null;
|
||||
dmData.recentMessageTimestamp =
|
||||
msgs.length !== 0 ? msgs[msgs.length - 1].createdAt : null;
|
||||
dmData.recentMessage = msgs.length !== 0 ? msgs[msgs.length - 1].message : null;
|
||||
dmData.recentMessageTimestamp = msgs.length !== 0 ? msgs[msgs.length - 1].createdAt : null;
|
||||
dmData.dmId = doc.id;
|
||||
resolve(dmData);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
|
||||
|
||||
|
||||
}).catch((err) => {
|
||||
console.err(err);
|
||||
return res.status(400).json({
|
||||
error: {
|
||||
message:
|
||||
"An error occurred when reading the DM document reference",
|
||||
return res.status(400).json({error: {
|
||||
message: "An error occurred when reading the DM document reference",
|
||||
error: err
|
||||
}
|
||||
});
|
||||
});
|
||||
}).then(dmData => {
|
||||
}});
|
||||
})
|
||||
}).then((dmData) => {
|
||||
dmsData.push(dmData);
|
||||
})
|
||||
);
|
||||
});
|
||||
)
|
||||
|
||||
// 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
|
||||
Promise.all(dmPromises)
|
||||
dmWaitPromise = 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
|
||||
) {
|
||||
if (a.recentMessageTimestamp === null && b.recentMessageTimestamp === null) {
|
||||
if (b.createdAt < a.createdAt) {
|
||||
return -1;
|
||||
} else if (b.createdAt > a.createdAt) {
|
||||
@@ -716,32 +691,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;
|
||||
}
|
||||
return res.status(200).json({data: dmsData})
|
||||
})
|
||||
.data().dmEnabled === false
|
||||
? false
|
||||
: true;
|
||||
});
|
||||
return res.status(200).json({ data: dmsData });
|
||||
})
|
||||
.catch(err => {
|
||||
return res.status(500).json({
|
||||
error: {
|
||||
.catch((err) => {
|
||||
return res.status(500).json({error:{
|
||||
message: "An error occurred while sorting",
|
||||
error: err
|
||||
}
|
||||
}});
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Toggles direct messages on or off depending on the requese
|
||||
/* Request Parameters
|
||||
@@ -750,44 +708,38 @@ exports.getDirectMessages = (req, res) => {
|
||||
exports.toggleDirectMessages = (req, res) => {
|
||||
const enable = req.body.enable;
|
||||
const user = req.userData.handle;
|
||||
db.doc(`/users/${user}`)
|
||||
.update({ dmEnabled: enable })
|
||||
db.doc(`/users/${user}`).update({dmEnabled: enable})
|
||||
.then(() => {
|
||||
return res.status(201).json({ message: "Success" });
|
||||
return res.status(201).json({message: "Success"});
|
||||
})
|
||||
.catch(err => {
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
};
|
||||
.catch((err) => {
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
}
|
||||
|
||||
// Returns a promise that resolves if user has DMs enabled
|
||||
// and rejects if there is an error or DMs are disabled
|
||||
isDirectMessageEnabled = username => {
|
||||
isDirectMessageEnabled = (username) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let result = {};
|
||||
result.code = null;
|
||||
result.message = null;
|
||||
if (username === null || username === undefined || username === "") {
|
||||
result.code = 400;
|
||||
result.message =
|
||||
"No user was sent in the request. The request should have a non-empty 'user' key.";
|
||||
result.message = "No user was sent in the request. The request should have a non-empty 'user' key.";
|
||||
reject(result);
|
||||
}
|
||||
|
||||
db.doc(`/users/${username}`)
|
||||
.get()
|
||||
.then(doc => {
|
||||
.then((doc) => {
|
||||
if (doc.exists) {
|
||||
// console.log(doc.data())
|
||||
if (
|
||||
doc.data().dmEnabled === true ||
|
||||
doc.data().dmEnabled === null ||
|
||||
doc.data().dmEnabled === undefined
|
||||
) {
|
||||
if (doc.data().dmEnabled === true || doc.data().dmEnabled === null || doc.data().dmEnabled === undefined) {
|
||||
// Assume DMs are enabled if they don't have a dmEnabled key
|
||||
resolve(result);
|
||||
} else {
|
||||
result.code = 400;
|
||||
result.code = 200;
|
||||
result.message = `${username} has DMs disabled`;
|
||||
reject(result);
|
||||
}
|
||||
@@ -798,46 +750,42 @@ isDirectMessageEnabled = username => {
|
||||
reject(result);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("HI");
|
||||
.catch((err) => {
|
||||
console.log("HI")
|
||||
console.error(err);
|
||||
result.code = 500;
|
||||
result.message = err;
|
||||
reject(result);
|
||||
})
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Returns a promise that resolves if the data in the DM is valid and
|
||||
// rejects if there are any error. Errors are returned in the promise
|
||||
verifyDirectMessageIntegrity = dmRef => {
|
||||
verifyDirectMessageIntegrity = (dmRef) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve("Not implemented yet");
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// Checks if there are any DM channels open with userB on userA's side
|
||||
oneWayCheck = (userA, userB) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
db.doc(`/users/${userA}`)
|
||||
.get()
|
||||
.then(userASnapshot => {
|
||||
.then((userASnapshot) => {
|
||||
const dmList = userASnapshot.data().dms;
|
||||
const dmRecipients = userASnapshot.data().dmRecipients;
|
||||
|
||||
if (
|
||||
dmList === null ||
|
||||
dmList === undefined ||
|
||||
dmRecipients === null ||
|
||||
dmRecipients === undefined
|
||||
) {
|
||||
if (dmList === null || dmList === undefined || dmRecipients === null || dmRecipients === undefined) {
|
||||
// They don't have any DMs yet
|
||||
console.log("No DMs array");
|
||||
userASnapshot.ref
|
||||
.set({ dms: [], dmRecipients: [] }, { merge: true })
|
||||
userASnapshot.ref.set({dms:[], dmRecipients:[]}, {merge: true})
|
||||
.then(() => {
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
} else if (dmList.length === 0) {
|
||||
// Their DMs are empty
|
||||
console.log("DMs array is empty");
|
||||
@@ -862,20 +810,17 @@ oneWayCheck = (userA, userB) => {
|
||||
// )
|
||||
// })
|
||||
|
||||
dmRecipients.forEach(dmRecipient => {
|
||||
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({
|
||||
code: 400,
|
||||
message: `You already have a DM with that user`
|
||||
});
|
||||
reject(new Error(`You already have a DM with ${userB}`));
|
||||
return;
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
resolve();
|
||||
|
||||
|
||||
// Promise.all(forEachPromises)
|
||||
// .then((dmDocs) => {
|
||||
// // Check if any of the DMs have for userA have userA and userB as the authors.
|
||||
@@ -921,10 +866,15 @@ oneWayCheck = (userA, userB) => {
|
||||
// }
|
||||
// })
|
||||
// })
|
||||
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Returns a promise that resolves if there is not already a DM channel
|
||||
// between the creator and recipient usernames. It rejects if one already
|
||||
@@ -935,39 +885,36 @@ checkNoDirectMessageExists = (creator, recipient) => {
|
||||
let recipientPromise = oneWayCheck(recipient, creator);
|
||||
let temp_array = [];
|
||||
temp_array.push(creatorPromise);
|
||||
temp_array.push(recipientPromise);
|
||||
temp_array.push(recipientPromise)
|
||||
|
||||
Promise.all(temp_array)
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
addDirectMessageToUser = (username, recipient, dmRef) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.doc(`/users/${username}`)
|
||||
.get()
|
||||
.then(docSnap => {
|
||||
db.doc(`/users/${username}`).get()
|
||||
.then((docSnap) => {
|
||||
let dmList = docSnap.data().dms;
|
||||
let dmRecipients = docSnap.data().dmRecipients;
|
||||
dmList.push(dmRef);
|
||||
dmRecipients.push(recipient);
|
||||
return db
|
||||
.doc(`/users/${username}`)
|
||||
.update({ dms: dmList, dmRecipients });
|
||||
return db.doc(`/users/${username}`).update({dms: dmList, dmRecipients});
|
||||
})
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Sends a DM from the caller to the requested DM document
|
||||
/* Request Parameters
|
||||
@@ -985,92 +932,66 @@ exports.sendDirectMessage = (req, res) => {
|
||||
createdAt: new Date().toISOString(),
|
||||
message,
|
||||
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 => {
|
||||
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 => {
|
||||
dmList.forEach((dmRef) => {
|
||||
dmRefPromises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
dmRef
|
||||
.get()
|
||||
.then(dmDoc => {
|
||||
dmRef.get()
|
||||
.then((dmDoc) => {
|
||||
let authors = dmDoc.data().authors;
|
||||
if (
|
||||
(authors[0] === creator && authors[1] === recipient) ||
|
||||
(authors[1] === creator && authors[0] === recipient)
|
||||
) {
|
||||
resolve({ correct: true, dmRef });
|
||||
resolve({correct: true, dmRef});
|
||||
} else {
|
||||
resolve({ correct: false, dmRef });
|
||||
resolve({correct: false, dmRef});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
return Promise.all(dmRefPromises);
|
||||
})
|
||||
.then(results => {
|
||||
.then((results) => {
|
||||
let correctDMRef = null;
|
||||
results.forEach(result => {
|
||||
results.forEach((result) => {
|
||||
if (result.correct) {
|
||||
correctDMRef = result.dmRef;
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
if (correctDMRef === null) {
|
||||
console.log(
|
||||
`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.`
|
||||
});
|
||||
console.log(`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.`});
|
||||
}
|
||||
|
||||
return db.collection(`/dm/${correctDMRef.id}/messages`).add(newMessage);
|
||||
})
|
||||
.then(newMsgRef => {
|
||||
return newMsgRef.update({ messageId: newMsgRef.id }, { merge: true });
|
||||
.then((newMsgRef) => {
|
||||
return newMsgRef.update({messageId: newMsgRef.id}, {merge: true});
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(200).json({ message: "OK" });
|
||||
return res.status(200).json({message: "OK"});
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res.status(500).json({ error: err });
|
||||
});
|
||||
};
|
||||
return res.status(500).json({error: err});
|
||||
})
|
||||
}
|
||||
|
||||
// Creates a DM between the caller and the user in the request
|
||||
/* Request Parameters
|
||||
@@ -1081,8 +1002,7 @@ exports.createDirectMessage = (req, res) => {
|
||||
const recipient = req.body.user;
|
||||
|
||||
// Check if they are DMing themselves
|
||||
if (creator === recipient)
|
||||
return res.status(400).json({ error: "You can't DM yourself" });
|
||||
if (creator === recipient) return res.status(400).json({error: "You can't DM yourself"});
|
||||
|
||||
// Check if this user has DMs enabled
|
||||
let creatorEnabled = isDirectMessageEnabled(creator);
|
||||
@@ -1091,63 +1011,56 @@ exports.createDirectMessage = (req, res) => {
|
||||
let recipientEnabled = isDirectMessageEnabled(recipient);
|
||||
|
||||
// Make sure that they don't already have a DM channel
|
||||
let noDMExists = checkNoDirectMessageExists(creator, recipient);
|
||||
let noDMExists = checkNoDirectMessageExists(creator, recipient)
|
||||
|
||||
let dataValidations = [creatorEnabled, recipientEnabled, noDMExists];
|
||||
|
||||
let dataValidations = [
|
||||
creatorEnabled,
|
||||
recipientEnabled,
|
||||
noDMExists
|
||||
]
|
||||
|
||||
Promise.all(dataValidations)
|
||||
.then(() => {
|
||||
// Create a new DM document
|
||||
return db.collection("dm").add({});
|
||||
return db.collection("dm").add({})
|
||||
})
|
||||
.then(dmDocRef => {
|
||||
.then((dmDocRef) => {
|
||||
// Fill it with some data.
|
||||
// Note that there isn't a messages collection by default.
|
||||
let dmData = {
|
||||
dmId: dmDocRef.id,
|
||||
authors: [creator, recipient],
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
// Update DM document
|
||||
let dmDocPromise = dmDocRef.set(dmData);
|
||||
|
||||
// Add the DM reference to the creator
|
||||
let updateCreatorPromise = addDirectMessageToUser(
|
||||
creator,
|
||||
recipient,
|
||||
dmDocRef
|
||||
);
|
||||
let updateCreatorPromise = addDirectMessageToUser(creator, recipient, dmDocRef);
|
||||
|
||||
// Add the DM reference to the recipient
|
||||
let updateRecipientPromise = addDirectMessageToUser(
|
||||
recipient,
|
||||
creator,
|
||||
dmDocRef
|
||||
);
|
||||
let updateRecipientPromise = addDirectMessageToUser(recipient, creator, dmDocRef);
|
||||
|
||||
// Wait for all promises
|
||||
return Promise.all([
|
||||
dmDocPromise,
|
||||
updateCreatorPromise,
|
||||
updateRecipientPromise
|
||||
]);
|
||||
return Promise.all([dmDocPromise, updateCreatorPromise, updateRecipientPromise]);
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json({ message: "Success!" });
|
||||
.then (() => {
|
||||
return res.status(201).json({message: "Success!"});
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
|
||||
if (err.code && err.message && err.code > 0) {
|
||||
// Specific error that I've created
|
||||
return res.status(err.code).json({ error: err.message });
|
||||
return res.status(err.code).json({error: err.message});
|
||||
} else {
|
||||
// Generic or firebase error
|
||||
return res.status(500).json({ error: err });
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
// Checks if the requested user has DMs enable or not
|
||||
/* Request Parameters
|
||||
@@ -1156,19 +1069,19 @@ exports.createDirectMessage = (req, res) => {
|
||||
exports.checkDirectMessagesEnabled = (req, res) => {
|
||||
isDirectMessageEnabled(req.body.user)
|
||||
.then(() => {
|
||||
return res.status(200).json({ enabled: true });
|
||||
return res.status(200).json({enabled: true});
|
||||
})
|
||||
.catch(result => {
|
||||
.catch((result) => {
|
||||
console.log(result);
|
||||
if (result.code === 200) {
|
||||
// DMs are disabled
|
||||
return res.status(200).json({ enabled: false });
|
||||
return res.status(200).json({enabled: false});
|
||||
} else {
|
||||
// Some other error occured
|
||||
return res.status(result.code).json({ err: result.message });
|
||||
return res.status(result.code).json({err: result.message});
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
exports.getUserHandles = (req, res) => {
|
||||
db.doc(`/users/${req.body.userHandle}`)
|
||||
@@ -1192,13 +1105,7 @@ exports.addSubscription = (req, res) => {
|
||||
let userRef = db.doc(`/users/${req.userData.handle}`);
|
||||
userRef.get().then(doc => {
|
||||
new_following = doc.data().following;
|
||||
const struct = {
|
||||
handle: req.body.following,
|
||||
topics: ["Admin"]
|
||||
};
|
||||
new_following
|
||||
? new_following.push(struct)
|
||||
: (new_following = req.body.following);
|
||||
new_following.push(req.body.following);
|
||||
|
||||
// add stuff
|
||||
userRef
|
||||
@@ -1211,11 +1118,8 @@ exports.addSubscription = (req, res) => {
|
||||
.catch(err => {
|
||||
return res.status(500).json({ err });
|
||||
});
|
||||
// return res.status(200).json({ message: "ok" });
|
||||
})
|
||||
.catch((error) => {
|
||||
return res.status(400).json({message: "That user doesn't exist", error});
|
||||
})
|
||||
return res.status(200).json({ message: "ok" });
|
||||
});
|
||||
};
|
||||
|
||||
exports.getSubs = (req, res) => {
|
||||
@@ -1242,32 +1146,25 @@ exports.uploadProfileImage = (req, res) => {
|
||||
|
||||
let imageFileName;
|
||||
let imageToBeUploaded = {};
|
||||
let oldImageFileName = req.userData.imageUrl
|
||||
? req.userData.imageUrl.split("/o/")[1].split("?alt")[0]
|
||||
: null;
|
||||
let oldImageFileName = req.userData.imageUrl ? req.userData.imageUrl.split("/o/")[1].split("?alt")[0] : null;
|
||||
// console.log(`old file: ${oldImageFileName}`);
|
||||
|
||||
busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
|
||||
if (mimetype !== "image/jpeg" && mimetype !== "image/png") {
|
||||
if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
|
||||
return res.status(400).json({ error: "Wrong filetype submitted" });
|
||||
}
|
||||
// console.log(fieldname);
|
||||
// console.log(filename);
|
||||
// console.log(mimetype);
|
||||
const imageExtension = filename.split(".")[filename.split(".").length - 1]; // Get the image file extension
|
||||
imageFileName = `${Math.round(
|
||||
Math.random() * 100000000000
|
||||
)}.${imageExtension}`; // Get a random filename
|
||||
imageFileName = `${Math.round(Math.random() * 100000000000)}.${imageExtension}`; // Get a random filename
|
||||
const filepath = path.join(os.tmpdir(), imageFileName);
|
||||
imageToBeUploaded = { filepath, mimetype };
|
||||
file.pipe(fs.createWriteStream(filepath));
|
||||
});
|
||||
busboy.on("finish", () => {
|
||||
// Save the file to the storage bucket
|
||||
admin
|
||||
.storage()
|
||||
.bucket(config.storageBucket)
|
||||
.upload(imageToBeUploaded.filepath, {
|
||||
admin.storage().bucket(config.storageBucket).upload(imageToBeUploaded.filepath, {
|
||||
resumable: false,
|
||||
metadata: {
|
||||
metadata: {
|
||||
@@ -1283,33 +1180,24 @@ exports.uploadProfileImage = (req, res) => {
|
||||
.then(() => {
|
||||
// Delete their old image if they have one
|
||||
if (oldImageFileName !== null && oldImageFileName !== "no-img.png") {
|
||||
admin
|
||||
.storage()
|
||||
.bucket(config.storageBucket)
|
||||
.file(oldImageFileName)
|
||||
.delete()
|
||||
admin.storage().bucket(config.storageBucket).file(oldImageFileName).delete()
|
||||
.then(() => {
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: "Image uploaded successfully1" });
|
||||
return res.status(201).json({ message: "Image uploaded successfully1"});
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: "Image uploaded successfully2" });
|
||||
});
|
||||
return res.status(201).json({ message: "Image uploaded successfully2"});
|
||||
})
|
||||
// return res.status(201).json({ message: "Image uploaded successfully"});
|
||||
} else {
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: "Image uploaded successfully3" });
|
||||
return res.status(201).json({ message: "Image uploaded successfully3"});
|
||||
}
|
||||
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
return res.status(500).json({ error: err.code})
|
||||
})
|
||||
});
|
||||
busboy.end(req.rawBody);
|
||||
|
||||
@@ -1365,7 +1253,7 @@ exports.uploadProfileImage = (req, res) => {
|
||||
// });
|
||||
// });
|
||||
// busboy.end(req.rawBody);
|
||||
};
|
||||
}
|
||||
|
||||
exports.removeSub = (req, res) => {
|
||||
let new_following = [];
|
||||
@@ -1374,7 +1262,7 @@ exports.removeSub = (req, res) => {
|
||||
new_following = doc.data().following;
|
||||
// remove username from array
|
||||
new_following.forEach(function(follower, index) {
|
||||
if (follower.handle === `${req.body.unfollow}`) {
|
||||
if (follower === `${req.body.unfollow}`) {
|
||||
new_following.splice(index, 1);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -100,33 +100,18 @@ app.post("/addSubscription", fbAuth, addSubscription);
|
||||
// remove one subscription
|
||||
app.post("/removeSub", fbAuth, removeSub);
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*
|
||||
* handlers/post.js *
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, getLikes, quoteWithPost, quoteWithoutPost, checkforLikePost, getOtherUsersPosts} = require("./handlers/post");
|
||||
|
||||
const {
|
||||
getallPostsforUser,
|
||||
getallPosts,
|
||||
putPost,
|
||||
hidePost,
|
||||
likePost,
|
||||
unlikePost,
|
||||
getLikes,
|
||||
quoteWithPost,
|
||||
quoteWithoutPost,
|
||||
checkforLikePost,
|
||||
getOtherUsersPosts,
|
||||
getAlert
|
||||
} = require("./handlers/post");
|
||||
|
||||
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
|
||||
|
||||
app.get("/getallPosts", getallPosts);
|
||||
|
||||
//Hides Post
|
||||
app.post("/hidePost", fbAuth, hidePost);
|
||||
|
||||
// Adds one post to the database
|
||||
app.post("/putPost", fbAuth, putPost);
|
||||
|
||||
@@ -140,8 +125,6 @@ app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost);
|
||||
|
||||
app.post("/getOtherUsersPosts", fbAuth, getOtherUsersPosts);
|
||||
|
||||
app.get("/getAlert", fbAuth, getAlert);
|
||||
|
||||
/*------------------------------------------------------------------*
|
||||
* handlers/topic.js *
|
||||
*------------------------------------------------------------------*/
|
||||
@@ -149,8 +132,7 @@ const {
|
||||
putTopic,
|
||||
getAllTopics,
|
||||
deleteTopic,
|
||||
getUserTopics,
|
||||
putNewTopic
|
||||
getUserTopics
|
||||
} = require("./handlers/topic");
|
||||
|
||||
// add topic to database
|
||||
@@ -165,6 +147,4 @@ app.post("/deleteTopic", fbAuth, deleteTopic);
|
||||
// get topic for this user
|
||||
app.post("/getUserTopics", fbAuth, getUserTopics);
|
||||
|
||||
app.post("/putNewTopic", fbAuth, putNewTopic);
|
||||
|
||||
exports.api = functions.https.onRequest(app);
|
||||
|
||||
44
twistter-frontend/package-lock.json
generated
44
twistter-frontend/package-lock.json
generated
@@ -2417,11 +2417,6 @@
|
||||
"resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz",
|
||||
"integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs="
|
||||
},
|
||||
"dayjs": {
|
||||
"version": "1.8.17",
|
||||
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.8.17.tgz",
|
||||
"integrity": "sha512-47VY/htqYqr9GHd7HW/h56PpQzRBSJcxIQFwqL3P20bMF/3az5c3PWdVY3LmPXFl6cQCYHL7c79b9ov+2bOBbw=="
|
||||
},
|
||||
"debug": {
|
||||
"version": "2.6.9",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
||||
@@ -3097,11 +3092,6 @@
|
||||
"merge": "^1.2.0"
|
||||
}
|
||||
},
|
||||
"exenv": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/exenv/-/exenv-1.2.2.tgz",
|
||||
"integrity": "sha1-KueOhdmJQVhnCwPUe+wfA72Ru50="
|
||||
},
|
||||
"exit-hook": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/exit-hook/-/exit-hook-1.1.1.tgz",
|
||||
@@ -3967,11 +3957,6 @@
|
||||
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
|
||||
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
|
||||
},
|
||||
"fuse.js": {
|
||||
"version": "3.4.6",
|
||||
"resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-3.4.6.tgz",
|
||||
"integrity": "sha512-H6aJY4UpLFwxj1+5nAvufom5b2BT2v45P1MkPvdGIK8fWjQx/7o6tTT1+ALV0yawQvbmvCF0ufl2et8eJ7v7Cg=="
|
||||
},
|
||||
"gauge": {
|
||||
"version": "2.7.4",
|
||||
"resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz",
|
||||
@@ -7182,22 +7167,6 @@
|
||||
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.9.0.tgz",
|
||||
"integrity": "sha512-tJBzzzIgnnRfEm046qRcURvwQnZVXmuCbscxUO5RWrGTXpon2d4c8mI0D8WE6ydVIm29JiLB6+RslkIvym9Rjw=="
|
||||
},
|
||||
"react-lifecycles-compat": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz",
|
||||
"integrity": "sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA=="
|
||||
},
|
||||
"react-modal": {
|
||||
"version": "3.11.1",
|
||||
"resolved": "https://registry.npmjs.org/react-modal/-/react-modal-3.11.1.tgz",
|
||||
"integrity": "sha512-8uN744Yq0X2lbfSLxsEEc2UV3RjSRb4yDVxRQ1aGzPo86QjNOwhQSukDb8U8kR+636TRTvfMren10fgOjAy9eA==",
|
||||
"requires": {
|
||||
"exenv": "^1.2.0",
|
||||
"prop-types": "^15.5.10",
|
||||
"react-lifecycles-compat": "^3.0.0",
|
||||
"warning": "^4.0.3"
|
||||
}
|
||||
},
|
||||
"react-redux": {
|
||||
"version": "7.1.1",
|
||||
"resolved": "https://registry.npmjs.org/react-redux/-/react-redux-7.1.1.tgz",
|
||||
@@ -9792,11 +9761,6 @@
|
||||
"resolved": "https://registry.npmjs.org/uid-number/-/uid-number-0.0.6.tgz",
|
||||
"integrity": "sha1-DqEOgDXo61uOREnwbaHHMGY7qoE="
|
||||
},
|
||||
"underscore": {
|
||||
"version": "1.9.1",
|
||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.1.tgz",
|
||||
"integrity": "sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg=="
|
||||
},
|
||||
"union-value": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz",
|
||||
@@ -10027,14 +9991,6 @@
|
||||
"makeerror": "1.0.x"
|
||||
}
|
||||
},
|
||||
"warning": {
|
||||
"version": "4.0.3",
|
||||
"resolved": "https://registry.npmjs.org/warning/-/warning-4.0.3.tgz",
|
||||
"integrity": "sha512-rpJyN222KWIvHJ/F53XSZv0Zl/accqHR8et1kpaMTD/fLCRxtV8iX8czMzY7sVZupTI3zcUTg8eycS2kNF9l6w==",
|
||||
"requires": {
|
||||
"loose-envify": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"watch": {
|
||||
"version": "0.10.0",
|
||||
"resolved": "https://registry.npmjs.org/watch/-/watch-0.10.0.tgz",
|
||||
|
||||
@@ -45,5 +45,5 @@
|
||||
"last 1 safari version"
|
||||
]
|
||||
},
|
||||
"proxy": "https://us-central1-twistter-e4649.cloudfunctions.net/api"
|
||||
"proxy": "http://localhost:5001/twistter-e4649/us-central1/api"
|
||||
}
|
||||
|
||||
@@ -84,23 +84,23 @@ class Writing_Microblogs extends Component {
|
||||
console.error(err);
|
||||
});
|
||||
console.log(postData.microBlogTopics);
|
||||
// let topicPromises = [];
|
||||
// postData.microBlogTopics.forEach(topic => {
|
||||
// topicPromises.push(axios
|
||||
// .post("/putTopic", {
|
||||
// following: topic
|
||||
// })
|
||||
// .then(res => {
|
||||
// console.log(res.data);
|
||||
// })
|
||||
// .catch(err => {
|
||||
// console.error(err);
|
||||
// })
|
||||
// )
|
||||
// });
|
||||
let topicPromises = [];
|
||||
postData.microBlogTopics.forEach(topic => {
|
||||
topicPromises.push(axios
|
||||
.post("/putTopic", {
|
||||
following: topic
|
||||
})
|
||||
.then(res => {
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
})
|
||||
)
|
||||
});
|
||||
event.preventDefault();
|
||||
// topicPromises.push(postPromise);
|
||||
Promise.all([postPromise])
|
||||
topicPromises.push(postPromise);
|
||||
Promise.all(topicPromises)
|
||||
.then(() => {
|
||||
this.setState({
|
||||
value: "",
|
||||
|
||||
@@ -41,55 +41,36 @@ class Home extends Component {
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
this.setState({ loading: true });
|
||||
this.setState({loading: true});
|
||||
let userPromise = axios
|
||||
.get("/user")
|
||||
.then(res => {
|
||||
console.log(res.data.credentials.following);
|
||||
let list = [];
|
||||
res.data.credentials.following.forEach(element => {
|
||||
list.push(element.handle);
|
||||
});
|
||||
this.setState({
|
||||
following: list,
|
||||
following: res.data.credentials.following,
|
||||
topics: res.data.credentials.followedTopics
|
||||
});
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
|
||||
let allPosts;
|
||||
let postPromise = axios
|
||||
.get("/getallPosts")
|
||||
.then(res => {
|
||||
// console.log(res.data);
|
||||
// this.setState({
|
||||
// posts: res.data
|
||||
// });
|
||||
allPosts = res.data;
|
||||
// console.log(allPosts)
|
||||
return axios.get("/getAlert")
|
||||
})
|
||||
.then((res) => {
|
||||
// console.log(res.data)
|
||||
// res.data.forEach((adminAlert) => {
|
||||
// allPosts.push(adminAlert);
|
||||
// })
|
||||
this.setState({
|
||||
posts: allPosts
|
||||
posts: res.data
|
||||
});
|
||||
})
|
||||
|
||||
.catch(err => console.log(err));
|
||||
|
||||
Promise.all([userPromise, postPromise])
|
||||
.then(() => {
|
||||
this.setState({
|
||||
loading: false
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
});
|
||||
})
|
||||
|
||||
this.props.getLikes();
|
||||
}
|
||||
@@ -100,21 +81,7 @@ class Home extends Component {
|
||||
});
|
||||
}
|
||||
|
||||
flagPost = (event) => {
|
||||
// Flags a post
|
||||
let postId = event.target.dataset.key ? event.target.dataset.key : event.target.parentNode.dataset.key;
|
||||
console.log(postId);
|
||||
axios.post(`/hidePost`, {postId})
|
||||
.then((res) => {
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
// event.preventDefault();
|
||||
}
|
||||
|
||||
handleClickLikeButton = (event) => {
|
||||
handleClickLikeButton = event => {
|
||||
// Need the ternary if statement because the user can click on the text or body of the
|
||||
// Button and they are two different html elements
|
||||
let postId = event.target.dataset.key
|
||||
@@ -147,15 +114,12 @@ class Home extends Component {
|
||||
let authenticated = this.props.user.authenticated;
|
||||
let { classes } = this.props;
|
||||
let username = this.props.user.credentials.handle;
|
||||
console.log(username);
|
||||
var hiddenBool = true;
|
||||
if (username === "Admin") {
|
||||
hiddenBool = false;
|
||||
}
|
||||
console.log(this.state.following);
|
||||
|
||||
console.log(hiddenBool);
|
||||
let postMarkup = this.state.posts ? ( this.state.following === undefined || this.state.following === null ? <Typography>You aren't following anybody right now</Typography> :
|
||||
this.state.posts.map(post => !post.hidden && this.state.following && (this.state.following.includes(post.userHandle) || post.userHandle === "Admin") ? (
|
||||
let postMarkup = this.state.posts ? (
|
||||
this.state.posts.map(post =>
|
||||
this.state.following ? (
|
||||
this.state.following.includes(post.userHandle) ? (
|
||||
<Card className={classes.card} key={post.postId}>
|
||||
<CardContent>
|
||||
<Typography>
|
||||
@@ -163,33 +127,38 @@ class Home extends Component {
|
||||
this.state.imageUrl ? (<img src={this.state.imageUrl} height="50" width="50" />) :
|
||||
(<img src={noImage} height="50" width="50"/>)
|
||||
} */}
|
||||
{
|
||||
post.profileImage ? (<img src={post.profileImage} height="50" width="50" />) :
|
||||
(<img src={noImage} height="50" width="50"/>)
|
||||
}
|
||||
{post.profileImage ? (
|
||||
<img src={post.profileImage} height="50" width="50" />
|
||||
) : (
|
||||
<img src={noImage} height="50" width="50" />
|
||||
)}
|
||||
</Typography>
|
||||
<Typography variant="h5">
|
||||
<b>{post.userHandle}</b>
|
||||
</Typography>
|
||||
<Typography variant="body2" color={"textSecondary"}>
|
||||
{this.formatDate(post.createdAt)}
|
||||
</Typography>
|
||||
<Typography variant="h5"><b>{post.userHandle}</b></Typography>
|
||||
<Typography variant="body2" color={"textSecondary"}>{this.formatDate(post.createdAt)}</Typography>
|
||||
<br />
|
||||
<Typography variant="body1"><b>{post.microBlogTitle}</b></Typography>
|
||||
<Typography variant="body1">
|
||||
<b>{post.microBlogTitle}</b>
|
||||
</Typography>
|
||||
<Typography variant="body2">{post.quoteBody}</Typography>
|
||||
<br />
|
||||
<Typography variant="body2">{post.body}</Typography>
|
||||
<br />
|
||||
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics.join(", ")}</Typography>
|
||||
<Typography variant="body2">
|
||||
<b>Topics:</b> {post.microBlogTopics}
|
||||
</Typography>
|
||||
<br />
|
||||
{!hiddenBool &&
|
||||
<Button
|
||||
onClick={this.flagPost}
|
||||
data-key={post.postId}
|
||||
variant = "contained"
|
||||
color = "primary"
|
||||
<Typography
|
||||
id={post.postId}
|
||||
data-likes={post.likeCount}
|
||||
variant="body2"
|
||||
color={"textSecondary"}
|
||||
>
|
||||
Hide Post
|
||||
</Button>
|
||||
}
|
||||
|
||||
<Typography id={post.postId} data-likes={post.likeCount} variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography>
|
||||
Likes {post.likeCount}
|
||||
</Typography>
|
||||
{/* <Like microBlog = {post.postId} count = {post.likeCount} name = {username}></Like> */}
|
||||
<Button
|
||||
onClick={this.handleClickLikeButton}
|
||||
@@ -197,32 +166,30 @@ class Home extends Component {
|
||||
disabled={loading}
|
||||
variant="outlined"
|
||||
color="primary"
|
||||
>{
|
||||
this.state.likes && this.state.likes.includes(post.postId) ? 'Unlike' : 'Like'
|
||||
}</Button>
|
||||
<Quote microblog = {post.postId}></Quote>
|
||||
>
|
||||
{this.state.likes && this.state.likes.includes(post.postId)
|
||||
? "Unlike"
|
||||
: "Like"}
|
||||
</Button>
|
||||
<Quote microblog={post.postId}></Quote>
|
||||
|
||||
{/* <button>Quote</button> */}
|
||||
|
||||
{/* <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount} Comments {post.commentCount}</Typography> */}
|
||||
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : (
|
||||
<p></p>
|
||||
)
|
||||
) : (
|
||||
<p>Loading</p>
|
||||
)
|
||||
)
|
||||
) : (
|
||||
<p>Loading post...</p>
|
||||
);
|
||||
|
||||
return authenticated ? (
|
||||
this.state.loading ? (
|
||||
<CircularProgress
|
||||
size={60}
|
||||
style={{ marginTop: "300px" }}
|
||||
></CircularProgress>
|
||||
) : (
|
||||
return (
|
||||
authenticated ? (
|
||||
this.state.loading ? (<CircularProgress size={60} style={{marginTop: "300px"}}></CircularProgress>) :
|
||||
<Grid container>
|
||||
<Grid item sm={4} xs={8}>
|
||||
<Writing_Microblogs />
|
||||
@@ -231,21 +198,16 @@ class Home extends Component {
|
||||
{postMarkup}
|
||||
</Grid>
|
||||
</Grid>
|
||||
)
|
||||
) : loading ? (
|
||||
<CircularProgress
|
||||
size={60}
|
||||
style={{ marginTop: "300px" }}
|
||||
></CircularProgress>
|
||||
) : (
|
||||
) : loading ?
|
||||
(<CircularProgress size={60} style={{marginTop: "300px"}}></CircularProgress>)
|
||||
:
|
||||
(
|
||||
<div>
|
||||
<div>
|
||||
<img src={logo} className="app-logo" alt="logo" />
|
||||
<br />
|
||||
<br />
|
||||
<br/><br/>
|
||||
<b>Welcome to Twistter!</b>
|
||||
<br />
|
||||
<br />
|
||||
<br/><br/>
|
||||
<b>See the most interesting topics people are following right now.</b>
|
||||
</div>
|
||||
|
||||
@@ -267,6 +229,7 @@ class Home extends Component {
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -449,7 +412,7 @@ class Like extends Component {
|
||||
});
|
||||
}
|
||||
|
||||
handleClick(){
|
||||
handleClick() {
|
||||
this.setState({
|
||||
like: !this.state.like
|
||||
});
|
||||
|
||||
@@ -83,10 +83,6 @@ const styles = {
|
||||
wordBreak: "break-all",
|
||||
color: 'black'
|
||||
},
|
||||
dmRecentMessageDisabled: {
|
||||
wordBreak: "break-all",
|
||||
color: 'red'
|
||||
},
|
||||
dmListItemContainer: {
|
||||
height: 100
|
||||
},
|
||||
@@ -430,19 +426,13 @@ export class directMessages extends Component {
|
||||
<Typography
|
||||
className={
|
||||
this.state.selectedChannel && this.state.selectedChannel.dmId === channel.dmId ? (
|
||||
channel.hasDirectMessagesEnabled ?
|
||||
classes.dmRecentMessageSelected
|
||||
:
|
||||
classes.dmRecentMessageDisabled
|
||||
) : (
|
||||
channel.hasDirectMessagesEnabled ?
|
||||
classes.dmRecentMessageUnselected
|
||||
:
|
||||
classes.dmRecentMessageDisabled
|
||||
)
|
||||
}
|
||||
>
|
||||
{!channel.hasDirectMessagesEnabled ? "This user has DMs disabled" :
|
||||
{
|
||||
!channel.recentMessage ?
|
||||
'No messages'
|
||||
:
|
||||
@@ -558,8 +548,8 @@ export class directMessages extends Component {
|
||||
>
|
||||
Create
|
||||
{creatingDirectMessage &&
|
||||
// Won't accept classes style for some reason
|
||||
<CircularProgress size={30} style={{position: "absolute"}}/>
|
||||
// Won't accept classes style for some reason
|
||||
}
|
||||
</Button>
|
||||
</Grid>
|
||||
@@ -607,16 +597,7 @@ export class directMessages extends Component {
|
||||
multiline
|
||||
rows={2}
|
||||
margin="dense"
|
||||
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]
|
||||
:
|
||||
""
|
||||
}
|
||||
value={this.state.drafts[this.state.selectedChannel.dmId] ? this.state.drafts[this.state.selectedChannel.dmId] : ""}
|
||||
onChange={this.handleChangeMessage}
|
||||
/>
|
||||
<Fab
|
||||
|
||||
@@ -79,7 +79,6 @@ class user extends Component {
|
||||
following: null,
|
||||
posts: null,
|
||||
myTopics: null,
|
||||
followingList: null,
|
||||
loading: false
|
||||
};
|
||||
}
|
||||
@@ -93,8 +92,7 @@ class user extends Component {
|
||||
.then(res => {
|
||||
console.log("removed sub");
|
||||
this.setState({
|
||||
following: false,
|
||||
myTopics: []
|
||||
following: false
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
@@ -117,26 +115,8 @@ class user extends Component {
|
||||
}
|
||||
};
|
||||
|
||||
handleAdd = newTopic => {
|
||||
axios
|
||||
.post("/putNewTopic", {
|
||||
handle: this.state.profile,
|
||||
topic: newTopic
|
||||
})
|
||||
.then(() => {
|
||||
let temp = this.state.myTopics;
|
||||
temp.push(newTopic);
|
||||
this.setState({
|
||||
myTopics: temp
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
console.err(err);
|
||||
});
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
this.setState({ loading: true });
|
||||
this.setState({loading: true});
|
||||
let otherUserPromise = axios
|
||||
.post("/getUserDetails", {
|
||||
handle: this.state.profile
|
||||
@@ -152,18 +132,11 @@ class user extends Component {
|
||||
let userPromise = axios
|
||||
.get("/user")
|
||||
.then(res => {
|
||||
let list = [];
|
||||
let fol = false;
|
||||
res.data.credentials.following.forEach(follow => {
|
||||
// console.log(follow);
|
||||
if (this.state.profile === follow.handle) {
|
||||
fol = true;
|
||||
list = follow.topics;
|
||||
}
|
||||
});
|
||||
this.setState({
|
||||
following: fol,
|
||||
myTopics: list
|
||||
following: res.data.credentials.following.includes(
|
||||
this.state.profile
|
||||
),
|
||||
myTopics: res.data.credentials.followedTopics
|
||||
});
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
@@ -180,43 +153,13 @@ class user extends Component {
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
|
||||
// Only add Admin posts if this is not the Admin account
|
||||
let alertPromise;
|
||||
if (this.state.profile !== "Admin") {
|
||||
alertPromise = axios
|
||||
.get("/getAlert")
|
||||
.then(res => {
|
||||
let temp = this.state.posts;
|
||||
// console.log(res.data);
|
||||
res.data.forEach(element => {
|
||||
element ? temp.push(element) : console.err;
|
||||
});
|
||||
// temp.push(res.data[0]);
|
||||
this.setState({
|
||||
posts: temp
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err);
|
||||
});
|
||||
} else {
|
||||
alertPromise = new Promise((resolve, reject) => {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
Promise.all([otherUserPromise, userPromise, posts, alertPromise])
|
||||
Promise.all([otherUserPromise, userPromise, posts])
|
||||
.then(() => {
|
||||
this.setState({ loading: false });
|
||||
this.setState({loading: false});
|
||||
})
|
||||
.catch(error => {
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
});
|
||||
}
|
||||
|
||||
formatDate(dateString) {
|
||||
let newDate = new Date(Date.parse(dateString));
|
||||
return newDate.toDateString();
|
||||
})
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -245,8 +188,8 @@ class user extends Component {
|
||||
<p>loading username...</p>
|
||||
);
|
||||
|
||||
// console.log(this.state.topics);
|
||||
// console.log(this.state.myTopics);
|
||||
console.log(this.state.topics);
|
||||
console.log(this.state.myTopics);
|
||||
let topicsMarkup = this.state.topics ? (
|
||||
this.state.topics.map(
|
||||
topic =>
|
||||
@@ -254,20 +197,16 @@ class user extends Component {
|
||||
this.state.myTopics.includes(topic) ? (
|
||||
<MyChip
|
||||
label={topic}
|
||||
key={{ topic }.id}
|
||||
key={{ topic }.topic.id}
|
||||
onDelete
|
||||
deleteIcon={<DoneIcon />}
|
||||
/>
|
||||
) : this.state.following ? (
|
||||
) : (
|
||||
<MyChip
|
||||
label={topic}
|
||||
key={{ topic }.id}
|
||||
key={{ topic }.topic.id}
|
||||
color="secondary"
|
||||
clickable
|
||||
onClick={key => this.handleAdd(topic)}
|
||||
/>
|
||||
) : (
|
||||
<MyChip label={topic} key={{ topic }.id} color="secondary" />
|
||||
)
|
||||
) : (
|
||||
<p></p>
|
||||
@@ -283,10 +222,10 @@ class user extends Component {
|
||||
) : (
|
||||
<img src={noImage} height="150" width="150" />
|
||||
);
|
||||
//(this.state.posts);
|
||||
|
||||
let postMarkup = this.state.posts ? (
|
||||
this.state.posts.map(post => (
|
||||
<Card className={classes.card} key={post.postId} data-key={post.postId}>
|
||||
<Card className={classes.card}>
|
||||
<CardContent>
|
||||
<Typography>
|
||||
{this.state.imageUrl ? (
|
||||
@@ -295,11 +234,11 @@ class user extends Component {
|
||||
<img src={noImage} height="50" width="50" />
|
||||
)}
|
||||
</Typography>
|
||||
<Typography variant="h4">
|
||||
<Typography variant="h7">
|
||||
<b>{post.userHandle}</b>
|
||||
</Typography>
|
||||
<Typography variant="body2" color={"textSecondary"}>
|
||||
{this.formatDate(post.createdAt)}
|
||||
{post.createdAt}
|
||||
</Typography>
|
||||
|
||||
<br />
|
||||
@@ -312,7 +251,7 @@ class user extends Component {
|
||||
<Typography variant="body2">{post.body}</Typography>
|
||||
<br />
|
||||
<Typography variant="body2">
|
||||
<b>Topics:</b> {post.microBlogTopics.join(", ")}
|
||||
<b>Topics:</b> {post.microBlogTopics}
|
||||
</Typography>
|
||||
<br />
|
||||
<Typography variant="body2" color={"textSecondary"}>
|
||||
@@ -325,13 +264,9 @@ class user extends Component {
|
||||
<p>Posts</p>
|
||||
);
|
||||
|
||||
return this.state.loading ? (
|
||||
<CircularProgress
|
||||
size={60}
|
||||
style={{ marginTop: "300px" }}
|
||||
></CircularProgress>
|
||||
) : (
|
||||
<Grid container spacing={10}>
|
||||
return (
|
||||
this.state.loading ? <CircularProgress size={60} style={{marginTop: "300px"}}></CircularProgress> :
|
||||
<Grid container spacing={24}>
|
||||
<Grid item sm={4} xs={8}>
|
||||
{imageMarkup}
|
||||
{profileMarkup}
|
||||
|
||||
@@ -269,15 +269,6 @@ class user extends Component {
|
||||
</Link>
|
||||
) : null;
|
||||
|
||||
let verifyButtonMarkup = this.state.profile === "Admin" ?
|
||||
<Link to="/verify">
|
||||
<Button className={classes.button} variant="outlined" color="primary">
|
||||
Verify Users
|
||||
</Button>
|
||||
</Link>
|
||||
:
|
||||
null
|
||||
|
||||
return (
|
||||
this.state.loading ? <CircularProgress size={60} style={{marginTop: "300px"}}></CircularProgress> :
|
||||
<div>
|
||||
@@ -287,7 +278,6 @@ class user extends Component {
|
||||
<Grid container>
|
||||
<Grid item sm>
|
||||
{editButtonMarkup}
|
||||
{verifyButtonMarkup}
|
||||
</Grid>
|
||||
<Grid item sm>
|
||||
{/* <Grid container direction="column"> */}
|
||||
|
||||
@@ -142,6 +142,5 @@ export const sendDirectMessage = (user, message) => (dispatch) => {
|
||||
sendDirectMessage: err.response.data
|
||||
}
|
||||
})
|
||||
dispatch({type: SET_NOT_LOADING_UI_4});
|
||||
})
|
||||
}
|
||||
@@ -36,7 +36,6 @@ export const getUserData = () => (dispatch) => {
|
||||
|
||||
// Sends login data to firebase and sets the user data in Redux
|
||||
export const loginUser = (loginData, history) => (dispatch) => {
|
||||
dispatch({type: CLEAR_ERRORS});
|
||||
dispatch({ type: LOADING_UI });
|
||||
axios
|
||||
.post("/login", loginData)
|
||||
@@ -58,7 +57,6 @@ export const loginUser = (loginData, history) => (dispatch) => {
|
||||
|
||||
// Sends signup data to firebase and sets the user data in Redux
|
||||
export const signupUser = (newUserData, history) => (dispatch) => {
|
||||
dispatch({type: CLEAR_ERRORS});
|
||||
dispatch({ type: LOADING_UI });
|
||||
axios
|
||||
.post("/signup", newUserData)
|
||||
|
||||
Reference in New Issue
Block a user