Module: GitContacts

Defined in:
lib/gitcontacts.rb,
lib/gitcontacts/User.rb,
lib/gitcontacts/util.rb,
lib/gitcontacts/Request.rb,
lib/gitcontacts/version.rb,
lib/gitcontacts/Contacts.rb,
lib/gitcontacts/Invitation.rb

Defined Under Namespace

Classes: Contacts, ContactsObject, Invitation, InvitationObject, Request, RequestObject, User, UserObject

Constant Summary collapse

VERSION =
"0.1.4"
@@errno =
:ok

Class Method Summary collapse

Class Method Details

.add_contacts(operator, name) ⇒ Object

code review: @abbshr



71
72
73
74
75
76
77
78
# File 'lib/gitcontacts.rb', line 71

def add_contacts operator, name
  return @@errno = :miss_args unless name
  @@errno = :ok
  gid = Contacts.create operator, :name => name
  contacts = Contacts.new gid
  User.new(operator).add_contacts gid
  gid
end

.add_contacts_card(operator, gid, payload) ⇒ Object

code review: @abbshr



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gitcontacts.rb', line 149

def add_contacts_card operator, gid, payload
  return @@errno = :forbidden unless relation_valid? operator, gid
  # request id
  qid = Request::create :uid => operator, :gid => gid, :action => "create"
  req = Request.new qid
  if req.auto_merge? operator
    # here should return card_id if success
    cid = req.allow operator
    Request::delete qid
    @@errno = :ok
    return cid
  else
    @@errno = :pend
    User.new(operator).add_request qid
    Contacts.new(gid).add_request qid
  end
end

.add_contacts_user(operator, gid, uid) ⇒ Object

code review: @abbshr



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gitcontacts.rb', line 229

def add_contacts_user operator, gid, uid
  return @@errno = :forbidden unless relation_valid? operator, gid
  return @@errno = :non_exist unless user_exist? uid
  contacts = Contacts.new(gid)
  if contacts.getadmins.include?(operator)
    @@errno = :ok
    contacts.add_user uid unless contacts.getusers.include? uid
    User.new(uid).add_contacts gid
  else
    @@errno = :forbidden
  end
end

.contacts_keysObject



13
14
15
# File 'lib/gitcontacts/util.rb', line 13

def contacts_keys
  [:name, :note, :users, :admins]
end

.create_user(hash) ⇒ Object



40
41
42
43
44
45
# File 'lib/gitcontacts.rb', line 40

def create_user hash
  return @@errno = :miss_args unless hash.include?(:email) && hash.include?(:password)
  return @@errno = :exist if user_exist?(hash[:email])
  User::create hash
  @@errno = :ok
end

.delete_contacts_card(operator, gid, cid) ⇒ Object

code review: @abbshr



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gitcontacts.rb', line 192

def delete_contacts_card operator, gid, cid
  return @@errno = :forbidden unless relation_valid? operator, gid
  return @@errno = :non_exist unless Gitdb::Contacts.new(operator).access(gid).get_card_by_id cid
  hash = {
    :uid => operator, 
    :gid => gid, 
    :action => "delete", 
    :card_id => cid
  }
  qid = Request::create hash
  req = Request.new qid
  if req.auto_merge? operator
    req.allow operator
    Request::delete qid
    @@errno = :ok
  else
    @errno = :pend
    User.new(operator).add_request qid
    Contacts.new(gid).add_request qid
  end
end

.edit_contacts_card(operator, gid, cid, payload) ⇒ Object

code review: @abbshr



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/gitcontacts.rb', line 168

def edit_contacts_card operator, gid, cid, payload
  return @@errno = :forbidden unless relation_valid? operator, gid
  return @@errno = :non_exist unless Gitdb::Contacts.new(operator).access(gid).get_card_by_id cid
  hash = {
    :uid => operator, 
    :gid => gid, 
    :action => "setdata", 
    :card_id => cid, 
    :content => JSON.generate(payload)
  }
  qid = Request::create hash
  req = Request.new qid
  if req.auto_merge? operator
    req.allow operator
    Request::delete qid
    @@errno = :ok
  else
    @@errno = :pend
    User.new(operator).add_request qid
    Contacts.new(gid).add_request qid
  end
end

.edit_contacts_meta(operator, gid, meta) ⇒ Object

code review: @abbshr



81
82
83
84
85
86
87
88
# File 'lib/gitcontacts.rb', line 81

