Skip to content

Instantly share code, notes, and snippets.

@mishrarohit
Created October 19, 2013 09:35
Show Gist options
  • Save mishrarohit/7053650 to your computer and use it in GitHub Desktop.
Save mishrarohit/7053650 to your computer and use it in GitHub Desktop.
The old notifications code for Allotrop. This is bad in too many ways. I have completely redone Allotrop notifications. This is out there as a reminder of the bad pattern that I used.
# Old Allotrop code for repost notifications
class RepostNotificationWorker
include Sidekiq::Worker
def perform(repost_id)
# debugger if Rails.env.development?
repost = Post.find(repost_id)
unless repost.nil?
original_post = Post.find(repost.original_post_id)
$redis.incr "repost_count:#{original_post.id}"
original_post_creator_id = original_post.user.id
original_post_creator_username = original_post.user.username
reposter_username = repost.user.username
reposter_id = repost.user.id
repost_count = ($redis.get "repost_count:#{original_post.id}").to_i
if repost_count > 0
previous_name1 = $redis.hget("notification_message:#{original_post_creator_id}:#{original_post.id}:repost", "name1")
end
$redis.multi do
if repost_count == 1
# Create notification_message
# Create a new message
# A {, B and 4 others} liked your post on Batman
# TODO Add post snippet
# name1 -- A
# name2 -- B
# count suffix -- and 4 others
# event - liked
# If a post has 3 reposts, the best place for the notification to point to is the original repost
$redis.hmset("notification_message:#{original_post_creator_id}:#{original_post.id}:repost",
"name1", "#{reposter_id}",
"name2", "",
"event", "reposted",
"post_creator_username", "#{original_post_creator_username}",
"post_id", "#{original_post.id}",
"count_suffix", ""
)
elsif repost_count == 2
$redis.hmset("notification_message:#{original_post_creator_id}:#{original_post.id}:repost",
"name2", "#{previous_name1}",
"name1", "#{reposter_id}",
"event", "reposted",
"post_creator_username", "#{original_post_creator_username}",
"post_id", "#{original_post.id}",
"count_suffix", ""
)
elsif (repost_count > 2)
$redis.hmset("notification_message:#{original_post_creator_id}:#{original_post.id}:repost",
"name2", "#{previous_name1}",
"name1", "#{reposter_id}",
"event", "reposted",
"post_creator_username", "#{original_post_creator_username}",
"post_id", "#{original_post.id}",
"count_suffix", "#{repost_count - 2}"
)
end
$redis.zadd("notifications:#{original_post_creator_id}", repost.created_at.to_i, "#{original_post.id}:repost" )
if original_post.user.email_notification.reposts?
UserMailer.repost_notification(original_post.user, repost.user, repost).deliver
end
end # End of redis multi
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment