Basically, I want to create a notification like Facebook and Stackoverflow. Specifically, in a Post-Comments system, when a post get commented, everyone involved (people who creates the post and who create comments, except the new commenter) gets a notification message that this post get commented. And the notification get dismissed when people have read it.
I have tried to use mailboxer gem to implement it, but saddly there is no example available using its related methods, including social_stream itself.
Is there other way to create the Notification System?
And when I try to create it from scratch I get several problems:
Model Notification
topic_id: integer
user_id: integer
checked: boolean #so we can tell whether the notification is read or not
I think we just need to turn every notification messages' "checked" attribute to true after user visit the index of notification.(In the NotificationsController)
def index
@notifications=current_user.notication.all
@notification.each do |notification|
notification.checked = true
end
@notification.save!
end
2.Selecting users to notify(and exclude the user making new comment)
I just have no idea in wrting queries....
3.Creating notifications
I think this should be something like
#in CommentController
def create
#after creating comments, creat notifications
@users.each do |user|
Notification.create(topic_id:@topic, user_id: user.id)
end
end
But I think this is really ugly
There is no need to anwer the 3 problems above, Any simple solution to the Notification System is preferable , thanks....
I think you are on the right path.
A slightly better notifications#index
def index
@notifications = current_user.notications
@notifications.update_all checked: true
end
Notify this users
User.uniq.joins(:comments).where(comments: {id: @comment.post.comment_ids}).reject {|user| user == current_user }
Unique users that participated in the @comment's post comments, reject (remove from result) current_user.
there is an amazing gem called public activity ,,you can customize it as you want and here is a screencast about it in railscast http://railscasts.com/episodes/406-public-activity hope that could help you.
In my rails application I made a similar notifications system like yours to send notifications to all users but in index action you can user
current_user.notifications.update_all(:checked=>true)
and also to send only one notifications to the user once not several times that somebody commented on the post you can use unique_by method
@comments [email protected]_by {|a| a[:user_id]}
then you can send notifications to only users of previous comments
@comments.each do |comment|
comment.user.notifications.create!(....
end
hope that could help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With