def edit_contacts_meta operator, gid, meta
  return @@errno = :forbidden unless relation_valid? operator, gid
  return @@errno = :miss_args unless meta
  @@errno = :ok
  contacts = Gitdb::Contacts.new operator
  contacts.access(gid).setmeta meta
  # TODO: check meta
end

.edit_contacts_user_privileges(operator, gid, uid, role) ⇒ Object

code review: @AustinChou



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/gitcontacts.rb', line 265

def edit_contacts_user_privileges operator, gid, uid, role
  return @@errno = :forbidden unless relation_valid? operator, gid
  contacts = Contacts.new gid
  admins = contacts.getadmins
  @@errno = :ok
  if admins.include?(operator)
    return @@errno = :forbidden if admins.length < 2 && operator == uid
    case role
    when "admin"
      contacts.add_admin uid unless contacts.getadmins.include? uid
    when "user"
      contacts.remove_admin uid
    else
      @@errno = :bad_args
    end
  else
    @@errno = :forbidden
  end
end

.edit_request_status(operator, gid, qid, action) ⇒ Object

code review: @AustinChou



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/gitcontacts.rb', line 297

def edit_request_status operator, gid, qid, action
  return @@errno = :non_exist unless Request.exist? qid
  return @@errno = :forbidden unless relation_valid?(operator, gid)
  return @@errno = :forbidden unless Contacts.new(gid).getadmins.include? operator
  @@errno = :ok
  req = Request.new(qid)
  case action
  when "permit"
    req.allow operator
    Request::delete qid
    User.new(req.getuid).remove_request qid
    Contacts.new(gid).remove_request qid
  when "reject"
    Request::delete qid
    User.new(req.getuid).remove_request qid
    Contacts.new(gid).remove_request qid
  else
    @@errno = :bad_args
  end
end

.errsymObject



19
20
21
# File 'lib/gitcontacts.rb', line 19

def errsym
  @@errno
end

.generate_code(n) ⇒ Object



4
5
6
# File 'lib/gitcontacts/util.rb', line 4

def generate_code n
  [*'a'..'z', *0..9, *'A'..'Z'].sample(n).join
end

.get_a_request(operator, qid) ⇒ Object



291
292
293
294
# File 'lib/gitcontacts.rb', line 291

def get_a_request operator, qid
  @@errno = :ok
  Request.exist?(qid) ? Request.new(qid) : @@errno = :non_exist
end

.get_a_user(operator, uid) ⇒ Object



47
48
49
50
51
# File 'lib/gitcontacts.rb', line 47

def get_a_user operator, uid
  return @@errno = :non_exist unless user_exist? uid
  @@errno = :ok
  User.new(uid).getinfo
end

.get_all_requests(operator) ⇒ Object

code review: @AustinChou



286
287
288
289
# File 'lib/gitcontacts.rb', line 286

def get_all_requests operator
  @@errno = :ok
  Contacts.new(gid).getrequests.map { |qid| Request.new qid }
end

.get_contacts_card(operator, gid, cid) ⇒ Object

code review: @abbshr



91
92
93
94
95
96
97
98
# File 'lib/gitcontacts.rb', line 91

def get_contacts_card operator, gid, cid
  return @@errno = :forbidden unless relation_valid? operator, gid
  contacts = Gitdb::Contacts.new(operator).access(gid)
  card = contacts.get_card_by_id cid
  return @@errno = :non_exist unless card
  @@errno = :ok
  card.getdata.merge! card.getmeta
end

code review: @abbshr



101
102
103
104
105
106
107
108
109
110
# File 'lib/gitcontacts.rb', line 101

def get_contacts_cards_by_related operator, gid, keyword
  return @@errno = :forbidden unless relation_valid? operator, gid
  @@errno = :ok
  contacts = Gitdb::Contacts.new operator
  contacts.access(gid).get_cards do |card|
    if card.getmeta[:owner].include?(keyword) || card.getdata.find { |k,v| v.include? keyword }
      card.getdata.merge! card.getmeta 
    end
  end
end

.get_contacts_history(operator, gid) ⇒ Object

code review: @abbshr



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gitcontacts.rb', line 113

def get_contacts_history operator, gid
  return @@errno = :forbidden unless relation_valid? operator, gid
  @@errno = :ok
  contacts = Gitdb::Contacts.new operator
  contacts.access(gid).read_change_history do |commit|
    {
      :author => commit.author,
      :operator => commit.committer,
      :mtime => commit.time,
      :oid => commit.oid,
      :message => commit.message
    }
  end
