gitcontacts 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32f8aa6d72a71df70529373d58120aaae71975a3
4
- data.tar.gz: 76a57917abbc81ef9698eea46b94192ed3c32b50
3
+ metadata.gz: 70a7b11e301760f9ef08d0f91888d14029eef928
4
+ data.tar.gz: 7d791fa4c6acfc337eeb1a7671471ee32fd94838
5
5
  SHA512:
6
- metadata.gz: b7f901de92143f961de9c289aad8b199ea3cbbc26ac9a4decd4d756f68d899624abb8b60923c7c33334678530c278a8984de77596e70b68115bec861cc2bfe56
7
- data.tar.gz: 8c168ee166b5ade5a9263640fcc55050d3ba77680da91ad1fb07b9b678415d14cd0ec8fb953642b0f574405b18799c74bb481eda48b937f085947bfea60b7dce
6
+ metadata.gz: c6572d28e0a798198777b7bb10819c16ed652da55b48989b17d5a844edf893d756a2c792c997563cad19ff25ab247304262e3885d0258a18bdc122a33954ee1d
7
+ data.tar.gz: 5d951e20902a33584144294c870f27696b82ee07eef4510fb287ad07c2df9c7f942fad33c8ab2ae0ce4d1bf1a305b561f85c351ed548392ca307d490545fb049
@@ -12,288 +12,307 @@ require "gitdb"
12
12
 
13
13
  module GitContacts
14
14
 
15
+ @@errno = :ok
16
+
15
17
  class << self
18
+
19
+ def errsym
20
+ @@errno
21
+ end
22
+
16
23
  def user_exist? uid
17
24
  User::exist? uid
18
25
  end
19
26
 
20
- def create_user hash
21
- User::create hash
22
- end
23
27
  # code review: @abbshr
24
28
  def password_valid? email, password
25
- if user = User.new(email)
26
- user if user.password_correct? Digest::MD5.hexdigest(password)
27
- end
29
+ User.new(email).password_correct? Digest::MD5.hexdigest(password)
28
30
  end
29
- # code review: @AustinChou
31
+
32
+ # code review: @AustinChou, @abbshr
33
+ # if operator belongs to contacts gid
30
34
  def relation_valid? operator, gid
31
- if User::exist?(operator) && Contacts::exist?(gid)
32
- user = User.new operator
33
- contacts = Contacts.new gid
34
- [user, contacts] if user.getcontacts.include?(gid) && contacts.getusers.include?(operator)
35
- end
35
+ Contacts::exist?(gid) &&
36
+ Contacts.new(gid).getusers.include?(operator) &&
37
+ User.new(operator).getcontacts.include?(gid)
38
+ end
39
+
40
+ def create_user hash
41
+ return @@errno = :miss_args unless hash.include?(:email) && hash.include?(:password)
42
+ return @@errno = :exist if user_exist?(hash[:email])
43
+ User::create hash
44
+ @@errno = :ok
36
45
  end
37
46
 
38
47
  def get_a_user operator, uid
39
- return unless User.exist?(operator)
40
- if User.exist? uid
41
- user = User.new uid
42
- {
43
- :uid => user.getuid,
44
- :email => user.getemail,
45
- :password => user.getpassword,
46
- :contacts => user.getcontacts,
47
- :request => user.getrequests
48
- }
49
- end
48
+ return @@errno = :non_exist unless user_exist? uid
49
+ @@errno = :ok
50
+ User.new(uid).getinfo
51
+ end
52
+
53
+ # => [ uid ]
54
+ def get_users operator
55
+ @errno = :ok
56
+ User::all
50
57
  end
51
58
 
52
59
  # code review: @abbshr
60
+ # e.g.: 获取联系人数量大于200的uid群组
53
61
  # meta => :owner, :gid, :count, :name
54
- def get_all_contacts operator
55
- contacts_arr = []
62
+ # get_contacts_if operator { |contacts| contacts.count > 200 }
63
+ def get_contacts_if operator, &condition
64
+ @@errno = :ok
56
65
  user = User.new operator
