GSoC 2022 CircuitVerse | Week 11 and 12 Report
vedant-jain03
Posted on September 5, 2022
Description
In previous two weeks, I have been working on completing the field around notifications for CircuitVerse.
We have deployed the new Notification version/UI. There are still data migration task pending that will migrate all the older notifications for respective users.
Webpush Implementation
I have been working on adding Webpush feature, that will enable user to get notified while not been in our site. Just like you see in mobile notifications in the navbar!
Basically, we need to register service-worker.js
in the user's browser.
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/serviceworker.js.erb', { scope: '/' });
}
So there is a gem webpush for adding webpush functionality into your Rails App. We had implementation for webpush already with the model named as Push_subscription
. Basically for each webpush notification there will be a record in the database which on subscribing triggered the user browser and he can see the notification in the web.
Noticed gem had really cool feature to have customized delivery method for notifications, so I created a custom delivery method Webpush
.
class DeliveryMethods::Webpush < Noticed::DeliveryMethods::Base
def deliver
# Logic for sending the notification
user = User.find(params[:user_id])
project = Project.find(params[:project_id])
url = "/users/#{recipient.id}/notifications"
recipient.push_subscriptions.each do |sub|
if params[:webpush_type] == "star"
sub.send_push_notification("#{user.name} starred your project #{project.name}!", url)
else
sub.send_push_notification("#{user.name} forked your project #{project.name}!", url)
end
end
end
end
and respectively add the delivery method in our notifications:
# app/notifications/fork_notification.rb
class ForkNotification < Noticed::Base
deliver_by :webpush, class: "DeliveryMethods::Webpush"
end
Bug : Couldn't find Project with 'id'
Initially, for every notifications we were saving the ids
of the project and the user(the one who triggered the notificaitons). Due to which while destroying the projects dependent notification were not getting deleted and hence was leading to an error.
Aboobacker assisted me to find the bug, and I finally made an approach to save objects in params
, so that destroying the projects should destroy the dependent notifications objects as well. But here was an issue, the new notification version was released 1.5 weeks before, and in the meantime many notifications with old parameters version that means with project_id
and user_id
in params
have been saved. That is why, we decided to create data_migration
file that will update the params in the correct way. Have a look at it:
class PopulateNoticedNotificationWithValidParams < ActiveRecord::DataMigration
def up
NoticedNotification.find_each do |notification|
if Project.where(id: notification.params[:project_id]).exists?
project = Project.find(notification.params[:project_id])
notification.params[:user] = User.find(notification.params[:user_id])
notification.params[:project] = project
notification.save
else
notification.destroy!
end
end
end
end
PR
- Under Review: Webpush notification
- Merged: fix: dependent notifications on project while destroy
Next Task:
- Complete notifications API.
- Suggested tags for projects.
Conclusion
I was not able to work for 3-4 days due to SIH and health issue. But I learned a lot as always!
Posted on September 5, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.