Private content in public post?

Is there any way to make content within a post available only to logged in users?

The use case I’m thinking of is virtual event postings. We have a ton of working groups in our community that meet virtually. We’d like to let casual visitors know that the groups and meetings exist – and when they are happening – but we don’t want to post the zoom links publicly.

1 Like

Hey Marty!

Whispers kind of do that: Create a Whisper post

:information_source: whisper posts can’t be “unwhispered” after being posted, but can be with this theme component: Toggle Whisper 👁

2 Likes

Thanks. That comes close. The ideal thing would be a way of formatting content within a post so that the rest of the post shows up for all and a message such as “link only available to logged-in users” shows up if you’re not logged in, but it looks like whispers will work well for now.

Newbie admin here, so still trying to find my way around all the functionality.

2 Likes

I don’t know much about Discourse modifications, but that seems a bit tricky and would require a plugin if you don’t want it circumvented by someone who knows how to tweak client-side code.

1 Like

I’ve developed a theme component that allows to hide posts on the front end from users who are not logged in. Of course, if they are people with rich development experience, they can easily get it directly from the backend.

I haven’t packaged this component yet, so please use the code below directly.

Head Tag:

<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 ["lycoris-hiddened"];
} 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>This post is hidden</p>`;
            hide_post();
        }
    }, {
        id: pid,
        afterAdopt: true,
        onlyStream: true,
    });
} else if (user?.trust_level == 4) {
    const s = document.createElement("style");
    s.innerHTML = tl4_css;
    document.body.appendChild(s);
    api.decorateCookedElement((el, helper) => {
        if (el?.querySelector(`[data-theme-hide="true"]`)) {
            el.innerHTML = `<div class="d-wrap" data-wrap="tips" data-type="warn"><p>帖子被设置为对社区未登录用户隐藏</p></div> ` + el.innerHTML;
        }
    }, {
        id: pid,
        afterAdopt: true,
        onlyStream: true,
    });
    if (!I18n.translations[I18n.currentLocale()].js.composer) {
        I18n.translations[I18n.currentLocale()].js.composer = {};
    }
    I18n.translations[I18n.currentLocale()].js.composer.hide_text = " ";
    I18n.translations[I18n.currentLocale()].js.composer.make_hide = "Hide this post";
    api.modifyClass("controller:composer", {
        pluginId: pid,
        actions: {
            insertHide() {
             this.get("toolbarEvent").applySurround(
                `<div data-theme-hide="true">`,
                `</div>\n`,
                "hide_text"
              );
            },
        },
    });
    api.addToolbarPopupMenuOptionsCallback(() => {
        return {
            action: "insertHide",
            icon: "far-eye-slash",
            label: "composer.make_hide",
        };
    });
}


    
</script>

CSS

.anon .lycoris-hiddened {
    display: none;

    .post-info.edits {
        display: none;
    
    }
}

2 Likes