Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
NOW I REMEMBER why i didnt do that before: make backlinks delete prop…
Browse files Browse the repository at this point in the history
…erly based on quotes from database.
  • Loading branch information
fatchan committed Jul 1, 2019
1 parent cb2fe7c commit dc3005a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 41 deletions.
4 changes: 2 additions & 2 deletions controllers/forms.js
Expand Up @@ -271,8 +271,8 @@ router.post('/board/:board/settings', csrf, Boards.exists, checkPermsMiddleware,
if (typeof req.body.reply_limit === 'number' && (req.body.reply_limit < 1 || req.body.reply_limit > 1000)) {
errors.push('Reply Limit must be from 1-1000');
}
if (typeof req.body.thread_limit === 'number' && (req.body.thread_limit < 1 || req.body.thread_limit > 250)) {
errors.push('Threads Limit must be 1-250');
if (typeof req.body.thread_limit === 'number' && (req.body.thread_limit < 10 || req.body.thread_limit > 250)) {
errors.push('Threads Limit must be 10-250');
}
if (typeof req.body.max_files === 'number' && (req.body.max_files < 0 || req.body.max_files > 3)) {
errors.push('Max files must be 0-3');
Expand Down
18 changes: 7 additions & 11 deletions db/posts.js
Expand Up @@ -301,22 +301,20 @@ module.exports = {
data.postId = postId;

//insert the post itself
await db.insertOne(data);
const postMongoId = await db.insertOne(data).then(result => result.insertedId); //_id of post

//add backlinks to the posts this post quotes
if (data.quotes.length > 0) {
if (data.thread && data.quotes.length > 0) {
await db.updateMany({
'postId': {
'$in': data.quotes
},
'board': board
'_id': {
'$in': data.quotes.map(q => q._id)
}
}, {
'$push': {
'backlinks': postId
'backlinks': { _id: postMongoId, postId: postId }
}
});
}

return postId;

},
Expand Down Expand Up @@ -377,9 +375,7 @@ module.exports = {
},

deleteAll: (board) => {
return db.deleteMany({
'board': board
});
return db.deleteMany();
},

exists: async (req, res, next) => {
Expand Down
21 changes: 15 additions & 6 deletions helpers/posting/quotes.js
Expand Up @@ -5,7 +5,7 @@ const Posts = require(__dirname+'/../../db/posts.js')
, quoteRegex = />>\d+/g
, crossQuoteRegex = />>>\/\w+\/\d*$/gm;

module.exports = async (board, text) => {
module.exports = async (board, text, thread) => {

//get the matches
const quotes = text.match(quoteRegex);
Expand Down Expand Up @@ -66,36 +66,45 @@ module.exports = async (board, text) => {
if (!postThreadIdMap[post.board]) {
postThreadIdMap[post.board] = {};
}
postThreadIdMap[post.board][post.postId] = post.thread || post.postId;
postThreadIdMap[post.board][post.postId] = {
'_id': post._id,
'thread': post.thread || post.postId,
'postId': post.postId
};
}
}

//then replace the quotes with only ones that exist
const addedQuotes = new Set();
const threadQuotes = [];
if (quotes && Object.keys(postThreadIdMap).length > 0) {
text = text.replace(quoteRegex, (match) => {
const quotenum = +match.substring(2);
if (postThreadIdMap[board] && postThreadIdMap[board][quotenum]) {
threadQuotes.push(quotenum)
return `<a class='quote' href='/${board}/thread/${postThreadIdMap[board][quotenum]}.html#${quotenum}'>&gt;&gt;${quotenum}</a>${postThreadIdMap[board][quotenum] === quotenum ? ' <small>(OP)</small> ' : ''}`;
if (!addedQuotes.has(postThreadIdMap[board][quotenum]._id) && postThreadIdMap[board][quotenum].thread === thread) {
threadQuotes.push(postThreadIdMap[board][quotenum]);
addedQuotes.add(postThreadIdMap[board][quotenum]._id);
}
return `<a class='quote' href='/${board}/thread/${postThreadIdMap[board][quotenum].thread}.html#${quotenum}'>&gt;&gt;${quotenum}</a>${postThreadIdMap[board][quotenum].postId == thread ? ' <small>(OP)</small> ' : ''}`;
}
return match;
});
}

if (crossQuotes) {
text = text.replace(crossQuoteRegex, (match) => {
const quote = match.split('/');
const quoteboard = quote[1];
const quotenum = +quote[2];
if (postThreadIdMap[quoteboard] && postThreadIdMap[quoteboard][quotenum]) {
return `<a class='quote' href='/${quoteboard}/thread/${postThreadIdMap[quoteboard][quotenum]}.html#${quotenum}'>&gt;&gt;&gt;/${quoteboard}/${quotenum}</a>`;
return `<a class='quote' href='/${quoteboard}/thread/${postThreadIdMap[quoteboard][quotenum].thread}.html#${quotenum}'>&gt;&gt;&gt;/${quoteboard}/${quotenum}</a>`;
} else if (!quote[2]) {
return `<a class='quote' href='/${quoteboard}/index.html'>&gt;&gt;&gt;/${quoteboard}/</a>`;
}
return match;
});
}

return { quotedMessage: text, threadQuotes: [...new Set(threadQuotes)] };
return { quotedMessage: text, threadQuotes };

}
47 changes: 40 additions & 7 deletions models/forms/deletepost.js
Expand Up @@ -3,7 +3,7 @@
const uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js')
, { remove } = require('fs-extra')
, Mongo = require(__dirname+'/../../db/db.js')
, Posts = require(__dirname+'/../../db/posts.js');
, Posts = require(__dirname+'/../../db/posts.js')

module.exports = async (posts, board) => {

Expand Down Expand Up @@ -38,13 +38,46 @@ module.exports = async (posts, board) => {
//combine them all into one array, there may be duplicates but it shouldnt matter
const allPosts = posts.concat(threadPosts);

//NOTE: this is where, when implemented, file ref counts would be decremented
//NOTE: this is where, when implemented, re-marking up posts that quoted deleted posts would be done
//could use a destructuring with Array.reduce when i need to get files array, backlinks array and mongoId array
//instead of doing 3 maps or big for loop
//get files for ref counting, backlinks for post re-markup, mongoids for deleting
const { postFiles, postBacklinks, postMongoIds } = allPosts.reduce((acc, post) => {
if (post.files.length > 0) {
acc.postFiles = acc.postFiles.concat(post.files);
}
if (post.backlinks.length > 0) {
acc.postBacklinks = acc.postBacklinks.concat(post.backlinks);
}
acc.postMongoIds.push(post._id);
return acc;
}, { postFiles: [], postBacklinks: [], postMongoIds: [] });

//is there a nicer way to do this
const bulkWrites = [];
for (let j = 0; j < allPosts.length; j++) {
const post = allPosts[j];
for (let i = 0; i < post.quotes.length; i++) {
const quote = post.quotes[i];
//remove the backlink to this post from any post that it quoted
bulkWrites.push({
'updateOne': {
'filter': {
'_id': quote._id
},
'update': {
'$pull': {
'backlinks': {
'postId': post.postId
}
}
}
}
});
}
}
await Posts.db.bulkWrite(bulkWrites);

//TODO: remarkup to unlink quotes in posts that quote deleted posts
//TODO: file ref counting decrement, oncei implement counting in make post

//get all mongoids and delete posts from
const postMongoIds = allPosts.map(post => Mongo.ObjectId(post._id));
const deletedPosts = await Posts.deleteMany(postMongoIds).then(result => result.deletedCount);

//hooray!
Expand Down
4 changes: 2 additions & 2 deletions models/forms/deletepostsfiles.js
Expand Up @@ -9,7 +9,7 @@ module.exports = async (posts, unlinkOnly) => {
let fileNames = [];
posts.forEach(post => {
fileNames = fileNames.concat(post.files.map(x => x.filename))
})
});

if (fileNames.length === 0) {
return {
Expand All @@ -18,6 +18,7 @@ module.exports = async (posts, unlinkOnly) => {
}

if (unlinkOnly) {
//TODO: decrement ref counters when implemented
return {
message:`Unlinked ${fileNames.length} file(s) across ${posts.length} post(s)`,
action:'$set',
Expand All @@ -39,5 +40,4 @@ module.exports = async (posts, unlinkOnly) => {
};
}


}
2 changes: 1 addition & 1 deletion models/forms/makepost.js
Expand Up @@ -225,7 +225,7 @@ module.exports = async (req, res, next) => {
let quotes = [];
if (message && message.length > 0) {
message = simpleMarkdown(message);
const { quotedMessage, threadQuotes } = await linkQuotes(req.params.board, message);
const { quotedMessage, threadQuotes } = await linkQuotes(req.params.board, message, req.body.thread || null);
message = quotedMessage;
quotes = threadQuotes;
message = sanitize(message, sanitizeOptions);
Expand Down
2 changes: 1 addition & 1 deletion views/includes/navbar.pug
Expand Up @@ -2,4 +2,4 @@ nav.navbar
a.nav-item(href='/') Home
a.nav-item.right(href='/logout') Logout
a.nav-item.right(href=`/${board ? board._id+'/' : 'global'}manage.html`) Manage
a.nav-item.right(href=`/login.html${board ? '?goto=/'+board._id : ''}`) Login
//a.nav-item.right(href=`/login.html${board ? '?goto=/'+board._id : ''}`) Login
2 changes: 1 addition & 1 deletion views/mixins/post.pug
Expand Up @@ -88,7 +88,7 @@ mixin post(post, truncate, manage=false, globalmanage=false)
if post.backlinks && post.backlinks.length > 0
.replies Replies:
each backlink in post.backlinks
a.quote(href=`/${post.board}/thread/${post.thread || post.postId}.html#${backlink}`) &gt;&gt;#{backlink}
a.quote(href=`/${post.board}/thread/${post.thread || post.postId}.html#${backlink.postId}`) &gt;&gt;#{backlink.postId}
|
if manage === true
each report in post.reports
Expand Down
2 changes: 2 additions & 0 deletions views/pages/changepassword.pug
Expand Up @@ -30,3 +30,5 @@ block content
iframe.captcha(src='/captcha.html' width=200 height=110 scrolling='no')
input#captcha(type='text', name='captcha', autocomplete='off' placeholder='captcha text' maxlength='6' required)
input(type='submit', value='Change Password')
p: a(href='/login.html') Login
p: a(href='/register.html') Register
3 changes: 2 additions & 1 deletion views/pages/login.pug
Expand Up @@ -16,5 +16,6 @@ block content
.required *
input#password(type='password', name='password', maxlength='100' required)
input(type='submit', value='submit')
p No account? #[a(href='/register.html') Register]
p: a(href='/register.html') Register
p: a(href='/changepassword.html') Change Password

3 changes: 2 additions & 1 deletion views/pages/register.pug
Expand Up @@ -26,4 +26,5 @@ block content
iframe.captcha(src='/captcha.html' width=200 height=110 scrolling='no')
input#captcha(type='text', name='captcha', autocomplete='off' placeholder='captcha text' maxlength='6' required)
input(type='submit', value='Register')
p Already have an account? #[a(href='/login.html') Login]
p: a(href='/login.html') Login
p: a(href='/changepassword.html') Change Password
11 changes: 3 additions & 8 deletions wipe.js
@@ -1,7 +1,7 @@

'use strict';

const Mongo = require(__dirname+'/db/db.js')
const Mongo = require(__dirname+'/db/db.js');

(async () => {
console.log('connecting to db...')
Expand All @@ -16,13 +16,8 @@ const Mongo = require(__dirname+'/db/db.js')
console.log('deleting accounts')
await Accounts.deleteAll();
console.log('deleting posts')
await Posts.deleteAll('pol');
await Posts.deleteAll('b');
await Posts.deleteAll('t');
await Posts.deleteAll();
console.log('deleting boards')
await Boards.deleteIncrement('pol');
await Boards.deleteIncrement('b');
await Boards.deleteIncrement('t');
await Boards.deleteAll();
console.log('deleting bans');
await Bans.deleteAll();
Expand Down Expand Up @@ -164,7 +159,7 @@ const Mongo = require(__dirname+'/db/db.js')
});
console.log('creating admin account: admin:changeme');
await Accounts.insertOne('admin', 'changeme', 3);
Mongo.client.close()
Mongo.client.close();
console.log('done');
process.exit(0);
})();

0 comments on commit dc3005a

Please sign in to comment.