Skip to content
Open
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
100 changes: 57 additions & 43 deletions server/src/controllers/post.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,25 @@ const getTopPosts = (daysAgo, maxResults) => async (req, res) => {
{ $unwind: "$author" },
{
$project: {
_id: 1, content: 1, image: 1, intent: 1, likes: 1,
commentsCount: 1, sharesCount: 1, likesCount: 1,
createdAt: 1, updatedAt: 1, poll: 1, isEdited: 1, editedAt: 1,
"author._id": 1, "author.username": 1, "author.name": 1,
"author.surname": 1, "author.avatar": 1,
},
_id: 1,
content: 1,
image: 1,
intent: 1,
likes: 1,
commentsCount: 1,
sharesCount: 1,
likesCount: 1,
createdAt: 1,
updatedAt: 1,
poll: 1,
isEdited: 1,
editedAt: 1,
"author._id": 1,
"author.username": 1,
"author.name": 1,
"author.surname": 1,
"author.avatar": 1,
},
}
]);

Expand Down Expand Up @@ -234,49 +247,50 @@ if (!intent || (!content && !req.file && !hasPoll)) {
let poll = undefined;

if (pollQuestion && pollOptions) {
const parsedOptions =
typeof pollOptions === "string"
? JSON.parse(pollOptions)
: pollOptions;

if (!Array.isArray(parsedOptions)) {
return res.status(400).json({
success: false,
message: "Poll options must be an array"
});
}
const parsedOptions =
typeof pollOptions === "string"
? JSON.parse(pollOptions)
: pollOptions;

const cleanedOptions = parsedOptions
.map(option => option?.trim())
.filter(Boolean);
if (!Array.isArray(parsedOptions)) {
return res.status(400).json({
success: false,
message: "Poll options must be an array"
});
}

if (cleanedOptions.length < 2) {
return res.status(400).json({
success: false,
message: "Poll requires at least 2 options"
});
}
const cleanedOptions = parsedOptions
.map(option => option?.trim())
.filter(Boolean);

if (
pollExpiresAt &&
new Date(pollExpiresAt) <= new Date()
) {
return res.status(400).json({
success: false,
message: "Poll expiry must be in the future"
});
}
if (cleanedOptions.length < 2) {
return res.status(400).json({
success: false,
message: "Poll requires at least 2 options"
});
}

poll = {
question: pollQuestion.trim(),
options: cleanedOptions.map(option => ({
text: option,
voters: []
})),
expiresAt: pollExpiresAt || null
};
if (
pollExpiresAt &&
new Date(pollExpiresAt) <= new Date()
) {
return res.status(400).json({
success: false,
message: "Poll expiry must be in the future"
});
}

poll = {
question: pollQuestion.trim(),
options: cleanedOptions.map(option => ({
text: option,
voters: []
})),
expiresAt: pollExpiresAt || null
};
}


const post = await Post.create({
author: req.user.id,
authorIsPrivate: req.user.isPrivate || false,
Expand Down
8 changes: 4 additions & 4 deletions server/src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,10 @@ export const getSuggestedUsers = asyncHandler(async (req, res) => {

export const searchUsers = asyncHandler(async (req, res) => {
const {
query,
cursor,
sortBy
} = req.query;
query,
cursor,
sortBy
} = req.query;

if (!query) {
return res.json({
Expand Down