You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/brain/api/content/bookmarks.js

169 lines
3.8 KiB
JavaScript

import StringUtils from '../../tools/utilities/StringUtils';
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';
var express = require('express');
var router = express.Router();
var Models = require('../../models');
var Models = require('../../models');
const scrape = require('scrape-metadata')
/*
Get Bookmark Listzz
*/
router.get('/archive', function (req, res, next) {
BMArchive.find()
.then((bookmarks) => {
console.log(bookmarks[1])
res.json(bookmarks)
})
.catch((err) => {
console.log(err)
})
});
/*
Get Bookmark List
*/
router.get('/', function (req, res, next) {
Bookmark.find().sort({
created: -1
})
.then((bookmarks) => {
res.json(bookmarks)
})
.catch((err) => {
console.log(err)
})
});
/*
Get Bookmark by ID
*/
router.get('/:id', function (req, res, next) {
});
/*
Create New BookMark
*/
router.post('/add', function (req, res, next) {
if (!req.session.user)
return res.json({
message: "You need to be logged in, champ."
});
var link = req.body.url;
Models.User.findById(req.session.user.id).then((user) => {
if (rightsManager.check(user.role, OBJECT_BOOKMARK, TASK_CREATE)) {
scrape(link, (err, meta) => {
var urlPieces = link.split("/");
Models.Bookmark.sync().then(f => {
Models.Bookmark.create({
source: urlPieces[0] + urlPieces[1] + '//' + urlPieces[2],
url: link,
image: meta.ogImage,
title: meta.title,
author: user.id,
listed: true
}).then(saved => {
res.json({
message: "link added"
});
}).catch(err => {
res.json({
message: "post error",
error: err
});
})
})
})
} else {
res.json({
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
/*
Update Bookmark by ID
*/
router.post('/update/:id', function (req, res, next) {
if (!req.session.user)
return res.json({
message: "You need to be logged in, champ."
});
console.log("ID: "+req.params.id);
let title = req.body.title;
let tags = req.body.tags;
let image = req.body.image;
Models.User.findById(req.session.user.id).then((user) => {
if (rightsManager.check(user.role, OBJECT_BOOKMARK, TASK_UPDATE)) {
Models.Bookmark.findOne({
where: {
id: req.params.id
}
}).then(saved => {
console.log('SAVED: '+req.body.title);
saved.update({
title: title,
image: image
}).then(updated => {
res.json({
message: "bookmark updated"
});
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
})
} else {
res.json({
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
/*
Delete Bookmark by ID
*/
router.delete('/:id', function (req, res, next) {
});
module.exports = router;