DEV: adds DiscourseEvent - topic_first_visited_by_user (#12195)

This event would mostly allow plugins to create workflows once users have visited a specific topic.
This commit is contained in:
Joffrey JAFFEUX 2021-02-24 10:50:02 +01:00 committed by GitHub
parent f2126acf5a
commit de32fa299d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -234,6 +234,8 @@ class TopicUser < ActiveRecord::Base
first_visited_at: now ,
last_visited_at: now
))
DiscourseEvent.trigger(:topic_first_visited_by_user, topic_id, user_id)
end
def track_visit!(topic_id, user_id)

View File

@ -477,4 +477,32 @@ describe TopicUser do
end
end
it "correctly triggers an event on first visit" do
begin
tracked_user = Fabricate(:user)
post = create_post
called = 0
visits = []
user_first_visit = -> (topic_id, user_id) do
visits << "#{topic_id}-#{user_id}"
called += 1
end
DiscourseEvent.on(:topic_first_visited_by_user, &user_first_visit)
expect(called).to eq(0)
TopicUser.change(tracked_user, post.topic.id, total_msecs_viewed: 1)
expect(visits).to eq(["#{post.topic.id}-#{tracked_user.id}"])
expect(called).to eq(1)
TopicUser.change(tracked_user, post.topic.id, total_msecs_viewed: 2)
expect(called).to eq(1)
ensure
DiscourseEvent.off(:topic_first_visited_by_user, &user_first_visit)
end
end
end