2016-12-21 23:03:40 -05:00
|
|
|
class UserActionCreator
|
|
|
|
def self.disable
|
|
|
|
@disabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enable
|
|
|
|
@disabled = false
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.log_notification(post, user, notification_type, acting_user_id = nil)
|
2016-12-21 23:03:40 -05:00
|
|
|
return if @disabled
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
action =
|
2014-02-02 21:57:44 -05:00
|
|
|
case notification_type
|
2017-07-27 21:20:09 -04:00
|
|
|
when Notification.types[:quoted]
|
|
|
|
UserAction::QUOTE
|
|
|
|
when Notification.types[:replied]
|
|
|
|
UserAction::RESPONSE
|
|
|
|
when Notification.types[:mentioned]
|
|
|
|
UserAction::MENTION
|
|
|
|
when Notification.types[:edited]
|
|
|
|
UserAction::EDIT
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-04-03 20:08:54 -04:00
|
|
|
# skip any invalid items, eg failed to save post and so on
|
|
|
|
return unless action && post && user && post.id
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
row = {
|
2015-07-31 14:22:28 -04:00
|
|
|
action_type: action,
|
|
|
|
user_id: user.id,
|
|
|
|
acting_user_id: acting_user_id || post.user_id,
|
|
|
|
target_topic_id: post.topic_id,
|
|
|
|
target_post_id: post.id
|
2013-02-05 14:16:51 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2015-09-18 12:48:43 -04:00
|
|
|
def self.log_post(model)
|
2016-12-21 23:03:40 -05:00
|
|
|
return if @disabled
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# first post gets nada
|
2015-04-23 13:33:29 -04:00
|
|
|
return if model.is_first_post?
|
2015-11-13 11:35:04 -05:00
|
|
|
return if model.topic.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
row = {
|
2015-07-31 14:22:28 -04:00
|
|
|
action_type: UserAction::REPLY,
|
|
|
|
user_id: model.user_id,
|
|
|
|
acting_user_id: model.user_id,
|
|
|
|
target_post_id: model.id,
|
|
|
|
target_topic_id: model.topic_id,
|
|
|
|
created_at: model.created_at
|
2013-02-05 14:16:51 -05:00
|
|
|
}
|
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
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
rows.each do |r|
|
2013-02-07 10:45:24 -05:00
|
|
|
if model.deleted_at.nil?
|
2013-02-28 13:54:12 -05:00
|
|
|
UserAction.log_action!(r)
|
2013-02-07 10:45:24 -05:00
|
|
|
else
|
2013-02-28 13:54:12 -05:00
|
|
|
UserAction.remove_action!(r)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-21 23:03:40 -05:00
|
|
|
def self.log_topic(model)
|
|
|
|
return if @disabled
|
2016-05-01 21:14:37 -04:00
|
|
|
|
|
|
|
# no action to log here, this can happen if a user is deleted
|
|
|
|
# then topic has no user_id
|
|
|
|
return unless model.user_id
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
row = {
|
2015-07-31 14:22:28 -04:00
|
|
|
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,
|
|
|
|
target_topic_id: model.id,
|
|
|
|
target_post_id: -1,
|
|
|
|
created_at: model.created_at
|
2013-02-05 14:16:51 -05:00
|
|
|
}
|
|
|
|
|
2017-11-09 15:33:26 -05:00
|
|
|
UserAction.remove_action!(row.merge(
|
|
|
|
action_type: model.archetype == Archetype.private_message ? UserAction::NEW_TOPIC : UserAction::NEW_PRIVATE_MESSAGE
|
|
|
|
))
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
rows = [row]
|
|
|
|
|
|
|
|
if model.private_message?
|
2013-02-28 13:54:12 -05:00
|
|
|
model.topic_allowed_users.reject { |a| a.user_id == model.user_id }.each do |ta|
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
rows.each do |r|
|
2013-02-07 10:45:24 -05:00
|
|
|
if model.deleted_at.nil?
|
2013-02-28 13:54:12 -05:00
|
|
|
UserAction.log_action!(r)
|
2013-02-07 10:45:24 -05:00
|
|
|
else
|
2013-02-28 13:54:12 -05:00
|
|
|
UserAction.remove_action!(r)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-21 23:03:40 -05:00
|
|
|
def self.log_post_action(model)
|
|
|
|
return if @disabled
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
action = UserAction::BOOKMARK if model.is_bookmark?
|
|
|
|
action = UserAction::LIKE if model.is_like?
|
|
|
|
|
2014-06-04 11:41:11 -04:00
|
|
|
post = Post.with_deleted.where(id: model.post_id).first
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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,
|
2014-06-04 11:41:11 -04:00
|
|
|
target_topic_id: post.topic_id,
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2014-06-04 11:41:11 -04:00
|
|
|
row[:user_id] = post.user_id
|
2013-02-05 14:16:51 -05:00
|
|
|
if model.deleted_at.nil?
|
|
|
|
UserAction.log_action!(row)
|
|
|
|
else
|
|
|
|
UserAction.remove_action!(row)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|