cirandas.net

ref: master

plugins/push_notification/lib/push_notification_plugin.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class PushNotificationPlugin < Noosfero::Plugin

  include Noosfero::Plugin::HotSpot
  include PushNotificationPlugin::Observers

  def self.plugin_name
    I18n.t("push_notification_plugin.lib.plugin.name")
  end

  def self.subscribe environment, notification, klass
    return nil unless PushNotificationPlugin::NotificationSettings::NOTIFICATIONS.keys.include?(notification)
    return nil unless klass.name.constantize.respond_to?("push_notification_#{notification}_additional_users".to_sym)

    notification_subscription = PushNotificationPlugin::NotificationSubscription.where(:notification => notification).first
    notification_subscription ||= PushNotificationPlugin::NotificationSubscription.new({:notification => notification,
      :environment => environment, :subscribers => [klass.name]})

    notification_subscription.subscribers |= [klass.name]
    notification_subscription.save
  end

  def self.unsubscribe environment, notification, klass
    return nil unless PushNotificationPlugin::NotificationSettings::NOTIFICATIONS.keys.include?(notification)
    notification_subscription = PushNotificationPlugin::NotificationSubscription.where(:notification => notification, :environment => environment).first
    unless notification_subscription.blank?
      if notification_subscription.subscribers.include?(klass.name)
        notification_subscription.subscribers -= [klass.name]
        return notification_subscription.save
      end
    end
    return false
  end

  def self.subscribers environment, notification
    return nil unless PushNotificationPlugin::NotificationSettings::NOTIFICATIONS.keys.include?(notification)
    notification_subscription = PushNotificationPlugin::NotificationSubscription.where(:notification =>  notification, :environment => environment)
    return [] if notification_subscription.blank?
    notification_subscription.first.subscribers.map{|s| s.constantize}
  end

  def self.api_mount_points
    [PushNotificationPlugin::API]
  end

  def self.plugin_description
    I18n.t("push_notification_plugin.lib.plugin.description")
  end

  def stylesheet?
    true
  end

  def control_panel_buttons
    if context.profile.person?
      {
        title: I18n.t("push_notification_plugin.lib.plugin.panel_button"),
        icon: 'push-notifications',
        url: {controller: 'push_notification_plugin_myprofile', action: 'index'}
      }
    end
  end
end