inkwell 1.1.1 → 1.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ class ChangeCommunityTableForAddingTypesAndUserAccess < ActiveRecord::Migration
2
+ def change
3
+ if ::Inkwell::Engine::config.respond_to?('community_table')
4
+ add_column ::Inkwell::Engine::config.community_table, :default_user_access, :string, :default => 'w'
5
+ add_column ::Inkwell::Engine::config.community_table, :writers_ids, :text, :default => '[]'
6
+ add_column ::Inkwell::Engine::config.community_table, :banned_ids, :text, :default => '[]'
7
+ add_column ::Inkwell::Engine::config.community_table, :muted_ids, :text, :default => '[]'
8
+ add_column ::Inkwell::Engine::config.community_table, :invitations_uids, :text, :default => '[]'
9
+ add_column ::Inkwell::Engine::config.community_table, :public, :boolean, :default => true
10
+ rename_column ::Inkwell::Engine::config.user_table, :communities_ids, :communities_info
11
+ remove_column ::Inkwell::Engine::config.user_table, :admin_of
12
+ end
13
+ end
14
+ end
@@ -27,16 +27,22 @@ module Inkwell
27
27
  def add_user(options = {})
28
28
  options.symbolize_keys!
29
29
  user = options[:user]
30
- return if self.include_user? user
30
+ raise "this user is already in this community" if self.include_user? user
31
+ raise "this user is banned" if self.include_banned_user? user
31
32
 
32
33
  users_ids = ActiveSupport::JSON.decode self.users_ids
33
34
  users_ids << user.id
34
35
  self.users_ids = ActiveSupport::JSON.encode users_ids
36
+ if (self.default_user_access == CommunityAccessLevels::WRITE) && !(self.include_muted_user? user)
37
+ writers_ids = ActiveSupport::JSON.decode self.writers_ids
38
+ writers_ids << user.id
39
+ self.writers_ids = ActiveSupport::JSON.encode writers_ids
40
+ end
35
41
  self.save
36
42
 
37
- communities_ids = ActiveSupport::JSON.decode user.communities_ids
38
- communities_ids << self.id
39
- user.communities_ids = ActiveSupport::JSON.encode communities_ids
43
+ communities_info = ActiveSupport::JSON.decode user.communities_info
44
+ communities_info << Hash[HashParams::COMMUNITY_ID => self.id, HashParams::ACCESS_LEVEL => self.default_user_access]
45
+ user.communities_info = ActiveSupport::JSON.encode communities_info
40
46
  user.save
41
47
 
42
48
  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
@@ -73,11 +79,20 @@ module Inkwell
73
79
  users_ids = ActiveSupport::JSON.decode self.users_ids
74
80
  users_ids.delete user.id
75
81
  self.users_ids = ActiveSupport::JSON.encode users_ids
82
+
83
+ writers_ids = ActiveSupport::JSON.decode self.writers_ids
84
+ writers_ids.delete user.id
85
+ self.writers_ids = ActiveSupport::JSON.encode writers_ids
86
+
87
+ admins_info = ActiveSupport::JSON.decode self.admins_info
88
+ admins_info.delete_if{|item| item['admin_id'] == user.id}
89
+ self.admins_info = ActiveSupport::JSON.encode admins_info
90
+
76
91
  self.save
77
92
 
78
- communities_ids = ActiveSupport::JSON.decode user.communities_ids
79
- communities_ids.delete self.id
80
- user.communities_ids = ActiveSupport::JSON.encode communities_ids
93
+ communities_info = ActiveSupport::JSON.decode user.communities_info
94
+ communities_info.delete_if {|item| item[HashParams::COMMUNITY_ID] == self.id}
95
+ user.communities_info = ActiveSupport::JSON.encode communities_info
81
96
  user.save
82
97
 
83
98
  timeline_items = ::Inkwell::TimelineItem.where(:owner_id => user.id, :owner_type => OwnerTypes::USER).where "from_source like '%{\"community_id\":#{self.id}%'"
@@ -91,27 +106,135 @@ module Inkwell
91
106
  end
92
107
  end
93
108
 
109
+ def include_writer?(user)
110
+ check_user user
111
+ writers_ids = ActiveSupport::JSON.decode self.writers_ids
112
+ writers_ids.include? user.id
113
+ end
114
+
94
115
  def include_user?(user)
