inkwell 1.1.7 → 1.2.0

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.
@@ -383,6 +383,19 @@ module Inkwell
383
383
  ActiveSupport::JSON.decode self.users_ids
384
384
  end
385
385
 
386
+ def writers_row
387
+ ActiveSupport::JSON.decode self.writers_ids
388
+ end
389
+
390
+ def admins_row
391
+ admins_info = ActiveSupport::JSON.decode self.admins_info
392
+ result = []
393
+ admins_info.each do |rec|
394
+ result << rec['admin_id']
395
+ end
396
+ result
397
+ end
398
+
386
399
  def create_invitation_request(user)
387
400
  raise "invitation request was already created" if self.include_invitation_request? user
388
401
  raise "it is impossible to create request. user is banned in this community" if self.include_banned_user? user
@@ -427,7 +440,77 @@ module Inkwell
427
440
  (invitations_uids.index{|uid| uid == user.id}) ? true : false
428
441
  end
429
442
 
443
+ def change_default_access_to_write
444
+ unless self.default_user_access == CommunityAccessLevels::WRITE
445
+ self.default_user_access = CommunityAccessLevels::WRITE
446
+ self.save
447
+ end
448
+ end
449
+
450
+ def change_default_access_to_read
451
+ unless self.default_user_access == CommunityAccessLevels::READ
452
+ self.default_user_access = CommunityAccessLevels::READ
453
+ self.save
454
+ end
455
+ end
456
+
457
+ def set_write_access(uids)
458
+ raise "array with users ids should be passed" unless uids.class == Array
459
+ users_ids = self.users_row
460
+ matching_ids = users_ids & uids
461
+ unless matching_ids.size == uids.size
462
+ mismatched_ids = uids - matching_ids
463
+ raise "there is no users with ids #{mismatched_ids} in the community"
464
+ end
465
+
466
+ current_writers_ids = ActiveSupport::JSON.decode self.writers_ids
467
+ already_added_ids = current_writers_ids & uids
468
+ uids -= already_added_ids
469
+
470
+ current_writers_ids += uids
471
+ self.writers_ids = ActiveSupport::JSON.encode current_writers_ids
472
+ self.save
473
+
474
+ user_class = Object.const_get ::Inkwell::Engine::config.user_table.to_s.singularize.capitalize
475
+ users = user_class.find uids
476
+ users.each do |user|
477
+ raise "user with id #{uid} does not exist" unless user
478
+ communities_info = ActiveSupport::JSON.decode user.communities_info
479
+ index = communities_info.index { |rec| rec[HashParams::COMMUNITY_ID] == self.id }
480
+ communities_info[index][HashParams::ACCESS_LEVEL] = CommunityAccessLevels::WRITE
481
+ user.communities_info = ActiveSupport::JSON.encode communities_info
482
+ user.save
483
+ end
484
+ end
485
+
486
+ def set_read_access(uids)
487
+ raise "array with users ids should be passed" unless uids.class == Array
488
+ users_ids = self.users_row
489
+ matching_ids = users_ids & uids
490
+ unless matching_ids.size == uids.size
491
+ mismatched_ids = uids - matching_ids
492
+ raise "there is no users with ids #{mismatched_ids} in the community"
493
+ end
494
+
495
+ matching_ids = self.admins_row & uids
496
+ raise "there is impossible to change access level to read for admins with ids #{matching_ids} in the community" unless matching_ids.empty?
497
+
498
+ current_writers_ids = ActiveSupport::JSON.decode self.writers_ids
499
+ current_writers_ids -= uids
500
+ self.writers_ids = ActiveSupport::JSON.encode current_writers_ids
501
+ self.save
430
502
 
503
+ user_class = Object.const_get ::Inkwell::Engine::config.user_table.to_s.singularize.capitalize
504
+ users = user_class.find uids
505
+ users.each do |user|
506
+ raise "user with id #{uid} does not exist" unless user
507
+ communities_info = ActiveSupport::JSON.decode user.communities_info
508
+ index = communities_info.index { |rec| rec[HashParams::COMMUNITY_ID] == self.id }
509
+ communities_info[index][HashParams::ACCESS_LEVEL] = CommunityAccessLevels::READ
510
+ user.communities_info = ActiveSupport::JSON.encode communities_info
511
+ user.save
512
+ end
513
+ end
431
514
 
432
515
  private
433
516
 
@@ -315,6 +315,24 @@ module Inkwell
315
315
  open_community.add_user :user => self
316
316
  end
317
317
 
318
+ def request_invitation(community)
319
+ community.create_invitation_request(self)
320
+ end
321
+
322
+ def approve_invitation_request(options = {})
323
+ options.symbolize_keys!
324
+ community = options[:community]
325
+ user = options[:user]
326
+ community.accept_invitation_request :user => user, :admin => self
327
+ end
328
+
329
+ def reject_invitation_request(options = {})
330
+ options.symbolize_keys!
331
+ community = options[:community]
332
+ user = options[:user]
333
+ community.reject_invitation_request :user => user, :admin => self
334
+ end
335
+
318
336
  def leave(community)
319
337
  community.remove_user :user => self
320
338
  end
@@ -1,3 +1,3 @@
1
1
  module Inkwell
2
- VERSION = "1.1.7"
2
+ VERSION = "1.2.0"
3
3
  end
Binary file
Binary file
@@ -811,14 +811,14 @@ describe "Community" do
811
811
  expect { @public_community.include_invitation_request?(@salkar) }.to raise_error
812
812
  end
813
813
 
814
- it "request invitation should be created" do
814
+ it "invitation request should be created" do
815
815
  @private_community = Community.create :name => "Private Community", :owner_id => @morozovm.id, :public => false
816
816
  @private_community.create_invitation_request @salkar
817
817
  @private_community.reload
818
818
  @private_community.invitations_uids.should == "[#{@salkar.id}]"
819
819
  end
820
820
 
821
- it "request invitation should not be created" do
821
+ it "invitation request should not be created" do
822
822
  expect { @community_1.create_invitation_request(@salkar) }.to raise_error
823
823
  @private_community = Community.create :name => "Private Community", :owner_id => @morozovm.id, :public => false
824
824
  @private_community.banned_ids = "[#{@talisman.id}]"
@@ -1558,4 +1558,299 @@ describe "Community" do
1558
1558
  @public_community.include_writer?(@salkar).should == true
1559
1559
  end
1560
1560
 
