Class: TentD::Model::Post

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserScoped

included

Methods included from TypeProperties

included, #type, #type=

Methods included from RandomPublicId

included

Methods included from Permissible

#assign_permissions, included, #permissions_json

Class Method Details

.create(data) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tentd/model/post.rb', line 71

def self.create(data)
  mentions = data.delete(:mentions)
  post = super(data)

  mentions.to_a.each do |mention|
    next unless mention[:entity]
    post.mentions.create(:entity => mention[:entity], :mentioned_post_id => mention[:post], :post_version_id => post.latest_version(:fields => [:id]).id)
  end

  if post.mentions.to_a.any? && post.original
    post.mentions.each do |mention|
      follower = Follower.first(:entity => mention.entity)
      next if follower && NotificationSubscription.first(:follower => follower, :type_base => post.type.base)

      Notifications.notify_entity(:entity => mention.entity, :post_id => post.id)
    end
  end

  post
end

.fetch_with_permissions(params, current_auth) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tentd/model/post.rb', line 92

def self.fetch_with_permissions(params, current_auth)
  super do |params, query, query_bindings|
    if params.since_time
      query << "AND posts.published_at > ?"
      query_bindings << Time.at(params.since_time.to_i)
    end

    if params.before_time
      query << "AND posts.published_at < ?"
      query_bindings << Time.at(params.before_time.to_i)
    end

    if params.post_types
      params.post_types = params.post_types.split(',').map { |url| URI.unescape(url) }
      if params.post_types.any?
        query << "AND posts.type_base IN ?"
        query_bindings << params.post_types.map { |t| TentType.new(t).base }
      end
    end

    if params.mentioned_post && params.mentioned_entity
      select = query.shift
      query.unshift "INNER JOIN mentions ON mentions.post_id = posts.id"
      query.unshift select

      query << "AND mentions.entity = ? AND mentions.mentioned_post_id = ?"
      query_bindings << params.mentioned_entity
      query_bindings << params.mentioned_post
    end

    unless params.return_count
      query << "ORDER BY posts.published_at DESC"
    end
  end
end

.public_attributesObject



128
129
130
# File 'lib/tentd/model/post.rb', line 128

def self.public_attributes
  [:app_name, :app_url, :entity, :type, :licenses, :content, :published_at]
end

.write_attributesObject



132
133
134
# File 'lib/tentd/model/post.rb', line 132

def self.write_attributes
  public_attributes + [:following_id, :original, :public, :mentions, :views]
end

Instance Method Details

#as_json(options = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tentd/model/post.rb', line 157

def as_json(options = {})
  attributes = super
  attributes[:type] = type.uri
  attributes[:version] = latest_version(:fields => [:version]).version
  attributes[:app] = { :url => attributes.delete(:app_url), :name => attributes.delete(:app_name) }

  attributes[:mentions] = mentions.map do |mention|
    h = { :entity => mention.entity }
    h[:post] = mention.mentioned_post_id if mention.mentioned_post_id
    h
  end

  if options[:app]
    attributes[:following_id] = following.public_id if following
  end

  Array(options[:exclude]).each { |k| attributes.delete(k) if k }
  attributes
end

#can_notify?(app_or_follow) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/tentd/model/post.rb', line 136

def can_notify?(app_or_follow)
  return true if public && original
  case app_or_follow
  when AppAuthorization
    app_or_follow.scopes && app_or_follow.scopes.map(&:to_sym).include?(:read_posts) ||
    app_or_follow.post_types && app_or_follow.post_types.include?(type.base)
  when Follower
    return false unless original
    q = permissions.all(:follower_access_id => app_or_follow.id)
    q += permissions.all(:group_public_id => app_or_follow.groups) if app_or_follow.groups.any?
    q.any?
  when Following
    return false unless original
    q = permissions.all(:following => app_or_follow)
    q += permissions.all(:group_public_id => app_or_follow.groups) if app_or_follow.groups.any?
    q.any?
  else
    false
  end
end

#create_version!(post = self) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/tentd/model/post.rb', line 39

def create_version!(post = self)
  attrs = post.attributes
  attrs.delete(:id)
  latest = post.versions.all(:order => :version.desc, :fields => [:version]).first
  attrs[:version] = latest ? latest.version + 1 : 1
  version = post.versions.create(attrs)
end

#latest_version(options = {}) ⇒ Object



47
48
49
# File 'lib/tentd/model/post.rb', line 47

def latest_version(options = {})
  versions.all({ :order => :version.desc }.merge(options)).first
end

#update(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tentd/model/post.rb', line 51

def update(data)
  mentions = data.delete(:mentions)
  last_version = latest_version(:fields => [:id])
  res = super(data)

  create_version! # after update hook doe not fire

  current_version = latest_version(:fields => [:id])

  if mentions.to_a.any?
    Mention.all(:post_id => self.id).update(:post_id => nil, :post_version_id => last_version.id)
    mentions.each do |mention|
      next unless mention[:entity]
      self.mentions.create(:entity => mention[:entity], :mentioned_post_id => mention[:post], :post_version_id => current_version.id)
    end
  end

  res
end