95
116
  check_user user
96
- communities_ids = ActiveSupport::JSON.decode user.communities_ids
97
- communities_ids.include? self.id
117
+ communities_info = ActiveSupport::JSON.decode user.communities_info
118
+ (communities_info.index{|item| item[HashParams::COMMUNITY_ID] == self.id}) ? true : false
119
+ end
120
+
121
+ def mute_user(options = {})
122
+ options.symbolize_keys!
123
+ user = options[:user]
124
+ admin = options[:admin]
125
+ raise "user should be passed in params" unless user
126
+ raise "admin should be passed in params" unless admin
127
+ check_user user
128
+ check_user admin
129
+ raise "admin is not admin" unless self.include_admin? admin
130
+ raise "user should be a member of this community" unless self.include_user? user
131
+ raise "this user is already muted" if self.include_muted_user? user
132
+ raise "it is impossible to mute yourself" if user == admin
133
+ raise "admin has no permissions to mute this user" if (self.include_admin? user) && (admin_level_of(admin) >= admin_level_of(user))
134
+
135
+
136
+ muted_ids = ActiveSupport::JSON.decode self.muted_ids
137
+ muted_ids << user.id
138
+ self.muted_ids = ActiveSupport::JSON.encode muted_ids
139
+ self.save
140
+ end
141
+
142
+ def unmute_user(options = {})
143
+ options.symbolize_keys!
144
+ user = options[:user]
145
+ admin = options[:admin]
146
+ raise "user should be passed in params" unless user
147
+ raise "admin should be passed in params" unless admin
148
+ check_user user
149
+ check_user admin
150
+ raise "admin is not admin" unless self.include_admin? admin
151
+ raise "user should be a member of this community" unless self.include_user? user
152
+ raise "this user is not muted" unless self.include_muted_user? user
153
+ raise "admin has no permissions to unmute this user" if (self.include_admin? user) && (admin_level_of(admin) >= admin_level_of(user))
154
+
155
+ muted_ids = ActiveSupport::JSON.decode self.muted_ids
156
+ muted_ids.delete user.id
157
+ self.muted_ids = ActiveSupport::JSON.encode muted_ids
158
+ self.save
159
+ end
160
+
161
+ def include_muted_user?(user)
162
+ check_user user
163
+ muted_ids = ActiveSupport::JSON.decode self.muted_ids
164
+ muted_ids.include? user.id
165
+ end
166
+
167
+ def ban_user(options = {})
168
+ options.symbolize_keys!
169
+ user = options[:user]
170
+ admin = options[:admin]
171
+ raise "user should be passed in params" unless user
172
+ raise "admin should be passed in params" unless admin
173
+ check_user user
174
+ check_user admin
175
+ raise "admin is not admin" unless self.include_admin? admin
176
+ if self.public
177
+ raise "user should be a member of public community" unless self.include_user?(user)
178
+ else
179
+ raise "user should be a member of private community or send invitation request to it" unless self.include_user?(user) || self.include_invitation_request?(user)
180
+ end
181
+ raise "this user is already banned" if self.include_banned_user? user
182
+ raise "admin has no permissions to ban this user" if (self.include_admin? user) && (admin_level_of(admin) >= admin_level_of(user))
183
+
184
+ banned_ids = ActiveSupport::JSON.decode self.banned_ids
185
+ banned_ids << user.id
186
+ self.banned_ids = ActiveSupport::JSON.encode banned_ids
187
+ self.save
188
+ unless self.public
189
+ self.reject_invitation_request :admin => admin, :user => user if self.include_invitation_request? user
190
+ end
191
+ self.remove_user :admin => admin, :user => user
192
+ end
193
+
194
+ def unban_user(options = {})
195
+ options.symbolize_keys!
196
+ user = options[:user]
197
+ admin = options[:admin]
198
+ raise "user should be passed in params" unless user
199
+ raise "admin should be passed in params" unless admin
200
+ check_user user
201
+ check_user admin
202
+ raise "admin is not admin" unless self.include_admin? admin
203
+ raise "this user is not banned" unless self.include_banned_user? user
204
+
205
+ banned_ids = ActiveSupport::JSON.decode self.banned_ids
206
+ banned_ids.delete user.id
207
+ self.banned_ids = ActiveSupport::JSON.encode banned_ids
208
+ self.save
209
+ end
210
+
211
+ def include_banned_user?(user)
212
+ check_user user
213
+ banned_ids = ActiveSupport::JSON.decode self.banned_ids
214
+ banned_ids.include? user.id
98
215
  end
