2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe UserAction do
|
|
|
|
|
2013-05-14 09:59:55 +08:00
|
|
|
before do
|
2016-12-22 12:03:40 +08:00
|
|
|
UserActionCreator.enable
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to validate_presence_of :action_type }
|
|
|
|
it { is_expected.to validate_presence_of :user_id }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'lists' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-16 06:08:28 +08:00
|
|
|
let(:public_post) { Fabricate(:post) }
|
|
|
|
let(:public_topic) { public_post.topic }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
let(:private_post) { Fabricate(:post) }
|
2013-02-26 00:42:20 +08:00
|
|
|
let(:private_topic) do
|
2013-02-16 06:08:28 +08:00
|
|
|
topic = private_post.topic
|
2014-09-11 15:39:20 +08:00
|
|
|
topic.update_columns(category_id: nil, archetype: Archetype::private_message)
|
2016-12-21 12:01:26 +08:00
|
|
|
TopicAllowedUser.create(topic_id: topic.id, user_id: user.id)
|
2013-02-16 06:08:28 +08:00
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def log_test_action(opts = {})
|
2013-02-16 06:08:28 +08:00
|
|
|
UserAction.log_action!({
|
2013-02-06 03:16:51 +08:00
|
|
|
action_type: UserAction::NEW_PRIVATE_MESSAGE,
|
2013-02-26 00:42:20 +08:00
|
|
|
user_id: user.id,
|
|
|
|
acting_user_id: user.id,
|
2013-02-16 06:08:28 +08:00
|
|
|
target_topic_id: private_topic.id,
|
2013-02-26 00:42:20 +08:00
|
|
|
target_post_id: private_post.id,
|
2013-02-16 06:08:28 +08:00
|
|
|
}.merge(opts))
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
describe "integration" do
|
|
|
|
before do
|
|
|
|
# Create some test data using a helper
|
|
|
|
log_test_action
|
|
|
|
log_test_action(action_type: UserAction::GOT_PRIVATE_MESSAGE)
|
|
|
|
log_test_action(action_type: UserAction::NEW_TOPIC, target_topic_id: public_topic.id, target_post_id: public_post.id)
|
|
|
|
log_test_action(action_type: UserAction::BOOKMARK)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
def stats_for_user(viewer = nil)
|
|
|
|
UserAction.stats(user.id, Guardian.new(viewer)).map { |r| r["action_type"].to_i }.sort
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
def stream_count(viewer = nil)
|
|
|
|
UserAction.stream(user_id: user.id, guardian: Guardian.new(viewer)).count
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
it 'includes the events correctly' do
|
|
|
|
PostActionNotifier.enable
|
2016-12-22 13:46:22 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
mystats = stats_for_user(user)
|
|
|
|
expecting = [UserAction::NEW_TOPIC, UserAction::NEW_PRIVATE_MESSAGE, UserAction::GOT_PRIVATE_MESSAGE, UserAction::BOOKMARK].sort
|
|
|
|
expect(mystats).to eq(expecting)
|
|
|
|
expect(stream_count(user)).to eq(4)
|
2013-04-10 10:50:00 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
other_stats = stats_for_user
|
|
|
|
expecting = [UserAction::NEW_TOPIC]
|
|
|
|
expect(stream_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
expect(other_stats).to eq(expecting)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
public_topic.trash!(user)
|
|
|
|
expect(stats_for_user).to eq([])
|
|
|
|
expect(stream_count).to eq(0)
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
# groups
|
|
|
|
category = Fabricate(:category, read_restricted: true)
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
public_topic.recover!
|
|
|
|
public_topic.category = category
|
|
|
|
public_topic.save
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
expect(stats_for_user).to eq([])
|
|
|
|
expect(stream_count).to eq(0)
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
group = Fabricate(:group)
|
|
|
|
u = Fabricate(:coding_horror)
|
|
|
|
group.add(u)
|
|
|
|
group.save
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
category.set_permissions(group => :full)
|
|
|
|
category.save
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
expect(stats_for_user(u)).to eq([UserAction::NEW_TOPIC])
|
|
|
|
expect(stream_count(u)).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
# duplicate should not exception out
|
|
|
|
log_test_action
|
2013-07-23 07:48:18 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
# recategorize belongs to the right user
|
|
|
|
category2 = Fabricate(:category)
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
public_post.revise(admin, category_id: category2.id)
|
2014-10-07 12:57:48 +08:00
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
action = UserAction.stream(user_id: public_topic.user_id, guardian: Guardian.new)[0]
|
|
|
|
expect(action.acting_user_id).to eq(admin.id)
|
|
|
|
expect(action.action_type).to eq(UserAction::EDIT)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "mentions" do
|
|
|
|
before do
|
|
|
|
log_test_action(action_type: UserAction::MENTION)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:stream) do
|
|
|
|
UserAction.stream(
|
|
|
|
user_id: user.id,
|
|
|
|
guardian: Guardian.new(user)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is returned by the stream" do
|
|
|
|
expect(stream).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "isn't returned when mentions aren't enabled" do
|
|
|
|
SiteSetting.enable_mentions = false
|
|
|
|
expect(stream).to be_blank
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-10-28 05:06:43 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-12-08 05:16:53 +08:00
|
|
|
describe "mentions" do
|
|
|
|
it "returns the mention in the stream" do
|
|
|
|
end
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'when user likes' do
|
2013-02-16 06:08:28 +08:00
|
|
|
|
2017-01-16 10:18:10 +08:00
|
|
|
let(:post) { Fabricate(:post) }
|
2013-02-16 06:08:28 +08:00
|
|
|
let(:likee) { post.user }
|
|
|
|
let(:liker) { Fabricate(:coding_horror) }
|
|
|
|
|
|
|
|
def likee_stream
|
|
|
|
UserAction.stream(user_id: likee.id, guardian: Guardian.new)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-16 06:08:28 +08:00
|
|
|
before do
|
|
|
|
@old_count = likee_stream.count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "creates a new stream entry" do
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(likee_stream.count).to eq(@old_count + 1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-16 06:08:28 +08:00
|
|
|
|
|
|
|
context "successful like" do
|
2013-02-26 00:42:20 +08:00
|
|
|
before do
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
2014-05-06 21:41:59 +08:00
|
|
|
@liker_action = liker.user_actions.find_by(action_type: UserAction::LIKE)
|
|
|
|
@likee_action = likee.user_actions.find_by(action_type: UserAction::WAS_LIKED)
|
2013-02-16 06:08:28 +08:00
|
|
|
end
|
|
|
|
|
2013-04-05 12:29:46 +08:00
|
|
|
it 'should result in correct data assignment' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@liker_action).not_to eq(nil)
|
|
|
|
expect(@likee_action).not_to eq(nil)
|
|
|
|
expect(likee.user_stat.reload.likes_received).to eq(1)
|
|
|
|
expect(liker.user_stat.reload.likes_given).to eq(1)
|
2013-04-05 12:29:46 +08:00
|
|
|
|
|
|
|
PostAction.remove_act(liker, post, PostActionType.types[:like])
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(likee.user_stat.reload.likes_received).to eq(0)
|
|
|
|
expect(liker.user_stat.reload.likes_given).to eq(0)
|
2013-02-16 06:08:28 +08:00
|
|
|
end
|
2013-04-05 12:29:46 +08:00
|
|
|
|
2017-01-16 10:18:10 +08:00
|
|
|
context 'private message' do
|
|
|
|
let(:post) { Fabricate(:private_message_post) }
|
|
|
|
let(:likee) { post.topic.topic_allowed_users.first.user }
|
|
|
|
let(:liker) { post.topic.topic_allowed_users.last.user }
|
|
|
|
|
|
|
|
it 'should not increase user stats' do
|
|
|
|
expect(@liker_action).not_to eq(nil)
|
|
|
|
expect(liker.user_stat.reload.likes_given).to eq(0)
|
|
|
|
expect(@likee_action).not_to eq(nil)
|
|
|
|
expect(likee.user_stat.reload.likes_received).to eq(0)
|
|
|
|
|
|
|
|
PostAction.remove_act(liker, post, PostActionType.types[:like])
|
|
|
|
expect(liker.user_stat.reload.likes_given).to eq(0)
|
|
|
|
expect(likee.user_stat.reload.likes_received).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-16 06:08:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "liking a private message" do
|
|
|
|
|
|
|
|
before do
|
2014-09-11 15:39:20 +08:00
|
|
|
post.topic.update_columns(category_id: nil, archetype: Archetype::private_message)
|
2013-02-16 06:08:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add the entry to the stream" do
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(likee_stream.count).not_to eq(@old_count + 1)
|
2013-02-16 06:08:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when a user posts a new topic' do
|
2014-03-18 12:22:39 +08:00
|
|
|
def process_alerts(post)
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@post = Fabricate(:old_post)
|
2014-03-18 12:22:39 +08:00
|
|
|
process_alerts(@post)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'topic action' do
|
|
|
|
before do
|
2014-05-06 21:41:59 +08:00
|
|
|
@action = @post.user.user_actions.find_by(action_type: UserAction::NEW_TOPIC)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'should exist' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@action).not_to eq(nil)
|
|
|
|
expect(@action.created_at).to be_within(1).of(@post.topic.created_at)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'should not log a post user action' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@post.user.user_actions.find_by(action_type: UserAction::REPLY)).to eq(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'when another user posts on the topic' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@other_user = Fabricate(:coding_horror)
|
|
|
|
@mentioned = Fabricate(:admin)
|
2013-02-13 17:38:43 +08:00
|
|
|
@response = Fabricate(:post, reply_to_post_number: 1, topic: @post.topic, user: @other_user, raw: "perhaps @#{@mentioned.username} knows how this works?")
|
2014-03-18 12:22:39 +08:00
|
|
|
|
|
|
|
process_alerts(@response)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'should log user actions correctly' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@response.user.user_actions.find_by(action_type: UserAction::REPLY)).not_to eq(nil)
|
|
|
|
expect(@post.user.user_actions.find_by(action_type: UserAction::RESPONSE)).not_to eq(nil)
|
|
|
|
expect(@mentioned.user_actions.find_by(action_type: UserAction::MENTION)).not_to eq(nil)
|
|
|
|
expect(@post.user.user_actions.joins(:target_post).where('posts.post_number = 2').count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not log a double notification for a post edit' do
|
|
|
|
@response.raw = "here it goes again"
|
2013-02-26 00:42:20 +08:00
|
|
|
@response.save!
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@response.user.user_actions.where(action_type: UserAction::REPLY).count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user bookmarks' do
|
2013-02-26 00:42:20 +08:00
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@post = Fabricate(:post)
|
|
|
|
@user = @post.user
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(@user, @post, PostActionType.types[:bookmark])
|
2014-05-06 21:41:59 +08:00
|
|
|
@action = @user.user_actions.find_by(action_type: UserAction::BOOKMARK)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'should create a bookmark action correctly' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@action.action_type).to eq(UserAction::BOOKMARK)
|
|
|
|
expect(@action.target_post_id).to eq(@post.id)
|
|
|
|
expect(@action.acting_user_id).to eq(@user.id)
|
|
|
|
expect(@action.user_id).to eq(@user.id)
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.remove_act(@user, @post, PostActionType.types[:bookmark])
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@user.user_actions.find_by(action_type: UserAction::BOOKMARK)).to eq(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-20 14:44:06 +08:00
|
|
|
|
2016-12-21 12:01:26 +08:00
|
|
|
describe 'secures private messages' do
|
2013-05-20 14:44:06 +08:00
|
|
|
|
|
|
|
let(:user) do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
2016-12-21 12:01:26 +08:00
|
|
|
let(:user2) do
|
2013-05-20 14:44:06 +08:00
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:private_message) do
|
2017-07-28 09:20:09 +08:00
|
|
|
PostCreator.create(user,
|
2013-05-20 14:44:06 +08:00
|
|
|
raw: 'this is a private message',
|
|
|
|
title: 'this is the pm title',
|
2016-12-21 12:01:26 +08:00
|
|
|
target_usernames: user2.username,
|
2013-05-20 14:44:06 +08:00
|
|
|
archetype: Archetype::private_message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-12-21 12:01:26 +08:00
|
|
|
def count_bookmarks
|
|
|
|
UserAction.stream(
|
|
|
|
user_id: user.id,
|
|
|
|
action_types: [UserAction::BOOKMARK],
|
|
|
|
ignore_private_messages: false,
|
|
|
|
guardian: Guardian.new(user)
|
|
|
|
).count
|
2013-05-20 14:44:06 +08:00
|
|
|
end
|
|
|
|
|
2016-12-21 12:01:26 +08:00
|
|
|
it 'correctly secures stream' do
|
|
|
|
PostAction.act(user, private_message, PostActionType.types[:bookmark])
|
|
|
|
|
|
|
|
expect(count_bookmarks).to eq(1)
|
|
|
|
|
|
|
|
private_message.topic.topic_allowed_users.where(user_id: user.id).destroy_all
|
|
|
|
|
|
|
|
expect(count_bookmarks).to eq(0)
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(user)
|
|
|
|
private_message.topic.topic_allowed_groups.create(group_id: group.id)
|
|
|
|
|
|
|
|
expect(count_bookmarks).to eq(1)
|
|
|
|
|
2013-05-20 14:44:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-07-17 14:40:15 +08:00
|
|
|
|
2013-08-02 07:59:12 +08:00
|
|
|
describe 'synchronize_target_topic_ids' do
|
2013-07-17 14:40:15 +08:00
|
|
|
it 'correct target_topic_id' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
|
|
|
|
action = UserAction.log_action!(
|
|
|
|
action_type: UserAction::NEW_PRIVATE_MESSAGE,
|
|
|
|
user_id: post.user.id,
|
|
|
|
acting_user_id: post.user.id,
|
|
|
|
target_topic_id: -1,
|
|
|
|
target_post_id: post.id,
|
|
|
|
)
|
|
|
|
|
2013-07-23 10:43:34 +08:00
|
|
|
UserAction.log_action!(
|
|
|
|
action_type: UserAction::NEW_PRIVATE_MESSAGE,
|
|
|
|
user_id: post.user.id,
|
|
|
|
acting_user_id: post.user.id,
|
|
|
|
target_topic_id: -2,
|
|
|
|
target_post_id: post.id,
|
|
|
|
)
|
2013-07-17 14:40:15 +08:00
|
|
|
|
2013-08-02 07:59:12 +08:00
|
|
|
UserAction.synchronize_target_topic_ids
|
2013-07-23 10:43:34 +08:00
|
|
|
|
2013-07-17 14:40:15 +08:00
|
|
|
action.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(action.target_topic_id).to eq(post.topic_id)
|
2013-07-17 14:40:15 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|