From dfe2e937593e82feb31a385c446642107721c735 Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Fri, 16 Feb 2024 16:58:07 +0100 Subject: [PATCH] Fix possible error if discussion relation is false --- LICENSE.txt | 2 +- js/dist/forum.js | 2 +- js/dist/forum.js.map | 2 +- js/src/forum/index.ts | 15 +++++++++++++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/LICENSE.txt b/LICENSE.txt index 4dfb544..2317691 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Clark Winkelmann +Copyright (c) 2022-2024 Clark Winkelmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/js/dist/forum.js b/js/dist/forum.js index 2b2a5b9..b26de00 100644 --- a/js/dist/forum.js +++ b/js/dist/forum.js @@ -1,2 +1,2 @@ -(()=>{var t={n:e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return t.d(r,{a:r}),r},d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};(()=>{"use strict";t.r(e),t.d(e,{filterPostMentions:()=>h});const r=flarum.core.compat["forum/app"];var n=t.n(r);const o=flarum.core.compat["common/extend"],s=flarum.core.compat["common/utils/extractText"];var a=t.n(s);const i=flarum.core.compat["common/components/Link"];var c=t.n(i);const u=flarum.core.compat["forum/components/CommentPost"];var l=t.n(u);const d=flarum.core.compat["forum/components/PostPreview"];var p=t.n(d);function f(t){return m("span.ProminentPostNumber",(n().forum.attribute("prominentPostNumberFormat")||"").replace("{number}",t.number()+""))}function h(t){var e=n().store.getById("posts",t.getAttribute("id"));e&&(e.user()||t.setAttribute("displayname",""))}n().initializers.add("prominent-post-numbers",(function(){(0,o.extend)(l().prototype,"headerItems",(function(t){var e=this.attrs.post;t.add("number",f(e),1)})),(0,o.extend)(p().prototype,"view",(function(t){var e=this;t.children.forEach((function(t){t&&t.attrs&&t.attrs.className&&-1!==t.attrs.className.indexOf("PostPreview-content")&&(e.attrs.post.user()||t.children.forEach((function(e,r){e&&"span"===e.tag&&e.text===a()(n().translator.trans("core.lib.username.deleted_text"))&&t.children.splice(r,1)})),t.children.splice(1,0,f(e.attrs.post),{tag:"#",children:" "}))}))})),(0,o.extend)(c().prototype,"view",(function(t){if("data-number"in this.attrs){var e=parseInt(this.attrs["data-number"]),r=n().route("discussion",{id:""});if(this.attrs.href&&0===this.attrs.href.indexOf(r)){var o=this.attrs.href.substr(r.length).split("/")[0],s=n().store.all("posts").find((function(t){var r;return t.number()===e&&(null==(r=t.discussion())?void 0:r.slug())===o}));s&&(t.children.unshift(f(s)," "),s.user()||t.children.forEach((function(e,r){e&&"span"===e.tag&&e.text===a()(n().translator.trans("core.lib.username.deleted_text"))&&t.children.splice(r,1)})))}}}))}))})(),module.exports=e})(); +(()=>{var t={n:e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return t.d(r,{a:r}),r},d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};(()=>{"use strict";t.r(e),t.d(e,{filterPostMentions:()=>h});const r=flarum.core.compat["forum/app"];var n=t.n(r);const o=flarum.core.compat["common/extend"],s=flarum.core.compat["common/utils/extractText"];var a=t.n(s);const i=flarum.core.compat["common/components/Link"];var c=t.n(i);const u=flarum.core.compat["forum/components/CommentPost"];var l=t.n(u);const d=flarum.core.compat["forum/components/PostPreview"];var p=t.n(d);function f(t){return m("span.ProminentPostNumber",(n().forum.attribute("prominentPostNumberFormat")||"").replace("{number}",t.number()+""))}function h(t){var e=n().store.getById("posts",t.getAttribute("id"));e&&(e.user()||t.setAttribute("displayname",""))}n().initializers.add("prominent-post-numbers",(function(){(0,o.extend)(l().prototype,"headerItems",(function(t){var e=this.attrs.post;t.add("number",f(e),1)})),(0,o.extend)(p().prototype,"view",(function(t){var e=this;t.children.forEach((function(t){t&&t.attrs&&t.attrs.className&&-1!==t.attrs.className.indexOf("PostPreview-content")&&(e.attrs.post.user()||t.children.forEach((function(e,r){e&&"span"===e.tag&&e.text===a()(n().translator.trans("core.lib.username.deleted_text"))&&t.children.splice(r,1)})),t.children.splice(1,0,f(e.attrs.post),{tag:"#",children:" "}))}))})),(0,o.extend)(c().prototype,"view",(function(t){if("data-number"in this.attrs){var e=parseInt(this.attrs["data-number"]),r=n().route("discussion",{id:""});if(this.attrs.href&&0===this.attrs.href.indexOf(r)){var o=this.attrs.href.substring(r.length).split("/")[0],s=n().store.all("posts").find((function(t){if(t.number()!==e)return!1;var r=t.discussion();return!!r&&r.slug()===o}));s&&(t.children.unshift(f(s)," "),s.user()||t.children.forEach((function(e,r){e&&"span"===e.tag&&e.text===a()(n().translator.trans("core.lib.username.deleted_text"))&&t.children.splice(r,1)})))}}}))}))})(),module.exports=e})(); //# sourceMappingURL=forum.js.map \ No newline at end of file diff --git a/js/dist/forum.js.map b/js/dist/forum.js.map index 0987363..27ef53b 100644 --- a/js/dist/forum.js.map +++ b/js/dist/forum.js.map @@ -1 +1 @@ -{"version":3,"file":"forum.js","mappings":"MACA,IAAIA,EAAsB,CCA1BA,EAAyBC,IACxB,IAAIC,EAASD,GAAUA,EAAOE,WAC7B,IAAOF,EAAiB,QACxB,IAAM,EAEP,OADAD,EAAoBI,EAAEF,EAAQ,CAAEG,EAAGH,IAC5BA,GCLRF,EAAwB,CAACM,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXP,EAAoBS,EAAEF,EA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AAKQ,QACjBY,EAAQF,SAASC,SAAQ,SAACI,EAAOC,GACzBD,GAAuB,SAAdA,EAAMjB,KAAkBiB,EAAME,OAASC,GAAAA,CAAYxB,IAAAA,WAAAA,MAAqB,oCACjFkB,EAAQF,SAASS,OAAOH,EAAO,MAK3CJ,EAAQF,SAASS,OAAO,EAAG,EAAG5B,EAAoB,EAAKe,MAAMd,MAAO,CAChEM,IAAK,IACLY,SAAU,cAOtBR,EAAAA,EAAAA,QAAOkB,IAAAA,UAAgB,QAAQ,SAAUX,GAErC,GAAM,gBAAiBJ,KAAKC,MAA5B,CAMA,IAAMV,EAASyB,SAAShB,KAAKC,MAAM,gBAE7BgB,EAAc5B,IAAAA,MAAU,aAAc,CACxC6B,GAAI,KAIR,GAAKlB,KAAKC,MAAMkB,MAAiD,IAAzCnB,KAAKC,MAAMkB,KAAKV,QAAQQ,GAAhD,CAIA,IAAMG,EAAiBpB,KAAKC,MAAMkB,KAAKE,OAAOJ,EAAYK,QAAQC,MAAM,KAAK,GAEvEpC,EAAOE,IAAAA,MAAAA,IAAoB,SAASmC,MAAK,SAAArC,GAAQ,MACnD,OAAOA,EAAKI,WAAaA,IAAU,SAAAJ,EAAKsC,mBAAL,IAAmBC,UAAWN,KAIhEjC,IAKLiB,EAAKC,SAASsB,QAAQzC,EAAoBC,GAAO,KAG7CA,EAAKQ,QAKTS,EAAKC,SAASC,SAAQ,SAACI,EAAOC,GACtBD,GAAuB,SAAdA,EAAMjB,KAAkBiB,EAAME,OAASC,GAAAA,CAAYxB,IAAAA,WAAAA,MAAqB,oCACjFe,EAAKC,SAASS,OAAOH,EAAO,gB","sources":["webpack://@clarkwinkelmann/prominent-post-numbers/webpack/bootstrap","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/compat get default export","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/define property getters","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/hasOwnProperty shorthand","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/make namespace object","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['forum/app']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['common/extend']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['common/utils/extractText']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['common/components/Link']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['forum/components/CommentPost']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['forum/components/PostPreview']\"","webpack://@clarkwinkelmann/prominent-post-numbers/./src/forum/index.ts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// getDefaultExport function for compatibility with non-harmony modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/app'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/extend'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/utils/extractText'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/components/Link'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/components/CommentPost'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/components/PostPreview'];","import app from 'flarum/forum/app';\nimport {extend} from 'flarum/common/extend';\nimport extractText from 'flarum/common/utils/extractText';\nimport Post from 'flarum/common/models/Post';\nimport Link from 'flarum/common/components/Link';\nimport CommentPost from 'flarum/forum/components/CommentPost';\nimport PostPreview from 'flarum/forum/components/PostPreview';\n\nfunction prominentPostNumber(post: Post) {\n return m('span.ProminentPostNumber', (app.forum.attribute('prominentPostNumberFormat') || '').replace('{number}', post.number() + ''));\n}\n\napp.initializers.add('prominent-post-numbers', () => {\n extend(CommentPost.prototype, 'headerItems', function (items) {\n // @ts-ignore\n const post = this.attrs.post as Post;\n\n // We want to add it just before PostMeta, unfortunately PostMeta doesn't have a priority\n // Luckily it's the first item without a priority so priority 1 should put us just before\n items.add('number', prominentPostNumber(post), 1);\n });\n\n extend(PostPreview.prototype, 'view', function (vdom) {\n vdom.children.forEach(preview => {\n if (!preview || !preview.attrs || !preview.attrs.className || preview.attrs.className.indexOf('PostPreview-content') === -1) {\n return;\n }\n\n if (!this.attrs.post.user()) {\n preview.children.forEach((child, index) => {\n if (child && child.tag === 'span' && child.text === extractText(app.translator.trans('core.lib.username.deleted_text'))) {\n preview.children.splice(index, 1);\n }\n });\n }\n\n preview.children.splice(1, 0, prominentPostNumber(this.attrs.post), {\n tag: '#', // The space must be inserted in vdom way to be valid\n children: ' ',\n });\n });\n });\n\n // There's no easy way to override Mention's template in addMentionedByList / CommentPost.prototype.footerItems\n // To avoid re-implementing the full method, we'll cheat by hooking into Link directly\n extend(Link.prototype, 'view', function (vdom) {\n // Links added by Mentions have a data-number attribute, skip if missing\n if (!('data-number' in this.attrs)) {\n return;\n }\n\n // It's simpler to retrieve the number from the data-attribute\n // Trying to read the href would be more complicated because first post link would not contain the number\n const number = parseInt(this.attrs['data-number']);\n\n const routePrefix = app.route('discussion', {\n id: '',\n });\n\n // Skip links that aren't to discussions/posts\n if (!this.attrs.href || this.attrs.href.indexOf(routePrefix) !== 0) {\n return;\n }\n\n const discussionSlug = this.attrs.href.substr(routePrefix.length).split('/')[0];\n\n const post = app.store.all('posts').find(post => {\n return post.number() === number && post.discussion()?.slug() === discussionSlug;\n });\n\n // If the post can't be found, ignore\n if (!post) {\n return;\n }\n\n // Add number in front\n vdom.children.unshift(prominentPostNumber(post), ' ');\n\n // If the post has an author, don't change anything else\n if (post.user()) {\n return;\n }\n\n // Remove [deleted] text\n vdom.children.forEach((child, index) => {\n if (child && child.tag === 'span' && child.text === extractText(app.translator.trans('core.lib.username.deleted_text'))) {\n vdom.children.splice(index, 1);\n }\n });\n });\n});\n\n// We don't need to validate or invalidate the tag here because Mention's original filter still runs\n// We'll just change the display name\nexport function filterPostMentions(tag: any) {\n const post = app.store.getById('posts', tag.getAttribute('id'));\n\n if (post) {\n const user = post.user();\n\n // Remove [deleted] text from mention\n if (!user) {\n tag.setAttribute('displayname', '');\n }\n }\n}\n"],"names":["__webpack_require__","module","getter","__esModule","d","a","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","flarum","core","compat","prominentPostNumber","post","m","app","replace","number","filterPostMentions","tag","getAttribute","user","setAttribute","extend","CommentPost","items","this","attrs","add","PostPreview","vdom","children","forEach","preview","className","indexOf","child","index","text","extractText","splice","Link","parseInt","routePrefix","id","href","discussionSlug","substr","length","split","find","discussion","slug","unshift"],"sourceRoot":""} \ No newline at end of file +{"version":3,"file":"forum.js","mappings":"MACA,IAAIA,EAAsB,CCA1BA,EAAyBC,IACxB,IAAIC,EAASD,GAAUA,EAAOE,WAC7B,IAAOF,EAAiB,QACxB,IAAM,EAEP,OADAD,EAAoBI,EAAEF,EAAQ,CAAEG,EAAGH,IAC5BA,GCLRF,EAAwB,CAACM,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXP,EAAoBS,EAAEF,EAAYC,KAASR,EAAoBS,EAAEH,EAASE,IAC5EE,OAAOC,eA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BD,GAAuB,SAAdA,EAAMjB,KAAkBiB,EAAME,OAASC,GAAAA,CAAYxB,IAAAA,WAAAA,MAAqB,oCACjFkB,EAAQF,SAASS,OAAOH,EAAO,MAK3CJ,EAAQF,SAASS,OAAO,EAAG,EAAG5B,EAAoB,EAAKe,MAAMd,MAAO,CAChEM,IAAK,IACLY,SAAU,cAOtBR,EAAAA,EAAAA,QAAOkB,IAAAA,UAAgB,QAAQ,SAAUX,GAErC,GAAM,gBAAiBJ,KAAKC,MAA5B,CAMA,IAAMV,EAASyB,SAAShB,KAAKC,MAAM,gBAE7BgB,EAAc5B,IAAAA,MAAU,aAAc,CACxC6B,GAAI,KAIR,GAAKlB,KAAKC,MAAMkB,MAAiD,IAAzCnB,KAAKC,MAAMkB,KAAKV,QAAQQ,GAAhD,CAIA,IAAMG,EAAiBpB,KAAKC,MAAMkB,KAAKE,UAAUJ,EAAYK,QAAQC,MAAM,KAAK,GAE1EpC,EAAOE,IAAAA,MAAAA,IAAoB,SAASmC,MAAK,SAAArC,GAC3C,GAAIA,EAAKI,WAAaA,EAClB,OAAO,EAGX,IAAMkC,EAAatC,EAAKsC,aAGxB,QAAKA,GAIEA,EAAWC,SAAWN,KAI5BjC,IAKLiB,EAAKC,SAASsB,QAAQzC,EAAoBC,GAAO,KAG7CA,EAAKQ,QAKTS,EAAKC,SAASC,SAAQ,SAACI,EAAOC,GACtBD,GAAuB,SAAdA,EAAMjB,KAAkBiB,EAAME,OAASC,GAAAA,CAAYxB,IAAAA,WAAAA,MAAqB,oCACjFe,EAAKC,SAASS,OAAOH,EAAO,gB","sources":["webpack://@clarkwinkelmann/prominent-post-numbers/webpack/bootstrap","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/compat get default export","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/define property getters","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/hasOwnProperty shorthand","webpack://@clarkwinkelmann/prominent-post-numbers/webpack/runtime/make namespace object","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['forum/app']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['common/extend']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['common/utils/extractText']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['common/components/Link']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['forum/components/CommentPost']\"","webpack://@clarkwinkelmann/prominent-post-numbers/external root \"flarum.core.compat['forum/components/PostPreview']\"","webpack://@clarkwinkelmann/prominent-post-numbers/./src/forum/index.ts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// getDefaultExport function for compatibility with non-harmony modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/app'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/extend'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/utils/extractText'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['common/components/Link'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/components/CommentPost'];","const __WEBPACK_NAMESPACE_OBJECT__ = flarum.core.compat['forum/components/PostPreview'];","import app from 'flarum/forum/app';\nimport {extend} from 'flarum/common/extend';\nimport extractText from 'flarum/common/utils/extractText';\nimport Post from 'flarum/common/models/Post';\nimport Link from 'flarum/common/components/Link';\nimport CommentPost from 'flarum/forum/components/CommentPost';\nimport PostPreview from 'flarum/forum/components/PostPreview';\n\nfunction prominentPostNumber(post: Post) {\n return m('span.ProminentPostNumber', (app.forum.attribute('prominentPostNumberFormat') || '').replace('{number}', post.number() + ''));\n}\n\napp.initializers.add('prominent-post-numbers', () => {\n extend(CommentPost.prototype, 'headerItems', function (items) {\n // @ts-ignore\n const post = this.attrs.post as Post;\n\n // We want to add it just before PostMeta, unfortunately PostMeta doesn't have a priority\n // Luckily it's the first item without a priority so priority 1 should put us just before\n items.add('number', prominentPostNumber(post), 1);\n });\n\n extend(PostPreview.prototype, 'view', function (vdom) {\n vdom.children.forEach(preview => {\n if (!preview || !preview.attrs || !preview.attrs.className || preview.attrs.className.indexOf('PostPreview-content') === -1) {\n return;\n }\n\n if (!this.attrs.post.user()) {\n preview.children.forEach((child, index) => {\n if (child && child.tag === 'span' && child.text === extractText(app.translator.trans('core.lib.username.deleted_text'))) {\n preview.children.splice(index, 1);\n }\n });\n }\n\n preview.children.splice(1, 0, prominentPostNumber(this.attrs.post), {\n tag: '#', // The space must be inserted in vdom way to be valid\n children: ' ',\n });\n });\n });\n\n // There's no easy way to override Mention's template in addMentionedByList / CommentPost.prototype.footerItems\n // To avoid re-implementing the full method, we'll cheat by hooking into Link directly\n extend(Link.prototype, 'view', function (vdom) {\n // Links added by Mentions have a data-number attribute, skip if missing\n if (!('data-number' in this.attrs)) {\n return;\n }\n\n // It's simpler to retrieve the number from the data-attribute\n // Trying to read the href would be more complicated because first post link would not contain the number\n const number = parseInt(this.attrs['data-number']);\n\n const routePrefix = app.route('discussion', {\n id: '',\n });\n\n // Skip links that aren't to discussions/posts\n if (!this.attrs.href || this.attrs.href.indexOf(routePrefix) !== 0) {\n return;\n }\n\n const discussionSlug = this.attrs.href.substring(routePrefix.length).split('/')[0];\n\n const post = app.store.all('posts').find(post => {\n if (post.number() !== number) {\n return false;\n }\n\n const discussion = post.discussion();\n\n // Can't use ?. operator to call slug() below because the relation can also be false\n if (!discussion) {\n return false;\n }\n\n return discussion.slug() === discussionSlug;\n });\n\n // If the post can't be found, ignore\n if (!post) {\n return;\n }\n\n // Add number in front\n vdom.children.unshift(prominentPostNumber(post), ' ');\n\n // If the post has an author, don't change anything else\n if (post.user()) {\n return;\n }\n\n // Remove [deleted] text\n vdom.children.forEach((child, index) => {\n if (child && child.tag === 'span' && child.text === extractText(app.translator.trans('core.lib.username.deleted_text'))) {\n vdom.children.splice(index, 1);\n }\n });\n });\n});\n\n// We don't need to validate or invalidate the tag here because Mention's original filter still runs\n// We'll just change the display name\nexport function filterPostMentions(tag: any) {\n const post = app.store.getById('posts', tag.getAttribute('id'));\n\n if (post) {\n const user = post.user();\n\n // Remove [deleted] text from mention\n if (!user) {\n tag.setAttribute('displayname', '');\n }\n }\n}\n"],"names":["__webpack_require__","module","getter","__esModule","d","a","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","flarum","core","compat","prominentPostNumber","post","m","app","replace","number","filterPostMentions","tag","getAttribute","user","setAttribute","extend","CommentPost","items","this","attrs","add","PostPreview","vdom","children","forEach","preview","className","indexOf","child","index","text","extractText","splice","Link","parseInt","routePrefix","id","href","discussionSlug","substring","length","split","find","discussion","slug","unshift"],"sourceRoot":""} \ No newline at end of file diff --git a/js/src/forum/index.ts b/js/src/forum/index.ts index d418abf..a37e3ed 100644 --- a/js/src/forum/index.ts +++ b/js/src/forum/index.ts @@ -62,10 +62,21 @@ app.initializers.add('prominent-post-numbers', () => { return; } - const discussionSlug = this.attrs.href.substr(routePrefix.length).split('/')[0]; + const discussionSlug = this.attrs.href.substring(routePrefix.length).split('/')[0]; const post = app.store.all('posts').find(post => { - return post.number() === number && post.discussion()?.slug() === discussionSlug; + if (post.number() !== number) { + return false; + } + + const discussion = post.discussion(); + + // Can't use ?. operator to call slug() below because the relation can also be false + if (!discussion) { + return false; + } + + return discussion.slug() === discussionSlug; }); // If the post can't be found, ignore