From 798c61cfd21c052da7d4f6e310b059d6ec340e32 Mon Sep 17 00:00:00 2001 From: nour-karoui Date: Fri, 10 May 2024 17:04:28 +0200 Subject: [PATCH] add a hook to break from iframe and redirect nodebb profiles to catalyst profiles --- lib/controllers.js | 22 ++++++++ library.js | 37 ++++++++------ plugin.json | 3 +- templates/partials/chats/message.tpl | 76 ---------------------------- templates/partials/topic/post.tpl | 3 +- 5 files changed, 47 insertions(+), 94 deletions(-) delete mode 100644 templates/partials/chats/message.tpl diff --git a/lib/controllers.js b/lib/controllers.js index 379ec06..6d92081 100644 --- a/lib/controllers.js +++ b/lib/controllers.js @@ -11,6 +11,28 @@ Controllers.renderAdminPage = (req, res) => { }); }; +Controllers.renderProfilePage = async (req, res) => { + res.send(` + + + + + `); + res.end(); +}; + Controllers.renderThemeSettings = async (req, res, next) => { const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query); if (!userData) { diff --git a/library.js b/library.js index ccb3414..5ce0c7f 100644 --- a/library.js +++ b/library.js @@ -191,20 +191,6 @@ library.filterTeasersConfigureStripTags = function (hookData) { return hookData; }; -library.filterProfileLinks = async function (data) { - const postContent = data.postData.content; - const config = await library.loadThemeConfig(0); - - const regex = /@(.*)<\/a>/gi; - const newContent = postContent.replace(regex, (atag, schemehost, host, uid, uname) => { - let result = atag.replace(`${schemehost}/uid`, config.catalystProfileUrl); - result = result.replace(`class="`, `target="_top" class="`); - return result; - }); - - return { ...data, postData: { ...data.postData, content: newContent } }; -}; - library.createUser = function (data, callback) { data.user.did = data.data.did; callback(null, data); @@ -231,7 +217,7 @@ const getChatroomForGroup = async (slug) => { }; library.addChatToCategory = function (data, callback) { - privileges.categories.list(data.category.cid).then((res, err) => { + privileges.categories.list(data.category.cid).then((res) => { const projectGroup = res.groups.find(group => group.isPrivate && group.name.startsWith('Project ')); if (!projectGroup) { return callback(null, data); @@ -243,3 +229,24 @@ library.addChatToCategory = function (data, callback) { }); }); }; +const removeTrailingSlash = (url) => { + if (url.endsWith('/')) { + return url.slice(0, -1); + } + return url; +}; + +library.redirectToCatalystProfile = async function (data) { + const config = await library.loadThemeConfig(0); + const baseUrl = removeTrailingSlash(config.catalystUrl.replace('https://', '')); + return controllers.renderProfilePage( + { + params: { + ...data.req.params, + redirectUrl: `https://${baseUrl}/profile/${data.templateData.username}`, + }, + ...data.req, + }, + data.res + ); +}; diff --git a/plugin.json b/plugin.json index c171386..72747dd 100644 --- a/plugin.json +++ b/plugin.json @@ -16,7 +16,8 @@ { "hook": "filter:user.whitelistFields", "method": "whitelistFields" }, { "hook": "filter:category.get", "method": "addChatToCategory"}, - { "hook": "filter:parse.post", "method": "filterProfileLinks", "priority": 11} + { "hook": "filter:parse.post", "method": "filterProfileLinks", "priority": 11}, + { "hook": "filter:account/profile.build", "method": "redirectToCatalystProfile", "priority": 11} ], "scripts": [ diff --git a/templates/partials/chats/message.tpl b/templates/partials/chats/message.tpl deleted file mode 100644 index 938dbc7..0000000 --- a/templates/partials/chats/message.tpl +++ /dev/null @@ -1,76 +0,0 @@ -
  • - - {{{ if messages.parent }}} - - {{{ end }}} - -
    - - - - {messages.fromUser.displayname} - - {{{ if messages.fromUser.banned }}} - [[user:banned]] - {{{ end }}} - {{{ if messages.fromUser.deleted }}} - [[user:deleted]] - {{{ end }}} - - -
    -
    -
    -
    - {messages.content} -
    - -
    -
    - - - -
    - - -
    -
    -
    -
    -
  • \ No newline at end of file diff --git a/templates/partials/topic/post.tpl b/templates/partials/topic/post.tpl index 29446aa..063e860 100644 --- a/templates/partials/topic/post.tpl +++ b/templates/partials/topic/post.tpl @@ -47,8 +47,7 @@ - - {posts.user.displayname} + {posts.user.displayname} hello world {{{ each posts.user.selectedGroups }}}