Getting data for the widget

AI-generated summary

Getting Data for the Widget

In the context of the “Getting data for the widget” discussion, Stranik is seeking an alternative method to retrieve the time_read variable, currently obtained through a JSON API call.

Stranik mentions that the currentUser object does not contain this data and that no data is transmitted through the {{plugin-outlet}} template.

gdpelican suggests adding recent_time_read to the serialized current user fields using DiscoursePluginRegistry.serialized_current_user_fields << 'recent_time_read'. This allows access to the data through User.current().recent_time_read.

However, Stranik notes that this method may not be suitable and seeks a more efficient approach to query for the data directly with minimal load.

I don’t want to use this approach in widgets:

$.ajax({
  url: "/u/"+ username +".json", 
  dataType: 'json',
  async: false,
  success: function(data) {
	
  ....data.user.time_read;

Is there any other way to get this variable: time_read? This is an example.

from the template

discourse/app/assets/javascripts/discourse/templates/discovery.hbs

through

{{plugin-outlet name="discovery-list-container-top"
                        args=(hash category=category listLoading=loading)}}

no data is transmitted.

Is there any other way (other than json) to get this data (time_read, badge_count etc.)

Extras. currentUser does not have this data.

Thank

Do you only need it for the current user?

Yes. Only for this user.

You might try something like this:

DiscoursePluginRegistry.serialized_current_user_fields << 'recent_time_read'

to add it to the serialized current user.

User.current().recent_time_read

Keep in mind that this will only be serialized once, on app load, and that to get an updated value you’ll either need to query for it directly, or use MessageBus to ‘live update’ the user’s read_time.

What is the best way to do that is with minimal load.?

This method is not quite suitable.