website/backend/routes/swit.js
2024-02-08 09:07:13 -05:00

235 lines
7.2 KiB
JavaScript

const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');
const Swit = require('../models/Swit');
const User = require('../models/User');
const Repost = require('../models/Repost');
// Verify the JWT in the Authorization header
function authenticate(req, res, next) {
const token = req.header('Authorization');
if (!token) return res.sendStatus(401);
jwt.verify(token, 'SECRET_KEY', (err, decoded) => {
if (err) return res.sendStatus(401);
req.userId = decoded.userId;
next();
});
}
router.get('/search', authenticate, async (req, res) => {
const query = req.query.q;
console.log(query)
const swits = await Swit.find({ text: { $regex: query, $options: 'i' } });
console.log(swits)
res.json(swits);
});
router.post('/swits', authenticate, async (req, res) => {
const { text } = req.body;
const user = await User.findById(req.userId);
const swit = new Swit({ text, user: req.userId, username: user.username });
await swit.save();
res.sendStatus(201);
console.log("swit success")
});
router.get('/swits', authenticate, async (req, res) => {
const swit = await Swit.find();
res.send(swit.reverse());
console.log("get swit success")
});
router.get('/swits/following', authenticate, async (req, res) => {
const me = await User.findById(req.userId);
const swits = await Swit.find({ user: { $in: me.following } });
res.json(swits.reverse());
});
router.get('/swits/user/:id', authenticate, async (req, res) => {
const { id } = req.params;
const swits = await Swit.find({ user: id });
console.log(swits)
res.json(swits.reverse());
});
router.post('/swits/:id/like', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.id);
if (!swit) return res.status(404).send('Swit not found');
if (swit.likes.includes(req.userId)) {
return res.status(400).json({ error: 'You have already liked this swit' });
}
if (!swit.likes.includes(req.userId)) {
swit.likes.push(req.userId);
await swit.save();
// if (socketId) req.app.io.to(socketId).emit('like', { userId: req.userId, switId: req.params.id }); // Emit the 'like' event to the user who posted the swit
}
res.sendStatus(200);
});
router.get('/swits/:id/likes', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.id).populate('likes');
if (!swit) return res.status(404).send('Swit not found');
res.json(swit.likes);
});
router.post('/swits/:id/unlike', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.id);
if (!swit) return res.status(404).send('Swit not found');
swit.likes = swit.likes.filter(id => id.toString() !== req.userId);
await swit.save();
res.sendStatus(200);
});
router.post('/swits/:id/comments', authenticate, async (req, res) => {
const { id } = req.params;
const { text } = req.body;
const swit = await Swit.findById(id);
if (!swit) return res.status(404).send('Swit not found');
const user = await User.findById(req.userId);
if (!user) return res.status(404).send('User not found');
const comment = { text, user: req.userId, username: user.username };
swit.comments.push(comment);
await swit.save();
// if (socketId) req.app.io.to(socketId).emit('comment', { userId: req.userId, switId: req.params.id }); // Emit the 'like' event to the user who posted the swit
res.sendStatus(200);
});
router.get('/swits/:id/comments', authenticate, async (req, res) => {
const { id } = req.params;
const swit = await Swit.findById(id);
if (!swit) return res.status(404).send('Swit not found');
res.send(swit.comments);
});
router.post('/swits/:switId/comments/:commentId/like', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.switId);
if (!swit) return res.status(404).send('Swit not found');
console.log(swit)
const comment = swit.comments.id(req.params.commentId);
if (!comment) return res.status(404).send('Comment not found');
if (comment.likes.includes(req.userId)) {
return res.status(400).json({ error: 'You have already liked this comment' });
}
comment.likes.push(req.userId);
await swit.save();
res.sendStatus(200);
});
router.post('/swits/:switId/comments/:commentId/unlike', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.switId);
if (!swit) return res.status(404).send('Swit not found');
const comment = swit.comments.id(req.params.commentId);
if (!comment) return res.status(404).send('Comment not found');
comment.likes = comment.likes.filter(id => id.toString() !== req.userId);
await swit.save();
res.sendStatus(200);
});
router.delete('/swits/:switId/comments/:commentId', authenticate, async (req, res) => {
const { switId, commentId } = req.params;
const swit = await Swit.findById(switId);
if (!swit) return res.status(404).send('Swit not found');
const commentIndex = swit.comments.findIndex(comment => comment._id.toString() === commentId);
if (commentIndex === -1) return res.status(404).send('Comment not found');
// Only allow the user who posted the comment to delete it
if (swit.comments[commentIndex].user.toString() !== req.userId) {
return res.status(403).send('You do not have permission to delete this comment');
}
swit.comments.splice(commentIndex, 1);
await swit.save();
res.sendStatus(204);
});
router.post('/swits/:id/repost', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.id);
if (!swit) return res.status(404).send('Swit not found');
// Check if the user has already reposted the swit
const existingRepost = await Repost.findOne({ user: req.userId, swit: req.params.id });
if (existingRepost) return res.status(400).json({ error: 'You have already reposted this swit' });
// Create a new Repost document
const repost = new Repost({ user: req.userId, swit: req.params.id });
console.log(repost)
await repost.save();
// Optionally, send a notification to the user who made the original swit
// Return the created repost
res.status(200).json(repost);
});
router.post('/swits/:id/unrepost', authenticate, async (req, res) => {
// Delete the Repost document
const result = await Repost.deleteOne({ user: req.userId, swit: req.params.id });
if (result.deletedCount === 0) return res.status(404).send('Repost not found');
res.status(200).json({message: 'Unreposted'});
});
router.put('/swits/:id', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.id);
if (swit.user.toString() !== req.userId) {
return res.status(403).send('You do not have permission to update this swit');
}
swit.text = req.body.text;
await swit.save();
res.send(swit);
});
router.get('/swits/:id', authenticate, async (req, res) => {
const swit = await Swit.findById(req.params.id);
if (!swit) return res.sendStatus(404);
res.send(swit);
});
router.delete('/swits/:id', authenticate, async (req, res) => {
const { id } = req.params;
// Check if the swit exists and belongs to the user
const swit = await Swit.findOne({ _id: id, user: req.userId });
if (!swit) return res.sendStatus(404);
// Delete the swit
await Swit.deleteOne({ _id: id });
res.sendStatus(204);
});
module.exports = router;