gitcontacts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a9eb26f244b726004f0df05f786463c6d9fefe0
4
+ data.tar.gz: 2f98fb079c95624d5787a2b13128458e14b327ce
5
+ SHA512:
6
+ metadata.gz: 3da4641d73fe11dca5ed32510955afb16b6de2d79e5d13a2dc2ed6fdc8e833c3350ab814e6248ebdda39f6feaa4b6ea33e99c4463198fcae6521ec85d6cb4e27
7
+ data.tar.gz: f3a228abbe2773a003ba6a418cea209e0b2813a2ca1b09a4f99057c8421cfff5cc4dcbb219bf17bb6aa924a8eb1449b10534703c102ec34b0b3727377a6bc8ef
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ # Specify your gem's dependencies in gitcontacts.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ran
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Gitcontacts
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gitcontacts'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gitcontacts
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/gitcontacts/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gitcontacts/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gitcontacts"
8
+ spec.version = GitContacts::VERSION
9
+ spec.authors = ["AustinChou"]
10
+ spec.email = ["austinchou0126@gmail.com"]
11
+ spec.summary = %q{"Git-Contacts service interface"}
12
+ spec.description = %q{"a contacts service powered by Git. this is the second layer, for access control & users management"}
13
+ spec.homepage = "http://github.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "redis", '~> 3.2'
22
+ spec.add_dependency "gitdb", '~> 0.1', ">= 0.1.3"
23
+ spec.add_dependency "redis-objects", "~> 1.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,130 @@
1
+ module GitContacts
2
+
3
+ class Contacts
4
+
5
+ def self::exist? gid
6
+ true if ContactsObject::exist?(gid)
7
+ end
8
+
9
+ def self::create gid, hash
10
+ # some keys are optional
11
+ if hash.keys.include?(:name)
12
+ obj = ContactsObject.new
13
+ obj.name = hash[:name]
14
+ obj.note = hash[:note] if hash.has_key?(:note)
15
+ obj.users << gid
16
+ obj.admins << gid
17
+ obj.gid
18
+ end
19
+ end
20
+
21
+ def initialize gid
22
+ @obj = ContactsObject::access gid
23
+ end
24
+
25
+ def getgid
26
+ @obj.gid if @obj
27
+ end
28
+
29
+ def getusers
30
+ @obj.users if @obj
31
+ end
32
+
33
+ def getadmins
34
+ @obj.admins if @obj
35
+ end
36
+
37
+ def add_user uid
38
+ @obj.users << uid if @obj
39
+ end
40
+
41
+ def add_admin uid
42
+ @obj.admins << uid if @obj
43
+ end
44
+
45
+ def remove_user uid
46
+ @obj.users.delete(uid) if @obj
47
+ end
48
+
49
+ def remove_admin uid
50
+ @obj.admins.delete(uid) if @obj
51
+ end
52
+
53
+ end
54
+
55
+ class ContactsObject
56
+ include Redis::Objects
57
+
58
+ value :name
59
+ value :note
60
+ set :users
61
+ set :admins
62
+ set :requests
63
+ value :owner
64
+
65
+ def self::key_prefix
66
+ "contacts_object:"
67
+ end
68
+
69
+ def self::exist? id
70
+ true if redis.keys(key_prefix+id+':*').count > 0
71
+ end
72
+
73
+ def self::access id
74
+ if exist? id
75
+ obj = allocate
76
+ obj.set_id id
77
+ obj.set_name Redis::Value.new(key_prefix+id+':name')
78
+ obj.set_note Redis::Value.new(key_prefix+id+':note')
79
+ obj.set_users Redis::Set.new(key_prefix+id+':users')
80
+ obj.set_admins Redis::Set.new(key_prefix+id+':admins')
81
+ obj.set_requests Redis::Set.new(key_prefix+id+':requests')
82
+ obj.set_owner Redis::Value.new(key_prefix+id+':owner')
83
+ obj
84
+ end
85
+ end
86
+
87
+
88
+ def initialize
89
+ @id = Digest::SHA1.hexdigest Time.now.to_s
90
+ end
91
+
92
+ def id
93
+ @id
94
+ end
95
+
96
+ def gid
97
+ @id
98
+ end
99
+
100
+ def set_id id
101
+ @id = id
102
+ end
103
+
104
+ def set_name name
105
+ @name = name
106
+ end
107
+
108
+ def set_note note
109
+ @note = note
110
+ end
111
+
112
+ def set_users users
113
+ @users = users
114
+ end
115
+
116
+ def set_admins admins
117
+ @admins = admins
118
+ end
119
+
120
+ def set_requests requests
121
+ @requests = requests
122
+ end
123
+
124
+ def set_owner owner
125
+ @owner = owner
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,111 @@
1
+ module GitContacts
2
+
3
+ class Invitation
4
+ class << self
5
+ def exist? invite_id
6
+ return true if InvitationObject::exist?(invite_id)
7
+ end
8
+
9
+ def create hash
10
+ # all keys are required
11
+ if hash.keys == GitContacts::invitation_keys
12
+ obj = InvitationObject.new
13
+ obj.uid = hash[:uid]
14
+ obj.gid = hash[:gid]
15
+ obj.inviter_id = hash[:inviter_id]
16
+ obj.invite_id
17
+ end
18
+ end
19
+
20
+ def delete invite_id
21
+ return true if InvitationObject::delete?(invite_id) > 0
22
+ end
23
+ end
24
+
25
+ def initialize invite_id
26
+ @obj = InvitationObject::access invite_id
27
+ end
28
+
29
+ def getuid
30
+ @obj.uid if @obj
31
+ end
32
+
33
+ def getgid
34
+ @obj.gid if @obj
35
+ end
36
+
37
+ def getinviter_id
38
+ @obj.inviter_id
39
+ end
40
+
41
+ def can_accept? uid
42
+ uid == getuid
43
+ end
44
+
45
+ def accept
46
+ # to-do
47
+ end
48
+
49
+ end
50
+
51
+ class InvitationObject
52
+ include Redis::Objects
53
+
54
+ value :uid
55
+ value :gid
56
+ value :inviter_id
57
+
58
+ def self::key_prefix
59
+ "invitation_object:"
60
+ end
61
+
62
+ def self::exist? id
63
+ true if redis.keys(key_prefix+id+':*').count > 0
64
+ end
65
+
66
+ def self::access id
67
+ if exist? id
68
+ obj = allocate
69
+ obj.set_id id
70
+ obj.set_uid Redis::Value.new(key_prefix+id+':uid')
71
+ obj.set_gid Redis::Value.new(key_prefix+id+':gid')
72
+ obj.set_inviter_id Redis::Value.new(key_prefix+id+':inviter_id')
73
+ obj
74
+ end
75
+ end
76
+
77
+ def self::delete id
78
+ redis.del(*(redis.keys(key_prefix+id+':*')))
79
+ end
80
+
81
+ def initialize
82
+ @id = Digest::SHA1.hexdigest(Time.now.to_s)
83
+ end
84
+
85
+ def id
86
+ @id
87
+ end
88
+
89
+ def invite_id
90
+ @id
91
+ end
92
+
93
+ def set_id id
94
+ @id = id
95
+ end
96
+
97
+ def set_uid uid
98
+ @uid = uid
99
+ end
100
+
101
+ def set_gid gid
102
+ @gid = gid
103
+ end
104
+
105
+ def set_inviter_id inviter_id
106
+ @inviter_id = inviter_id
107
+ end
108
+
109
+ end
110
+
111
+ end
@@ -0,0 +1,171 @@
1
+ module GitContacts
2
+
3
+ class Request
4
+ class << self
5
+
6
+ def exist? request_id
7
+ return true if RequestObject::exist?(request_id)
8
+ end
9
+
10
+ def create hash
11
+ # all keys are required
12
+ if hash.keys == GitContacts::request_keys
13
+ obj = RequestObject.new
14
+ obj.uid = hash[:uid]
15
+ obj.gid = hash[:gid]
16
+ obj.card_id = hash[:card_id]
17
+ obj.action = hash[:action]
18
+ obj.content = hash[:content]
19
+ obj.request_id
20
+ end
21
+ end
22
+
23
+ def delete request_id
24
+ return true if RequestObject::delete(request_id) > 0
25
+ end
26
+
27
+ end
28
+
29
+ def initiazlie request_id
30
+ @obj = RequestObject::access request_id
31
+ end
32
+
33
+ def getuid
34
+ @obj.uid if @obj
35
+ end
36
+
37
+ def getgid
38
+ @obj.gid if @obj
39
+ end
40
+
41
+ def getaction
42
+ @obj.action if @obj
43
+ end
44
+
45
+ def getcard_id
46
+ @obj.card_id if @obj
47
+ end
48
+
49
+ def get_req_time
50
+ @obj.time if @obj
51
+ end
52
+
53
+ def getcontent
54
+ @obj.content if @obj
55
+ end
56
+
57
+ def auto_merge? uid
58
+ false
59
+ end
60
+
61
+ def allow operator
62
+ author = User.new getuid
63
+ contacts = Gitdb::Contacts.new(uid).access getgid
64
+ card = Gitdb::Card.new contacts.repo
65
+
66
+ case getaction
67
+ when 'create'
68
+ card.create uid
69
+ when 'setmeta'
70
+ card.access(getcard_id).setmeta getcontent
71
+ when 'setdata'
72
+ card.access(getcard_id).setdata getcontent
73
+ when 'delete'
74
+ card.access(getcard_id).delete
75
+ end
76
+
77
+ commit_obj = {
78
+ :author => { :name => author.getuid, :email => author.getemail, :time => get_req_time },
79
+ :committer => { :name => operator.getuid, :email => operator.getemail, :time => Time.now }
80
+ }
81
+
82
+ contacts.make_a_commit commit_obj
83
+ end
84
+
85
+ def deny
86
+ # deny merge here
87
+ GitContacts::Request::delete @obj.request_id
88
+ end
89
+ end
90
+
91
+
92
+ class RequestObject
93
+ include Redis::Objects
94
+
95
+ value :uid
96
+ value :gid
97
+ value :time
98
+ value :card_id
99
+ value :action
100
+ hash_key :content
101
+
102
+ def self::key_prefix
103
+ "request_object:"
104
+ end
105
+
106
+ def self::exist? id
107
+ true if redis.keys(key_prefix+id+':*').count > 0
108
+ end
109
+
110
+ def self::delete id
111
+ redis.del(*(redis.keys(key_prefix+id+':*')))
112
+ end
113
+
114
+ def self::access id
115
+ if exist? id
116
+ obj = allocate
117
+ obj.set_id id
118
+ obj.set_uid Redis::Value.new(key_prefix+id+':uid')
119
+ obj.set_gid Redis::Value.new(key_prefix+id+':gid')
120
+ obj.set_card_id Redis::Value.new(key_prefix+id+':card_id')
121
+ obj.set_action Redis::Value.new(key_prefix+id+':action')
122
+ obj.set_time Redis::Value.new "#{key_prefix}#{id}:time"
123
+ obj.set_content Redis::HashKey.new(key_prefix+id+':content')
124
+ obj
125
+ end
126
+ end
127
+
128
+
129
+ def initialize
130
+ @id = Digest::SHA1.hexdigest(Time.now.to_s)
131
+ end
132
+
133
+ def id
134
+ @id
135
+ end
136
+
137
+ def request_id
138
+ @id
139
+ end
140
+
141
+ def set_id id
142
+ @id = id
143
+ end
144
+
145
+ def set_uid uid
146
+ @uid = uid
147
+ end
148
+
149
+ def set_gid gid
150
+ @gid = gid
151
+ end
152
+
153
+ def set_card_id card_id
154
+ @card_id = card_id
155
+ end
156
+
157
+ def set_time time
158
+ @time = time
159
+ end
160
+
161
+ def set_action action
162
+ @action = action
163
+ end
164
+
165
+ def set_content content
166
+ @content = content
167
+ end
168
+
169
+ end
170
+
171
+ end
@@ -0,0 +1,136 @@
1
+ module GitContacts
2
+
3
+ class User
4
+
5
+ def self::exist? uid
6
+ true if UserObject::exist?(uid)
7
+ end
8
+
9
+ def self::email_to_uid email
10
+
11
+ end
12
+
13
+ def self::create hash
14
+ # some keys are optional
15
+ if hash.keys.include?(:email) && hash.keys.include?(:password)
16
+ obj = UserObject.new
17
+ obj.email = hash[:email]
18
+ obj.password = hash[:password]
19
+ obj.uid
20
+ end
21
+ end
22
+
23
+ def initialize uid
24
+ @obj = UserObject::access uid
25
+ end
26
+
27
+ def getuid
28
+ @obj.uid if @obj
29
+ end
30
+
31
+ def getname
32
+ @obj.name if @obj
33
+ end
34
+
35
+ def getemail
36
+ @obj.email if @obj
37
+ end
38
+
39
+ def getpassword
40
+ @obj.password if @obj
41
+ end
42
+
43
+ def getcontacts
44
+ @obj.contacts if @obj
45
+ end
46
+
47
+ def getrequests
48
+ @obj.requests if @obj
49
+ end
50
+
51
+ def password_correct? sha
52
+ sha == getpassword && sha != nil && sha != ""
53
+ end
54
+
55
+ def set_password sha
56
+ @obj.password = sha if sha != nil && sha != ""
57
+ end
58
+
59
+ def add_contacts gid
60
+ @obj.contacts << gid if @obj
61
+ end
62
+
63
+ def remove_contacts gid
64
+ @obj.contacts.delete gid if @obj
65
+ end
66
+
67
+ def add_request request_id
68
+ @obj.requests << request_id if @obj
69
+ end
70
+
71
+ def remove_request request_id
72
+ @obj.requests.delete request_id if @obj
73
+ end
74
+
75
+ end
76
+
77
+ class UserObject
78
+ include Redis::Objects
79
+
80
+ value :uid
81
+ value :email
82
+ value :password
83
+ set :contacts
84
+ set :requests
85
+
86
+ def self::key_prefix
87
+ "user_object:"
88
+ end
89
+
90
+ def self::exist? uid
91
+ true if redis.keys(key_prefix+uid+':*').count > 0
92
+ end
93
+
94
+ def self::access uid
95
+ if exist? uid
96
+ obj = allocate
97
+ obj.set_uid uid
98
+ obj.set_email Redis::Value.new(key_prefix+id+':email')
99
+ obj.set_password Redis::Value.new(key_prefix+id+':password')
100
+ obj.set_contacts Redis::Set.new(key_prefix+id+':contacts')
101
+ obj.set_requests Redis::Set.new(key_prefix+id+':requests')
102
+ obj
103
+ end
104
+ end
105
+
106
+ def initialize
107
+ @uid = Digest::SHA1.hexdigest Time.now.to_s
108
+ end
109
+
110
+ def id
111
+ @uid
112
+ end
113
+
114
+ def set_uid uid
115
+ @uid = uid
116
+ end
117
+
118
+ def set_email email
119
+ @email = email
120
+ end
121
+
122
+ def set_password password
123
+ @password = password
124
+ end
125
+
126
+ def set_contacts contacts
127
+ @contacts = contacts
128
+ end
129
+
130
+ def set_requests requests
131
+ @requests = requests
132
+ end
133
+
134
+ end
135
+
136
+ end
@@ -0,0 +1,25 @@
1
+ module GitContacts
2
+ class << self
3
+
4
+ def generate_code n
5
+ [*'a'..'z', *0..9, *'A'..'Z'].sample(n).join
6
+ end
7
+
8
+ # List writeable keys
9
+ def user_keys
10
+ [:email, :password, :contacts, :requests]
11
+ end
12
+
13
+ def contacts_keys
14
+ [:name, :note, :users, :admins]
15
+ end
16
+
17
+ def invitation_keys
18
+ [:uid, :gid, :inviter_id]
19
+ end
20
+
21
+ def request_keys
22
+ [:uid, :gid, :card_id, :action, :content]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module GitContacts
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,258 @@
1
+ require 'redis'
2
+ require 'redis-objects'
3
+
4
+ require 'gitcontacts/util'
5
+ require 'gitcontacts/User'
6
+ require 'gitcontacts/Request'
7
+ require 'gitcontacts/Contacts'
8
+ require 'gitcontacts/Invitation'
9
+ require "gitcontacts/version"
10
+ require 'digest/sha1'
11
+ require "gitdb"
12
+
13
+ module GitContacts
14
+
15
+ class << self
16
+ def user_exist? uid
17
+ User::exist? uid
18
+ end
19
+
20
+ def create_user hash
21
+ User::create hash
22
+ end
23
+
24
+ def password_valid uid, password
25
+ if user = User.new(uid)
26
+ user.password_correct? Digest::SHA1.hexdigest password
27
+ end
28
+ end
29
+
30
+ def relation_valid? operator, gid
31
+ #if User::exist?(operator) && Contacts::exist?(gid)
32
+ user = User.new operator
33
+ contacts = GitContacts.new gid
34
+ [user, contacts] #if user.getcontacts.include?(gid) && contacts.getusers.include?(operator)
35
+ #end
36
+ end
37
+
38
+ # meta => :owner, :gid, :count, :name
39
+ def get_all_contacts operator
40
+ contacts_arr = []
41
+ user = User.new operator
42
+ contacts = Gitdb::Contacts.new uid
43
+ user.getcontacts.each do |gid|
44
+ return unless GitContacts::relation_valid? operator gid
45
+ contacts.access gid
46
+ contacts_arr << contacts.getmeta
47
+ end
48
+ contacts_arr
49
+ end
50
+
51
+ # e.g.: 获取联系人数量大于200的uid群组
52
+ # get_contacts_if { |contacts| contacts.count > 200 }
53
+ def get_contacts_if &condition
54
+ GitContacts::get_all_contacts(uid).select &condition
55
+ end
56
+
57
+ def add_contacts operator, name
58
+ #return unless GitContacts::relation_valid? operator gid
59
+ contacts = Gitdb::Contacts.new operator
60
+ contacts.create name
61
+ end
62
+
63
+ def edit_contacts_meta operator, gid, new_meta
64
+ return unless GitContacts::relation_valid? operator gid
65
+ contacts = Gitdb::Contacts.new operator
66
+ contacts.access gid
67
+ contacts.setmeta new_meta
68
+ end
69
+
70
+ def get_contacts_card operator, gid, card_id
71
+ return unless GitContacts::relation_valid? operator gid
72
+ contacts = Gitdb::Contacts.new operator
73
+ contacts.access gid
74
+ contacts.get_card_by_id card_id
75
+ end
76
+ =begin
77
+ def get_contacts_cards_by_owner operator, owner
78
+ return unless GitContacts::relation_valid? operator gid
79
+ contacts = Gitdb::Contacts.new operator
80
+ contacts.access gid
81
+ contacts.get_cards do |card|
82
+ card.getdata if card.getmeta[:owner].include? owner
83
+ end
84
+ end
85
+
86
+ def get_contacts_cards_by_name operator, name
87
+ return unless GitContacts::relation_valid? operator gid
88
+ contacts = Gitdb::Contacts.new operator
89
+ contacts.access gid
90
+ contacts.get_cards do |card|
91
+ card.getdata if card.getdata[:firstname].include? name || card.getdata[:firstname].include? name
92
+ end
93
+ end
94
+
95
+ def get_contacts_cards_by_number operator, number
96
+ return unless GitContacts::relation_valid? operator gid
97
+ contacts = Gitdb::Contacts.new operator
98
+ contacts.access gid
99
+ contacts.get_cards do |card|
100
+ info = card.getdata
101
+ cond = info[:mobile].include? number || info[:phone].include? number || info[:im].include? number
102
+ info if cond
103
+ end
104
+ end
105
+
106
+ def get_contacts_cards_by_birthday date
107
+ contacts = Gitdb::Contacts.new uid
108
+ contacts.access gid
109
+ contacts.get_cards do |card|
110
+ card.getdata if card.getdata[:birthday].include? date
111
+ end
112
+ end
113
+
114
+ def get_contacts_cards_by_email email
115
+ contacts = Gitdb::Contacts.new uid
116
+ contacts.access gid
117
+ contacts.get_cards do |card|
118
+ card.getdata if card.getdata[:email].include? email
119
+ end
120
+ end
121
+
122
+ def get_contacts_cards_by_etc info
123
+ contacts = Gitdb::Contacts.new uid
124
+ contacts.access gid
125
+ contacts.get_cards do |card|
126
+ card.getdata if card.getdata[:address].include? info || card.getdata[:note].include? info
127
+ end
128
+ end
129
+ =end
130
+ def get_contacts_cards_by_related operator, gid, keyword
131
+ return unless GitContacts::relation_valid? operator gid
132
+ contacts = Gitdb::Contacts.new operator
133
+ contacts.access gid
134
+ contacts.get_cards do |card|
135
+ info = card.getdata
136
+ info if card.getmeta[:owner].include? keyword || info.find { |k| true if info[k].include? keyword }
137
+ end
138
+ end
139
+
140
+ def get_contacts_history operator, gid
141
+ contacts = Gitdb::Contacts.new operator
142
+ contacts.access gid
143
+ contacts.read_change_history do |commit|
144
+ commit_obj = {}
145
+ commit_obj[:author] = commit.author
146
+ commit_obj[:operator] = commit.committer
147
+ commit_obj[:modified_time] = commit.time
148
+ commit_obj[:oid] = commit.oid
149
+ commit_obj[:message] = commit.message
150
+ commit_obj
151
+ end
152
+ end
153
+
154
+ def revert_to operator, gid
155
+ contacts = Gitdb::Contacts.new operator
156
+ contacts.access gid
157
+ operator = {
158
+ :name => operator,
159
+ :email => User.new(operator).getemail,
160
+ :time => Time.now
161
+ }
162
+ contacts.revert_to oid, { :author => operator, :committer => operator, :message => "revert to revision #{oid}" }
163
+ end
164
+
165
+ def add_contacts_card operator, gid, payload
166
+ return unless GitContacts::relation_valid? operator gid
167
+ # request id
168
+ qid = Request::create :uid => operator, :gid => gid, :action => "create", :time => Time.now, :content => payload
169
+ # create a rqeuest
170
+ req = Request.new qid
171
+ if req.auto_merge? operator
172
+ req.allow operator
173
+ Request::delete qid
174
+ # here should return card_id if success
175
+ end
176
+ end
177
+
178
+ def edit_contacts_card operator, gid, card_id, payload
179
+ return unless GitContacts::relation_valid? operator gid
180
+ qid = GCRequest::create :uid => operator, :gid => gid, :action => "setdata", :time => Time.now, :card_id => card_id, :content => payload
181
+ req = GCRequest.new qid
182
+ if req.auto_merge? operator
183
+ req.allow operator
184
+ Request::delete qid
185
+ end
186
+ end
187
+
188
+ def delete_contacts_card operator, gid, card_id
189
+ return unless GitContacts::relation_valid? operator gid
190
+ qid = GCRequest::create :uid => operator, :gid => gid, :action => "delete", :time => Time.now, :card_id => card_id
191
+ req = GCRequest.new qid
192
+ if req.auto_merge? operator
193
+ req.allow operator
194
+ Request::delete qid
195
+ end
196
+ end
197
+
198
+ def get_contacts_users operator, gid
199
+ return unless result = GitContacts::relation_valid?(operator, gid)
200
+ contacts = result.last
201
+ contacts.getusers
202
+ end
203
+
204
+ def add_contacts_user operator, gid, uid
205
+ return unless result = GitContacts::relation_valid?(operator, gid)
206
+ user = result.first
207
+ contacts = result.last
208
+ if contacts.getadmins.include?(user.getuid)
209
+ contacts.add_user uid
210
+ true
211
+ end
212
+ end
213
+
214
+ def edit_contacts_user_privileges operator, gid, uid, payload
215
+ return unless result = GitContacts::relation_valid?(operator, gid)
216
+ user = result.first
217
+ contacts = result.last
218
+ if contacts.getadmins.include?(user.getuid)
219
+ case payload[:role]
220
+ when "admin"
221
+ contacts.add_admin uid
222
+ true
223
+ when "users"
224
+ contacts.remove_admin uid
225
+ true
226
+ end
227
+ end
228
+ end
229
+
230
+ def invite_contacts_user operator, gid, payload
231
+ return unless result = GitContacts::relation_valid?(operator, gid)
232
+ user = result.first
233
+ contacts = result.last
234
+ if contacts.getadmins.include?(user.getuid)
235
+ Inviation::create :uid => User::email_to_uid(payload[:email]), :contacts => gid
236
+ end
237
+ end
238
+
239
+ def edit_invitation_status operator, payload
240
+ invitation = Inviation.new payload[:invite_id]
241
+ if invitation.accept? operator
242
+ user = User.new operator
243
+ gid = invitation.getcontacts
244
+ user.add_contacts gid
245
+ contacts = GCContacts.new gid
246
+ contacts.adduser user.getuid
247
+ Inviation::delete payload[:invite_id]
248
+ true
249
+ end
250
+ end
251
+
252
+ def get_all_requests operator
253
+ end
254
+
255
+ def edit_request_status operator
256
+ end
257
+ end
258
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitcontacts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - AustinChou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gitdb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.1.3
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.1'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.3
47
+ - !ruby/object:Gem::Dependency
48
+ name: redis-objects
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.7'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.7'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ description: '"a contacts service powered by Git. this is the second layer, for access
90
+ control & users management"'
91
+ email:
92
+ - austinchou0126@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".gitignore"
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - gitcontacts.gemspec
103
+ - lib/gitcontacts.rb
104
+ - lib/gitcontacts/Contacts.rb
105
+ - lib/gitcontacts/Invitation.rb
106
+ - lib/gitcontacts/Request.rb
107
+ - lib/gitcontacts/User.rb
108
+ - lib/gitcontacts/util.rb
109
+ - lib/gitcontacts/version.rb
110
+ homepage: http://github.com
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: '"Git-Contacts service interface"'
134
+ test_files: []