1561
+ it "user should be able to request invitation to private community" do
1562
+ @private_community = Community.create :name => "Private Community", :owner_id => @morozovm.id, :public => false
1563
+ @salkar.request_invitation @private_community
1564
+ @private_community.reload
1565
+ @private_community.invitations_uids.should == "[#{@salkar.id}]"
1566
+ end
1567
+
1568
+ it "admin should be able to accept invitation request" do
1569
+ @private_community = Community.create :name => "Private Community", :owner_id => @morozovm.id, :public => false
1570
+ @private_community.create_invitation_request @salkar
1571
+ @private_community.reload
1572
+ @morozovm.approve_invitation_request :user => @salkar, :community => @private_community
1573
+ @salkar.reload
1574
+ @private_community.reload
1575
+ @private_community.include_user?(@salkar).should == true
1576
+ @private_community.users_ids.should == "[#{@morozovm.id},#{@salkar.id}]"
1577
+ @private_community.writers_ids.should == "[#{@morozovm.id},#{@salkar.id}]"
1578
+ @salkar.communities_row.should == [@private_community.id]
1579
+ end
1580
+
1581
+ it "admin should be able to reject invitation request" do
1582
+ @private_community = Community.create :name => "Private Community", :owner_id => @morozovm.id, :public => false
1583
+ @private_community.create_invitation_request(@salkar)
1584
+ @private_community.reload
1585
+ @private_community.include_invitation_request?(@salkar).should == true
1586
+ @morozovm.reject_invitation_request :user => @salkar, :community => @private_community
1587
+ @private_community.reload
1588
+ @private_community.include_invitation_request?(@salkar).should == false
1589
+ end
1590
+
1591
+ it "default user access should changed to write" do
1592
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1593
+ @public_community.default_user_access = 'r'
1594
+ @public_community.save
1595
+ @public_community.reload
1596
+ @public_community.default_user_access.should == 'r'
1597
+ @public_community.change_default_access_to_write
1598
+ @public_community.reload
1599
+ @public_community.default_user_access.should == 'w'
1600
+ end
1601
+
1602
+ it "default user access should changed to read" do
1603
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1604
+ @public_community.reload
1605
+ @public_community.default_user_access.should == 'w'
1606
+ @public_community.change_default_access_to_read
1607
+ @public_community.reload
1608
+ @public_community.default_user_access.should == 'r'
1609
+ end
1610
+
1611
+ it "write access should be granted in the public community" do
1612
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1613
+ @public_community.default_user_access = 'r'
1614
+ @public_community.save
1615
+ @salkar.join @public_community
1616
+ @talisman.join @public_community
1617
+ @spy.join @public_community
1618
+
1619
+ @public_community.reload
1620
+ @salkar.reload
1621
+ @talisman.reload
1622
+ @spy.reload
1623
+
1624
+ @public_community.set_write_access [@salkar.id, @talisman.id]
1625
+
1626
+ @public_community.reload
1627
+ @salkar.reload
1628
+ @talisman.reload
1629
+ @spy.reload
1630
+
1631
+ writers_ids = ActiveSupport::JSON.decode(@public_community.writers_ids)
1632
+ writers_ids.include?(@salkar.id).should == true
1633
+ writers_ids.include?(@talisman.id).should == true
1634
+ writers_ids.include?(@spy.id).should == false
1635
+ @public_community.include_writer?(@salkar).should == true
1636
+ @public_community.include_writer?(@talisman).should == true
1637
+ @public_community.include_writer?(@spy).should == false
1638
+ communities_info = ActiveSupport::JSON.decode @salkar.communities_info
1639
+ communities_info.size.should == 1
1640
+ communities_info[0].should == {"c_id"=>@public_community.id, "a"=>"w"}
1641
+ communities_info = ActiveSupport::JSON.decode @talisman.communities_info
1642
+ communities_info.size.should == 2
1643
+ communities_info[1].should == {"c_id" => @public_community.id, "a" => "w"}
1644
+ communities_info = ActiveSupport::JSON.decode @spy.communities_info
1645
+ communities_info.size.should == 1
1646
+ communities_info[0].should == {"c_id" => @public_community.id, "a" => "r"}
1647
+ end
1648
+
1649
+ it "write access should be granted in the private community" do
1650
+ @private_community = Community.create :name => "community", :owner_id => @morozovm.id, :public => false
1651
+ @private_community.default_user_access = 'r'
1652
+ @private_community.save
1653
+
1654
+ @private_community.add_user :user => @salkar #only for test
1655
+ @private_community.add_user :user => @talisman #only for test
1656
+ @private_community.add_user :user => @spy #only for test
1657
+
1658
+ @private_community.reload
1659
+ @salkar.reload
1660
+ @talisman.reload
1661
+ @spy.reload
1662
+
1663
+ @private_community.set_write_access [@salkar.id, @talisman.id]
1664
+
1665
+ @private_community.reload
1666
+ @salkar.reload
1667
+ @talisman.reload
1668
+ @spy.reload
1669
+
1670
+ writers_ids = ActiveSupport::JSON.decode(@private_community.writers_ids)
1671
+ writers_ids.include?(@salkar.id).should == true
1672
+ writers_ids.include?(@talisman.id).should == true
1673
+ writers_ids.include?(@spy.id).should == false
1674
+ @private_community.include_writer?(@salkar).should == true
1675
+ @private_community.include_writer?(@talisman).should == true
1676
+ @private_community.include_writer?(@spy).should == false
1677
+ communities_info = ActiveSupport::JSON.decode @salkar.communities_info
1678
+ communities_info.size.should == 1
1679
+ communities_info[0].should == {"c_id" => @private_community.id, "a" => "w"}
1680
+ communities_info = ActiveSupport::JSON.decode @talisman.communities_info
1681
+ communities_info.size.should == 2
1682
+ communities_info[1].should == {"c_id" => @private_community.id, "a" => "w"}
1683
+ communities_info = ActiveSupport::JSON.decode @spy.communities_info
1684
+ communities_info.size.should == 1
1685
+ communities_info[0].should == {"c_id" => @private_community.id, "a" => "r"}
1686
+ end
1687
+
1688
+ it "error should not raised when W access granted to user with W access" do
1689
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1690
+ @salkar.join @public_community
1691
+ @talisman.join @public_community
1692
+ @spy.join @public_community
1693
+
1694
+ @public_community.reload
1695
+ @salkar.reload
1696
+ @talisman.reload
1697
+ @spy.reload
1698
+
1699
+ @public_community.set_write_access @public_community.users_row
1700
+ writers_ids = ActiveSupport::JSON.decode(@public_community.writers_ids)
1701
+ writers_ids.should == [@morozovm.id, @salkar.id, @talisman.id, @spy.id]
1702
+ end
1703
+
1704
+ it "passed empty array should not lead to error" do
1705
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1706
+ @public_community.set_write_access []
1707
+ @private_community = Community.create :name => "community", :owner_id => @morozovm.id, :public => false
1708
+ @private_community.set_write_access []
1709
+ end
1710
+
1711
+ it "write access should not be granted" do
1712
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1713
+ expect {@public_community.set_write_access [@salkar.id, @talisman.id]}.to raise_error
1714
+ expect {@public_community.set_write_access [@talisman.id]}.to raise_error
1715
+ expect {@public_community.set_write_access @talisman}.to raise_error
1716
+ expect {@public_community.set_write_access [-1]}.to raise_error
1717
+ end
1718
+
1719
+ it "read access should be set in the public community" do
1720
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1721
+ @salkar.join @public_community
1722
+ @talisman.join @public_community
1723
+ @spy.join @public_community
1724
+
1725
+ @public_community.reload
1726
+ @salkar.reload
1727
+ @talisman.reload
1728
+ @spy.reload
1729
+
1730
+ @public_community.set_read_access [@salkar.id, @talisman.id]
1731
+
1732
+ @public_community.reload
1733
+ @salkar.reload
1734
+ @talisman.reload
1735
+ @spy.reload
1736
+
1737
+ writers_ids = ActiveSupport::JSON.decode(@public_community.writers_ids)
1738
+ writers_ids.include?(@salkar.id).should == false
1739
+ writers_ids.include?(@talisman.id).should == false
1740
+ writers_ids.include?(@spy.id).should == true
1741
+ @public_community.include_writer?(@salkar).should == false
1742
+ @public_community.include_writer?(@talisman).should == false
1743
+ @public_community.include_writer?(@spy).should == true
1744
+ communities_info = ActiveSupport::JSON.decode @salkar.communities_info
1745
+ communities_info.size.should == 1
1746
+ communities_info[0].should == {"c_id" => @public_community.id, "a" => "r"}
1747
+ communities_info = ActiveSupport::JSON.decode @talisman.communities_info
1748
+ communities_info.size.should == 2
1749
+ communities_info[1].should == {"c_id" => @public_community.id, "a" => "r"}
1750
+ communities_info = ActiveSupport::JSON.decode @spy.communities_info
1751
+ communities_info.size.should == 1
1752
+ communities_info[0].should == {"c_id" => @public_community.id, "a" => "w"}
1753
+ end
1754
+
1755
+ it "read access should be set in the private community" do
1756
+ @private_community = Community.create :name => "community", :owner_id => @morozovm.id, :public => false
1757
+
1758
+ @private_community.add_user :user => @salkar #only for test
1759
+ @private_community.add_user :user => @talisman #only for test
1760
+ @private_community.add_user :user => @spy #only for test
1761
+
1762
+ @private_community.reload
1763
+ @salkar.reload
1764
+ @talisman.reload
1765
+ @spy.reload
1766
+
1767
+ @private_community.set_read_access [@salkar.id, @talisman.id]
1768
+
1769
+ @private_community.reload
1770
+ @salkar.reload
1771
+ @talisman.reload
1772
+ @spy.reload
1773
+
1774
+ writers_ids = ActiveSupport::JSON.decode(@private_community.writers_ids)
1775
+ writers_ids.include?(@salkar.id).should == false
1776
+ writers_ids.include?(@talisman.id).should == false
1777
+ writers_ids.include?(@spy.id).should == true
1778
+ @private_community.include_writer?(@salkar).should == false
1779
+ @private_community.include_writer?(@talisman).should == false
1780
+ @private_community.include_writer?(@spy).should == true
1781
+ communities_info = ActiveSupport::JSON.decode @salkar.communities_info
1782
+ communities_info.size.should == 1
1783
+ communities_info[0].should == {"c_id" => @private_community.id, "a" => "r"}
1784
+ communities_info = ActiveSupport::JSON.decode @talisman.communities_info
1785
+ communities_info.size.should == 2
1786
+ communities_info[1].should == {"c_id" => @private_community.id, "a" => "r"}
1787
+ communities_info = ActiveSupport::JSON.decode @spy.communities_info
1788
+ communities_info.size.should == 1
1789
+ communities_info[0].should == {"c_id" => @private_community.id, "a" => "w"}
1790
+ end
1791
+
1792
+ it "error should not raised when R access set to user with R access" do
1793
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1794
+ @public_community.default_user_access = 'r'
1795
+ @public_community.save
1796
+ @salkar.join @public_community
1797
+ @talisman.join @public_community
1798
+ @spy.join @public_community
1799
+
1800
+ @public_community.reload
1801
+ @salkar.reload
1802
+ @talisman.reload
1803
+ @spy.reload
1804
+
1805
+ @public_community.set_read_access (@public_community.users_row - [@morozovm.id])
1806
+ writers_ids = ActiveSupport::JSON.decode(@public_community.writers_ids)
1807
+ writers_ids.should == [@morozovm.id]
1808
+ end
1809
+
1810
+ it "passed empty array should not lead to error" do
1811
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1812
+ @public_community.set_read_access []
1813
+ @private_community = Community.create :name => "community", :owner_id => @morozovm.id, :public => false
1814
+ @private_community.set_read_access []
1815
+ end
1816
+
1817
+ it "write access should not be granted" do
1818
+ @public_community = Community.create :name => "community", :owner_id => @morozovm.id
1819
+ expect { @public_community.set_read_access [@salkar.id, @talisman.id] }.to raise_error
1820
+ expect { @public_community.set_read_access [@talisman.id] }.to raise_error
1821
+ expect { @public_community.set_read_access @talisman }.to raise_error
1822
+ expect { @public_community.set_read_access [-1] }.to raise_error
1823
+
1824
+ expect { @public_community.set_read_access [@morozovm.id] }.to raise_error
1825
+ end
1826
+
1827
+ it "admins ids should be returned" do
1828
+ @public_community = Community.create :name => "Community", :owner_id => @morozovm.id
1829
+ @talisman.join @public_community
1830
+ @salkar.join @public_community
1831
+ @spy.join @public_community
1832
+ @spy.reload
1833
+ @salkar.reload
1834
+ @public_community.reload
1835
+ @morozovm.reload
1836
+ @talisman.reload
1837
+ @public_community.add_admin :user => @talisman, :admin => @morozovm
1838
+ @public_community.add_admin :user => @salkar, :admin => @morozovm
1839
+ @public_community.reload
1840
+ @public_community.admins_row.should == [@morozovm.id, @talisman.id, @salkar.id]
1841
+ end
1842
+
1843
+ it "writers ids should be returned" do
1844
+ @public_community = Community.create :name => "Community", :owner_id => @morozovm.id
1845
+ @talisman.join @public_community
1846
+ @salkar.join @public_community
1847
+ @spy.join @public_community
1848
+ @spy.reload
1849
+ @salkar.reload
1850
+ @public_community.reload
1851
+ @morozovm.reload
1852
+ @talisman.reload
1853
+ @public_community.writers_row.should == [@morozovm.id, @talisman.id, @salkar.id, @spy.id]
1854
+ end
1855
+
1561
1856
  end