smoohted out update sync functionality for posts

This commit is contained in:
Ro 2018-11-21 20:08:53 -05:00
parent bc1b4fa3e8
commit b057c50ad4
10 changed files with 29523 additions and 530 deletions

View file

@ -19,11 +19,7 @@ var router = express.Router();
var multer = require('multer');
var fs = require('fs-extra');
var Models = require('../../models');
var uuidv4 = require('uuid/v4');
var md = require('markdown-it')('commonmark');
var sanitize = require('sanitize-html');
const dateUtils = new DateUtils();
const stringUtils = new StringUtils();
const rightsManager = new RightsManager();
var uploadPath = "./content/blog-images/" + dateUtils.getDate('year', new Date()) + "/" + dateUtils.getDate('month', new Date());
var Sequelize = require('sequelize');
@ -54,6 +50,9 @@ var post_upload = multer(
{
storage: storage
}).array('post_image');
//** SYNC POSTS */
router.post("/sync", (req, res, next) =>
{
let payload = req.body;
@ -68,27 +67,37 @@ router.post("/sync", (req, res, next) =>
{
where:
{
"post": {
[Op.contains]: {
"post":
{
[Op.contains]:
{
slug: item.post.slug
}
}
}
}).then(found =>{
if (!_.isEqual(item.post, found.post) ) found.update(item)
}).then(found =>
{
if (!_.isEqual(item.post, found.post)){
found.update(item).then(updated=>{
console.log("UPDATED", updated);
}).catch(err=>{
Models.FreshPost.create(item).then(fresh =>{
//console.log("***ERROR***", err);
})
} else{
//chilld
}
}).catch(err =>
{
Models.FreshPost.create(item).then(fresh =>
{
//console.log(fresh)
})
})
}
res.json(
{
message: "postsSynced"
});
}
else
{
@ -99,62 +108,6 @@ router.post("/sync", (req, res, next) =>
}
});
})
router.post('/jsontest-edit', function(req, res, next)
{
Models.FreshPost.findById(req.body.id).then(fresh =>
{
fresh.update(req.body)
res.json(
{
message: "jsonPostUpdated"
});
}).catch(err =>
{
console.log(err)
})
})
router.get('/', function(req, res, next)
{
Models.Post.findAll(
{
order: [
['id']
]
}).then(function(posts)
{
var count = posts.length;
var list = [];
for (let index = 0; index < count; index++)
{
let item = posts[index];
let post = {
post:
{
uuid: item.uuid,
title: item.title,
slug: item.slug,
tags: item.tags,
feature: item.feature_image,
author: "Are0h",
html: item.html,
plaintext: item.plaintext,
featured: item.featured,
published: item.published,
page: item.page,
created: item.created_at,
updated: item.updated_at,
deleted: false
}
}
list.push(post);
}
res.json(list);
}).catch(function(err)
{
//next(err);
})
})
router.get('/json', function(req, res, next)
{
Models.FreshPost.findAll(
@ -170,213 +123,7 @@ router.get('/json', function(req, res, next)
//next(err);
})
})
/***
ADD POST
*/
router.post('/add', function(req, res, next)
{
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)
{
res.json(
{
message: err
});
throw err;
}
else
{
var postImages = [];
if (req.files != "")
{
for (let i = 0; i < req.files.length; i++)
{
postImages.push(req.files[i].path);
}
}
else
{
console.log("NOTHING TO SAVE");
}
Models.Post.sync().then(f =>
{
var html = req.body.post_plaintext;
html = html.replace(/<\/?span[^>]*>/g, ""); //removes highightjs styling
console.log("REGULAR: " + html);
let buffed = sanitize(html,
{
allowedTags: ['del', 'a', 'iframe', 'img'],
allowedAttributes:
{
a: ['href', 'name', 'target'],
img: ['src'],
iframe: ['height', 'width', 'src', 'frameborder', 'allow', 'allowfullscreen']
}
})
buffed = stringUtils.decodeHTML(buffed)
Models.Post.create(
{
uuid: uuidv4(),
title: req.body.title,
slug: req.body.slug,
plaintext: buffed,
tags: req.body.tags,
page: req.body.status_page,
featured: req.body.status_feature,
published: req.body.status_published,
author_id: req.session.user.id,
origin_date: new Date(req.body.origin_date),
html: md.render(buffed,
{
html: true,
xhtmlOut: true,
}),
feature_image: JSON.stringify(postImages)
}).then(saved =>
{
res.json(
{
message: "postAdded",
postID: saved.slug
});
}).catch(err =>
{
console.log(err)
})
})
}
});
}
else
{
res.json(
{
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
/***
UPDATE POST
*/
router.post('/update/:id', 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_UPDATE))
{
feature_upload(req, res, function(err)
{
if (err)
{
res.json(
{
message: err
});
throw err;
}
else
{
var postImages = [];
if (req.files != "")
{
for (let i = 0; i < req.files.length; i++)
{
postImages.push(req.files[i].path);
}
}
else
{
console.log("NOTHING TO SAVE");
}
Models.Post.findOne(
{
where:
{
id: req.params.id
}
}).then(post =>
{
if (postImages.length == 0) postImages = JSON.parse(post.feature_image);
var html = req.body.post_plaintext;
html = html.replace(/<\/?span[^>]*>/g, ""); //removes highightjs styling
let cleaned = sanitize(html,
{
allowedTags: ['del', 'a', 'iframe', 'img'],
allowedAttributes:
{
a: ['href', 'name', 'target'],
img: ['src'],
iframe: ['height', 'width', 'src', 'frameborder', 'allow', 'allowfullscreen']
}
})
cleaned = stringUtils.decodeHTML(cleaned)
post.update(
{
title: req.body.title,
slug: req.body.slug,
plaintext: cleaned,
origin_date: new Date(req.body.origin_date),
tags: req.body.tags,
page: req.body.status_page,
featured: req.body.status_feature,
published: req.body.status_published,
html: md.render(cleaned,
{
html: true,
xhtmlOut: true
}),
feature_image: JSON.stringify(postImages)
}).then(updated =>
{
res.json(
{
message: "postUpdated"
});
}).catch(err =>
{
console.log(err)
res.json(
{
message: "postError",
error: err
});
})
}).catch(err =>
{
//console.log(err)
res.json(
{
message: "postError",
error: err
});
})
}
});
}
else
{
res.json(
{
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
/***
POST IMAGE
*/
@ -422,43 +169,4 @@ router.post('/add-post-image', function(req, res, next)
}
});
});
router.post('/delete/:id', function(req, res, next)
{
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_DELETE))
{
Models.Post.findOne(
{
where:
{
id: req.params.id
}
}).then(post =>
{
post.destroy().then(deleted =>
{
res.json(
{
message: "postDeleted"
});
})
}).catch(err =>
{
console.log(err);
})
}
else
{
res.json(
{
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
module.exports = router;

View file

@ -1,4 +1,3 @@
import DateUtils from '../../tools/utilities/DateUtils';
var express = require('express');
var router = express.Router();
@ -6,110 +5,137 @@ var Models = require('../../models');
var hljs = require('highlight.js/lib/highlight');
var hljs_md = require('highlight.js/lib/languages/markdown');
hljs.registerLanguage('markdown', hljs_md);
var Sequelize = require('sequelize');
const Op = Sequelize.Op;
const dateUtils = new DateUtils();
//--------------------------
// POSTS
//--------------------------
router.get('/:page?', function (req, res) {
router.get('/:page?', function(req, res)
{
var pageNum = req.params.page;
if (pageNum == "" || pageNum == null) pageNum = 1;
console.log(pageNum);
var offset = ((pageNum * 5) - 5);
if (req.session.user) {
Models.Post.findAll({
if (req.session.user)
{
Models.FreshPost.findAll(
{
order: [
['id', 'DESC']
]
}).then(function (posts) {
}).then(function(posts)
{
var count = Math.round(posts.length / 6);
//console.log("COUNT: "+count);
var pageItems = [];
var itemLimit = 6;
var rangeStart = (pageNum * itemLimit) - itemLimit;
for (var i = 0; i < itemLimit; i++) {
try {
if (posts[i + rangeStart].id != null) {
for (var i = 0; i < itemLimit; i++)
{
try
{
if (posts[i + rangeStart].id != null)
{
pageItems.push(posts[i + rangeStart]);
}
} catch (e) {
}
catch (e)
{
//console.log(e)
}
}
res.render('dash/posts-index', {
res.render('dash/posts-index',
{
title: 'Dashbord | Posts',
mode: 'admin',
items: pageItems,
page_index: pageNum,
page_count: count
});
}).then(function (value) {
}).then(function(value)
{
//console.log(value);
}).catch(function (err) {
}).catch(function(err)
{
//next(err);
})
} else {
}
else
{
console.log("REDIRECT")
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST ADD DISPLAY
//--------------------------
router.get('/add/new', function (req, res) {
if (req.session.user) {
res.render('dash/post-edit', {
router.get('/add/new', function(req, res)
{
if (req.session.user)
{
res.render('dash/post-edit',
{
title: 'Make New Post',
mode: 'admin',
date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()),
status:['false', 'false', 'false'],
status: ['false', 'false', 'false'],
edit: false
});
} else {
}
else
{
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST EDIT DISPLAY
//--------------------------
router.get('/edit/:id', function (req, res) {
if (req.session.user) {
Models.Post.findOne({
where: {
router.get('/edit/:id', function(req, res)
{
if (req.session.user)
{
Models.FreshPost.findOne(
{
where:
{
"post":
{
[Op.contains]:
{
slug: req.params.id
}
}).then(post => {
let featured_img = JSON.parse(post.feature_image);
}
}
}).then(item =>
{
let featured_img = JSON.parse(item.post.feature);
let featured = 'null';
if (featured_img.length != 0)
featured = featured_img[0].substr(7, featured_img[0].length);
let pretty = hljs.highlight('markdown', post.plaintext).value;
let sexydate
if (post.origin_date == "" || post.origin_date == null)
sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at)
else
sexydate = post.origin_date
res.render('dash/post-edit', {
if (featured_img.length != 0) featured = featured_img[0].substr(7, featured_img[0].length);
let pretty = hljs.highlight('markdown', item.post.plaintext).value;
let sexydate = dateUtils.getDate('year', item.post.created) + "-" + dateUtils.getDate('month', item.post.created) + "-" + dateUtils.getDate('day', item.post.created)
res.render('dash/post-edit',
{
id: item.id,
title: 'Edit Post',
mode: 'admin',
post: post,
post: item.post,
date: sexydate,
colored: pretty,
html: post.plaintext,
html: item.post.plaintext,
feature: featured,
status:[String(post.page), String(post.featured), String(post.published)],
status: [String(item.post.page), String(item.post.featured), String(item.post.published)],
edit: true
});
}).then(function (value) {
}).then(function(value)
{
console.log("VALUE: " + value);
}).catch(function (err) {
}).catch(function(err)
{
console.log(err);
})
} else {
}
else
{
res.redirect('/@/dashboard');
}
});

View file

@ -62,128 +62,7 @@ router.get('/settings/', function(req, res)
//--------------------------
// POSTS
//--------------------------
router.get('/posts/:page?', function(req, res)
{
var pageNum = req.params.page;
if (pageNum == "" || pageNum == null) pageNum = 1;
var offset = ((pageNum * 5) - 5);
if (req.session.user)
{
Models.Post.findAll(
{
order: [
['id', 'DESC']
]
}).then(function(posts)
{
var count = Math.round(posts.length / 6);
//console.log("COUNT: "+count);
var pageItems = [];
var itemLimit = 6;
var rangeStart = (pageNum * itemLimit) - itemLimit;
for (var i = 0; i < itemLimit; i++)
{
try
{
if (posts[i + rangeStart].id != null)
{
pageItems.push(posts[i + rangeStart]);
}
}
catch (e)
{
//console.log(e)
}
}
res.render('dash/posts-index',
{
title: 'Dashbord | Posts',
mode: 'admin',
items: pageItems,
page_index: pageNum,
page_count: count
});
}).then(function(value)
{
//console.log(value);
}).catch(function(err)
{
//next(err);
})
}
else
{
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST ADD DISPLAY
//--------------------------
router.get('/posts/add/new', function(req, res)
{
if (req.session.user)
{
res.render('dash/post-edit',
{
title: 'Make New Post',
mode: 'admin',
date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()),
status: ['false', 'false', 'false'],
edit: false
});
}
else
{
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST EDIT DISPLAY
//--------------------------
router.get('/posts/edit/:id', function(req, res)
{
if (req.session.user)
{
Models.Post.findOne(
{
where:
{
slug: req.params.id
}
}).then(post =>
{
let featured_img = JSON.parse(post.feature_image);
let featured = 'null';
if (featured_img.length != 0) featured = featured_img[0].substr(7, featured_img[0].length);
let pretty = hljs.highlight('markdown', post.plaintext).value;
let sexydate
if (post.origin_date == "" || post.origin_date == null) sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at)
else sexydate = post.origin_date
res.render('dash/post-edit',
{
title: 'Edit Post',
mode: 'admin',
post: post,
date: sexydate,
colored: pretty,
html: post.plaintext,
feature: featured,
status: [String(post.page), String(post.featured), String(post.published)],
edit: true
});
}).then(function(value)
{
console.log("VALUE: " + value);
}).catch(function(err)
{
console.log(err);
})
}
else
{
res.redirect('/@/dashboard');
}
});
/**
//--------------------------
// MAIN FIPAMO DISPLAY

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -17,7 +17,7 @@ block main-content
-post_date = date
-post_status = status
#post-edit-index
#post-edit-index(data-index=id)
#post-edit-index-wrapper
//h2 EDIT
=post_title

View file

@ -10,9 +10,9 @@ block main-content
#posts-list
- var index = 0;
- for ( index; index < items.length; index++)
- var date = new Date(items[index].created_at)
- var date = new Date(items[index].post.created)
a.post-list-link(href="/@/dashboard/posts/edit/"+items[index].slug id=items[index].uuid)
= items[index].title
= items[index].post.title
br
span= date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()
- var next = parseInt(page_index, 10) + 1

View file

@ -56,7 +56,7 @@ export default class Base
self.start();
fipamoPosts.postList.toArray(array =>
{
console.log(array[21].post.title);
//console.log(array[21].post.title);
})
}).catch(Dexie.BulkError, e =>
{

View file

@ -11,6 +11,8 @@ from '../../../../../brain//tools/utilities/DataUtils';
import * as DataEvent from '../../../../../brain//tools/events/DataEvent';
import StringUtils from '../../../../../brain//tools/utilities/StringUtils';
import Dexie from 'dexie';
import sanitize from 'sanitize-html'
var md = require('markdown-it')('commonmark');
class PostActions
{
//--------------------------
@ -18,14 +20,74 @@ class PostActions
//--------------------------
constructor()
{
var folio = [];
this.dataUtils = new DataUtils();
this.db = new Dexie("fipamo_posts");
this.db.version(1).stores(
{
postList: 'id,post'
});
}
//--------------------------
// methods
//--------------------------
start()
{}
update(id, data, files)
{
let self = this;
return new Promise((resolve, reject) =>
{
let txt = document.createElement("textarea");
txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
let html = txt.value;
html = html.replace(/<\/?span[^>]*>/g, ""); //removes highightjs styling
let buffed = sanitize(html,
{
allowedTags: ['del', 'a', 'iframe', 'img'],
allowedAttributes:
{
a: ['href', 'name', 'target'],
img: ['src'],
iframe: ['height', 'width', 'src', 'frameborder', 'allow', 'allowfullscreen']
}
})
buffed = new StringUtils().decodeHTML(buffed)
data.title = document.getElementById('post_title').value;
data.slug = new StringUtils().cleanString(document.getElementById('post_title').value)
data.plaintext = buffed;
data.html = md.render(buffed,
{
html: true,
xhtmlOut: true,
})
data.created = document.getElementById('post-date').value;
data.tags = document.getElementById('post_tags').value;
data.page = document.getElementById('option-page').getAttribute('data-active')
data.featured = document.getElementById('option-feature').getAttribute('data-active')
data.published = document.getElementById('option-published').getAttribute('data-active')
this.db.postList.update(Number(id), {post:data}).then(updated =>
{
self.db.postList.toArray(array =>
{
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, array).then((response) =>
{
resolve(
{
response
})
}).catch((err) =>
{
reject(
{
err
});
})
})
//(updated) ? resolve("UPDATED") : reject("NOT UPDATED");
});
})
}
sync(files)
{
let self = this;
@ -49,11 +111,8 @@ class PostActions
{
postList: 'id,post'
});
fipamoPosts.postList.toArray(array =>
{
console.log(array);
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, array).then((response) =>
{
resolve(

View file

@ -17,7 +17,7 @@ import TinyDatePicker from 'tiny-date-picker';
import DateUtils from '../../../../../brain/tools/utilities/DateUtils';
import TextEditor from '../../../../../brain/tools/ui/TextEditor';
import Dexie from 'dexie';
class PostEditor
export default class PostEditor
{
//--------------------------
// constructor
@ -32,15 +32,13 @@ class PostEditor
this.dateUtils = new DateUtils();
this.urlPieces = document.URL.split("/");
this.post = [];
this.postID = document.getElementById('post-edit-index').getAttribute('data-index');
var fipamoPosts = new Dexie("fipamo_posts");
fipamoPosts.version(1).stores(
{
postList: 'id, post'
});
fipamoPosts.postList.get(1).then(post=>{
});
fipamoPosts.postList.get(Number(this.postID)).then(obj => this.setPost(obj.post));
if (document.getElementById('edit-post-text'))
{
this.editor = new TextEditor(document.getElementById('edit-post-text'), document.getElementById('header').offsetHeight + document.getElementById('post-header').offsetHeight + document.getElementById('post-feature').offsetHeight);
@ -59,11 +57,15 @@ class PostEditor
}
});
}
this.start();
}
//--------------------------
// methods
//--------------------------
setPost(array)
{
this.post = array;
this.start();
}
start()
{
let self = this;
@ -118,6 +120,14 @@ class PostEditor
case EditorEvent.EDITOR_UPDATE:
let edit = false;
//if (e == EditorEvent.EDITOR_UPDATE) edit = true;
new PostActions().update(this.postID, this.post, PostEditor.uploadFiles).then(response=>{
this.editor.notify(DataEvent.POST_UPDATED, this.postID);
}).catch(err=>{
console.log("ERROR", err)
})
/**
new PostActions().sync(PostEditor.uploadFiles).then((response) =>
{
let note = JSON.parse(response['response']['request'].response);
@ -127,7 +137,7 @@ class PostEditor
{
console.log(err)
});
*/
/*
new PostActions().submitPost(edit, PostEditor.uploadFiles).then((response) =>
{
@ -267,8 +277,3 @@ class PostEditor
}
}
PostEditor.uploadFiles = [];
export
{
PostEditor as
default
}