99
216
 
100
217
  def add_admin(options = {})
101
218
  options.symbolize_keys!
102
219
  user = options[:user]
103
220
  admin = options[:admin]
221
+ raise "user should be passed in params" unless user
104
222
  raise "admin should be passed in params" unless admin
105
- raise "user is already admin" if self.include_admin?(user)
106
- raise "admin is not admin" unless self.include_admin?(admin)
107
- raise "user should be a member of this community" unless self.include_user?(user)
223
+ check_user user
224
+ check_user admin
225
+ raise "user is already admin" if self.include_admin? user
226
+ raise "admin is not admin" unless self.include_admin? admin
227
+ raise "user should be a member of this community" unless self.include_user? user
108
228
 
109
- admin_level_granted_for_user = admin_level_of(admin) + 1
229
+ self.unmute_user :user => user, :admin => admin if self.include_muted_user? user
110
230
 
111
- admin_positions = ActiveSupport::JSON.decode user.admin_of
112
- admin_positions << Hash['community_id' => self.id, 'admin_level' => admin_level_granted_for_user]
113
- user.admin_of = ActiveSupport::JSON.encode admin_positions
114
- user.save
231
+ unless self.include_writer? user
232
+ writers_ids = ActiveSupport::JSON.decode self.writers_ids
233
+ writers_ids << user.id
234
+ self.writers_ids = ActiveSupport::JSON.encode writers_ids
235
+ end
236
+
237
+ admin_level_granted_for_user = admin_level_of(admin) + 1
115
238
 
116
239
  admins_info = ActiveSupport::JSON.decode self.admins_info
117
240
  admins_info << Hash['admin_id' => user.id, 'admin_level' => admin_level_granted_for_user]
@@ -130,11 +253,6 @@ module Inkwell
130
253
  raise "admin has no permissions to delete this user from admins" if (admin_level_of(admin) >= admin_level_of(user)) && (user != admin)
131
254
  raise "community owner can not be removed from admins" if admin_level_of(user) == 0
132
255
 
133
- admin_positions = ActiveSupport::JSON.decode user.admin_of
134
- admin_positions.delete_if{|rec| rec['community_id'] == self.id}
135
- user.admin_of = ActiveSupport::JSON.encode admin_positions
136
- user.save
137
-
138
256
  admins_info = ActiveSupport::JSON.decode self.admins_info
139
257
  admins_info.delete_if{|rec| rec['admin_id'] == user.id}
140
258
  self.admins_info = ActiveSupport::JSON.encode admins_info
@@ -142,16 +260,17 @@ module Inkwell
142
260
  end
143
261
 
144
262
  def admin_level_of(admin)
145
- admin_positions = ActiveSupport::JSON.decode admin.admin_of
146
- index = admin_positions.index{|item| item['community_id'] == self.id}
263
+ admin_positions = ActiveSupport::JSON.decode self.admins_info
264
+ index = admin_positions.index{|item| item['admin_id'] == admin.id}
147
265
  raise "admin is not admin" unless index
148
266
  admin_positions[index]['admin_level']
149
267
  end
150
268
 
151
269
  def include_admin?(user)
152
270
  check_user user
153
- admin_positions = ActiveSupport::JSON.decode user.admin_of
154
- (admin_positions.index{|item| item['community_id'] == self.id} == nil) ? false : true
271
+
272
+ admin_positions = ActiveSupport::JSON.decode self.admins_info
273
+ (admin_positions.index{|item| item['admin_id'] == user.id}) ? true : false
155
274
  end
156
275
 
157
276
  def add_post(options = {})
@@ -159,7 +278,8 @@ module Inkwell
159
278
  user = options[:user]
160
279
  post = options[:post]
161
280
  raise "user should be passed in params" unless user
