mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.2 KiB
101 lines
3.2 KiB
'use strict';
|
|
|
|
const Posts = require(__dirname+'/../../db/posts.js')
|
|
, Boards = require(__dirname+'/../../db/boards.js')
|
|
, quoteRegex = />>\d+/g
|
|
, crossQuoteRegex = />>>\/\w+\/\d*$/gm;
|
|
|
|
module.exports = async (board, text) => {
|
|
|
|
//get the matches
|
|
const quotes = text.match(quoteRegex);
|
|
const crossQuotes = text.match(crossQuoteRegex);
|
|
if (!quotes && !crossQuotes) {
|
|
return { quotedMessage: text, threadQuotes: [] };
|
|
}
|
|
|
|
//make query for db including crossquotes
|
|
const queryOrs = []
|
|
const crossQuoteMap = {};
|
|
if (quotes) {
|
|
const quoteIds = [...new Set(quotes.map(q => +q.substring(2)))]; //only uniques
|
|
queryOrs.push({
|
|
'board': board,
|
|
'postId': {
|
|
'$in': quoteIds
|
|
}
|
|
});
|
|
}
|
|
if (crossQuotes) {
|
|
for (let i = 0; i < crossQuotes.length; i++) {
|
|
const crossQuote = crossQuotes[i].split('/');
|
|
const crossQuoteBoard = crossQuote[1];
|
|
const crossQuotePostId = +crossQuote[2];
|
|
if (crossQuoteBoard === board) {
|
|
continue;
|
|
}
|
|
if (!crossQuoteMap[crossQuoteBoard]) {
|
|
crossQuoteMap[crossQuoteBoard] = [];
|
|
}
|
|
crossQuoteMap[crossQuoteBoard].push(crossQuotePostId);
|
|
}
|
|
const crossQuoteBoards = Object.keys(crossQuoteMap)
|
|
for (let i = 0; i < crossQuoteBoards.length; i++) {
|
|
const crossQuoteBoard = crossQuoteBoards[i];
|
|
const crossQuoteBoardPostIds = crossQuoteMap[crossQuoteBoard];
|
|
queryOrs.push({
|
|
'board': crossQuoteBoard,
|
|
'postId': {
|
|
'$in': crossQuoteBoardPostIds
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
//get all the posts from quotes
|
|
const postThreadIdMap = {};
|
|
if (queryOrs.length > 0) {
|
|
const posts = await Posts.getPostsForQuotes(queryOrs);
|
|
//if none of the quotes were real, dont do a replace
|
|
if (posts.length === 0) {
|
|
return { quotedMessage: text, threadQuotes: [] };
|
|
}
|
|
//turn the result into a map of postId => threadId/postId
|
|
for (let i = 0; i < posts.length; i++) {
|
|
const post = posts[i];
|
|
if (!postThreadIdMap[post.board]) {
|
|
postThreadIdMap[post.board] = {};
|
|
}
|
|
postThreadIdMap[post.board][post.postId] = post.thread || post.postId;
|
|
}
|
|
}
|
|
|
|
//then replace the quotes with only ones that exist
|
|
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}'>>>${quotenum}</a>${postThreadIdMap[board][quotenum] === quotenum ? ' <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}'>>>>/${quoteboard}/${quotenum}</a>`;
|
|
} else if (!quote[2]) {
|
|
return `<a class='quote' href='/${quoteboard}/index.html'>>>>/${quoteboard}/</a>`;
|
|
}
|
|
return match;
|
|
});
|
|
}
|
|
|
|
return { quotedMessage: text, threadQuotes: [...new Set(threadQuotes)] };
|
|
|
|
}
|
|
|