Compare commits

...
Author SHA1 Message Date
Muhsin Keloth d8c4b10511 feat: Show subscribed notifications 2024-01-08 13:28:58 +05:30
Muhsin Keloth 48cc651a0d fix: review comments 2024-01-08 11:29:23 +05:30
Sojan JoseandGitHub 85284fdcc4 Merge branch 'develop' into CW-2773 2024-01-05 15:39:03 -08:00
Muhsin KelothandGitHub 9a151673c2 Merge branch 'develop' into CW-2773 2024-01-05 12:36:34 +05:30
Muhsin KelothandGitHub 430a4343c2 Merge branch 'develop' into CW-2773 2024-01-05 12:29:31 +05:30
Muhsin Keloth 4285fb20f3 Update notification_finder_spec.rb 2024-01-04 14:27:31 +05:30
Muhsin KelothandGitHub 3474473e38 Merge branch 'develop' into CW-2773 2024-01-03 13:04:30 +05:30
Muhsin Keloth 8275cdd7d1 chore: fix remaining specs 2023-12-20 13:25:01 +05:30
Muhsin Keloth 7605d6c855 chore: Sort notifications based on last_activity_at. 2023-12-20 13:21:33 +05:30
Muhsin KelothandGitHub 7b32bd7c14 Merge branch 'develop' into CW-2773 2023-12-20 13:07:30 +05:30
Muhsin KelothandGitHub c1fae81a2a Merge branch 'develop' into CW-2773 2023-12-20 11:07:26 +05:30
Muhsin Keloth 839d679961 feat: Get notifications based on subscribed notification types. 2023-12-19 18:20:53 +05:30
Muhsin KelothandGitHub 3803923ed1 Merge branch 'develop' into CW-2773 2023-12-15 09:13:31 +05:30
Muhsin KelothandGitHub 523e1defff Merge branch 'develop' into CW-2773 2023-12-14 19:45:28 +05:30
Muhsin Keloth 340ba9bc53 Update notification_finder_spec.rb 2023-12-14 19:44:10 +05:30
Muhsin Keloth ce339d0350 Update notifications_controller.rb 2023-12-14 19:30:16 +05:30
Muhsin Keloth 1906c0ff63 feat: add NotificationFinder 2023-12-14 12:53:01 +05:30
6 changed files with 161 additions and 12 deletions
@@ -8,8 +8,8 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
def index
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
@count = notifications.count
@notifications = notifications.page(@current_page).per(RESULTS_PER_PAGE)
@notifications = notification_finder.perform
@count = @notifications.count
end
def read_all
@@ -61,7 +61,7 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
@current_page = params[:page] || 1
end
def notifications
@notifications ||= current_user.notifications.where(account_id: current_account.id)
def notification_finder
@notification_finder ||= NotificationFinder.new(Current.user, Current.account, params)
end
end
+65
View File
@@ -0,0 +1,65 @@
class NotificationFinder
attr_reader :current_user, :current_account, :params
RESULTS_PER_PAGE = 15
def initialize(current_user, current_account, params)
@current_user = current_user
@current_account = current_account
@params = params
end
def perform
set_up
notifications
end
private
def set_up
find_all_notifications
filter_by_notification_settings
filter_by_status
end
def find_all_notifications
@notifications = current_user.notifications.where(account_id: @current_account.id)
end
def filter_by_notification_settings
notification_settings = current_user.notification_settings.find_by(account_id: @current_account.id)
subscribed_notification_types = get_subscribed_notification_types(notification_settings)
@notifications = filter_notifications_by_types(@notifications, subscribed_notification_types)
end
def get_subscribed_notification_types(notification_settings)
subscribed_notification_types = []
::Notification::NOTIFICATION_TYPES.each_key do |notification_type|
email_flag = "email_#{notification_type}".to_sym
push_flag = "push_#{notification_type}".to_sym
if notification_settings.public_send(email_flag) || notification_settings.public_send(push_flag)
subscribed_notification_types << notification_type
end
end
subscribed_notification_types
end
def filter_notifications_by_types(notifications, types)
notifications.where(notification_type: types)
end
def filter_by_status
@notifications = @notifications.where('snoozed_until > ?', DateTime.now.utc) if params[:status] == 'snoozed'
end
def current_page
params[:page] || 1
end
def notifications
@notifications.page(current_page).per(RESULTS_PER_PAGE).order(last_activity_at: :desc)
end
end
@@ -158,6 +158,7 @@ export default {
this.$store.dispatch('notifications/unReadCount');
this.$store.dispatch('teams/get');
this.$store.dispatch('attributes/get');
this.$store.dispatch('userNotificationSettings/get');
this.fetchCustomViews();
},
@@ -1,14 +1,46 @@
const NOTIFICATION_TYPES = {
conversation_creation: 1,
conversation_assignment: 2,
assigned_conversation_new_message: 3,
conversation_mention: 4,
participating_conversation_new_message: 5,
};
function getSubscribedNotificationTypes(pushFlags, emailFlags) {
return Object.keys(NOTIFICATION_TYPES).filter(notificationType => {
const emailFlag = `email_${notificationType}`;
const pushFlag = `push_${notificationType}`;
return pushFlags.includes(pushFlag) || emailFlags.includes(emailFlag);
});
}
export const getters = {
getNotifications($state) {
return Object.values($state.records).sort((n1, n2) => n2.id - n1.id);
getNotifications($state, __, rootState) {
const notificationSettings =
rootState.userNotificationSettings.record || {};
const pushFlags = notificationSettings.selected_push_flags || {};
const emailFlags = notificationSettings.selected_email_flags || {};
const subscribedNotificationTypes = getSubscribedNotificationTypes(
pushFlags,
emailFlags
);
return Object.values($state.records)
.filter(notification =>
subscribedNotificationTypes.includes(notification.notification_type)
)
.sort((n1, n2) => n2.id - n1.id);
},
getUIFlags($state) {
return $state.uiFlags;
},
getNotification: $state => id => {
const notification = $state.records[id];
return notification || {};
return $state.records[id] || {};
},
getMeta: $state => {
return $state.meta;
},
-4
View File
@@ -47,10 +47,6 @@ class Notification < ApplicationRecord
after_create_commit :process_notification_delivery, :dispatch_create_event
after_destroy_commit :dispatch_destroy_event
# TODO: Get rid of default scope
# https://stackoverflow.com/a/1834250/939299
default_scope { order(id: :desc) }
PRIMARY_ACTORS = ['Conversation'].freeze
def push_event_data
+55
View File
@@ -0,0 +1,55 @@
require 'rails_helper'
describe NotificationFinder do
subject(:notification_finder) { described_class.new(user, account, params) }
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
before do
create(:notification, account: account, user: user, updated_at: DateTime.now.utc + 1.day, last_activity_at: DateTime.now.utc + 1.day)
create(:notification, account: account, user: user, snoozed_until: DateTime.now.utc + 3.days, updated_at: DateTime.now.utc,
last_activity_at: DateTime.now.utc)
create(:notification, account: account, user: user, updated_at: DateTime.now.utc + 2.days, last_activity_at: DateTime.now.utc + 2.days)
create(:notification, account: account, user: user, updated_at: DateTime.now.utc + 4.days, last_activity_at: DateTime.now.utc + 4.days,
notification_type: :conversation_creation)
create(:notification, account: account, user: user, updated_at: DateTime.now.utc + 5.days, last_activity_at: DateTime.now.utc + 5.days,
notification_type: :conversation_mention)
create(:notification, account: account, user: user, updated_at: DateTime.now.utc + 6.days, last_activity_at: DateTime.now.utc + 6.days,
notification_type: :participating_conversation_new_message)
end
describe '#perform' do
context 'when params are empty' do
let(:params) { {} }
it 'returns notifications which aren not snoozed' do
result = notification_finder.perform
expect(result.length).to be 3
end
it 'orders notifications by last activity at' do
result = notification_finder.perform
expect(result.first.last_activity_at).to be > result.last.last_activity_at
end
it 'filter notifications by user preferences' do
notification_setting = user.notification_settings.find_by(account_id: account.id)
notification_setting.selected_email_flags = [:email_conversation_creation]
notification_setting.save!
result = notification_finder.perform
expect(result.length).to be 4
end
end
context 'when snoozed param is passed' do
let(:params) { { status: 'snoozed' } }
it 'returns only snoozed notifications' do
result = notification_finder.perform
expect(result.length).to be 1
end
end
end
end