Find a user by ID or username?

(Niels) #1

And another “simple” question from me and basically a follow up to Showing simple text with decorateWidget

I want to decorate the post with a custom field from the posting user. To do this, I want to get the user object. I already realized, that I can get the username or user_id from the helper, i.e.,

var username = helper.attrs.username
var user_id = helper.attrs.user_id

How can I reach the user object from there? I found something about a User.find() method somewhere, but I’m not able to access it even though I import User from discourse/models/user. I also try to user Discourse.User.findByUsername(), but that returns a Promise and I have no idea how to handle that.

Do I need some additional imports or am I doing things wrong? Thanks a lot!

(Vinoth Kannan) #2

Through plugin API you can do below

Then you can get that addition attributes like helper.attrs.poster_age and helper.attrs.poster_height. Also don’t forgot to add these attributes in post serializer.

(Niels) #3

That’s a nice idea. However, I don’t think it will work in my specific case. I didn’t mention that the shown information should be restricted.

How I understand the serializer, the data would be sent to every user within the json file. Thus, the data would be accessible for everyone. Or is it possible to only add the data to the serializer for specific users?

(Vinoth Kannan) #4

You can add any conditions on serializer. You may restrict guests, groups, non-staff users, etc.,