end

.get_contacts_if(operator, &condition) ⇒ Object

code review: @abbshr e.g.: 获取联系人数量大于200的uid群组meta => :owner, :gid, :count, :name get_contacts_if operator { |contacts| contacts.count > 200 }



63
64
65
66
67
68
# File 'lib/gitcontacts.rb', line 63

def get_contacts_if operator, &condition
  @@errno = :ok
  user = User.new operator
  contacts = Gitdb::Contacts.new operator
  user.getcontacts.map { |gid| contacts.access(gid).getmeta }.select &condition
end

.get_contacts_user(operator, gid, uid) ⇒ Object



221
222
223
224
225
226
# File 'lib/gitcontacts.rb', line 221

def get_contacts_user operator, gid, uid
  return @@errno = :forbidden unless relation_valid? operator, gid
  return @@errno = :non_exist unless user_exist? operator
  @@errno = :ok
  User.new(uid).getinfo
end

.get_contacts_user_privileges(operator, gid, uid) ⇒ Object



258
259
260
261
262
# File 'lib/gitcontacts.rb', line 258

def get_contacts_user_privileges operator, gid, uid
  return @@errno = :forbidden unless relation_valid? operator, gid
  @@errno = :ok
  Contacts.new(gid).getadmins.include?(uid) ? 'admin' : 'user'
end

.get_contacts_users(operator, gid) ⇒ Object

code review: @abbshr



215
216
217
218
219
# File 'lib/gitcontacts.rb', line 215

def get_contacts_users operator, gid
  return @@errno = :forbidden unless relation_valid? operator, gid
  @@errno = :ok
  Contacts.new(gid).getusers
end

.get_users(operator) ⇒ Object

> [ uid ]



54
55
56
57
# File 'lib/gitcontacts.rb', line 54

def get_users operator
  @errno = :ok
  User::all
end

.invitation_keysObject



17
18
19
# File 'lib/gitcontacts/util.rb', line 17

def invitation_keys
  [:uid, :gid, :inviter_id]
end

.password_valid?(email, password) ⇒ Boolean

code review: @abbshr

Returns:

  • (Boolean)


28
29
30
# File 'lib/gitcontacts.rb', line 28

def password_valid? email, password
  User.new(email).password_correct? Digest::MD5.hexdigest(password)
end

.relation_valid?(operator, gid) ⇒ Boolean

code review: @AustinChou, @abbshr if operator belongs to contacts gid

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/gitcontacts.rb', line 34

def relation_valid? operator, gid
  Contacts::exist?(gid) && 
  Contacts.new(gid).getusers.include?(operator) && 
  User.new(operator).getcontacts.include?(gid)
end

.remove_contacts_user(operator, gid, uid) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/gitcontacts.rb', line 242

def remove_contacts_user operator, gid, uid
  return @@errno = :forbidden unless relation_valid? operator, gid
  contacts = Contacts.new(gid)
  admins = contacts.getadmins
  @@errno = :ok
  if admins.include?(operator)
    return @@errno = :forbidden if admins.length < 2 && operator == uid
    contacts.remove_admin uid
    contacts.remove_user uid
  elsif operator == uid
    contacts.remove_user uid
  else
    @@errno = :forbidden
  end
end

.request_keysObject



21
22
23
# File 'lib/gitcontacts/util.rb', line 21

def request_keys
  [:uid, :gid, :action, :content]
end

.revert_to(operator, gid, oid) ⇒ Object

code review: @abbshr



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gitcontacts.rb', line 129

def revert_to operator, gid, oid
  return @@errno = :forbidden unless relation_valid? operator, gid
  return @@errno = :miss_args unless oid
  contacts = Gitdb::Contacts.new(operator).access gid
  return @@errno = :non_exist unless contacts.read_change_history { |commit| commit.oid }.include?(oid)
  @@errno = :ok
  operator = {
    :name => operator,
    :email => User.new(operator).getemail,
    :time => Time.now
  }
  commit = { 
    :author => operator, 
    :committer => operator, 
    :message => "revert to revision #{oid}" 
  }
  contacts.revert_to oid, commit
end

.user_exist?(uid) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/gitcontacts.rb', line 23

def user_exist? uid
  User::exist? uid
end

.user_keysObject

List writeable keys



9
10
11
# File 'lib/gitcontacts/util.rb', line 9

def user_keys
  [:email, :password, :contacts, :requests]
end