Potential fix fo #2

This commit is contained in:
Logan Cusano
2023-03-12 03:47:49 -04:00
parent 72134b1b7b
commit ffacd19883
3 changed files with 102 additions and 59 deletions

View File

@@ -3,8 +3,10 @@ const { DebugBuilder } = require("./utilities/debugBuilder");
const log = new DebugBuilder("server", "libUtils");
const { NodeHtmlMarkdown } = require('node-html-markdown');
const { parse } = require("node-html-parser");
const crypto = require("crypto");
const imageRegex = /(http(s?):)([/|.|\w|\s|-])*((\.(?:jpg|gif|png|webm))|(\/gallery\/(?:[/|.|\w|\s|-])*))/g;
const youtubeVideoRegex = /((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)/g
exports.EmmeliaEmbedBuilder = class PostEmbedBuilder extends EmbedBuilder {
constructor() {
@@ -74,10 +76,24 @@ exports.onError = (error) => {
exports.sendPost = (post, source, channel, callback) => {
log.DEBUG("Sending post from source: ", post, source);
post.content = parse(post.content);
// Reset the content parameter with the encoded parameter
post.content = parse(post['content:encoded'] ?? post.content);
const postTitle = post.title;
const postLink = post.link;
const postContent = NodeHtmlMarkdown.translate(post.content.text);
// Get the post content and trim it to length or add a placeholder if necessary
var postText = String(post.content.text);
if (postText.length >= 300) postText = `${postText.slice(0, 300).substring(0, Math.min(String(post.content.text).length, String(post.content.text).lastIndexOf(" ")))}...`;
else if (postText.length === 0) postText = `*This post has no content* [Direct Link](${post.link})`;
var postContent = postText;
// Check for embedded youtube videos and add the first four as links
const ytVideos = String(post.content).match(youtubeVideoRegex);
if (ytVideos) {
for (const ytVideo of ytVideos.slice(0,4)){
// If the video is an embed, replace the embed to make it watchable
if (ytVideo.includes("embed")) ytVideo = ytVideo.replace("embed/", "watch?v=");
postContent += `\nEmbeded Video from Post: [YouTube](${ytVideo})`
}
}
log.DEBUG("Post content: ", postContent);
const postId = post.postId;
@@ -89,7 +105,9 @@ exports.sendPost = (post, source, channel, callback) => {
const linksInPost = post.content.querySelectorAll("a");
if (linksInPost) {
log.DEBUG("Found links in post:", linksInPost);
for (const link of linksInPost) {
for (const link of linksInPost) {
// Check to see if this link is a youtube video that was already found, if so skip it
if (ytVideos?.includes(link)) continue;
const images = String(link.getAttribute("href")).match(imageRegex);
log.DEBUG("Images found in post:", images);
if (images) {
@@ -99,13 +117,13 @@ exports.sendPost = (post, source, channel, callback) => {
}
}
log.DEBUG("Sending an RSS post to discord", postTitle, postId)
log.DEBUG("Sending an RSS post to discord", postTitle, postId, postContent)
try{
const rssMessage = new this.EmmeliaEmbedBuilder()
.setColor(0x0099FF)
.setTitle(postTitle)
.setURL(postLink)
.addFields({ name: "Post Content", value: postContent.slice(0,1024), inline: false })
.addFields({ name: "Post Content", value: postContent, inline: false })
.addFields({ name: 'Published', value: postPubDate, inline: true })
.addFields({ name: 'Source', value: postSourceLink, inline: true });
@@ -125,4 +143,8 @@ exports.sendPost = (post, source, channel, callback) => {
log.ERROR("Error sending message: ", err);
return callback(err, undefined);
}
}
exports.returnHash = (...stringsIncluded) => {
return crypto.createHash('sha1').update(`${stringsIncluded.join("-<<??//\\\\??>>-")}`).digest("base64");
}