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.0.2"

Class Method Summary collapse

Class Method Details

.add_contacts(operator, name) ⇒ Object



57
58
59
60
61
62
# File 'lib/gitcontacts.rb', line 57

def add_contacts operator, name
  #return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.create name
  contacts.getmeta[:gid]
end

.add_contacts_card(operator, gid, payload) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gitcontacts.rb', line 166

def add_contacts_card operator, gid, payload
  return unless GitContacts::relation_valid? operator gid
  # request id
  qid = Request::create :uid => operator, :gid => gid, :action => "create", :time => Time.now, :content => payload
  # create a rqeuest
  req = Request.new qid
  if req.auto_merge? operator
    Request::delete qid
    # here should return card_id if success
    return req.allow operator
  end
  true
end

.add_contacts_user(operator, gid, uid) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/gitcontacts.rb', line 208

def add_contacts_user operator, gid, uid
  return unless result = GitContacts::relation_valid?(operator, gid)
  user = result.first
  contacts = result.last
  if contacts.getadmins.include?(user.getuid)
    contacts.add_user uid
    true
  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



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

def create_user hash
  User::create hash
end

.delete_contacts_card(operator, gid, card_id) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/gitcontacts.rb', line 191

def delete_contacts_card operator, gid, card_id
  return unless GitContacts::relation_valid? operator gid
  qid = GCRequest::create :uid => operator, :gid => gid, :action => "delete", :time => Time.now, :card_id => card_id
  req = GCRequest.new qid
  if req.auto_merge? operator
    req.allow operator
    Request::delete qid
  end
  true
end

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



180
181
182
183
184
185
186
187
188
189
# File 'lib/gitcontacts.rb', line 180

def edit_contacts_card operator, gid, card_id, payload
  return unless GitContacts::relation_valid? operator gid
  qid = GCRequest::create :uid => operator, :gid => gid, :action => "setdata", :time => Time.now, :card_id => card_id, :content => payload
  req = GCRequest.new qid
  if req.auto_merge? operator
    req.allow operator
    Request::delete qid
  end
  true
end

.edit_contacts_meta(operator, gid, new_meta) ⇒ Object



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

def edit_contacts_meta operator, gid, new_meta
  return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.setmeta new_meta
end

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/gitcontacts.rb', line 218

def edit_contacts_user_privileges operator, gid, uid, payload
  return unless result = GitContacts::relation_valid?(operator, gid)
  user = result.first
  contacts = result.last
  if contacts.getadmins.include?(user.getuid)
    case payload[:role]
    when "admin"
      contacts.add_admin uid
      true
    when "users"
      contacts.remove_admin uid
      true
    end
  end
end

.edit_invitation_status(operator, payload) ⇒ Object



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

def edit_invitation_status operator, payload
  invitation = Inviation.new payload[:invite_id]
  if invitation.accept? operator
    user = User.new operator
    gid = invitation.getcontacts
    user.add_contacts gid
    contacts = GCContacts.new gid
    contacts.adduser user.getuid
    Inviation::delete payload[:invite_id]
    true
  end
end

.edit_request_status(operator) ⇒ Object



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

def edit_request_status operator
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_all_contacts(operator) ⇒ Object

meta => :owner, :gid, :count, :name



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitcontacts.rb', line 39

def get_all_contacts operator
  contacts_arr = []
  user = User.new operator
  contacts = Gitdb::Contacts.new uid
  user.getcontacts.each do |gid|
    return unless GitContacts::relation_valid? operator gid
    contacts.access gid
    contacts_arr << contacts.getmeta
  end
  contacts_arr
end

.get_all_requests(operator) ⇒ Object



256
257
# File 'lib/gitcontacts.rb', line 256

def get_all_requests operator
end

.get_contacts_card(operator, gid, card_id) ⇒ Object



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

def get_contacts_card operator, gid, card_id
  return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.get_card_by_id card_id
end

def get_contacts_cards_by_owner operator, owner

  return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.get_cards do |card|
    card.getdata if card.getmeta[:owner].include? owner
  end
