Class: CloudObject::Subscriber

Inherits:
ApplicationLesliRecord
  • Object
show all
Defined in:
app/models/lesli/cloud_object/subscriber.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#action:discussion_created, ... (readonly)

Returns:

  • (:discussion_created, :action_created, :file_created, :activity_created, :workflow_updated, :priority_updated, :http_get, :http_post, :http_put, :http_patch, :http_update)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/lesli/cloud_object/subscriber.rb', line 49

enum action: {
    object_created: "object_created",
    object_updated: "object_updated",
    object_destroyed: "object_destroyed",
    object_status_updated: "object_status_updated",

    action_created: "action_created",
    action_updated: "action_updated",
    action_destroyed: "action_destroyed",

    discussion_created: "discussion_created",
    discussion_updated: "discussion_updated",
    discussion_destroyed: "discussion_destroyed",

    file_created: "file_created",
    file_updated: "file_updated",
    file_destroyed: "file_destroyed"
}

#notification_type:web, :email (readonly)

Returns:

  • (:web, :email)


72
73
74
75
76
77
78
# File 'app/models/lesli/cloud_object/subscriber.rb', line 72

enum notification_type: {
    email: "email",                 # notification sent by email only
    webpush: "webpush",             # notification for web interface only
    mobilepush: "mobilepush",       # notification for mobile only
    mobiledialog: "mobiledialog",   # open a dialog in the main app screen
    push: "push",                   # webpush & mobilepush
}

Class Method Details

.add_subscriber(cloud_object, user, action = nil, notification_type = :web) ⇒ void

This method returns an undefined value.

Examples:

first_ticket = CloudHelp::Ticket.find( 1 )
second_ticket = CloudHelp::Ticket.find( 2 )
user = current_user
CloudHelp::Ticket::Subscriber.add_subscriber( first_ticket, current_user )
CloudHelp::Ticket::Subscriber.add_subscriber( second_ticket, current_user, :http_post, :email )

Parameters:

  • cloud_object (ApplicationRecord)

    Cloud object to which an user can subscribe to

  • user (User)

    The user that is subscribing to the cloud_ubject

  • action (Symbol) (defaults to: nil)

    A valid action from this class’s action enum to wich the user will be subscribed

  • notification_type (Symbol) (defaults to: :web)

    A valid notification_type from this class’s notification_type enum



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/lesli/cloud_object/subscriber.rb', line 94

def self.add_subscriber(cloud_object, user, action=nil, notification_type= :web)

    if action
        return cloud_object.subscribers.create(
            user_creator: user,
            action: action,
            notification_type: self.notification_types[notification_type]
        )
    end

    self.actions.values.each do |action|
        cloud_object.subscribers.create(
            user_creator: user,
            action: action,
            notification_type: self.notification_types[notification_type]
        )
    end
end

.cloud_object_modelClass

Returns The class of the association ‘belongs_to’.

Examples:

puts DeutscheLeibrenten::Project::Discussion.cloud_object_model.new # This will display an instance of DeutscheLeibrenten::Project

Returns:

  • (Class)

    The class of the association ‘belongs_to’



182
183
184
# File 'app/models/lesli/cloud_object/subscriber.rb', line 182

def self.cloud_object_model
    self.reflect_on_association(:cloud_object).klass
end

.notify_subscribers(current_user, cloud_object, action, subject: nil, body: nil, url: nil) ⇒ void

This method returns an undefined value.

Examples:

ticket = CloudHelp::Ticket.find( 1 )
ClodHelp::Ticket::Subscriber.notify_subscribers(
    ticket,
    "A new comment has been added to ticket #{ticket.id}",
    :discussion_created
)

Parameters:

  • cloud_object (ApplicationRecord)

    Cloud object to which an user can subscribe to

  • subject (String) (defaults to: nil)

    The subject that will be shown in the notification

  • action (Symbol)

    A valid action from this class’s action enum



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/lesli/cloud_object/subscriber.rb', line 128

def self.notify_subscribers(current_user, cloud_object, action, subject: nil, body: nil, url: nil)

    unless cloud_object.subscribers.blank?
        subscribers = cloud_object.subscribers.where(action: action)
        subscribers = subscribers.where("users_id != ?", current_user.id) if current_user
        
        subscribers.each do |subscriber|
            Courier::Bell::Notification.new(
                subscriber.user_main || subscriber.user_creator,
                subject || action,
                body: body,
                url: url,
                category: "info",
                channel: subscriber.notification_type
            )
        end
    end
end

.subscription_actions(cloud_object, user) ⇒ Array

Returns Array of subscriptions. There is one subscription per action.

Examples:

ticket = CloudHelp::Ticket.find( 1 )
subscription_actions = CloudHelp::Ticket::Subscriber.subscription_actions(
    ticket,
    current_user
)

Parameters:

  • cloud_object (ApplicationRecord)

    Cloud object to which an user can subscribe to

  • user (User)

    The user that is subscribing to the cloud_object

Returns:

  • (Array)

    Array of subscriptions. There is one subscription per action.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/lesli/cloud_object/subscriber.rb', line 159

def self.subscription_actions(cloud_object, user)
    data = { }
    actions = self.actions.keys
    actions.each do |action|
        data[action] = {
            action: action,
            subscribed: false,
            notification_type: :email
        }
    end
    cloud_object.subscribers.where(users_id: user.id).each do |subscriber|
        data[subscriber.action][:id] = subscriber.id
        data[subscriber.action][:subscribed] = true
        data[subscriber.action][:notification_type] = subscriber.notification_type
    end
    data.values
end

Instance Method Details

#user_mainUser

Returns This method will always return nil.

Returns:

  • (User)

    This method will always return nil



42
43
44
# File 'app/models/lesli/cloud_object/subscriber.rb', line 42

def user_main
    return nil
end