Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8c4b10511 | ||
|
|
48cc651a0d | ||
|
|
85284fdcc4 | ||
|
|
9a151673c2 | ||
|
|
430a4343c2 | ||
|
|
4285fb20f3 | ||
|
|
3474473e38 | ||
|
|
8275cdd7d1 | ||
|
|
7605d6c855 | ||
|
|
7b32bd7c14 | ||
|
|
c1fae81a2a | ||
|
|
839d679961 | ||
|
|
3803923ed1 | ||
|
|
523e1defff | ||
|
|
340ba9bc53 | ||
|
|
ce339d0350 | ||
|
|
1906c0ff63 |
@@ -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
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user