How to implement a login function to view content, in order to better attract users to register

This is easy to do. There is an attribute in the topic model that shows whether you have replied to it. It is called posted. I wrote a component before ( Composer tip under specific tag topics ) that determines whether you have replied. If you have a programming background, you can take a look at what I wrote. Combined with the decorateCookedElement method of the API, it can be implemented to hide content when not replied to.

I am too lazy to write the specific code. I have been very busy recently.

<script type="text/discourse-plugin" version="1.6.0">

const user = api.getCurrentUser();

const I18n = require("I18n");

const pid = "post_hider"

const tl4_css = `
.d-editor-preview [data-theme-hide] {
  background: var(--tertiary);
  color: var(--secondary);
  border-top: 2px solid var(--secondary);
  position: sticky;
  top: 0;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.d-editor-preview [data-theme-hide]::before {
  content: "This post is hidden";
}
`

api.addPostClassesCallback((attrs) => {
if (attrs?.cooked?.includes(`div data-theme-hide="true"`)) {
  return ["hiddened"]; // For adding CSS, how you write the CSS next is up to you
} else {
  return [];
}
});

if (!user) {
    api.decorateCookedElement((el, helper) => {
        function hide_post() {
            setTimeout( () => {
                try {
                    // document.querySelector(`[data-post-id="${helper?.widget?.attrs?.id}"]`)?.parentElement?.remove();
                } catch (err) {
                    console.log(el);
                    console.log(err);
                }
            }, 1000);
        }
        if (el?.querySelector(`[data-theme-hide="true"]`)) {
            el.innerHTML = `<p>Post hidden by community</p>`; // Modify this line
            hide_post();
        }
    }, {
        id: pid,
        afterAdopt: true,
        onlyStream: true,
    });
}


</script>

I remembered that I have a piece of code here that I used to hide the entire post from non-logged-in users. You can use it for reference.

4 Likes