Hyperlinks in ticket descriptions
This commit is contained in:
@@ -5,7 +5,7 @@ import { getE621Post, getE621PostByMd5, getPostUrl, hasBlacklistedTags } from '.
|
||||
import { Database } from '../shared/Database';
|
||||
import { logDeletion, logEdit } from '../utils/message-logger';
|
||||
import { isEdited } from '../utils/message-utils';
|
||||
import { channelIsInStaffCategory } from '../utils';
|
||||
import { blipIDRegex, channelIsInStaffCategory, commentIDRegex, forumTopicIDRegex, poolIDRegex, postIDRegex, recordIDRegex, searchLinkRegex, setIDRegex, takedownIDRegex, ticketIDRegex, userIDRegex, wikiLinkRegex } from '../utils';
|
||||
|
||||
export type Message<InGuild extends boolean = boolean> = OmitPartialGroupDMChannel<DiscordMessage<InGuild>>;
|
||||
export type Partial = OmitPartialGroupDMChannel<PartialMessage>;
|
||||
@@ -19,20 +19,6 @@ const imageRegex_DEV = new RegExp('!?https?://(?:.*@)?localhost:3000/+data/+(?:s
|
||||
|
||||
const md5Regex = new RegExp('^([a-f0-9]{32}).(?:png|apng|jpg|jpeg|gif|webm|mp4)$', 'gi');
|
||||
|
||||
const postIDRegex = new RegExp('post #([0-9]+)', 'gi');
|
||||
const userIDRegex = new RegExp('user #([0-9]+)', 'gi');
|
||||
const forumTopicIDRegex = new RegExp('topic #([0-9]+)', 'gi');
|
||||
const commentIDRegex = new RegExp('comment #([0-9]+)', 'gi');
|
||||
const blipIDRegex = new RegExp('blip #([0-9]+)', 'gi');
|
||||
const poolIDRegex = new RegExp('pool #([0-9]+)', 'gi');
|
||||
const setIDRegex = new RegExp('set #([0-9]+)', 'gi');
|
||||
const takedownIDRegex = new RegExp('takedown #([0-9]+)', 'gi');
|
||||
const recordIDRegex = new RegExp('record #([0-9]+)', 'gi');
|
||||
const ticketIDRegex = new RegExp('ticket #([0-9]+)', 'gi');
|
||||
const tagSearchRegex = '(?:[\\S]| )+?';
|
||||
const wikiLinkRegex = new RegExp(`\\[\\[(${tagSearchRegex})]]`, 'gi');
|
||||
const searchLinkRegex = new RegExp(`{{(${tagSearchRegex})}}`, 'gi');
|
||||
|
||||
const regexTesters = [
|
||||
{ runInDev: false, regex: postRegex, handler: postHandler },
|
||||
{ runInDev: false, regex: imageRegex, handler: imageHandler },
|
||||
|
||||
@@ -2,9 +2,72 @@ import { createClient } from '@redis/client';
|
||||
import { Database } from './Database';
|
||||
import { config } from '../config';
|
||||
import { APIEmbedField, Client, EmbedAuthorOptions, EmbedBuilder, SendableChannels, TextBasedChannel } from 'discord.js';
|
||||
import { humanizeCapitalization } from '../utils/string-utils';
|
||||
import { blipIDRegex, commentIDRegex, forumTopicIDRegex, humanizeCapitalization, poolIDRegex, postIDRegex, shouldAlert, recordIDRegex, searchLinkRegex, setIDRegex, takedownIDRegex, ticketIDRegex, userIDRegex, wikiLinkRegex } from '../utils';
|
||||
import { BanUpdate, Ticket, TicketPhrase, TicketUpdate } from '../types';
|
||||
import { shouldAlert } from '../utils/ticket-utils';
|
||||
|
||||
// TODO: Condense this and the message event handler regex array.
|
||||
const linkReplacers = [
|
||||
{
|
||||
regex: blipIDRegex,
|
||||
replacement: '/blips/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: commentIDRegex,
|
||||
replacement: '/comments/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: forumTopicIDRegex,
|
||||
replacement: '/forum_topics/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: poolIDRegex,
|
||||
replacement: '/pools/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: postIDRegex,
|
||||
replacement: '/posts/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: recordIDRegex,
|
||||
replacement: '/user_feedbacks/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: searchLinkRegex,
|
||||
replacement: '/posts?tags={match}',
|
||||
encodeURI: true
|
||||
},
|
||||
{
|
||||
regex: setIDRegex,
|
||||
replacement: '/post_sets/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: takedownIDRegex,
|
||||
replacement: '/takedowns/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: ticketIDRegex,
|
||||
replacement: '/tickets/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: userIDRegex,
|
||||
replacement: '/users/{match}',
|
||||
encodeURI: false
|
||||
},
|
||||
{
|
||||
regex: wikiLinkRegex,
|
||||
replacement: '/wiki_pages/{match}',
|
||||
encodeURI: true
|
||||
}
|
||||
];
|
||||
|
||||
const MAX_DESCRIPTION_LENGTH = 500;
|
||||
|
||||
@@ -119,8 +182,36 @@ function getURL(ticket: Ticket): string {
|
||||
return `${config.E621_BASE_URL}/tickets/${ticket.id}`;
|
||||
}
|
||||
|
||||
function getLinks(input: string, limit: number = Number.MAX_SAFE_INTEGER): string {
|
||||
const length = input.length;
|
||||
|
||||
const replacedIndexes: { start: number, end: number }[] = [];
|
||||
|
||||
for (const replacer of linkReplacers) {
|
||||
input = input.replaceAll(replacer.regex, (match, group1) => {
|
||||
const replaced = `[${match}](${config.E621_BASE_URL}${(replacer.replacement).replace('{match}', replacer.encodeURI ? encodeURIComponent(group1) : group1)})`;
|
||||
const start = input.indexOf(match);
|
||||
replacedIndexes.push({ start, end: start + replaced.length });
|
||||
|
||||
return replaced;
|
||||
});
|
||||
}
|
||||
|
||||
if (length > limit) {
|
||||
for (const replacedIndex of replacedIndexes) {
|
||||
if (replacedIndex.start < limit && replacedIndex.end >= limit) {
|
||||
return input.substring(0, replacedIndex.end) + '...';
|
||||
}
|
||||
}
|
||||
|
||||
return input.substring(0, limit) + '...';
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
function getDescription(ticket: Ticket): string {
|
||||
return ticket.reason.length <= MAX_DESCRIPTION_LENGTH ? ticket.reason : ticket.reason.substring(0, MAX_DESCRIPTION_LENGTH);
|
||||
return ticket.reason.length <= MAX_DESCRIPTION_LENGTH ? getLinks(ticket.reason) : getLinks(ticket.reason, MAX_DESCRIPTION_LENGTH);
|
||||
}
|
||||
|
||||
function getAuthor(ticket: Ticket): EmbedAuthorOptions {
|
||||
|
||||
@@ -12,6 +12,7 @@ export * from './ms-to-human';
|
||||
export * from './note-utils';
|
||||
export * from './record-utils';
|
||||
export * from './refresh-commands';
|
||||
export * from './search-regex';
|
||||
export * from './string-utils';
|
||||
export * from './ticket-utils';
|
||||
export * from './wait';
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
export const postIDRegex = new RegExp('post #([0-9]+)', 'gi');
|
||||
export const userIDRegex = new RegExp('user #([0-9]+)', 'gi');
|
||||
export const forumTopicIDRegex = new RegExp('topic #([0-9]+)', 'gi');
|
||||
export const commentIDRegex = new RegExp('comment #([0-9]+)', 'gi');
|
||||
export const blipIDRegex = new RegExp('blip #([0-9]+)', 'gi');
|
||||
export const poolIDRegex = new RegExp('pool #([0-9]+)', 'gi');
|
||||
export const setIDRegex = new RegExp('set #([0-9]+)', 'gi');
|
||||
export const takedownIDRegex = new RegExp('takedown #([0-9]+)', 'gi');
|
||||
export const recordIDRegex = new RegExp('record #([0-9]+)', 'gi');
|
||||
export const ticketIDRegex = new RegExp('ticket #([0-9]+)', 'gi');
|
||||
|
||||
const tagSearchRegex = '(?:[\\S]| )+?';
|
||||
export const wikiLinkRegex = new RegExp(`\\[\\[(${tagSearchRegex})]]`, 'gi');
|
||||
export const searchLinkRegex = new RegExp(`{{(${tagSearchRegex})}}`, 'gi');
|
||||
Reference in New Issue
Block a user