entities.rb
11.1 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
module Api
module Entities
Entity.format_with :timestamp do |date|
date.strftime('%Y/%m/%d %H:%M:%S') if date
end
PERMISSIONS = {
:admin => 0,
:self => 10,
:private_content => 20,
:logged_user => 30,
:anonymous => 40
}
def self.can_display_profile_field? profile, options, permission_options={}
permissions={:field => "", :permission => :private_content}
permissions.merge!(permission_options)
field = permissions[:field]
permission = permissions[:permission]
return true if profile.public? && profile.public_fields.map{|f| f.to_sym}.include?(field.to_sym)
current_person = options[:current_person]
current_permission = if current_person.present?
if current_person.is_admin?
:admin
elsif current_person == profile
:self
elsif profile.display_private_info_to?(current_person)
:private_content
else
:logged_user
end
else
:anonymous
end
PERMISSIONS[current_permission] <= PERMISSIONS[permission]
end
def self.expose_optional_field?(field, options = {})
return false if options[:params].nil?
optional_fields = options[:params][:optional_fields] || []
optional_fields.include?(field.to_s)
end
class Image < Entity
root 'images', 'image'
expose :url do |image, options|
image.public_filename
end
expose :icon_url do |image, options|
image.public_filename(:icon)
end
expose :minor_url do |image, options|
image.public_filename(:minor)
end
expose :portrait_url do |image, options|
image.public_filename(:portrait)
end
expose :thumb_url do |image, options|
image.public_filename(:thumb)
end
end
class CategoryBase < Entity
root 'categories', 'category'
expose :name, :id, :slug
end
class Category < CategoryBase
root 'categories', 'category'
expose :full_name do |category, options|
category.full_name
end
expose :parent, :using => CategoryBase, if: { parent: true }
expose :children, :using => CategoryBase, if: { children: true }
expose :image, :using => Image
expose :display_color
end
class Region < Category
root 'regions', 'region'
expose :parent_id
end
class Block < Entity
root 'blocks', 'block'
expose :id, :type, :settings, :position, :enabled
expose :mirror, :mirror_block_id, :title
expose :api_content, if: lambda { |object, options| options[:display_api_content] || object.display_api_content_by_default? }
expose :permissions do |block, options|
Entities.permissions_for_entity(block, options[:current_person], :allow_edit?)
end
end
class Box < Entity
root 'boxes', 'box'
expose :id, :position
expose :blocks, :using => Block do |box, options|
box.blocks.select {|block| block.visible_to_user?(options[:current_person]) || block.allow_edit?(options[:current_person]) }
end
end
class Profile < Entity
expose :identifier, :name, :id
expose :created_at, :format_with => :timestamp
expose :updated_at, :format_with => :timestamp
expose :additional_data do |profile, options|
hash ={}
profile.public_values.each do |value|
hash[value.custom_field.name]=value.value
end
profile.public_fields.each do |field|
hash[field] = profile.send(field.to_sym)
end
private_values = profile.custom_field_values - profile.public_values
private_values.each do |value|
if Entities.can_display_profile_field?(profile,options)
hash[value.custom_field.name]=value.value
end
end
hash
end
expose :image, :using => Image
expose :region, :using => Region
expose :type
expose :custom_header
expose :custom_footer
expose :layout_template
expose :permissions do |profile, options|
Entities.permissions_for_entity(profile, options[:current_person],
:allow_post_content?, :allow_edit?, :allow_destroy?)
end
end
class UserBasic < Entity
expose :id
expose :login
end
class Person < Profile
root 'people', 'person'
expose :user, :using => UserBasic, documentation: {type: 'User', desc: 'The user data of a person' }
expose :vote_count
expose :comments_count do |person, options|
person.comments.count
end
expose :following_articles_count do |person, options|
person.following_articles.count
end
expose :articles_count do |person, options|
person.articles.count
end
expose :friends_count do |person, options|
person.friends.size
end
end
class Enterprise < Profile
root 'enterprises', 'enterprise'
end
class Community < Profile
root 'communities', 'community'
expose :description
expose :admins, :if => lambda { |community, options| community.display_info_to? options[:current_person]} do |community, options|
community.admins.map{|admin| {"name"=>admin.name, "id"=>admin.id, "username" => admin.identifier}}
end
expose :categories, :using => Category
expose :members_count
expose :members, :if => lambda {|community, options| Entities.expose_optional_field?(:members, options)}
end
class CommentBase < Entity
expose :body, :title, :id
expose :created_at, :format_with => :timestamp
expose :author, :using => Profile
expose :reply_of, :using => CommentBase
expose :permissions do |comment, options|
Entities.permissions_for_entity(comment, options[:current_person],
:allow_destroy?)
end
end
class Comment < CommentBase
root 'comments', 'comment'
expose :children, as: :replies, :using => Comment
end
class ArticleBase < Entity
root 'articles', 'article'
expose :id
expose :body
expose :abstract, documentation: {type: 'String', desc: 'Teaser of the body'}
expose :created_at, :format_with => :timestamp
expose :updated_at, :format_with => :timestamp
expose :title, :documentation => {:type => "String", :desc => "Title of the article"}
expose :created_by, :as => :author, :using => Profile, :documentation => {type: 'Profile', desc: 'The profile author that create the article'}
expose :profile, :using => Profile, :documentation => {type: 'Profile', desc: 'The profile associated with the article'}
expose :categories, :using => Category
expose :image, :using => Image
expose :votes_for
expose :votes_against
expose :setting
expose :position
expose :hits
expose :start_date
expose :end_date, :documentation => {type: 'DateTime', desc: 'The date of finish of the article'}
expose :tag_list
expose :children_count
expose :slug, :documentation => {:type => "String", :desc => "Trimmed and parsed name of a article"}
expose :path
expose :followers_count
expose :votes_count
expose :comments_count
expose :archived, :documentation => {:type => "Boolean", :desc => "Defines if a article is readonly"}
expose :type
expose :comments, using: CommentBase, :if => lambda{|comment,options| Entities.expose_optional_field?(:comments, options)}
expose :published
expose :accept_comments?, as: :accept_comments
end
def self.permissions_for_entity(entity, current_person, *method_names)
method_names.map { |method| entity.send(method, current_person) ? method.to_s.gsub(/\?/,'') : nil }.compact
end
class Article < ArticleBase
root 'articles', 'article'
expose :parent, :using => ArticleBase
expose :children, :using => ArticleBase do |article, options|
article.children.published.limit(V1::Articles::MAX_PER_PAGE)
end
expose :permissions do |article, options|
Entities.permissions_for_entity(article, options[:current_person],
:allow_edit?, :allow_post_content?, :allow_delete?, :allow_create?,
:allow_publish_content?)
end
end
class User < Entity
root 'users', 'user'
attrs = [:id,:login,:email,:activated?]
aliases = {:activated? => :activated}
attrs.each do |attribute|
name = aliases.has_key?(attribute) ? aliases[attribute] : attribute
expose attribute, :as => name, :if => lambda{|user,options| Entities.can_display_profile_field?(user.person, options, {:field => attribute})}
end
expose :person, :using => Person, :if => lambda{|user,options| user.person.display_info_to? options[:current_person]}
expose :permissions, :if => lambda{|user,options| Entities.can_display_profile_field?(user.person, options, {:field => :permissions, :permission => :self})} do |user, options|
output = {}
user.person.role_assignments.map do |role_assigment|
if role_assigment.resource.respond_to?(:identifier) && !role_assigment.role.nil?
output[role_assigment.resource.identifier] = role_assigment.role.permissions
end
end
output
end
end
class UserLogin < User
root 'users', 'user'
expose :private_token, documentation: {type: 'String', desc: 'A valid authentication code for post/delete api actions'}, if: lambda {|object, options| object.activated? }
end
class Task < Entity
root 'tasks', 'task'
expose :id
expose :type
expose :requestor, using: Profile
expose :status
expose :created_at
expose :data
expose :accept_details
expose :reject_details
expose :accept_disabled?, as: :accept_disabled
expose :reject_disabled?, as: :reject_disabled
expose :target do |task, options|
type_map = {Profile => ::Profile, Environment => ::Environment}.find {|h| task.target.kind_of?(h.last)}
type_map.first.represent(task.target) unless type_map.nil?
end
end
class Environment < Entity
expose :name
expose :id
expose :description
expose :layout_template
expose :signup_intro
expose :terms_of_use
expose :top_url, as: :host
expose :settings, if: lambda { |instance, options| options[:is_admin] }
end
class Tag < Entity
root 'tags', 'tag'
expose :name
end
class Activity < Entity
root 'activities', 'activity'
expose :id, :created_at, :updated_at
expose :user, :using => Profile
expose :target do |activity, opts|
type_map = {Profile => ::Profile, ArticleBase => ::Article}.find {|h| activity.target.kind_of?(h.last)}
type_map.first.represent(activity.target) unless type_map.nil?
end
expose :params, :if => lambda { |activity, options| activity.kind_of?(ActionTracker::Record)}
expose :content, :if => lambda { |activity, options| activity.kind_of?(Scrap)}
expose :verb do |activity, options|
activity.kind_of?(Scrap) ? 'leave_scrap' : activity.verb
end
end
class Role < Entity
root 'roles', 'role'
expose :id
expose :name
expose :key
end
end
end