2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe UserAction do
|
|
|
|
|
2013-05-13 21:59:55 -04:00
|
|
|
before do
|
|
|
|
ActiveRecord::Base.observers.enable :all
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it { should validate_presence_of :action_type }
|
|
|
|
it { should validate_presence_of :user_id }
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'lists' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-15 17:08:28 -05:00
|
|
|
let(:public_post) { Fabricate(:post) }
|
|
|
|
let(:public_topic) { public_post.topic }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
let(:private_post) { Fabricate(:post) }
|
2013-02-25 11:42:20 -05:00
|
|
|
let(:private_topic) do
|
2013-02-15 17:08:28 -05:00
|
|
|
topic = private_post.topic
|
2013-02-25 11:42:20 -05:00
|
|
|
topic.update_column(:archetype, Archetype::private_message)
|
2013-02-15 17:08:28 -05:00
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_test_action(opts={})
|
|
|
|
UserAction.log_action!({
|
2013-02-05 14:16:51 -05:00
|
|
|
action_type: UserAction::NEW_PRIVATE_MESSAGE,
|
2013-02-25 11:42:20 -05:00
|
|
|
user_id: user.id,
|
|
|
|
acting_user_id: user.id,
|
2013-02-15 17:08:28 -05:00
|
|
|
target_topic_id: private_topic.id,
|
2013-02-25 11:42:20 -05:00
|
|
|
target_post_id: private_post.id,
|
2013-02-15 17:08:28 -05:00
|
|
|
}.merge(opts))
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
before do
|
2013-02-15 17:08:28 -05:00
|
|
|
# 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)
|
2013-02-25 11:42:20 -05:00
|
|
|
log_test_action(action_type: UserAction::BOOKMARK)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04: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-05 14:16:51 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def stream_count(viewer=nil)
|
|
|
|
UserAction.stream(user_id: user.id, guardian: Guardian.new(viewer)).count
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
it 'includes the events correctly' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
mystats = stats_for_user(user)
|
|
|
|
expecting = [UserAction::NEW_TOPIC, UserAction::NEW_PRIVATE_MESSAGE, UserAction::GOT_PRIVATE_MESSAGE, UserAction::BOOKMARK].sort
|
|
|
|
mystats.should == expecting
|
|
|
|
stream_count(user).should == 4
|
2013-04-09 22:50:00 -04:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
other_stats = stats_for_user
|
|
|
|
expecting = [UserAction::NEW_TOPIC]
|
|
|
|
stream_count.should == 1
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
other_stats.should == expecting
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-09 15:20:18 -04:00
|
|
|
public_topic.trash!(user)
|
2013-04-29 02:33:24 -04:00
|
|
|
stats_for_user.should == []
|
|
|
|
stream_count.should == 0
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
# groups
|
2013-04-09 22:50:00 -04:00
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
category = Fabricate(:category, read_restricted: true)
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2013-05-07 00:39:01 -04:00
|
|
|
public_topic.recover!
|
2013-04-29 02:33:24 -04:00
|
|
|
public_topic.category = category
|
|
|
|
public_topic.save
|
|
|
|
|
|
|
|
stats_for_user.should == []
|
|
|
|
stream_count.should == 0
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
|
|
|
u = Fabricate(:coding_horror)
|
|
|
|
group.add(u)
|
|
|
|
group.save
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
category.set_permissions(group => :full)
|
2013-04-29 02:33:24 -04:00
|
|
|
category.save
|
|
|
|
|
|
|
|
stats_for_user(u).should == [UserAction::NEW_TOPIC]
|
|
|
|
stream_count(u).should == 1
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-22 19:48:18 -04:00
|
|
|
# duplicate should not exception out
|
|
|
|
log_test_action
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'when user likes' do
|
2013-02-15 17:08:28 -05:00
|
|
|
|
|
|
|
let!(:post) { Fabricate(:post) }
|
|
|
|
let(:likee) { post.user }
|
|
|
|
let(:liker) { Fabricate(:coding_horror) }
|
|
|
|
|
|
|
|
def likee_stream
|
|
|
|
UserAction.stream(user_id: likee.id, guardian: Guardian.new)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-15 17:08:28 -05:00
|
|
|
before do
|
|
|
|
@old_count = likee_stream.count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "creates a new stream entry" do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
2013-02-15 17:08:28 -05:00
|
|
|
likee_stream.count.should == @old_count + 1
|
2013-07-22 19:48:18 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-15 17:08:28 -05:00
|
|
|
|
|
|
|
context "successful like" do
|
2013-02-25 11:42:20 -05:00
|
|
|
before do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
2013-02-15 17:08:28 -05:00
|
|
|
@liker_action = liker.user_actions.where(action_type: UserAction::LIKE).first
|
|
|
|
@likee_action = likee.user_actions.where(action_type: UserAction::WAS_LIKED).first
|
|
|
|
end
|
|
|
|
|
2013-04-05 00:29:46 -04:00
|
|
|
it 'should result in correct data assignment' do
|
2013-02-25 11:42:20 -05:00
|
|
|
@liker_action.should_not be_nil
|
|
|
|
@likee_action.should_not be_nil
|
2013-10-03 23:28:49 -04:00
|
|
|
likee.user_stat.reload.likes_received.should == 1
|
|
|
|
liker.user_stat.reload.likes_given.should == 1
|
2013-04-05 00:29:46 -04:00
|
|
|
|
|
|
|
PostAction.remove_act(liker, post, PostActionType.types[:like])
|
2013-10-03 23:28:49 -04:00
|
|
|
likee.user_stat.reload.likes_received.should == 0
|
|
|
|
liker.user_stat.reload.likes_given.should == 0
|
2013-02-15 17:08:28 -05:00
|
|
|
end
|
2013-04-05 00:29:46 -04:00
|
|
|
|
2013-02-15 17:08:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "liking a private message" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
post.topic.update_column(:archetype, Archetype::private_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add the entry to the stream" do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
2013-02-25 11:42:20 -05:00
|
|
|
likee_stream.count.should_not == @old_count + 1
|
2013-02-15 17:08:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when a user posts a new topic' do
|
2013-02-25 11:42:20 -05:00
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@post = Fabricate(:old_post)
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'topic action' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@action = @post.user.user_actions.where(action_type: UserAction::NEW_TOPIC).first
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should exist' do
|
2013-02-05 14:16:51 -05:00
|
|
|
@action.should_not be_nil
|
|
|
|
@action.created_at.should be_within(1).of(@post.topic.created_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should not log a post user action' do
|
2013-04-30 20:52:31 -04:00
|
|
|
@post.user.user_actions.where(action_type: UserAction::REPLY).first.should be_nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'when another user posts on the topic' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@other_user = Fabricate(:coding_horror)
|
|
|
|
@mentioned = Fabricate(:admin)
|
2013-02-13 04:38:43 -05:00
|
|
|
@response = Fabricate(:post, reply_to_post_number: 1, topic: @post.topic, user: @other_user, raw: "perhaps @#{@mentioned.username} knows how this works?")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
it 'should log user actions correctly' do
|
2013-04-30 20:52:31 -04:00
|
|
|
@response.user.user_actions.where(action_type: UserAction::REPLY).first.should_not be_nil
|
2013-02-13 04:38:43 -05:00
|
|
|
@post.user.user_actions.where(action_type: UserAction::RESPONSE).first.should_not be_nil
|
2013-02-05 14:16:51 -05:00
|
|
|
@mentioned.user_actions.where(action_type: UserAction::MENTION).first.should_not be_nil
|
2013-04-29 02:33:24 -04:00
|
|
|
@post.user.user_actions.joins(:target_post).where('posts.post_number = 2').count.should == 1
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not log a double notification for a post edit' do
|
|
|
|
@response.raw = "here it goes again"
|
2013-02-25 11:42:20 -05:00
|
|
|
@response.save!
|
2013-04-30 20:52:31 -04:00
|
|
|
@response.user.user_actions.where(action_type: UserAction::REPLY).count.should == 1
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user bookmarks' do
|
2013-02-25 11:42:20 -05:00
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@post = Fabricate(:post)
|
|
|
|
@user = @post.user
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(@user, @post, PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
@action = @user.user_actions.where(action_type: UserAction::BOOKMARK).first
|
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
it 'should create a bookmark action correctly' do
|
2013-02-05 14:16:51 -05:00
|
|
|
@action.action_type.should == UserAction::BOOKMARK
|
|
|
|
@action.target_post_id.should == @post.id
|
2013-02-25 11:42:20 -05:00
|
|
|
@action.acting_user_id.should == @user.id
|
2013-02-05 14:16:51 -05:00
|
|
|
@action.user_id.should == @user.id
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.remove_act(@user, @post, PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
@user.user_actions.where(action_type: UserAction::BOOKMARK).first.should be_nil
|
|
|
|
end
|
|
|
|
end
|
2013-05-20 02:44:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe 'private messages' do
|
|
|
|
|
|
|
|
let(:user) do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:target_user) do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:private_message) do
|
|
|
|
PostCreator.create( user,
|
|
|
|
raw: 'this is a private message',
|
|
|
|
title: 'this is the pm title',
|
|
|
|
target_usernames: target_user.username,
|
|
|
|
archetype: Archetype::private_message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:response) do
|
|
|
|
PostCreator.create(user, raw: 'oops I forgot to mention this', topic_id: private_message.topic_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:private_message2) do
|
|
|
|
PostCreator.create( target_user,
|
|
|
|
raw: 'this is a private message',
|
|
|
|
title: 'this is the pm title',
|
|
|
|
target_usernames: user.username,
|
|
|
|
archetype: Archetype::private_message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-07-17 02:40:15 -04:00
|
|
|
|
2013-08-01 19:59:12 -04:00
|
|
|
describe 'synchronize_favorites' do
|
2013-08-01 21:07:18 -04:00
|
|
|
it 'corrects out of sync favs' do
|
2013-08-01 19:59:12 -04:00
|
|
|
post = Fabricate(:post)
|
|
|
|
post.topic.toggle_star(post.user, true)
|
2013-08-01 21:07:18 -04:00
|
|
|
UserAction.delete_all
|
2013-08-01 19:59:12 -04:00
|
|
|
|
|
|
|
action1 = UserAction.log_action!(
|
|
|
|
action_type: UserAction::STAR,
|
|
|
|
user_id: post.user.id,
|
|
|
|
acting_user_id: post.user.id,
|
2013-08-01 21:07:18 -04:00
|
|
|
target_topic_id: 99,
|
|
|
|
target_post_id: -1,
|
|
|
|
)
|
|
|
|
|
|
|
|
action2 = UserAction.log_action!(
|
|
|
|
action_type: UserAction::STAR,
|
|
|
|
user_id: Fabricate(:user).id,
|
|
|
|
acting_user_id: post.user.id,
|
|
|
|
target_topic_id: post.topic_id,
|
|
|
|
target_post_id: -1,
|
2013-08-01 19:59:12 -04:00
|
|
|
)
|
|
|
|
|
2013-08-01 21:07:18 -04:00
|
|
|
UserAction.synchronize_favorites
|
|
|
|
|
|
|
|
actions = UserAction.all.to_a
|
|
|
|
|
|
|
|
actions.length.should == 1
|
|
|
|
actions.first.action_type.should == UserAction::STAR
|
|
|
|
actions.first.user_id.should == post.user.id
|
2013-08-01 19:59:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'synchronize_target_topic_ids' do
|
2013-07-17 02:40:15 -04: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-22 22:43:34 -04: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 02:40:15 -04:00
|
|
|
|
2013-08-01 19:59:12 -04:00
|
|
|
UserAction.synchronize_target_topic_ids
|
2013-07-22 22:43:34 -04:00
|
|
|
|
2013-07-17 02:40:15 -04:00
|
|
|
action.reload
|
|
|
|
action.target_topic_id.should == post.topic_id
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|