57
66
  contacts = Gitdb::Contacts.new operator
58
- user.getcontacts.each do |gid|
59
- break unless GitContacts::relation_valid? operator, gid
60
- contacts.access gid
61
- contacts_arr << contacts.getmeta
62
- end
63
- contacts_arr
64
- end
65
- # code review: @abbshr
66
- # e.g.: 获取联系人数量大于200的uid群组
67
- # get_contacts_if { |contacts| contacts.count > 200 }
68
- def get_contacts_if uid, &condition
69
- GitContacts::get_all_contacts(uid).select &condition
67
+ user.getcontacts.map { |gid| contacts.access(gid).getmeta }.select &condition
70
68
  end
69
+
71
70
  # code review: @abbshr
72
71
  def add_contacts operator, name
73
- #return unless GitContacts::relation_valid? operator gid
74
- git_contacts = Gitdb::Contacts.new operator
75
- git_contacts.create name
76
- gid = git_contacts.getmeta[:gid]
77
- Contacts.create gid, { :name => name }
72
+ return @@errno = :miss_args unless name
73
+ @@errno = :ok
74
+ gid = Contacts.create operator, :name => name
78
75
  contacts = Contacts.new gid
79
- contacts.add_user operator
80
- contacts.add_admin operator
81
- user = User.new operator
82
- user.add_contacts gid
76
+ User.new(operator).add_contacts gid
83
77
  gid
84
78
  end
79
+
85
80
  # code review: @abbshr
86
- def edit_contacts_meta operator, gid, new_meta
87
- return unless GitContacts::relation_valid? operator, gid
88
- puts operator, gid, new_meta
81
+ def edit_contacts_meta operator, gid, meta
82
+ return @@errno = :forbidden unless relation_valid? operator, gid
83
+ return @@errno = :miss_args unless meta
84
+ @@errno = :ok
89
85
  contacts = Gitdb::Contacts.new operator
90
- contacts.access gid
91
- contacts.setmeta new_meta
92
- true
86
+ contacts.access(gid).setmeta meta
87
+ # TODO: check meta
93
88
  end
89
+
94
90
  # code review: @abbshr
95
- def get_contacts_card operator, gid, card_id
96
- return unless GitContacts::relation_valid? operator, gid
97
- contacts = Gitdb::Contacts.new operator
98
- contacts.access gid
99
- contacts.get_card_by_id(card_id).getdata.merge! card.getmeta
91
+ def get_contacts_card operator, gid, cid
92
+ return @@errno = :forbidden unless relation_valid? operator, gid
93
+ contacts = Gitdb::Contacts.new(operator).access(gid)
94
+ card = contacts.get_card_by_id cid
95
+ return @@errno = :non_exist unless card
96
+ @@errno = :ok
97
+ card.getdata.merge! card.getmeta
100
98
  end
99
+
101
100
  # code review: @abbshr
102
101
  def get_contacts_cards_by_related operator, gid, keyword
103
- return unless GitContacts::relation_valid? operator, gid
102
+ return @@errno = :forbidden unless relation_valid? operator, gid
103
+ @@errno = :ok
104
104
  contacts = Gitdb::Contacts.new operator
105
- contacts.access gid
106
- contacts.get_cards do |card|
107
- info = card.getdata
108
- info.merge! card.getmeta if (card.getmeta[:owner].include? keyword) || info.find { |k,v| true if v.include? keyword }
105
+ contacts.access(gid).get_cards do |card|
106
+ if card.getmeta[:owner].include?(keyword) || card.getdata.find { |k,v| v.include? keyword }
107
+ card.getdata.merge! card.getmeta
108
+ end
109
109
  end
110
110
  end
111
+
111
112
  # code review: @abbshr
112
113
  def get_contacts_history operator, gid
114
+ return @@errno = :forbidden unless relation_valid? operator, gid
115
+ @@errno = :ok
113
116
  contacts = Gitdb::Contacts.new operator