end

def get_contacts_cards_by_name operator, name 
  return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.get_cards do |card|
    card.getdata if card.getdata[:firstname].include? name || card.getdata[:firstname].include? name
  end
end

def get_contacts_cards_by_number operator, number
  return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.get_cards do |card|
    info = card.getdata
    cond = info[:mobile].include? number || info[:phone].include? number || info[:im].include? number
    info if cond
  end
end

def get_contacts_cards_by_birthday date
  contacts = Gitdb::Contacts.new uid
  contacts.access gid
  contacts.get_cards do |card|
    card.getdata if card.getdata[:birthday].include? date
  end
end

def get_contacts_cards_by_email email
  contacts = Gitdb::Contacts.new uid
  contacts.access gid
  contacts.get_cards do |card|
    card.getdata if card.getdata[:email].include? email
  end
end

def get_contacts_cards_by_etc info
  contacts = Gitdb::Contacts.new uid
  contacts.access gid
  contacts.get_cards do |card|
    card.getdata if card.getdata[:address].include? info || card.getdata[:note].include? info
  end
end


131
132
133
134
135
136
137
138
139
# File 'lib/gitcontacts.rb', line 131

def get_contacts_cards_by_related operator, gid, keyword
  return unless GitContacts::relation_valid? operator gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.get_cards do |card|
    info = card.getdata
    info if card.getmeta[:owner].include? keyword || info.find { |k| true if info[k].include? keyword }
  end
end

.get_contacts_history(operator, gid) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gitcontacts.rb', line 141

def get_contacts_history operator, gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  contacts.read_change_history do |commit|
    commit_obj = {}
    commit_obj[:author] = commit.author
    commit_obj[:operator] = commit.committer
    commit_obj[:modified_time] = commit.time
    commit_obj[:oid] = commit.oid
    commit_obj[:message] = commit.message
    commit_obj
  end
end

.get_contacts_if(&condition) ⇒ Object

e.g.: 获取联系人数量大于200的uid群组get_contacts_if { |contacts| contacts.count > 200 }



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

def get_contacts_if &condition
  GitContacts::get_all_contacts(uid).select &condition
end

.get_contacts_users(operator, gid) ⇒ Object



202
203
204
205
206
# File 'lib/gitcontacts.rb', line 202

def get_contacts_users operator, gid
  return unless result = GitContacts::relation_valid?(operator, gid)
  contacts = result.last
  contacts.getusers
end

.invitation_keysObject



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

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

.invite_contacts_user(operator, gid, payload) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/gitcontacts.rb', line 234

def invite_contacts_user operator, gid, payload
  return unless result = GitContacts::relation_valid?(operator, gid)
  user = result.first
  contacts = result.last
  if contacts.getadmins.include?(user.getuid)
    Inviation::create :uid => User::email_to_uid(payload[:email]), :contacts => gid
  end
end

.password_valid?(email, password) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/gitcontacts.rb', line 24

def password_valid? email, password
  if user = User.new(email)
    user if user.password_correct? Digest::SHA1.hexdigest password
  end
end

.relation_valid?(operator, gid) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/gitcontacts.rb', line 30

def relation_valid? operator, gid
  #if User::exist?(operator) && Contacts::exist?(gid)
    user = User.new operator
    contacts = GitContacts.new gid
    [user, contacts] #if user.getcontacts.include?(gid) && contacts.getusers.include?(operator)
  #end
end

.request_keysObject



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

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

.revert_to(operator, gid) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/gitcontacts.rb', line 155

def revert_to operator, gid
  contacts = Gitdb::Contacts.new operator
  contacts.access gid
  operator = {
    :name => operator,
    :email => User.new(operator).getemail,
    :time => Time.now
  }
  contacts.revert_to oid, { :author => operator, :committer => operator, :message => "revert to revision #{oid}" }
end

.user_exist?(uid) ⇒ Boolean

Returns:

  • (Boolean)


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

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