mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 02:50:00 +08:00
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:
parent
f2126acf5a
commit
de32fa299d
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user