Class: TentD::Model::NotificationSubscription

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, TypeProperties, UserScoped
Defined in:
lib/tentd/model/notification_subscription.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserScoped

included

Methods included from TypeProperties

included, #type, #type=

Class Method Details

.notify_all(type, post_id) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/tentd/model/notification_subscription.rb', line 21

def self.notify_all(type, post_id)
  post = Post.first(:id => post_id, :fields => [:id, :original, :public])
  post.user.notification_subscriptions.all(:type_base => [TentType.new(type).base, 'all'],
                                           :fields => [:id, :app_authorization_id, :follower_id]).each do |subscription|
    next unless post.can_notify?(subscription.subject)
    Notifications.notify(:subscription_id => subscription.id, :post_id => post_id, :view => subscription.type_view)
  end
end

.notify_entity(entity, post_id, view = 'full') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tentd/model/notification_subscription.rb', line 30

def self.notify_entity(entity, post_id, view='full')
  post = Post.first(:id => post_id)
  return if post.entity == entity
  if follow = post.user.followers.first(:entity => entity) || post.user.followings.first(:entity => entity)
    return unless post.can_notify?(follow)
    client = TentClient.new(follow.notification_servers, follow.auth_details.merge(:faraday_adapter => TentD.faraday_adapter))
    path = follow.notification_path
  else
    return unless post.public
    profile, server_url = TentClient.new(nil, :faraday_adapter => TentD.faraday_adapter).discover(entity).get_profile
    server_urls = API::CoreProfileData.new(profile).servers
    client = TentClient.new(server_urls, :faraday_adapter => TentD.faraday_adapter)
    path = 'posts'
  end
  client.post.create(post.as_json(:view => view), :url => path)
end

Instance Method Details

#notify_about(post_id, view = 'full') ⇒ Object



47
48
49
50
51
52
53
# File 'lib/tentd/model/notification_subscription.rb', line 47

def notify_about(post_id, view='full')
  post = Post.first(:id => post_id)
  client = TentClient.new(subject.notification_servers, subject.auth_details.merge(:faraday_adapter => TentD.faraday_adapter))
  permissions = subject.respond_to?(:scopes) && subject.scopes.include?(:read_permissions)
  client.post.create(post.as_json(:app => !!app_authorization, :permissions => permissions, :view => view), :url => subject.notification_path)
rescue Faraday::Error::ConnectionFailed
end

#subjectObject



17
18
19
# File 'lib/tentd/model/notification_subscription.rb', line 17

def subject
  app_authorization || follower
end