114
- contacts.access gid
115
- contacts.read_change_history do |commit|
116
- commit_obj = {}
117
- commit_obj[:author] = commit.author
118
- commit_obj[:operator] = commit.committer
119
- commit_obj[:modified_time] = commit.time
120
- commit_obj[:oid] = commit.oid
121
- commit_obj[:message] = commit.message
122
- commit_obj
117
+ contacts.access(gid).read_change_history do |commit|
118
+ {
119
+ :author => commit.author,
120
+ :operator => commit.committer,
121
+ :mtime => commit.time,
122
+ :oid => commit.oid,
123
+ :message => commit.message
124
+ }
123
125
  end
124
126
  end
127
+
125
128
  # code review: @abbshr
126
129
  def revert_to operator, gid, oid
127
- puts operator, gid, oid
128
- contacts = Gitdb::Contacts.new operator
129
- contacts.access gid
130
+ return @@errno = :forbidden unless relation_valid? operator, gid
131
+ return @@errno = :miss_args unless oid
132
+ contacts = Gitdb::Contacts.new(operator).access gid
133
+ return @@errno = :non_exist unless contacts.read_change_history { |commit| commit.oid }.include?(oid)
134
+ @@errno = :ok
130
135
  operator = {
131
136
  :name => operator,
132
137
  :email => User.new(operator).getemail,
133
138
  :time => Time.now
134
139
  }
135
- contacts.revert_to oid, { :author => operator, :committer => operator, :message => "revert to revision #{oid}" }
140
+ commit = {
141
+ :author => operator,
142
+ :committer => operator,
143
+ :message => "revert to revision #{oid}"
144
+ }
145
+ contacts.revert_to oid, commit
136
146
  end
147
+
137
148
  # code review: @abbshr
138
149
  def add_contacts_card operator, gid, payload
139
- return unless result = GitContacts::relation_valid?(operator, gid)
140
- user = result.first
150
+ return @@errno = :forbidden unless relation_valid? operator, gid
141
151
  # request id
142
152
  qid = Request::create :uid => operator, :gid => gid, :action => "create"
143
- # create a request
144
153
  req = Request.new qid
145
154
  if req.auto_merge? operator
146
155
  # here should return card_id if success
147
156
  cid = req.allow operator
148
157
  Request::delete qid
158
+ @@errno = :ok
149
159
  return cid
150
160
  else
151
- user.add_request qid
161
+ @@errno = :pend
162
+ User.new(operator).add_request qid
163
+ Contacts.new(gid).add_request qid
152
164
  end
153
- true
154
165
  end
166
+
155
167
  # code review: @abbshr
156
- def edit_contacts_card operator, gid, card_id, payload
157
- return unless result = GitContacts::relation_valid?(operator, gid)
158
- user = result.first
159
- qid = Request::create :uid => operator, :gid => gid, :action => "setdata", :card_id => card_id, :content => JSON.generate(payload)
168
+ def edit_contacts_card operator, gid, cid, payload
169
+ return @@errno = :forbidden unless relation_valid? operator, gid
170
+ return @@errno = :non_exist unless Gitdb::Contacts.new(operator).access(gid).get_card_by_id cid
171
+ hash = {
172
+ :uid => operator,
173
+ :gid => gid,
174
+ :action => "setdata",
175
+ :card_id => cid,
176
+ :content => JSON.generate(payload)
177
+ }
178
+ qid = Request::create hash
160
179
  req = Request.new qid
161
180
  if req.auto_merge? operator
162
181
  req.allow operator
163
182
  Request::delete qid
183
+ @@errno = :ok
164
184
  else
165
- user.add_request qid
185
+ @@errno = :pend
186
+ User.new(operator).add_request qid
187
+ Contacts.new(gid).add_request qid
166
188
  end
167
- true
168
189
  end
190
+
169
191
  # code review: @abbshr
