spacedeck-open/routes/api/space_messages.js

96 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-04-07 01:29:05 +02:00
"use strict";
var config = require('config');
const db = require('../../models/db');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const uuidv4 = require('uuid/v4');
2017-04-07 01:29:05 +02:00
var redis = require('../../helpers/redis');
var mailer = require('../../helpers/mailer');
var uploader = require('../../helpers/uploader');
var space_render = require('../../helpers/space-render');
var phantom = require('../../helpers/phantom');
var async = require('async');
var fs = require('fs');
var _ = require("underscore");
var archiver = require('archiver');
var request = require('request');
var url = require("url");
var path = require("path");
var crypto = require('crypto');
var glob = require('glob');
var express = require('express');
var router = express.Router({mergeParams: true});
// JSON MAPPINGS
var userMapping = {
_id: 1,
nickname: 1,
email: 1,
avatar_thumb_uri: 1
};
var spaceMapping = {
_id: 1,
name: 1,
thumbnail_url: 1
};
var roleMapping = {
"none": 0,
"viewer": 1,
"editor": 2,
"admin": 3
}
// MESSAGES
router.get('/', function(req, res, next) {
db.Message.findAll({where:{
space_id: req.space._id
}, include: ['user']})
.then(function(messages) {
res.status(200).json(messages);
});
2017-04-07 01:29:05 +02:00
});
router.post('/', function(req, res, next) {
var attrs = req.body;
attrs.space_id = req.space._id;
2017-04-07 01:29:05 +02:00
if (req.user) {
attrs.user = req.user;
attrs.user_id = req.user._id;
2017-04-07 01:29:05 +02:00
} else {
attrs.user = null;
}
var msg = attrs;
msg._id = uuidv4();
2017-04-07 01:29:05 +02:00
db.Message.create(msg).then(function() {
if (msg.message.length <= 1) return;
// TODO reimplement notifications
res.distributeCreate("Message", msg);
2017-04-07 01:29:05 +02:00
});
});
router.delete('/:message_id', function(req, res, next) {
db.Message.findOne({where:{
2017-04-07 01:29:05 +02:00
"_id": req.params.message_id
}}).then(function(msg) {
2017-04-07 01:29:05 +02:00
if (!msg) {
res.sendStatus(404);
} else {
msg.destroy().then(function() {
res.distributeDelete("Message", msg);
2017-04-07 01:29:05 +02:00
});
}
});
});
module.exports = router;