# Is \`topic\_users.last\_visited\_at\` supposed to be updated on every visit?

**URL:** https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164
**Category:** Development
**Created:** [April 7, 2016, 4:47am UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164 "2016-04-07T04:47:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [April 7, 2016, 4:47am UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/1 "2016-04-07T04:47:19Z")

</div>

Am I completely misunderstanding entirely what these values are for?

I’ve been using [Data Explorer](https://meta.discourse.org/t/32566?silent=true) to review activity and noted that `topic_users.last_visited_at` didn’t seem to be updating / showing topics I have recently visited.

Is `topic_users.last_visited_at` supposed to be updated on every visit to a topic?

Here is the Query I was using with [Data Explorer](https://meta.discourse.org/t/32566?silent=true).

```SQL
-- [params]
-- int :user_id = 1

SELECT
    topic_id,
    last_visited_at,
    first_visited_at
from topic_users
WHERE
    user_id = :user_id
ORDER BY
    topic_users.last_visited_at DESC

```

When this is executed I see NULL’s for topics I have visited and even some I have created:

 ![](https://global.discourse-cdn.com/meta/original/3X/2/f/2f9ae3a08bede6131fd5af894d5b1bd4cfb7cbbe.png)

Scrolling down further to get to some dates I see:

 ![](https://global.discourse-cdn.com/meta/original/3X/c/0/c035d1bf5bfea9f17a697d38558de51822149d3e.png)

Where I have visited each of these topics today 2016-04-07, however it looks like only the first visit was stamped.

So are `last_visited_at` and `first_visited_at` updated correctly - or am I just misunderstanding entirely what these values are for?

Is there a timestamp that’s updated when a user visits a topic?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [April 7, 2016, 5:39am UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/2 "2016-04-07T05:39:07Z")

</div>

Hmmm. Odd.

I just tried it and had similar results. NULLs until back to April 3

I was thinking that was around when I last upgraded, and since I always do a db:migrate after I update that maybe that was what was needed.  
But no joy.

Then I worried that maybe something broke with the upgrade. So I checked using different user ids.  
One had NULL back only 2 days, and another had no NULLs at all and was up-to-date.

Very puzzling.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [April 7, 2016, 10:30pm UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/3 "2016-04-07T22:30:38Z")

</div>

I’m pretty sure I’m ready to call this a bug.

I’ve been trying to find out where `last_visited_at` is updated so far I have found:

1. The spec successfully tests a call to `TopicUser::track_visit` updates the value.
2. `TopicUser::change` - change in notification level watching/tracking etc)
3. `TopicUser::update_last_read` - keeps track of the last read post.

The problem seems to be:

1. There are no calls to `TopicUser::track_visit` outside of the spec.
2. `TopicUser::update_last_read` only updates the `last_visited_at` value when:

- “The user read at least one post in a topic that they haven’t viewed before”

* * *

I’m thinking that the following should be reviewed:

**`1.`** Should there be an explicit call when `track_view` is set when requesting the topic? I think there should be.  
**`2.`** Change the `UPDATE_TOPIC_USER_SQL` to also update `last_visited_at` time as used in `TopicUser::update_last_read` so the value is kept up to date along with reading times.

> <https://github.com/discourse/discourse/blob/8f4bc2228fd5def7a4190222b0ae993a384791d9/app/models/topic_user.rb#L143-L148>

**`3.`** There is a possibility that `last_visited_at` and `first_visited_at` are being set to `NULL` here when `TopicUser::change` tracking state changes:

> <https://github.com/discourse/discourse/blob/8f4bc2228fd5def7a4190222b0ae993a384791d9/app/models/topic_user.rb#L85-L103>

But I don’t know enough / have a test environment to check for sure.

**`4.`** If point 3 above is not where the NULL is coming from there is either `TopicUser` rows being put in the table with initially `last_visited_at` and `first_visited_at` set to `NULL` or somewhere else they are being set to `NULL`.

Thoughts?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [June 19, 2016, 4:27am UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/4 "2016-06-19T04:27:07Z")

</div>

I just ran this

```plaintext
SELECT *
from topic_users
WHERE
    last_visited_at IS NULL
    AND ( last_read_post_number > 0 
       OR highest_seen_post_number > 0 )
ORDER BY id DESC

```

The few results were odd but not totally unexpected.  
Members were Watching the category when they visited the topics,  
The topics were subsequently moved, unlisted, deleted, and one the category permissions were changed.

When I saw these back in April I wrote them off as most likely a set-up quirk rather than a Discourse Bug. .

Are you still getting such rows added?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [July 25, 2016, 6:47am UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/5 "2016-07-25T06:47:49Z")

</div>

> [@DeanMarkTaylor](#):
>
> There are no calls to TopicUser::track\_visit outside of the spec.

Well actually…

> <https://github.com/discourse/discourse/blob/main/app/controllers/topics_controller.rb#L613>

This seems to be working fine for me.

The null rows will usually be users who never really visited and instead got a row there cause they were watching a category or had mailing list mode enabled. As soon as we send a mail out to a user we add a row in topic users.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [July 25, 2016, 2:02pm UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/6 "2016-07-25T14:02:24Z")

</div>

> [@DeanMarkTaylor](#):
>
> There are no calls to TopicUser::track\_visit outside of the spec.

> [@sam](#):
>
> Well actually…

> [@sam](#):
>
> This seems to be working fine for me.

That code was added after the time of my report / investigation:  
[https://github.com/discourse/discourse/commit/6137bb46d31f0bb3ef3a4488c68a17a750ddb84e](https://github.com/discourse/discourse/commit/6137bb46d31f0bb3ef3a4488c68a17a750ddb84e)