170
- def delete_contacts_card operator, gid, card_id
171
- return unless result = GitContacts::relation_valid?(operator, gid)
172
- user = result.first
173
- qid = Request::create :uid => operator, :gid => gid, :action => "delete", :card_id => card_id
192
+ def delete_contacts_card operator, gid, cid
193
+ return @@errno = :forbidden unless relation_valid? operator, gid
194
+ return @@errno = :non_exist unless Gitdb::Contacts.new(operator).access(gid).get_card_by_id cid
195
+ hash = {
196
+ :uid => operator,
197
+ :gid => gid,
198
+ :action => "delete",
199
+ :card_id => cid
200
+ }
201
+ qid = Request::create hash
174
202
  req = Request.new qid
175
203
  if req.auto_merge? operator
176
204
  req.allow operator
177
205
  Request::delete qid
206
+ @@errno = :ok
178
207
  else
179
- user.add_request qid
208
+ @errno = :pend
209
+ User.new(operator).add_request qid
210
+ Contacts.new(gid).add_request qid
180
211
  end
181
- true
182
212
  end
213
+
183
214
  # code review: @abbshr
184
215
  def get_contacts_users operator, gid
185
- return unless result = GitContacts::relation_valid?(operator, gid)
186
- contacts = result.last
187
- contacts.getusers
216
+ return @@errno = :forbidden unless relation_valid? operator, gid
217
+ @@errno = :ok
218
+ Contacts.new(gid).getusers
188
219
  end
220
+
189
221
  def get_contacts_user operator, gid, uid
190
- return unless GitContacts::relation_valid?(operator, gid)
191
- User.new uid if User.exist? uid
222
+ return @@errno = :forbidden unless relation_valid? operator, gid
223
+ return @@errno = :non_exist unless user_exist? operator
224
+ @@errno = :ok
225
+ User.new(uid).getinfo
192
226
  end
227
+
193
228
  # code review: @abbshr
194
229
  def add_contacts_user operator, gid, uid
195
- return unless result = GitContacts::relation_valid?(operator, gid)
196
- user = result.first
197
- contacts = result.last
198
- if contacts.getadmins.include?(user.getuid)
199
- contacts.add_user uid
200
- true
230
+ return @@errno = :forbidden unless relation_valid? operator, gid
231
+ return @@errno = :non_exist unless user_exist? uid
232
+ contacts = Contacts.new(gid)
233
+ if contacts.getadmins.include?(operator)
234
+ @@errno = :ok
235
+ contacts.add_user uid unless contacts.getusers.include? uid
236
+ User.new(uid).add_contacts gid
237
+ else
238
+ @@errno = :forbidden
201
239
  end
202
240
  end
241
+
203
242
  def remove_contacts_user operator, gid, uid
204
- return unless result = GitContacts::relation_valid?(operator, gid)
205
- user = result.first
206
- contacts = result.last
207
- if contacts.getadmins.include?(user.getuid)
243
+ return @@errno = :forbidden unless relation_valid? operator, gid
244
+ contacts = Contacts.new(gid)
245
+ admins = contacts.getadmins
246
+ @@errno = :ok
247
+ if admins.include?(operator)
248
+ return @@errno = :forbidden if admins.length < 2 && operator == uid
249
+ contacts.remove_admin uid
208
250
  contacts.remove_user uid
209
- true
251
+ elsif operator == uid
252
+ contacts.remove_user uid
253
+ else
254
+ @@errno = :forbidden
210
255
  end
211
256
  end
212
257
 
213
258
  def get_contacts_user_privileges operator, gid, uid
214
- return unless GitContacts::relation_valid?(operator, gid)
215
- privilege = 'user' if contacts.getusers.include? uid
216
- privilege = 'admin' if contacts.getadmins.include? uid
259
+ return @@errno = :forbidden unless relation_valid? operator, gid
260
+ @@errno = :ok
261
+ Contacts.new(gid).getadmins.include?(uid) ? 'admin' : 'user'
217
262
  end
218
263
 
219
264
  # code review: @AustinChou
