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.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/: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;