# How to get the current\_user details in Discourse.Dialect.postProcessText for both client and server side?

**URL:** <https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391>\
**Category:** Development\
**Created:** [August 9, 2016, 11:07am UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391 "2016-08-09T11:07:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishnau2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/krishnau2/32/121598_2.png) [@krishnau2](https://meta.discourse.org/u/krishnau2)\
**Post date:** [August 9, 2016, 11:07am UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/1 "2016-08-09T11:07:34Z")

</div>

Hi,

In my discourse app, I want to mask sensitive content inside posts (email/phone numbers etc.)

I wrote a plugin by following ALL CAPS plugin by @sam.

This is what I came up with

```
function mask_sensitive_content (text){
  var emailRegex = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/ig;
  var mobileRegex = /\b\+?\d[\d]{8,12}\d/g;
  
  var emailReplaceWith = ' ---@---.--- ';
  var mobileReplaceWith = ' ########## ';

  return text.replace(mobileRegex, mobileReplaceWith)
    .replace(emailRegex, emailReplaceWith);
}

Discourse.Dialect.postProcessText(function (text) {
  return mask_sensitive_content(text);
});

```

This masking works both on the client-side and server-side (the data is being written into db after masking).

But I now need to exclude admins and moderators from masking. I looked through several plugins and discussions in meta.discourse and made this:

```
Discourse.Dialect.postProcessText(function (text) {

  var current_user = null
  if(Discourse.User && Discourse.User.current()){
    current_user = Discourse.User.current();
  }

  // Not masking the content if its an admin or moderator
  if(current_user != null && (current_user.admin == false || current_user.moderato == false)){
    return mask_sensitive_content(text);
  }
});

```

This works perfectly in the editor, but when I save the content, it is not getting masked. Seems like my code doesn’t have any effect.

On further investigation I found Discourse.User.current() is causing a problem in the backend while trying to cook the content, since Discourse.User is not available there.

Do you have any hints on how I should go about this?

---

<div class="post-metadata">

**Author:** ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)\
**Post date:** [August 11, 2016, 7:08am UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/3 "2016-08-11T07:08:57Z")

</div>

Have a look at

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/pretty-text/engines/discourse-markdown/censored.js.es6](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/pretty-text/engines/discourse-markdown/censored.js.es6) 🙂

---

<div class="post-metadata">

**Author:** ![krishnau2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/krishnau2/32/121598_2.png) [@krishnau2](https://meta.discourse.org/u/krishnau2)\
**Post date:** [August 11, 2016, 9:53am UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/4 "2016-08-11T09:53:40Z")

</div>

@tgxworld Thanks for your reply, But in my case I can’t keep a list of censored words.

My intention is to censor email and phone numbers in the post with an exemption for the admin & moderator ( admin/moderator should be able to post email and mobile number).

Please advice.

---

<div class="post-metadata">

**Author:** ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)\
**Post date:** [August 11, 2016, 10:22am UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/5 "2016-08-11T10:22:05Z")

</div>

I think you misunderstood, he is showing the new way of registering your dialect, and how you can access site settings in the `registerOptions` nd assign it to `opts` to be used by your `setup` function.

---

<div class="post-metadata">

**Author:** ![krishnau2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/krishnau2/32/121598_2.png) [@krishnau2](https://meta.discourse.org/u/krishnau2)\
**Post date:** [August 11, 2016, 1:59pm UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/6 "2016-08-11T13:59:20Z")

</div>

@cpradio Thanks for the clarification, I was actually using the old plugin interface to manipulate text in the editor.

I tried the new way as shown by @tgxworld, Unfortunately even that didn’t work. I ended up in the same situation. Here is the sample code

```
import { registerOption } from 'pretty-text/pretty-text';

registerOption((siteSettings, opts) => {
  opts.features["sensitive_content_masking"] = true;
});

function maskSensitiveContent(text, user){
 
  var result = text. //Performing my regex and masking

  if(user !== null && (user.admin === true || user.moderator === true)){
    return text;
  }else{
    return result;
  }
}

export function setup(helper){
  helper.addPreProcessor(text => {
    return maskSensitiveContent(text, helper.getOptions().currentUser);
  });
}

```

This works perfectly in the editor and I can see int the preview section that the content is getting masked.  
But when I save this I am getting error `Cannot read property 'admin' of undefined/nTypeError:` of around this line

> <https://github.com/discourse/discourse/blob/main/lib/pretty_text.rb#L162>

Seems like `currentUser` or not even the `opts` is available in the server side when it try to cook with the V8 context.

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [August 11, 2016, 2:39pm UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/7 "2016-08-11T14:39:50Z")

</div>

You want two cooked per post, one for admin and one for others?

I think one cooked only is stored per post in the DB.

---

<div class="post-metadata">

**Author:** ![krishnau2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/krishnau2/32/121598_2.png) [@krishnau2](https://meta.discourse.org/u/krishnau2)\
**Post date:** [August 11, 2016, 7:15pm UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/8 "2016-08-11T19:15:36Z")

</div>

@Falco No, I need only one cooked post.

Consider this example:

Normal user creating a post with the content **_“contact me in this email [xyz@xyz.com](mailto:xyz@xyz.com)”_**  
everyone including admin should see this **_“contact me in this email —@—.—”_** The email is masked. I want to save this in DB as cooked.

If admin/moderator creating a post with content **_“send your queries to [support@abc.com](mailto:support@abc.com)”_** everyone should see this **_“send your queries to [support@abc.com](mailto:support@abc.com)”_** (without masking the email).

This is my requirement, how should I go about this?

---

<div class="post-metadata">

**Author:** ![krishnau2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/krishnau2/32/121598_2.png) [@krishnau2](https://meta.discourse.org/u/krishnau2)\
**Post date:** [August 17, 2016, 6:17am UTC](https://meta.discourse.org/t/how-to-get-the-current-user-details-in-discourse-dialect-postprocesstext-for-both-client-and-server-side/48391/9 "2016-08-17T06:17:06Z")

</div>

Hi, I am stuck in this issue, any help? Or any other way to achieve this.
