discourse/lib/pretty_text/shims.js
Martin Brennan b2acc416e7
FIX: Server-side hashtag lookups of secure categories for a user (#19377)
* FIX: Use Category.secured(guardian) for hashtag datasource

Follow up to comments in #19219, changing the category
hashtag datasource to use Category.secured(guardian) instead
of Site.new(guardian).categories here since the latter does
more work for not much benefit, and the query time is the
same. Also eliminates some Hash -> Model back and forth
busywork. Add some more specs too.

* FIX: Server-side hashtag lookup cooking user loading

When we were using the PrettyText.options.currentUser
and parsing back and forth with JSON for the hashtag
lookups server-side, we had a bug where the user's
secure categories were not loaded since we never actually
loaded a User model from the database, only parsed it
from JSON.

This commit fixes the issue by instead using the
PretyText.options.userId and looking up the user directly
from the database when calling hashtag_lookup via the
PrettyText::Helpers code when cooking server-side. Added
the missing spec to check for this as well.
2022-12-09 10:34:25 +10:00

138 lines
3.4 KiB
JavaScript

__PrettyText = require("pretty-text/pretty-text").default;
__buildOptions = require("pretty-text/pretty-text").buildOptions;
__performEmojiUnescape = require("pretty-text/emoji").performEmojiUnescape;
__emojiReplacementRegex = require("pretty-text/emoji").emojiReplacementRegex;
__performEmojiEscape = require("pretty-text/emoji").performEmojiEscape;
__resetTranslationTree =
require("pretty-text/engines/discourse-markdown/emoji").resetTranslationTree;
I18n = {
t(a, b) {
return __helpers.t(a, b);
},
};
define("I18n", ["exports"], function (exports) {
exports.default = I18n;
});
// Formatting doesn't currently need any helper context
define("discourse-common/lib/helpers", ["exports"], function (exports) {
exports.helperContext = function () {
return {};
};
});
__utils = require("discourse/lib/utilities");
__emojiUnicodeReplacer = null;
__setUnicode = function (replacements) {
const regexp = new RegExp(__emojiReplacementRegex, "g");
__emojiUnicodeReplacer = function (text) {
regexp.lastIndex = 0;
let m;
while ((m = regexp.exec(text)) !== null) {
let match = m[0];
let replacement = replacements[match];
if (!replacement) {
// if we can't find replacement for an emoji match
// attempts to look for the same without trailing variation selector
match = match.replace(/\ufe0f$/g, "");
replacement = replacements[match];
}
if (!replacement) {
continue;
}
replacement = ":" + replacement + ":";
const before = text.charAt(m.index - 1);
if (!/\B/.test(before)) {
replacement = "\u200b" + replacement;
}
text = text.replace(match, replacement);
}
// fixes Safari VARIATION SELECTOR-16 issue with some emojis
// https://meta.discourse.org/t/emojis-selected-on-ios-displaying-additional-rectangles/86132
text = text.replace(/\ufe0f/g, "");
return text;
};
};
__paths = {};
function __getURLNoCDN(url) {
if (!url) {
return url;
}
// if it's a non relative URL, return it.
if (url !== "/" && !/^\/[^\/]/.test(url)) {
return url;
}
if (url.includes(__paths.baseUri)) {
return url;
}
if (url[0] !== "/") {
url = "/" + url;
}
return __paths.baseUri + url;
}
function __getURL(url) {
url = __getURLNoCDN(url);
// only relative urls
if (__paths.CDN && /^\\\/[^\\\/]/.test(url)) {
url = __paths.CDN + url;
} else if (__paths.S3CDN) {
url = url.replace(__paths.S3BaseUrl, __paths.S3CDN);
}
return url;
}
function __lookupUploadUrls(urls) {
return __helpers.lookup_upload_urls(urls);
}
function __getTopicInfo(i) {
return __helpers.get_topic_info(i);
}
// TODO (martin) Remove this when everything is using hashtag_lookup
// after enable_experimental_hashtag_autocomplete is default.
function __categoryLookup(c) {
return __helpers.category_tag_hashtag_lookup(c);
}
function __hashtagLookup(slug, cookingUserId, typesInPriorityOrder) {
return __helpers.hashtag_lookup(slug, cookingUserId, typesInPriorityOrder);
}
function __lookupAvatar(p) {
return __utils.avatarImg(
{ size: "tiny", avatarTemplate: __helpers.avatar_template(p) },
__getURL
);
}
function __formatUsername(username) {
return __helpers.format_username(username);
}
function __lookupPrimaryUserGroup(username) {
return __helpers.lookup_primary_user_group(username);
}
function __getCurrentUser(userId) {
return __helpers.get_current_user(userId);
}