220
- def edit_contacts_user_privileges operator, gid, uid, payload
221
- return unless result = GitContacts::relation_valid?(operator, gid)
222
- user = result.first
223
- contacts = result.last
224
- if contacts.getadmins.include?(user.getuid) && contacts.getusers.include?(uid)
225
- case payload
265
+ def edit_contacts_user_privileges operator, gid, uid, role
266
+ return @@errno = :forbidden unless relation_valid? operator, gid
267
+ contacts = Contacts.new gid
268
+ admins = contacts.getadmins
269
+ @@errno = :ok
270
+ if admins.include?(operator)
271
+ return @@errno = :forbidden if admins.length < 2 && operator == uid
272
+ case role
226
273
  when "admin"
227
- contacts.add_admin uid
228
- true
229
- when "users"
274
+ contacts.add_admin uid unless contacts.getadmins.include? uid
275
+ when "user"
230
276
  contacts.remove_admin uid
231
- true
277
+ else
278
+ @@errno = :bad_args
232
279
  end
280
+ else
281
+ @@errno = :forbidden
233
282
  end
234
283
  end
235
- =begin
236
- def invite_contacts_user operator, gid, payload
237
- return unless result = GitContacts::relation_valid?(operator, gid)
238
- user = result.first
239
- contacts = result.last
240
- if contacts.getadmins.include?(user.getuid)
241
- Inviation::create :uid => User::email_to_uid(payload[:email]), :contacts => gid
242
- end
243
- end
244
-
245
- def edit_invitation_status operator, payload
246
- invitation = Inviation.new payload[:invite_id]
247
- if invitation.accept? operator
248
- user = User.new operator
249
- gid = invitation.getcontacts
250
- user.add_contacts gid
251
- contacts = GCContacts.new gid
252
- contacts.adduser user.getuid
253
- Inviation::delete payload[:invite_id]
254
- true
255
- end
256
- end
257
- =end
258
284
 
259
285
  # code review: @AustinChou
260
286
  def get_all_requests operator
261
- requests = []
262
- if user = User.new(operator)
263
- user.getrequests.each do |qid|
264
- requests << Request.new(qid)
265
- end
266
- end
267
- requests
287
+ @@errno = :ok
288
+ Contacts.new(gid).getrequests.map { |qid| Request.new qid }
268
289
  end
269
290
 
270
291
  def get_a_request operator, qid
271
- if user = User.new(operator)
272
- Request.new qid if Request.exist? qid
273
- end
292
+ @@errno = :ok
293
+ Request.exist?(qid) ? Request.new(qid) : @@errno = :non_exist
274
294
  end
275
295
 
276
296
  # code review: @AustinChou
277
- def edit_request_status operator, qid, payload
278
- if req = Request.new(qid)
279
- if result = GitContacts::relation_valid?(operator, req.getgid)
280
- user = result.first
281
- contacts = result.last
282
- if contacts.getadmins.include?(user.getuid)
283
- case payload
284
- when "permit"
285
- req.allow operator
286
- Request::delete qid
287
- when "reject"
288
- Request::delete qid
289
- end
290
- author = User.new req.getuid
291
- author.remove_request qid
292
- true
293
- end
294
- end
297
+ def edit_request_status operator, gid, qid, action
298
+ return @@errno = :non_exist unless Request.exist? qid
299
+ return @@errno = :forbidden unless relation_valid?(operator, gid)
300
+ return @@errno = :forbidden unless Contacts.new(gid).getadmins.include? operator
301
+ @@errno = :ok
302
+ req = Request.new(qid)
303
+ case action
304
+ when "permit"
305
+ req.allow operator
306
+ Request::delete qid
307
+ User.new(req.getuid).remove_request qid
308
+ Contacts.new(gid).remove_request qid
309
+ when "reject"
310
+ Request::delete qid
311
+ User.new(req.getuid).remove_request qid
312
+ Contacts.new(gid).remove_request qid
313
+ else
314
+ @@errno = :bad_args
295
315
  end
296
316
  end
297
-
298
317
  end
299
318
  end
@@ -6,17 +6,16 @@ module GitContacts
6
6
  true if ContactsObject::exist?(gid)
7
7
  end
8
8
 
