cirandas.net

ref: master

plugins/push_notification/controllers/push_notification_plugin_myprofile_controller.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require_relative "../lib/notification_settings"
require_relative "../lib/device_token"

class PushNotificationPluginMyprofileController < MyProfileController

  append_view_path File.join(File.dirname(__FILE__) + '/../views')

  def index
    @devices = current_user.device_tokens
    @settings = filter_notifications current_user.notification_settings.hash_flags
  end

  def delete_device
    device = PushNotificationPlugin::DeviceToken.find(params["device"])
    device.delete
    redirect_to :action => "index"
  end

  def update_settings
    current_user.notification_settings.set_notifications(params["settings"] || {})
    current_user.save
    redirect_to :action => "index"
  end

  private

  def filter_notifications hash_flags
    server_settings = Noosfero::Plugin::Settings.new(environment, PushNotificationPlugin)
    server_notifications = server_settings.notifications || {}
    filtered_settings = {}
    hash_flags.each do |notification, enabled|
      filtered_settings[notification] = enabled if server_notifications[notification] == "1"
    end
    filtered_settings
  end
end