Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Following and followers #14

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions public/js/followersAndFollowing.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ function outputUsers(results, container) {
function createUserHtml(userData, showFollowButton) {

const name = userData.firstName + " " + userData.lastName;
const isFollowing = (userLoggedIn.following && userLoggedIn.following.includes(userData._id))
let followButton = ""

const text = isFollowing ? "Подписан" : "Подписаться"
const buttonClass = isFollowing ? "followButton following" : "followButton"


if (showFollowButton && userLoggedIn._id != userData._id) {
followButton = `<div class='followButtonContainer'>
<button class='${buttonClass}' data-user='${userData._id}'>${text}</button>
</div>`
}

return `<div class='user'>
<div class='userImageContainer'>
Expand All @@ -47,5 +59,6 @@ function createUserHtml(userData, showFollowButton) {
<span class='username'>@${userData.username}</span>
</div>
</div>
${followButton}
</div>`;
}
2 changes: 1 addition & 1 deletion public/js/home.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$(document).ready(() => {
$.get("/api/posts", results => {
$.get("/api/posts", { followingOnly: true }, results => {
outputPosts(results, $(".postsContainer"))
})
})
Expand Down
23 changes: 22 additions & 1 deletion routes/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,33 @@ router.get("/", async (req, res, next) => {

const searchObj = req.query;

if (searchObj.isReply !== undefined) {
if(searchObj.isReply !== undefined) {
const isReply = searchObj.isReply == "true";
searchObj.replyTo = { $exists: isReply };
delete searchObj.isReply;
}

if(searchObj.followingOnly !== undefined) {
const followingOnly = searchObj.followingOnly == "true";

if(followingOnly) {
let objectIds = [];

if(!req.session.user.following) {
req.session.user.following = [];
}

req.session.user.following.forEach(user => {
objectIds.push(user);
})

objectIds.push(req.session.user._id);
searchObj.postedBy = { $in: objectIds };
}

delete searchObj.followingOnly;
}

const results = await getPosts(searchObj);

res.status(200).send(results)
Expand Down