Skip to content

Commit

Permalink
feat: New group notification (CircuitVerse#3389)
Browse files Browse the repository at this point in the history
* feat: added notification for new group event

Signed-off-by: VaibhavUpreti <[email protected]>
Co-authored-by: Vedant Jain <[email protected]>
  • Loading branch information
VaibhavUpreti and vedant-jain03 authored Mar 25, 2023
1 parent c064c94 commit e4b7461
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 24 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/users.scss
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@
padding: 12px 10px;
}

.fa-users {
padding: 12px 10px;
}

.fa-comment {
padding: 10px;
}
Expand Down
30 changes: 19 additions & 11 deletions app/controllers/users/noticed_notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,26 @@ def mark_as_read
notification = NoticedNotification.find(params[:notification_id])
notification.update(read_at: Time.zone.now)
answer = NotifyUser.new(params).call
notification_path(answer)
end

def mark_all_as_read
NoticedNotification.where(recipient: current_user, read_at: nil).update_all(read_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations
redirect_to notifications_path(current_user)
end

def read_all_notifications
NoticedNotification.where(recipient: current_user, read_at: nil).update_all(read_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations
redirect_back(fallback_location: root_path)
end
end

private

def notification_path(answer)
case answer.type
when "new_group"
redirect_to group_path(answer.first_param)
when "new_assignment"
redirect_to group_assignment_path(answer.first_param, answer.second)
when "star", "fork"
Expand All @@ -25,14 +44,3 @@ def mark_as_read
redirect_to root_path
end
end

def mark_all_as_read
NoticedNotification.where(recipient: current_user, read_at: nil).update_all(read_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations
redirect_to notifications_path(current_user)
end

def read_all_notifications
NoticedNotification.where(recipient: current_user, read_at: nil).update_all(read_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations
redirect_back(fallback_location: root_path)
end
end
8 changes: 7 additions & 1 deletion app/models/group_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ class GroupMember < ApplicationRecord
belongs_to :group, counter_cache: true
belongs_to :user
has_many :assignments, through: :group

after_commit :send_welcome_notification, on: :create
after_commit :send_welcome_email, on: :create
scope :mentor, -> { where(mentor: true) }
scope :member, -> { where(mentor: false) }

has_noticed_notifications model_name: "NoticedNotification", dependent: :destroy

def send_welcome_notification
NewGroupNotification.with(group: self).deliver_later(user)
end

def send_welcome_email
GroupMailer.new_member_email(user, group).deliver_later
end
Expand Down
16 changes: 16 additions & 0 deletions app/notifications/new_group_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class NewGroupNotification < Noticed::Base
deliver_by :database, association: :noticed_notifications

def message
@group = params[:group][:group_id]
group = Group.find(@group)
user = group.primary_mentor.name
t("users.notifications.new_group_notification", group: group.name, user: user)
end

def icon
"fa fa-users"
end
end
23 changes: 16 additions & 7 deletions app/services/notify_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class NotifyUser
Result = Struct.new(:success, :type, :first_param, :second)
def initialize(params)
@notification = NoticedNotification.find(params[:notification_id])
@assignment = @notification.params[:assignment]
@project = @notification.params[:project]
@assignment = @notification.params[:assignment]
@thread = @notification.params[:forum_thread]
end

Expand All @@ -20,19 +20,28 @@ def call

def type_check
case @notification.type
when "StarNotification"
Result.new("true", "star", @project.author, @project)
when "ForkNotification"
Result.new("true", "fork", @project.author, @project)
when "NewGroupNotification"
handle_group_notification
when "StarNotification", "ForkNotification"
Result.new("true", @notification.type[0, 4].downcase, @project.author, @project)
when "NewAssignmentNotification"
Result.new("true", "new_assignment", @assignment.group, @assignment)
when "ForumCommentNotification"
@post = @notification.params[:forum_post]
Result.new("true", "forum_comment", @thread, @post.id)
handle_forum_comment
when "ForumThreadNotification"
Result.new("true", "forum_thread", @thread)
else
Result.new("false", "no_type", root_path)
end
end

def handle_group_notification
@group = @notification.params[:group][:group_id]
Result.new("true", "new_group", @group, 1)
end

def handle_forum_comment
@post = @notification.params[:forum_post]
Result.new("true", "forum_comment", @thread, @post.id)
end
end
3 changes: 2 additions & 1 deletion config/locales/views/users/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ en:
see_all : "See All"
fork_notification: "%{user} forked your Project %{project}"
star_notification: "%{user} starred your Project %{project}"
new_group_notification: "%{user} added you to a new group %{group}"
new_assignment_notification: "You have been assigned a new assignment '%{assignment_name}'"
forum_thread_notification: "%{user} posted a new thread '%{thread}'"
comment_notif: "%{user} commented on thread '%{thread}'"
comment_notif: "%{user} commented on thread '%{thread}'"
3 changes: 2 additions & 1 deletion config/locales/views/users/hi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ hi:
see_all : "सभी देखें"
fork_notification: "%{user} ने आपके प्रोजेक्ट %{project} को फोर्क किया"
star_notification: "%{user} ने आपका प्रोजेक्ट %{project} स्टार किया"
new_group_notification: "%{user} ने आपको एक नए समूह: %{group} में जोड़ा है"
new_assignment_notification: "आपको एक नया कार्य सौंपा गया है! - '%{assignment_name}'"
forum_thread_notification: "%{user} ने एक नया सूत्र '%{thread}' पोस्ट किया"
comment_notif: "%{user} ने '%{thread}' पर टिप्पणी की"
comment_notif: "%{user} ने '%{thread}' पर टिप्पणी की"
3 changes: 0 additions & 3 deletions spec/controllers/assignments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@
end

it "sends notifications to group members" do
sign_in @primary_mentor
post group_assignments_path(@group), params: { assignment:
{ description: "group assignment", name: "Test Name" } }
expect(@member.noticed_notifications.count).to eq(1)
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/models/group_member_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
describe "callbacks" do
it "calls respective callbacks" do
expect_any_instance_of(described_class).to receive(:send_welcome_email)
expect_any_instance_of(described_class).to receive(:send_welcome_notification)
FactoryBot.create(:group_member, user: @user, group: @group)
end
end
Expand All @@ -27,5 +28,12 @@
group_member.send_welcome_email
end.to have_enqueued_job.on_queue("mailers")
end

it "sends welcome notification" do
group_member = FactoryBot.create(:group_member, user: @user, group: @group)
expect do
group_member.send_welcome_notification
end.to change { @user.noticed_notifications.count }.by(1)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# creates a test user
user = FactoryBot.create(:user)
post "/api/v1/password/forgot", params: { email: user.email }, as: :json
@user = user
end

it "returns status 200 and should send reset password instructions" do
Expand Down

0 comments on commit e4b7461

Please sign in to comment.