Emoji only mode WIP:

Fix emojis regexp. The RCPE2 library can't handle long regexp, so we
switch to Oniguruma.
This commit is contained in:
John Livingston 2024-09-05 19:00:42 +02:00
parent 1a75b30c50
commit 5db4f46421
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
4 changed files with 15 additions and 43 deletions

View File

@ -10,12 +10,12 @@ set -euo pipefail
# This script download the Prosody AppImage from the https://github.com/JohnXLivingston/prosody-appimage project.
repo_base_url='https://github.com/JohnXLivingston/prosody-appimage/releases/download'
wanted_release='v0.12.4-2'
wanted_release='v0.12.4-3'
x86_64_filename='prosody-x86_64.AppImage'
x86_64_sha256sum='664d9f3b1ea6dc5fdbe29ef8e8b4c0655abdff697e8c94bfecc894ef2c2fea08'
x86_64_sha256sum='83a583ac7036387514bed17afab257dab4161ccdd0ab7453818c78b51f830357'
aarch64_filename='prosody-aarch64.AppImage'
aarch64_sha256sum='9911c0d581a92a817e9795a7944773a07e85151127233a2e551eb07dc4c44fb5'
aarch64_sha256sum='7b7e6bf30d4498fc99a40022232c3065707ee4f4df24dc17947b007621634304'
download_dir="$(pwd)/vendor/prosody-appimage"
dist_dir="$(pwd)/dist/server/prosody"

View File

@ -11,7 +11,8 @@
local st = require "util.stanza";
local jid_bare = require "util.jid".bare;
local rex = require "rex_pcre2"; -- We are using PCRE2 (Perl Compatible Regular Expression)
local rex = require "rex_onig"; -- We are using Oniguruma because PCRE2 does not handle long regexp.
rex.setdefaultsyntax ("PERL");
-- Plugin dependencies
local mod_muc = module:depends "muc";
@ -87,7 +88,7 @@ function handle_groupchat(event)
if (r == nil) then
return;
end
room.x_peertubelivechat_emoji_only_compiled_regexp = rex.new(r, "i");
room.x_peertubelivechat_emoji_only_compiled_regexp = rex.new(r, "i", "UTF8");
end
-- only consider messages with body (ie: ignore chatstate and other non-text xmpp messages)

View File

@ -403,6 +403,7 @@ export class Emojis {
*/
public async getChannelEmojisOnlyRegexp (channelId: number): Promise<string | undefined> {
const parts = [...this.commonEmojisCodes]
if (await this.channelHasCustomEmojis(channelId)) {
const def = await this.channelCustomEmojisDefinition(channelId)
if (def) {
@ -411,19 +412,8 @@ export class Emojis {
}
// Note: validShortName should ensure we won't put special chars.
// And for the common emojis, we assume that there is no special regexp chars (other that +, which will be escaped).
const regexp = '^\\s*(?:(?:' + parts.map((s) => s.replace(/[+]/g, '\\$&')).join('|') + ')\\s*)+\\s*$'
// As a safety net, we check if it is a valid javascript regexp.
try {
const s = new RegExp(regexp)
if (!s) {
throw new Error('Can\'t create the RegExp from ' + regexp)
}
} catch (err) {
this.logger.error('Invalid Emoji Only regexp for channel ' + channelId.toString() + ': ' + regexp)
return undefined
}
// And for the common emojis, we assume that there is no special regexp chars
const regexp = '^\\s*(?:(?:' + parts.join('|') + ')\\s*)+\\s*$'
return regexp
}
@ -489,7 +479,7 @@ async function _getConverseEmojiCodes (options: RegisterServerOptions): Promise<
if (key === 'custom') { continue } // These are not used.
r.push(
...Object.values(block)
.map((d: any) => d.cp ? _convert(d.cp) : d.sn)
.map((d: any) => d.cp ? _emojiCpToRegexp(d.cp) : d.sn)
.filter((sn: string) => sn && sn !== '')
)
}
@ -503,38 +493,19 @@ async function _getConverseEmojiCodes (options: RegisterServerOptions): Promise<
}
/**
* Converts unicode code points and code pairs to their respective characters.
* Converts unicode code points and code pairs to the corresponding Regexp class.
* See ConverseJS emoji/utils.js for more info.
* @param {string} unicode
*/
function _convert (unicode: string): string {
function _emojiCpToRegexp (unicode: string): string {
if (unicode.includes('-')) {
const parts = []
const s = unicode.split('-')
for (let i = 0; i < s.length; i++) {
const part = parseInt(s[i], 16)
if (part >= 0x10000 && part <= 0x10FFFF) {
const hi = Math.floor((part - 0x10000) / 0x400) + 0xD800
const lo = ((part - 0x10000) % 0x400) + 0xDC00
parts.push(String.fromCharCode(hi) + String.fromCharCode(lo))
} else {
parts.push(String.fromCharCode(part))
}
parts.push('\\x{' + s[i] + '}')
}
return parts.join('')
}
return _fromCodePoint(unicode)
}
function _fromCodePoint (codepoint: string): string {
let code = typeof codepoint === 'string' ? parseInt(codepoint, 16) : codepoint
if (code < 0x10000) {
return String.fromCharCode(code)
}
code -= 0x10000
return String.fromCharCode(
0xD800 + (code >> 10),
0xDC00 + (code & 0x3FF)
)
return '\\x{' + unicode + '}'
}

View File

@ -115,7 +115,7 @@ async function updateProsodyRoom (
apiData.removeAffiliationsFor = data.removeAffiliationsFor
}
try {
logger.debug('Calling update room API on url: ' + apiUrl + ', with data: ' + JSON.stringify(apiData))
logger.debug('Calling update room API on url: ' + apiUrl)
const result = await got(apiUrl, {
method: 'POST',
headers: {