162
- raise "user should be a member of community" unless self.include_user?(user)
281
+ raise "user should be a member of community" unless self.include_user? user
282
+ raise "user is muted" if self.include_muted_user? user
163
283
  raise "post should be passed in params" unless post
164
284
  check_post post
165
285
  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
@@ -263,22 +383,74 @@ module Inkwell
263
383
  ActiveSupport::JSON.decode self.users_ids
264
384
  end
265
385
 
386
+ def create_invitation_request(user)
387
+ raise "invitation request was already created" if self.include_invitation_request? user
388
+ raise "it is impossible to create request. user is banned in this community" if self.include_banned_user? user
389
+ raise "it is impossible to create request for public community" if self.public
390
+
391
+ invitations_uids = ActiveSupport::JSON.decode self.invitations_uids
392
+ invitations_uids << user.id
393
+ self.invitations_uids = ActiveSupport::JSON.encode invitations_uids
394
+ self.save
395
+ end
396
+
397
+ def accept_invitation_request(options = {})
398
+ options.symbolize_keys!
399
+ user = options[:user]
400
+ admin = options[:admin]
401
+ check_user user
402
+ check_user admin
403
+ raise "admin is not admin in this community" unless self.include_admin? admin
404
+ raise "this user is already in this community" if self.include_user? user
405
+ raise "there is no invitation request for this user" unless self.include_invitation_request? user
406
+
407
+ self.add_user :user => user
408
+
409
+ remove_invitation_request user
410
+ end
411
+
412
+ def reject_invitation_request(options = {})
413
+ options.symbolize_keys!
414
+ user = options[:user]
415
+ admin = options[:admin]
416
+ check_user user
417
+ check_user admin
418
+ raise "there is no invitation request for this user" unless self.include_invitation_request? user
419
+ raise "admin is not admin in this community" unless self.include_admin? admin
420
+
421
+ remove_invitation_request user
422
+ end
423
+
424
+ def include_invitation_request?(user)
425
+ raise "invitations work only for private community. this community is public." if self.public
426
+ invitations_uids = ActiveSupport::JSON.decode self.invitations_uids
427
+ (invitations_uids.index{|uid| uid == user.id}) ? true : false
428
+ end
429
+
430
+
431
+
266
432
  private
433
+
434
+ def remove_invitation_request(user)
435
+ invitations_uids = ActiveSupport::JSON.decode self.invitations_uids
436
+ invitations_uids.delete user.id
437
+ self.invitations_uids = ActiveSupport::JSON.encode invitations_uids
438
+ self.save
439
+ end
440
+
267
441
  def processing_a_community
268
442
  user_class = Object.const_get ::Inkwell::Engine::config.user_table.to_s.singularize.capitalize
269
- user = user_class.find self.owner_id
443
+ owner = user_class.find self.owner_id
270
444
 
271
- admin_positions = ActiveSupport::JSON.decode user.admin_of
272
- admin_positions << Hash['community_id' => self.id, 'admin_level' => 0]
273
- user.admin_of = ActiveSupport::JSON.encode admin_positions
274
- communities_ids = ActiveSupport::JSON.decode user.communities_ids
275
- communities_ids << self.id
276
- user.communities_ids = ActiveSupport::JSON.encode communities_ids
277
- user.save
445
+ communities_info = ActiveSupport::JSON.decode owner.communities_info
446
+ communities_info << Hash[HashParams::COMMUNITY_ID => self.id, HashParams::ACCESS_LEVEL => self.default_user_access]
447
+ owner.communities_info = ActiveSupport::JSON.encode communities_info
448
+ owner.save
278
449
 
279
- admins_info = [Hash['admin_id' => user.id, 'admin_level' => 0]]
450
+ admins_info = [Hash['admin_id' => owner.id, 'admin_level' => 0]]
280
451
  self.admins_info = ActiveSupport::JSON.encode admins_info
281
- self.users_ids = ActiveSupport::JSON.encode [user.id]
452
+ self.users_ids = ActiveSupport::JSON.encode [owner.id]
453
+ self.writers_ids = ActiveSupport::JSON.encode [owner.id]
282
454
  self.save
283
455
  end
284
456
 
@@ -287,12 +459,9 @@ module Inkwell
287
459
  users_ids = ActiveSupport::JSON.decode self.users_ids
