-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathuser.rb
More file actions
108 lines (87 loc) · 3.3 KB
/
user.rb
File metadata and controls
108 lines (87 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'travis/api/v3/models/user_preferences'
module Travis::API::V3
class Models::User < Model
has_many :memberships, dependent: :destroy
has_many :permissions, dependent: :destroy
has_many :emails, dependent: :destroy
has_many :tokens, dependent: :destroy
has_many :organizations, through: :memberships
has_many :stars
has_many :email_unsubscribes
has_many :user_beta_features
has_many :beta_features, through: :user_beta_features
has_many :beta_migration_requests
has_preferences Models::UserPreferences
after_initialize do
ensure_preferences
end
before_save do
ensure_preferences
end
serialize :github_oauth_token, Travis::Model::EncryptedColumn.new
scope :with_github_token, -> { where('github_oauth_token IS NOT NULL')}
NEW_USER_INDICATOR_LENGTH = 5
def recently_signed_up
# We need indicator, which tells if user is signed up for the very first time
# is_syncing == true && synced_at == nil is not good indicator, because travis-github-sync
# picks user's repos immediatelly.
# If first_logged_in_at is not older than 5sec we are sure this is first call after first handshake.
first_logged_in_at = read_attribute(:first_logged_in_at)
return false if first_logged_in_at.nil?
Time.now - first_logged_in_at < NEW_USER_INDICATOR_LENGTH
end
def repository_ids
repositories.pluck(:id)
end
def repositories
Models::Repository.where(
'((repositories.owner_type = ? AND repositories.owner_id = ?) OR repositories.id IN (?))'.freeze,
'User'.freeze,
id,
shared_repositories_ids
).order('id ASC')
end
def organizations_repositories_ids
@organizations_repositories_ids ||= organizations.empty? ? [] : Models::Repository.where(owner_id: organizations.pluck(:id)).pluck(:id)
end
def access_repositories_ids
@access_repositories_ids ||= permissions.pluck(:repository_id)
end
def shared_repositories_ids
access_repositories_ids - organizations_repositories_ids
end
def token
tokens.first_or_create.token
end
def starred_repository_ids
@starred_repository_ids ||= stars.map(&:repository_id)
end
def email_unsubscribed_repository_ids
@email_unsubscribed_repository_ids ||= email_unsubscribes.map(&:repository_id)
end
def permission?(roles, options = {})
roles, options = nil, roles if roles.is_a?(Hash)
scope = permissions.where(options)
scope = scope.by_roles(roles) if roles
scope.any?
end
def installation
return @installation if defined? @installation
@installation = Models::Installation.find_by(owner_type: 'User', owner_id: id, removed_by_id: nil)
end
def touch
update(last_activity_at: Time.now) if last_activity_at.nil? || Time.now.utc - last_activity_at > 300
end
def github?
vcs_type == 'GithubUser'
end
def ensure_preferences
return if attributes['preferences'].nil?
self.preferences = self['preferences'].is_a?(String) ? JSON.parse(self['preferences']) : self['preferences']
end
def custom_keys
return @custom_keys if defined? @custom_keys
@custom_keys = Models::CustomKey.where(owner_type: 'User', owner_id: id)
end
end
end