2018-11-11 20:49:26 +01:00
|
|
|
import DateUtils from '../../tools/utilities/DateUtils';
|
|
|
|
import StringUtils from '../../tools/utilities/StringUtils';
|
2018-11-27 19:48:02 +01:00
|
|
|
import * as DataEvent from '../../tools/events/DataEvent';
|
2018-12-06 22:49:49 +01:00
|
|
|
import sanitize from 'sanitize-html';
|
2018-11-11 20:49:26 +01:00
|
|
|
import RightsManager,
|
|
|
|
{
|
|
|
|
TASK_CREATE,
|
|
|
|
TASK_UPDATE,
|
|
|
|
TASK_READ,
|
|
|
|
TASK_DELETE,
|
|
|
|
OBJECT_CLIENT_ADMIN,
|
|
|
|
OBJECT_CLIENT_USER,
|
|
|
|
OBJECT_PROJECT_CLIENT,
|
|
|
|
OBJECT_PROJECT_FOLIO,
|
|
|
|
OBJECT_BOOKMARK,
|
|
|
|
OBJECT_POST
|
|
|
|
}
|
|
|
|
from '../../tools/utilities/RightsManager';
|
2018-12-11 18:35:29 +01:00
|
|
|
const express = require('express');
|
2018-12-06 22:49:49 +01:00
|
|
|
const router = express.Router();
|
|
|
|
const multer = require('multer');
|
|
|
|
const md = require('markdown-it')('commonmark');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const Models = require('../../models');
|
2018-11-11 20:49:26 +01:00
|
|
|
const dateUtils = new DateUtils();
|
|
|
|
const rightsManager = new RightsManager();
|
2018-12-06 22:49:49 +01:00
|
|
|
const uploadPath = "./content/blog-images/" + dateUtils.getDate('year', new Date()) + "/" + dateUtils.getDate('month', new Date());
|
|
|
|
const Sequelize = require('sequelize');
|
2018-11-21 01:42:52 +01:00
|
|
|
const Op = Sequelize.Op;
|
2018-12-06 22:49:49 +01:00
|
|
|
const _ = require('lodash');
|
2018-11-11 20:49:26 +01:00
|
|
|
fs.ensureDir(uploadPath, function(err)
|
|
|
|
{
|
|
|
|
//console.log(err) // => null
|
|
|
|
// dir has now been created, including the directory it is to be placed in
|
|
|
|
})
|
|
|
|
var storage = multer.diskStorage(
|
|
|
|
{
|
|
|
|
destination: function(req, file, cb)
|
|
|
|
{
|
|
|
|
cb(null, uploadPath)
|
|
|
|
},
|
|
|
|
filename: function(req, file, cb)
|
|
|
|
{
|
|
|
|
var splice = file.originalname.split(':');
|
|
|
|
cb(null, splice[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var feature_upload = multer(
|
|
|
|
{
|
|
|
|
storage: storage
|
|
|
|
}).array('feature_image');
|
|
|
|
var post_upload = multer(
|
|
|
|
{
|
|
|
|
storage: storage
|
|
|
|
}).array('post_image');
|
2018-11-22 02:08:53 +01:00
|
|
|
//** SYNC POSTS */
|
2018-11-21 01:42:52 +01:00
|
|
|
router.post("/sync", (req, res, next) =>
|
2018-11-14 17:32:35 +01:00
|
|
|
{
|
2018-11-21 01:42:52 +01:00
|
|
|
let payload = req.body;
|
|
|
|
Models.User.findById(req.session.user.id).then((user) =>
|
|
|
|
{
|
|
|
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_UPDATE))
|
|
|
|
{
|
|
|
|
for (let index = 0; index < payload.length; index++)
|
|
|
|
{
|
|
|
|
const item = payload[index];
|
|
|
|
Models.FreshPost.findOne(
|
|
|
|
{
|
|
|
|
where:
|
|
|
|
{
|
2018-11-22 02:08:53 +01:00
|
|
|
"post":
|
|
|
|
{
|
|
|
|
[Op.contains]:
|
|
|
|
{
|
2018-12-08 19:21:48 +01:00
|
|
|
uuid: item.post.uuid
|
2018-11-21 01:42:52 +01:00
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).then(found =>
|
|
|
|
{
|
2018-12-06 22:49:49 +01:00
|
|
|
let buffed = sanitize(item.post.plaintext,
|
|
|
|
{
|
|
|
|
allowedTags: ['del', 'a', 'iframe', 'img', ],
|
|
|
|
allowedAttributes:
|
|
|
|
{
|
|
|
|
a: ['href', 'name', 'target'],
|
|
|
|
img: ['src'],
|
|
|
|
iframe: ['height', 'width', 'src', 'frameborder', 'allow', 'allowfullscreen']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
buffed = new StringUtils().decodeHTML(buffed);
|
|
|
|
item.post.plaintext = buffed;
|
|
|
|
item.post.html = md.render(buffed,
|
|
|
|
{
|
|
|
|
html: true,
|
|
|
|
xhtmlOut: true,
|
2018-12-08 19:21:48 +01:00
|
|
|
});
|
2018-11-26 23:14:13 +01:00
|
|
|
if (!_.isEqual(item.post, found.post))
|
|
|
|
{
|
|
|
|
found.update(item).then(updated =>
|
|
|
|
{
|
2018-11-22 02:08:53 +01:00
|
|
|
console.log("UPDATED", updated);
|
2018-11-26 23:14:13 +01:00
|
|
|
}).catch(err =>
|
|
|
|
{
|
2018-11-22 02:08:53 +01:00
|
|
|
//console.log("***ERROR***", err);
|
|
|
|
})
|
2018-11-26 23:14:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-22 02:08:53 +01:00
|
|
|
//chilld
|
2018-11-21 01:42:52 +01:00
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
}).catch(err =>
|
|
|
|
{
|
2018-12-08 19:21:48 +01:00
|
|
|
//console.log("***ERRRORZ****", err);
|
2018-11-22 02:08:53 +01:00
|
|
|
Models.FreshPost.create(item).then(fresh =>
|
|
|
|
{
|
2018-11-21 01:42:52 +01:00
|
|
|
//console.log(fresh)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
res.json(
|
2018-11-22 02:08:53 +01:00
|
|
|
{
|
|
|
|
message: "postsSynced"
|
|
|
|
});
|
2018-11-21 01:42:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.json(
|
|
|
|
{
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
router.get('/json', function(req, res, next)
|
|
|
|
{
|
|
|
|
Models.FreshPost.findAll(
|
2018-11-14 17:32:35 +01:00
|
|
|
{
|
|
|
|
order: [
|
|
|
|
['id', 'DESC']
|
|
|
|
]
|
|
|
|
}).then(function(posts)
|
|
|
|
{
|
2018-12-04 03:19:10 +01:00
|
|
|
let newlist = [];
|
2018-12-06 22:49:49 +01:00
|
|
|
for (let index = 0; index < posts.length; index++)
|
|
|
|
{
|
2018-12-04 03:19:10 +01:00
|
|
|
let item = posts[index].post;
|
2018-12-06 22:49:49 +01:00
|
|
|
if (typeof item.deleted == 'undefined' || item.deleted == false)
|
|
|
|
{
|
|
|
|
newlist.push(posts[index])
|
|
|
|
}
|
|
|
|
else
|
2018-12-04 03:19:10 +01:00
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.json(newlist)
|
2018-11-14 17:32:35 +01:00
|
|
|
}).catch(function(err)
|
|
|
|
{
|
|
|
|
//next(err);
|
|
|
|
})
|
|
|
|
})
|
2018-11-11 20:49:26 +01:00
|
|
|
/***
|
|
|
|
POST IMAGE
|
|
|
|
*/
|
|
|
|
router.post('/add-post-image', function(req, res, next)
|
|
|
|
{
|
|
|
|
//console.log(req.body);
|
|
|
|
if (!req.session.user) return res.json(
|
|
|
|
{
|
|
|
|
message: "You need to be logged in, champ."
|
|
|
|
});
|
|
|
|
Models.User.findById(req.session.user.id).then((user) =>
|
|
|
|
{
|
|
|
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE))
|
|
|
|
{
|
|
|
|
post_upload(req, res, function(err)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
//console.log('Error in Saving Entry: ' + err);
|
|
|
|
res.json(
|
|
|
|
{
|
|
|
|
message: err
|
|
|
|
});
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var postImage = req.files[0].path;
|
|
|
|
return res.json(
|
|
|
|
{
|
2018-11-27 19:48:02 +01:00
|
|
|
message: DataEvent.POST_IMAGE_ADDED,
|
2018-11-11 20:49:26 +01:00
|
|
|
url: postImage.substr(7, postImage.length)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.json(
|
|
|
|
{
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-11-26 23:14:13 +01:00
|
|
|
router.post('/add-feature-image', function(req, res, next)
|
|
|
|
{
|
|
|
|
//console.log(req.body);
|
|
|
|
if (!req.session.user) return res.json(
|
|
|
|
{
|
|
|
|
message: "You need to be logged in, champ."
|
|
|
|
});
|
|
|
|
Models.User.findById(req.session.user.id).then((user) =>
|
|
|
|
{
|
|
|
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE))
|
|
|
|
{
|
|
|
|
feature_upload(req, res, function(err)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
//console.log('Error in Saving Entry: ' + err);
|
|
|
|
res.json(
|
|
|
|
{
|
|
|
|
message: err
|
|
|
|
});
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var postImage = req.files[0].path;
|
|
|
|
return res.json(
|
|
|
|
{
|
2018-11-27 19:48:02 +01:00
|
|
|
message: DataEvent.FEATURE_IMAGE_ADDED,
|
2018-11-26 23:14:13 +01:00
|
|
|
url: postImage.substr(7, postImage.length)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.json(
|
|
|
|
{
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-11-11 20:49:26 +01:00
|
|
|
module.exports = router;
|