Free and Open-source video streaming suite. https://miracle-tv.live
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

70 lines
2.2 KiB

  1. import { Response, Router } from 'express';
  2. import { Request } from 'server/lib/http/interfaces';
  3. import Activities from 'server/db/Activities';
  4. import {
  5. sendData,
  6. dataOrNotFound,
  7. sendCreated,
  8. sendUnauthorized,
  9. sendNoContent,
  10. } from 'server/lib/http/response';
  11. import { getIdFromParams } from 'server/lib/http/request';
  12. import { isAdmin, isMod } from 'server/lib/crypto';
  13. import { authenticateUser, acceptsSchema } from 'server/lib/http/middleware';
  14. import createActivityJson from 'miracle-tv-shared/src/types/api/v1/schemas/activity/createActivity.json';
  15. function getList (_: any, res: Response) {
  16. Activities.getActivities().then(sendData(res));
  17. }
  18. function getId (req: Request, res: Response) {
  19. const id = getIdFromParams(req);
  20. Activities.getActivity(id)
  21. .then(dataOrNotFound(res, { id }));
  22. }
  23. function create (req: Request, res: Response) {
  24. const activity = {
  25. ...req.body,
  26. created_by_id: req.session.user.id,
  27. };
  28. Activities.createActivity(activity).then(sendCreated(res));
  29. }
  30. function update (req: Request, res: Response) {
  31. const id = getIdFromParams(req);
  32. const currentUser = req.session.user;
  33. Activities.getActivityRaw(id).then((activity) => {
  34. console.log(id, activity);
  35. if (activity.created_by_id === currentUser.id || isMod(currentUser) || isAdmin(currentUser)) {
  36. Activities.updateActivity(id, { ...activity, ...req.body }).then(sendData(res));
  37. } else {
  38. sendUnauthorized(res)({ id: currentUser.id });
  39. }
  40. });
  41. }
  42. function del (req: Request, res: Response) {
  43. const id = getIdFromParams(req);
  44. const currentUser = req.session.user;
  45. Activities.getActivity(id).then((activity) => {
  46. if (activity.created_by_id === currentUser.id || isMod(currentUser) || isAdmin(currentUser)) {
  47. Activities.deleteActivity(id).then(() => sendNoContent(res));
  48. } else {
  49. sendUnauthorized(res)({ id: currentUser.id });
  50. }
  51. });
  52. }
  53. const router = Router();
  54. router.get('/', getList);
  55. router.post('/', authenticateUser, acceptsSchema(createActivityJson), create);
  56. router.get('/:id', getId);
  57. router.put('/:id', authenticateUser, acceptsSchema(createActivityJson), update);
  58. router.delete('/:id', authenticateUser, del);
  59. export default router;