2017-04-07 01:29:05 +02:00
|
|
|
"use strict";
|
|
|
|
var config = require('config');
|
2018-04-11 21:14:00 +02:00
|
|
|
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) {
|
2018-04-11 21:14:00 +02:00
|
|
|
db.Message.findAll({where:{
|
|
|
|
space_id: req.space._id
|
2018-04-12 16:38:48 +02:00
|
|
|
}, include: ['user']})
|
2018-04-11 21:14:00 +02:00
|
|
|
.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;
|
2018-04-11 21:14:00 +02:00
|
|
|
attrs.space_id = req.space._id;
|
2017-04-07 01:29:05 +02:00
|
|
|
|
|
|
|
if (req.user) {
|
|
|
|
attrs.user = req.user;
|
2018-04-12 16:38:48 +02:00
|
|
|
attrs.user_id = req.user._id;
|
2017-04-07 01:29:05 +02:00
|
|
|
} else {
|
|
|
|
attrs.user = null;
|
|
|
|
}
|
|
|
|
|
2018-04-11 21:14:00 +02:00
|
|
|
var msg = attrs;
|
|
|
|
msg._id = uuidv4();
|
2017-04-07 01:29:05 +02:00
|
|
|
|
2018-04-12 16:38:48 +02:00
|
|
|
db.Message.create(msg).then(function() {
|
2018-04-11 21:14:00 +02:00
|
|
|
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) {
|
2018-04-11 21:14:00 +02:00
|
|
|
db.Message.findOne({where:{
|
2017-04-07 01:29:05 +02:00
|
|
|
"_id": req.params.message_id
|
2018-04-12 16:38:48 +02:00
|
|
|
}}).then(function(msg) {
|
2017-04-07 01:29:05 +02:00
|
|
|
if (!msg) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
2018-04-12 16:38:48 +02:00
|
|
|
msg.destroy().then(function() {
|
|
|
|
res.distributeDelete("Message", msg);
|
2017-04-07 01:29:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|