discourse/app/models/user_action_observer.rb

190 lines
4.5 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
class UserActionObserver < ActiveRecord::Observer
observe :post_action, :topic, :post, :notification, :topic_user
def after_save(model)
2013-02-07 10:45:24 -05:00
case
when (model.is_a?(PostAction) and (model.is_bookmark? or model.is_like?))
2013-02-05 14:16:51 -05:00
log_post_action(model)
when (model.is_a?(Topic))
log_topic(model)
when (model.is_a?(Post))
log_post(model)
when (model.is_a?(Notification))
log_notification(model)
when (model.is_a?(TopicUser))
log_topic_user(model)
end
end
2013-02-07 10:45:24 -05:00
protected
2013-02-05 14:16:51 -05:00
def log_topic_user(model)
action = UserAction::STAR
2013-02-07 10:45:24 -05:00
row = {
action_type: action,
user_id: model.user_id,
acting_user_id: model.user_id,
target_topic_id: model.topic_id,
2013-02-05 14:16:51 -05:00
target_post_id: -1,
created_at: model.starred_at
}
2013-02-07 10:45:24 -05:00
if model.starred
2013-02-05 14:16:51 -05:00
UserAction.log_action!(row)
2013-02-07 10:45:24 -05:00
else
UserAction.remove_action!(row)
2013-02-05 14:16:51 -05:00
end
end
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
def log_notification(model)
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
action =
case model.notification_type
when Notification.Types[:quoted]
UserAction::QUOTE
when Notification.Types[:replied]
UserAction::RESPONSE
when Notification.Types[:mentioned]
UserAction::MENTION
when Notification.Types[:edited]
UserAction::EDIT
end
# like is skipped
return unless action
post = Post.where(post_number: model.post_number, topic_id: model.topic_id).first
# stray data
return unless post
2013-02-07 10:45:24 -05:00
row = {
action_type: action,
user_id: model.user_id,
acting_user_id: (action == UserAction::EDIT) ? post.last_editor_id : post.user_id,
target_topic_id: model.topic_id,
2013-02-05 14:16:51 -05:00
target_post_id: post.id,
created_at: model.created_at
}
2013-02-07 10:45:24 -05:00
if post.deleted_at.nil?
2013-02-05 14:16:51 -05:00
UserAction.log_action!(row)
2013-02-07 10:45:24 -05:00
else
UserAction.remove_action!(row)
2013-02-05 14:16:51 -05:00
end
end
def log_post(model)
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
# first post gets nada
return if model.post_number == 1
2013-02-07 10:45:24 -05:00
row = {
action_type: UserAction::POST,
user_id: model.user_id,
acting_user_id: model.user_id,
2013-02-05 14:16:51 -05:00
target_post_id: model.id,
target_topic_id: model.topic_id,
created_at: model.created_at
}
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
rows = [row]
2013-02-07 10:45:24 -05:00
if model.topic.private_message?
2013-02-05 14:16:51 -05:00
rows = []
model.topic.topic_allowed_users.each do |ta|
row = row.dup
2013-02-07 10:45:24 -05:00
row[:user_id] = ta.user_id
2013-02-05 14:16:51 -05:00
row[:action_type] = ta.user_id == model.user_id ? UserAction::NEW_PRIVATE_MESSAGE : UserAction::GOT_PRIVATE_MESSAGE
rows << row
end
end
rows.each do |row|
2013-02-07 10:45:24 -05:00
if model.deleted_at.nil?
2013-02-05 14:16:51 -05:00
UserAction.log_action!(row)
2013-02-07 10:45:24 -05:00
else
UserAction.remove_action!(row)
2013-02-05 14:16:51 -05:00
end
end
return if model.topic.private_message?
# a bit odd but we may have stray records
2013-02-07 10:45:24 -05:00
if model.topic and model.topic.user_id != model.user_id
row[:action_type] = UserAction::TOPIC_RESPONSE
row[:user_id] = model.topic.user_id
2013-02-05 14:16:51 -05:00
2013-02-07 10:45:24 -05:00
if model.deleted_at.nil?
2013-02-05 14:16:51 -05:00
UserAction.log_action!(row)
2013-02-07 10:45:24 -05:00
else
UserAction.remove_action!(row)
2013-02-05 14:16:51 -05:00
end
end
end
def log_topic(model)
2013-02-07 10:45:24 -05:00
row = {
action_type: model.archetype == Archetype.private_message ? UserAction::NEW_PRIVATE_MESSAGE : UserAction::NEW_TOPIC,
user_id: model.user_id,
acting_user_id: model.user_id,
2013-02-05 14:16:51 -05:00
target_topic_id: model.id,
2013-02-07 10:45:24 -05:00
target_post_id: -1,
2013-02-05 14:16:51 -05:00
created_at: model.created_at
}
rows = [row]
if model.private_message?
model.topic_allowed_users.reject{|a| a.user_id == model.user_id}.each do |ta|
row = row.dup
2013-02-07 10:45:24 -05:00
row[:user_id] = ta.user_id
2013-02-05 14:16:51 -05:00
row[:action_type] = UserAction::GOT_PRIVATE_MESSAGE
rows << row
end
end
rows.each do |row|
2013-02-07 10:45:24 -05:00
if model.deleted_at.nil?
2013-02-05 14:16:51 -05:00
UserAction.log_action!(row)
2013-02-07 10:45:24 -05:00
else
UserAction.remove_action!(row)
2013-02-05 14:16:51 -05:00
end
end
end
def log_post_action(model)
action = UserAction::BOOKMARK if model.is_bookmark?
action = UserAction::LIKE if model.is_like?
row = {
2013-02-07 10:45:24 -05:00
action_type: action,
user_id: model.user_id,
acting_user_id: model.user_id,
2013-02-05 14:16:51 -05:00
target_post_id: model.post_id,
target_topic_id: model.post.topic_id,
created_at: model.created_at
}
if model.deleted_at.nil?
UserAction.log_action!(row)
else
UserAction.remove_action!(row)
end
2013-02-07 10:45:24 -05:00
if model.is_like?
2013-02-05 14:16:51 -05:00
row[:action_type] = UserAction::WAS_LIKED
row[:user_id] = model.post.user_id
if model.deleted_at.nil?
UserAction.log_action!(row)
else
UserAction.remove_action!(row)
end
end
end
end