Request from API doesn't update User#last_seen_at

Last version is ignoring request coming from API to update user visits stats in forum.

Other stats like ‘number of likes’ or ‘pageviews’ are right, but it seems that something has happened with user visit stats.

Before, when i pulled for latest topics or post in topics, it updated the User#last_seen_at dates, but now there are now 9 days ago, I don’t have any new visit from my users, when normally i have hundreds.

When i visit the user active list page in ‘/admin/users/list/active’, i see the ‘seen’ column fixed to date when i did the update.

I updated from 2.2.0.beta3 to last current version 2.3.0.beta1 and i didn’t have any issue while updating.

Any help with this issue, is really appreciated

This is expected, “last seen” should not be updated on API requests.

https://github.com/discourse/discourse/blob/master/lib/auth/default_current_user_provider.rb#L243-L253

There was a bug for a few months which caused last_seen to be updated erroneously for API requests.

5 Likes

Very well

Thanks @david for your soon response.

So now how do i update my stats since almost 100% of my forum activities are made by the API ?

It exist any endpoint to update the user visits?

No, there isn’t one. If you have control over the API server you could try and “trick” discourse into updating last seen for the request. From the code I quoted above you can see how it works.

So you could make a request with “X-Requested-With" header containing “XMLHttpRequest” (to trick the xhr? bit), and “DISCOURSE_VISIBLE” as “true”.

Note that this wouldn’t be a “supported” way to use the API, but I don’t foresee any problems.

3 Likes

Thank you @david

But just one last question :

I don’t understand why ‘a bug’ since, if a user pull for the last topics, it is not considered as a visit ?

Thank you again for your help

The API is typically used for things where users are not present - for example the official Discourse mobile app uses the API to check for new topics in the background.

1 Like

I would expect a user viewing something via API to be the same as if they hit that API via their browser.

I feel like that is an exception to the principle of least surprise.

I understand why that decision was made, but perhaps we could accept a HTTP header or parameter that indicates if we want to treat the user as a regular user?

2 Likes

:+1: we have the DISCOURSE_VISIBLE header for XHR requests, we could certainly make that work on API requests as well.

5 Likes

You can now set DISCOURSE_VISIBLE=“true” on API requests, and Discourse will update the last_seen time for the user

https://github.com/discourse/discourse/commit/af86cf46dc003f19ba52f71826f057884f1119ba

4 Likes

This topic was automatically closed after 24 hours. New replies are no longer allowed.