288
460
  users_ids.each do |user_id|
289
461
  user = user_class.find user_id
290
- admin_positions = ActiveSupport::JSON.decode user.admin_of
291
- admin_positions.delete_if{|rec| rec['community_id'] == self.id}
292
- user.admin_of = ActiveSupport::JSON.encode admin_positions
293
- communities_ids = ActiveSupport::JSON.decode user.communities_ids
294
- communities_ids.delete self.id
295
- user.communities_ids = ActiveSupport::JSON.encode communities_ids
462
+ communities_info = ActiveSupport::JSON.decode user.communities_info
463
+ communities_info.delete_if {|item| item[HashParams::COMMUNITY_ID] == self.id}
464
+ user.communities_info = ActiveSupport::JSON.encode communities_info
296
465
  user.save
297
466
  end
298
467
 
@@ -54,7 +54,7 @@ module Inkwell
54
54
  result
55
55
  end
56
56
 
57
- def create_comment(options={})
57
+ def create_comment(options = {})
58
58
  options.symbolize_keys!
59
59
  raise "for_object should be passed" unless options[:for_object]
60
60
  raise "comment body should be passed" unless options[:body]
@@ -66,7 +66,12 @@ module Inkwell
66
66
  end
67
67
 
68
68
  def communities_row
69
- ActiveSupport::JSON.decode self.communities_ids
69
+ communities_info = ActiveSupport::JSON.decode self.communities_info
70
+ result = []
71
+ communities_info.each do |item|
72
+ result << item[HashParams::COMMUNITY_ID]
73
+ end
74
+ result
70
75
  end
71
76
 
72
77
  def favorite(obj)
@@ -306,6 +311,7 @@ module Inkwell
306
311
  #wrappers for community methods
307
312
 
308
313
  def join(open_community)
314
+ raise "it is impossible to join private community. use invitation request to do it." unless open_community.public
309
315
  open_community.add_user :user => self
310
316
  end
311
317
 
@@ -320,10 +326,46 @@ module Inkwell
320
326
  from_community.remove_user :user => user, :admin => self
321
327
  end
322
328
 
329
+ def ban(options = {})
330
+ options.symbolize_keys!
331
+ in_community = options[:in_community]
332
+ user = options[:user]
333
+ in_community.ban_user :user => user, :admin => self
334
+ end
335
+
336
+ def unban(options = {})
337
+ options.symbolize_keys!
338
+ in_community = options[:in_community]
339
+ user = options[:user]
340
+ in_community.unban_user :user => user, :admin => self
341
+ end
342
+
343
+ def mute(options = {})
344
+ options.symbolize_keys!
345
+ in_community = options[:in_community]
346
+ user = options[:user]
347
+ in_community.mute_user :user => user, :admin => self
348
+ end
349
+
350
+ def unmute(options = {})
351
+ options.symbolize_keys!
352
+ in_community = options[:in_community]
353
+ user = options[:user]
354
+ in_community.unmute_user :user => user, :admin => self
355
+ end
356
+
357
+ def can_send_post_to_community?(community)
358
+ return false unless community.include_user? self
359
+ return false if community.include_muted_user? self
360
+ return false unless community.include_writer? self
361
+ true
362
+ end
363
+
323
364
  def send_post_to_community(options = {})
324
365
  options.symbolize_keys!
325
366
  to_community = options[:to_community]
326
367
  post = options[:post]
368
+ raise "this user have no permissions to send post to this community" unless self.can_send_post_to_community? to_community
327
369
  to_community.add_post :post => post, :user => self
328
370
  end
329
371
 
@@ -46,5 +46,16 @@ module Inkwell
46
46
  USER = 'u'
47
47
  COMMUNITY = 'c'
48
48
  end
49
+
50
+ module HashParams
51
+ COMMUNITY_ID = 'c_id'
52
+ USER_ID = 'u_id'
53
+ ACCESS_LEVEL = 'a'
54
+ end
55
+
56
+ module CommunityAccessLevels
57
+ WRITE = 'w'
58
+ READ = 'r'
59
+ end
49
60
  end
50
61
  end
@@ -1,3 +1,3 @@
1
1
  module Inkwell
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.7"
3
3
  end