9
- def self::create gid, hash
10
- # some keys are optional
11
- if hash.keys.include?(:name)
12
- obj = ContactsObject.new
13
- obj.set_gid gid
14
- obj.name = hash[:name]
15
- obj.note = hash[:note] if hash.has_key?(:note)
16
- #obj.users << gid
17
- #obj.admins << gid
18
- obj.gid
19
- end
9
+ def self::create uid, hash
10
+ # some keys are optional
11
+ gid = Gitdb::Contacts.new(uid).create(hash[:name]).getmeta[:gid]
12
+ obj = ContactsObject.new
13
+ obj.set_gid gid
14
+ obj.name = hash[:name]
15
+ obj.note = hash[:note] if hash.has_key?(:note)
16
+ obj.users << uid
17
+ obj.admins << uid
18
+ gid
20
19
  end
21
20
 
22
21
  def initialize gid
@@ -35,6 +34,10 @@ module GitContacts
35
34
  @obj.admins if @obj
36
35
  end
37
36
 
37
+ def getrequests
38
+ @obj.requests if @obj
39
+ end
40
+
38
41
  def add_user uid
39
42
  @obj.users << uid if @obj
40
43
  end
@@ -43,6 +46,10 @@ module GitContacts
43
46
  @obj.admins << uid if @obj
44
47
  end
45
48
 
49
+ def add_request request_id
50
+ @obj.requests << request_id if @obj
51
+ end
52
+
46
53
  def remove_user uid
47
54
  @obj.users.delete(uid) if @obj
48
55
  end
@@ -50,6 +57,10 @@ module GitContacts
50
57
  def remove_admin uid
51
58
  @obj.admins.delete(uid) if @obj
52
59
  end
60
+
61
+ def remove_request request_id
62
+ @obj.requests.delete request_id if @obj
63
+ end
53
64
 
54
65
  end
55
66
 
@@ -95,7 +95,9 @@ module GitContacts
95
95
 
96
96
  def deny
97
97
  # deny merge here
98
- GitContacts::Request::delete @obj.request_id
98
+ author = User.new getuid
99
+ author.remove_request @obj.request_id
100
+ Request::delete @obj.request_id
99
101
  end
100
102
  end
101
103
 
@@ -3,16 +3,18 @@ module GitContacts
3
3
  class User
4
4
 
5
5
  def self::exist? email
6
- true if UserObject::exist?(email)
6
+ UserObject::exist?(email)
7
7
  end
8
8
 
9
9
  def self::create hash
10
10
  # some keys are optional
11
- if hash.keys.include?(:email) && hash.keys.include?(:password) && !User::exist?(hash[:email])
12
- obj = UserObject.new
13
- obj.set_email hash[:email]
14
- obj.password = Digest::MD5.hexdigest(hash[:password])
15
- end
11
+ obj = UserObject.new
12
+ obj.set_email hash[:email]
13
+ obj.password = Digest::MD5.hexdigest(hash[:password])
14
+ end
15
+
16
+ def self::all
17
+ UserObject::all
16
18
  end
17
19
 
18
20
  def initialize email
@@ -45,6 +47,15 @@ module GitContacts
45
47
  @obj.requests.members if @obj
46
48
  end
47
49
 
50
+ def getinfo
51
+ {
52
+ :uid => getuid,
53
+ :email => getemail,
54
+ :contacts => getcontacts,
55
+ :request => getrequests
56
+ }
57
+ end
58
+
48
59
  def password_correct? sha
49
60
  sha == getpassword && sha != nil && sha != ""
50
61
  end
@@ -88,6 +99,11 @@ module GitContacts
88
99
  true if redis.keys(key_prefix+email+':*').count > 0
89
100
  end
90
101
 
102
+ def self::all
103
+ keys = redis.keys %(#{key_prefix}*:password)
104
+ keys.map { |key| key.split(":")[1] }
105
+ end
106
+
91
107
  def self::access email
92
108
  obj = allocate
93
109
  obj.set_email email
@@ -1,3 +1,3 @@
1
1
  module GitContacts
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitcontacts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - AustinChou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis