api_helpers.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. require('../models/db');
  3. var config = require('config');
  4. const redis = require('../helpers/redis');
  5. // FIXME TODO object.toJSON()
  6. var saveAction = (actionKey, object) => {
  7. if (object.constructor.modelName == "Space")
  8. return;
  9. let attr = {
  10. action: actionKey,
  11. space: object.space_id || object.space,
  12. user: object.user_id || object.user,
  13. editor_name: object.editor_name,
  14. object: object
  15. };
  16. /*let action = new Action(attr);
  17. action.save(function(err) {
  18. if (err)
  19. console.error("saved create action err:", err);
  20. });*/
  21. };
  22. module.exports = (req, res, next) => {
  23. res.header("Cache-Control", "no-cache");
  24. req['channelId'] = req.headers['x-spacedeck-channel'];
  25. req['spacePassword'] = req.headers['x-spacedeck-spacepassword'];
  26. req['spaceAuth'] = req.query['spaceAuth'] || req.headers['x-spacedeck-space-auth'];
  27. res['distributeCreate'] = function(model, object) {
  28. if (!object) return;
  29. redis.sendMessage("create", model, object, req.channelId);
  30. this.status(201).json(object);
  31. saveAction("create", object);
  32. };
  33. res['distributeUpdate'] = function(model, object) {
  34. if (!object) return;
  35. redis.sendMessage("update", model, object, req.channelId);
  36. this.status(200).json(object);
  37. saveAction("update", object);
  38. };
  39. res['distributeDelete'] = function(model, object) {
  40. if (!object) return;
  41. redis.sendMessage("delete", model, object, req.channelId);
  42. this.sendStatus(204);
  43. saveAction("delete", object);
  44. };
  45. next();
  46. }