Class: NotifyUser::BaseNotificationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/notify_user/base_notifications_controller.rb

Direct Known Subclasses

NotificationsController

Instance Method Summary collapse

Instance Method Details

#collectionObject



10
11
12
13
14
15
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 10

def collection
  @notifications = NotifyUser::BaseNotification.for_target(@user)
                                                .order("created_at DESC")
                                                .where('parent_id IS NULL')
  collection_pagination
end

#collection_paginationObject



17
18
19
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 17

def collection_pagination
  @notifications = @notifications.page(params[:page]).per(params[:per_page])
end

#indexObject



5
6
7
8
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 5

def index
  collection
  respond_to_method
end

#mark_allObject



34
35
36
37
38
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 34

def mark_all
  @notifications = NotifyUser::BaseNotification.for_target(@user).where('state != ?', 'read')
  @notifications.update_all(state: :read)
  render json: @notifications
end

#mark_readObject



28
29
30
31
32
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 28

def mark_read
  @notifications = NotifyUser::BaseNotification.for_target(@user).where('id IN (?)', params[:ids])
  @notifications.update_all(state: :read)
  render json: @notifications
end

#mass_subscriptionsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 92

def mass_subscriptions
  types = build_notification_types()
  if params[:type]
    types[:subscriptions].each do |type|
      unsubscribe = NotifyUser::Unsubscribe.has_unsubscribed_from(@user, type[:type])
      if params[:type][type[:type]] == "1"
        NotifyUser::Unsubscribe.unsubscribe(@user,type[:type])
      else
        if unsubscribe.empty?
          #if unsubscribe doesn't exist create it
          unsubscribe = NotifyUser::Unsubscribe.create(target: @user, type: type[:type])
        end
      end
    end
    flash[:message] = "Successfully updated your notifcation settings"
  end
  redirect_to notify_user_notifications_unsubscribe_path
end

#notifications_countObject



40
41
42
43
44
45
46
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 40

def notifications_count
  @notifications = NotifyUser::BaseNotification.for_target(@user)
    .where('parent_id IS NULL')
    .where('state IN (?)', ["sent_as_aggregation_parent", "sent", "pending"])

  render json: { :count => @notifications.count }
end

#readObject

get



62
63
64
65
66
67
68
69
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 62

def read
  @notification = NotifyUser::BaseNotification.for_target(@user).where('id = ?', params[:id]).first
  unless @notification.read?
    @notification.mark_as_read!
  end

  redirect_logic(@notification)
end

#redirect_logic(notification) ⇒ Object



71
72
73
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 71

def redirect_logic(notification)
  render :text => "redirect setup goes here"
end

#respond_to_methodObject



21
22
23
24
25
26
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 21

def respond_to_method
  respond_to do |format|
    format.html
    format.json {render :json => @notifications, meta: { pagination: { per_page: @notifications.limit_value, total_pages: @notifications.total_pages, total_objects: @notifications.total_count } }}
  end
end

#subscribeObject



137
138
139
140
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 137

def subscribe
  NotifyUser::Unsubscribe.subscribe(@user, params[:type]) if params[:type]
  redirect_to notify_user_notifications_unsubscribe_path
end

#subscriptionsObject

endpoint for accessing subscriptions and their statuses



86
87
88
89
90
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 86

def subscriptions
  update_subscriptions(params[:types]) if params[:types]
  @types = build_notification_types
  render :json => @types
end

#unauth_unsubscribeObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 122

def unauth_unsubscribe
  if params[:token] && params[:type]
    if NotifyUser::UserHash.confirm_hash(params[:token], params[:type])
      user_hash = NotifyUser::UserHash.where(token: params[:token], type: params[:type]).first
      @user = user_hash.target
      unsubscribe = NotifyUser::Unsubscribe.create(target: @user, type: params[:type])
      user_hash.deactivate
      return render :text => "successfully unsubscribed from #{params[:type]} notifications"
    else
      return render :text => "invalid token"
    end
  end
  return render :text => "Something went wrong please try again later"
end

#unsubscribeObject



75
76
77
78
79
80
81
82
83
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 75

def unsubscribe
  if params[:type]
    NotifyUser::Unsubscribe.unsubscribe(@user, params[:type])
    redirect_to notify_user_notifications_unsubscribe_path
  end
  @types = build_notification_types
  @unsubscribale_types = NotifyUser.unsubscribable_notifications
  @unsubscribale_channels = NotifyUser::BaseNotification.channels
end

#unsubscribe_from_objectObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 48

def unsubscribe_from_object
  case params[:subscription][:unsubscribe]
  when true
    NotifyUser::Unsubscribe.unsubscribe(@user, params[:subscription][:type], params[:subscription][:group_id])
  when false
    NotifyUser::Unsubscribe.subscribe(@user, params[:subscription][:type], params[:subscription][:group_id])
  else
    raise "unsubscribe field required"
  end

  render json: {status: "OK"}, status: 201
end

#update_subscriptions(types) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 111

def update_subscriptions(types)
  types.each do |type|
    unsubscribe = NotifyUser::Unsubscribe.has_unsubscribed_from(@user, type[:type])
    if type[:status] == '0'
      NotifyUser::Unsubscribe.unsubscribe(@user, type[:type])
    else
      NotifyUser::Unsubscribe.subscribe(@user, type[:type])
    end
  end
end