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.

269 lines
12 KiB

7 years ago
  1. var express = require('express');
  2. var app = express();
  3. var publicationModel = require('../models/publicationModel');
  4. var userModel = require('../models/userModel');
  5. //var trainerModel = require('../models/trainerModel');
  6. var crypto = require('crypto');
  7. /**GET '/publications' **/
  8. exports.getAllPublications = function (req, res) {
  9. publicationModel.find()
  10. .limit(Number(req.query.pageSize))
  11. .skip(Number(req.query.pageSize)*Number(req.query.page))
  12. .lean()
  13. .populate('user', 'name avatar')
  14. .exec(function (err, publications) {
  15. if (err) return res.send(500, err.message);
  16. if (!publications) {
  17. //
  18. } else if (publications) {
  19. res.status(200).jsonp(publications);
  20. }
  21. });
  22. };
  23. /** GET '/publications/getById/:publicationid' **/
  24. exports.getPublicationById = function (req, res) {
  25. publicationModel.findOne({_id: req.params.publicationid})
  26. .lean()
  27. .populate('likes', 'name avatar')
  28. .exec(function (err, publication) {
  29. if (err) return res.send(500, err.message);
  30. if (!publication) {
  31. res.json({success: false, message: 'publication not found.'});
  32. } else if (publication) {
  33. res.status(200).jsonp(publication);
  34. }
  35. });
  36. };
  37. /**POST '/publications' **/
  38. exports.postPublication = function (req, res) {
  39. userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) {
  40. if (err) return res.send(500, err.message);
  41. if (!user) {
  42. res.json({success: false, message: 'user not found.'});
  43. } else if (user) {
  44. //aquí ja hem agafat el user a partir del seu token
  45. var publication = new publicationModel({
  46. title: req.body.title,
  47. content: req.body.content,
  48. date: new Date(),
  49. user: user._id,
  50. photo: req.body.photo
  51. });
  52. //fins aquí tenim la variable publication amb els continguts
  53. //ara cal 1r guardar el model publication a la base de dades
  54. publication.save(function (err, publication) {
  55. if (err) return res.send(500, err.message);
  56. //i 2n, afegir la id de la publicació generada al user.publications
  57. user.publications.push(publication._id);
  58. /* gamification */
  59. var reward = {
  60. concept: "added new publication to Timeline",
  61. date: Date(),
  62. value: +1
  63. };
  64. user.points.history.push(reward);
  65. user.points.total = user.points.total + 1;
  66. /* end of gamification */
  67. user.save(function (err, user) {
  68. if (err) return res.send(500, err.message);
  69. res.status(200).jsonp(user);
  70. });
  71. });
  72. }//end else if
  73. });
  74. };
  75. /**GET '/users/:userid/publications' **/
  76. exports.getUserPublicationsByUserId = function (req, res) {
  77. userModel.findOne({_id: req.params.userid}, function (err, user) {
  78. if (err) return res.send(500, err.message);
  79. }).populate('publications')
  80. .exec(function (error, user) {
  81. if (error !== null) res.send(500, error.message);
  82. console.log(JSON.stringify(user, null, "\t"));
  83. res.status(200).jsonp(user.publications);
  84. });
  85. };
  86. /**DELETE '/publications/:publicationid' **/
  87. exports.deletePublicationById = function (req, res) {
  88. userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) {
  89. if (err) return res.send(500, err.message);
  90. if (!user) {
  91. res.json({success: false, message: 'user not found.'});
  92. } else if (user) {
  93. for (var i = 0; i < user.publications.length; i++) {
  94. if (user.publications[i].equals(req.params.publicationid)) {//només si el user és qui ha fet la publication la pot esborrar
  95. user.publications.splice(i, 1);
  96. user.save(function (err, user) {//guardem l'user
  97. if (err) return res.send(500, err.message);
  98. publicationModel.findByIdAndRemove({_id: req.params.publicationid}, function (err) {
  99. if (err !== null) return res.send(500, err.message);
  100. res.status(200).jsonp('Deleted');
  101. });
  102. });
  103. }
  104. }
  105. }
  106. });
  107. };
  108. /** POST '/publications/:publicationid/like' **/
  109. exports.likePublication = function (req, res) {
  110. userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) {
  111. if (err) return res.send(500, err.message);
  112. if (!user) {
  113. res.json({success: false, message: 'user not found.'});
  114. } else if (user) {
  115. console.log(user.name);
  116. //ara busquem el userB
  117. publicationModel.findOne({_id: req.params.publicationid}, function (err, publication) {
  118. if (err) return res.send(500, err.message);
  119. if (!publication) {
  120. res.json({success: false, message: 'publication not found.'});
  121. } else if (publication) {
  122. // for(var i=0; i<userB.timeline)
  123. publication.likes.push(user._id);
  124. publication.save(function (err, publication) {
  125. if (err) return res.send(500, err.message);
  126. /* gamification */
  127. var reward = {
  128. concept: "liked publication " + publication.title,
  129. date: Date(),
  130. value: +1
  131. };
  132. user.points.history.push(reward);
  133. user.points.total = user.points.total + 1;
  134. /* end of gamification */
  135. user.save(function (err, user) {
  136. if (err) return res.send(500, err.message);
  137. //ara busquem el user que ha fet la publication que ha rebut el like
  138. userModel.findOne({_id: publication.user})
  139. .exec(function (err, userB) {
  140. /*notification*/
  141. var notification = {
  142. state: "pendent",
  143. message: "user clicked like",
  144. link: "user/"+userB._id,
  145. icon: "newlike.png",
  146. date: Date()
  147. };
  148. userB.notifications.push(notification);
  149. /* end of notification*/
  150. userB.save(function (err, user) {
  151. if (err) return res.send(500, err.message);
  152. publicationModel.findOne({_id: req.params.publicationid})
  153. .lean()
  154. .populate('user', 'name avatar')
  155. .exec(function (err, publication) {
  156. if (err) return res.send(500, err.message);
  157. if (!publication) {
  158. //
  159. } else if (publication) {
  160. res.status(200).jsonp(publication);
  161. }
  162. });
  163. });
  164. });
  165. });
  166. });
  167. }//end else if
  168. });
  169. }//end else if
  170. });
  171. };
  172. /** POST '/publications/:publicationid/dislike' **/
  173. exports.dislikePublication = function (req, res) {
  174. userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) {
  175. if (err) return res.send(500, err.message);
  176. if (!user) {
  177. res.json({success: false, message: 'user not found.'});
  178. } else if (user) {
  179. console.log(user.name);
  180. //ara busquem el userB
  181. publicationModel.findOne({_id: req.params.publicationid}, function (err, publication) {
  182. if (err) return res.send(500, err.message);
  183. if (!publication) {
  184. res.json({success: false, message: 'publication not found.'});
  185. } else if (publication) {
  186. for (var i = 0; i < publication.likes.length; i++) {
  187. if (publication.likes[i].equals(user._id)) {
  188. publication.likes.splice(i, 1);
  189. }
  190. }
  191. publication.save(function (err, publication) {
  192. if (err) return res.send(500, err.message);
  193. /* gamification */
  194. var reward = {
  195. concept: "disliked publication " + publication.title,
  196. date: Date(),
  197. value: -1
  198. };
  199. user.points.history.push(reward);
  200. user.points.total = user.points.total - 1;
  201. /* end of gamification */
  202. user.save(function (err, user) {
  203. if (err) return res.send(500, err.message);
  204. publicationModel.findOne({_id: req.params.publicationid})
  205. .lean()
  206. .populate('user', 'name avatar')
  207. .exec(function (err, publication) {
  208. if (err) return res.send(500, err.message);
  209. if (!publication) {
  210. //
  211. } else if (publication) {
  212. res.status(200).jsonp(publication);
  213. }
  214. });
  215. });
  216. });
  217. }//end else if
  218. });
  219. }//end else if
  220. });
  221. };
  222. /** GET '/publications/newsfeed' **/
  223. var ObjectId = require('mongodb').ObjectID;
  224. exports.getNewsFeed = function (req, res) {//getPublicationsFromFollowingUsers
  225. //primer agafem l'user que fa la petició, per saber quins users està seguint
  226. var newsfeed = [];
  227. userModel.findOne({'tokens.token': req.headers['x-access-token']})
  228. .exec(function (err, user) {
  229. if (err) return res.send(500, err.message);
  230. if (!user) {
  231. res.json({success: false, message: 'getting newsfeed failed. user not found.'});
  232. } else if (user) {
  233. console.log("getting newsfeed for user: " + user.name);
  234. var following = [];
  235. for (var i = 0; i < user.following.length; i++) {//això ho fem perquè necessitem la array amb el contingut en format objectid
  236. following.push(new ObjectId(user.following[i]));
  237. }
  238. following.push(new ObjectId(user._id));//així també reb les seves pròpies publicacions
  239. publicationModel.find({user: {$in: following}})
  240. .lean()
  241. .populate('user', 'name avatar')
  242. .exec(function (err, publications) {
  243. if (err) return res.send(500, err.message);
  244. if (!publications) {
  245. //
  246. } else if (publications) {
  247. res.status(200).jsonp(publications);
  248. }
  249. });
  250. }
  251. });
  252. };