Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Recommendable
Defined in:
app/models/user.rb

Constant Summary collapse

ADMIN_USERS =
['andrew', 'barisbalic', 'malditogeek', 'olizilla', 'thattommyhall']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_auth_hash(hash) ⇒ Object



147
148
149
# File 'app/models/user.rb', line 147

def self.create_from_auth_hash(hash)
  create!(AuthHash.new(hash).)
end

.find_by_auth_hash(hash) ⇒ Object



156
157
158
159
# File 'app/models/user.rb', line 156

def self.find_by_auth_hash(hash)
  conditions = AuthHash.new(hash)..slice(:provider, :uid)
  where(conditions).first
end

Instance Method Details

#active_subscriptionObject



53
54
55
# File 'app/models/user.rb', line 53

def active_subscription
  @active_subscription ||= payola_subscriptions.active.select{|sub| sub.plan.present? }.sort{|sub| sub.plan.amount }
end

#admin?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/user.rb', line 116

def admin?
  ADMIN_USERS.include?(nickname)
end

#all_subscribed_project_idsObject



108
109
110
# File 'app/models/user.rb', line 108

def all_subscribed_project_ids
  (subscribed_projects.pluck(:id) + watched_dependent_projects.pluck(:id)).uniq
end

#all_subscribed_projectsObject



96
97
98
# File 'app/models/user.rb', line 96

def all_subscribed_projects
  Project.where(id: all_subscribed_project_ids)
end

#all_subscribed_versionsObject



112
113
114
# File 'app/models/user.rb', line 112

def all_subscribed_versions
  Version.where(project_id: all_subscribed_project_ids)
end

#api_keyObject



132
133
134
# File 'app/models/user.rb', line 132

def api_key
  api_keys.first.try(:access_token)
end

#assign_from_auth_hash(hash) ⇒ Object



151
152
153
154
# File 'app/models/user.rb', line 151

def assign_from_auth_hash(hash)
  ignored_fields = new_record? ? [] : %i(email)
  update_attributes(AuthHash.new(hash)..except(*ignored_fields))
end

#avatar_url(size = 60) ⇒ Object



169
170
171
# File 'app/models/user.rb', line 169

def avatar_url(size = 60)
  "https://avatars.githubusercontent.com/u/#{uid}?size=#{size}"
end

#can_enable_private_repo_tracking?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/user.rb', line 70

def can_enable_private_repo_tracking?
  private_repo_token.blank?
end

#can_monitor?(github_repository) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/user.rb', line 124

def can_monitor?(github_repository)
  repository_permissions.where(github_repository: github_repository).where(admin: true).any?
end

#can_read?(github_repository) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
# File 'app/models/user.rb', line 265

def can_read?(github_repository)
  repository_permissions.where(github_repository: github_repository).where(pull: true).any?
end

#can_track_private_repos?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/user.rb', line 74

def can_track_private_repos?
  admin? || has_active_subscription?
end

#can_watch?(repo) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
# File 'app/models/user.rb', line 82

def can_watch?(repo)
  return true if admin?
  if repo.private?
    can_track_private_repos? && !reached_private_repo_limit?
  else
    !needs_to_enable_github_access?
  end
end

#create_api_keyObject



128
129
130
# File 'app/models/user.rb', line 128

def create_api_key
  api_keys.create
end

#current_planObject



49
50
51
# File 'app/models/user.rb', line 49

def current_plan
  @current_plan ||= payola_subscriptions.active.select{|sub| sub.plan.present? }.sort{|sub| sub.plan.amount }.last.try(:plan)
end

#current_private_repo_countObject



61
62
63
# File 'app/models/user.rb', line 61

def current_private_repo_count
  watched_github_repositories.where(private: true).count
end

#download_orgsObject



227
228
229
230
231
232
233
# File 'app/models/user.rb', line 227

def download_orgs
  github_client.orgs.each do |org|
    GithubCreateOrgWorker.perform_async(org.)
  end
rescue Octokit::Unauthorized, Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
end

#download_selfObject



219
220
221
222
223
224
225
# File 'app/models/user.rb', line 219

def download_self
  user = GithubUser.find_or_create_by(github_id: self.uid) do |u|
    u. = self.nickname
    u.user_type = 'User'
  end
  GithubUpdateUserWorker.perform_async(nickname)
end

#full_nameObject



104
105
106
# File 'app/models/user.rb', line 104

def full_name
  name.presence || nickname
end

#github_clientObject



165
166
167
# File 'app/models/user.rb', line 165

def github_client
  AuthToken.new_client(token)
end

#github_settings_urlObject



136
137
138
139
140
141
142
143
144
145
# File 'app/models/user.rb', line 136

def github_settings_url
  if private_repo_token.present?
    key = ENV['GITHUB_PRIVATE_KEY']
  elsif
    key = ENV['GITHUB_PUBLIC_KEY']
  else
    key = ENV['GITHUB_KEY']
  end
  "https://github.com/settings/connections/applications/#{key}"
end

#github_urlObject



173
174
175
# File 'app/models/user.rb', line 173

def github_url
  "https://github.com/#{nickname}"
end

#has_active_subscription?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/user.rb', line 45

def has_active_subscription?
  active_subscription.present?
end

#max_private_repo_countObject



57
58
59
# File 'app/models/user.rb', line 57

def max_private_repo_count
  current_plan.try(:repo_count) || 0
end

#monitoring_enabled?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/models/user.rb', line 120

def monitoring_enabled?
  public_repo_token.present? || private_repo_token.present?
end

#mute(project) ⇒ Object



257
258
259
# File 'app/models/user.rb', line 257

def mute(project)
  project_mutes.find_or_create_by(project: project)
end

#muted?(project) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
# File 'app/models/user.rb', line 253

def muted?(project)
  project_mutes.where(project_id: project.id).any?
end

#needs_to_enable_github_access?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/user.rb', line 78

def needs_to_enable_github_access?
  private_repo_token.blank? && public_repo_token.blank?
end

#reached_private_repo_limit?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'app/models/user.rb', line 65

def reached_private_repo_limit?
  return false if admin?
  current_private_repo_count >= max_private_repo_count
end

#subscribe_to_repo(github_repository) ⇒ Object



235
236
237
238
# File 'app/models/user.rb', line 235

def subscribe_to_repo(github_repository)
  hook = github_repository.create_webhook(token)
  repository_subscriptions.find_or_create_by(github_repository_id: github_repository.id, hook_id: hook.try(:id))
end

#subscribed_to?(project) ⇒ Boolean

Returns:

  • (Boolean)


245
246
247
# File 'app/models/user.rb', line 245

def subscribed_to?(project)
  subscriptions.find_by_project_id(project.id)
end

#subscribed_to_repo?(github_repository) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
# File 'app/models/user.rb', line 249

def subscribed_to_repo?(github_repository)
  repository_subscriptions.find_by_github_repository_id(github_repository.id)
end

#to_sObject



100
101
102
# File 'app/models/user.rb', line 100

def to_s
  full_name
end

#tokenObject



161
162
163
# File 'app/models/user.rb', line 161

def token
  private_repo_token.presence || public_repo_token.presence || read_attribute(:token)
end

#unmute(project) ⇒ Object



261
262
263
# File 'app/models/user.rb', line 261

def unmute(project)
  project_mutes.where(project_id: project.id).delete_all
end

#unsubscribe_from_repo(github_repository) ⇒ Object



240
241
242
243
# File 'app/models/user.rb', line 240

def unsubscribe_from_repo(github_repository)
  sub = subscribed_to_repo?(github_repository)
  sub.destroy
end

#update_repo_permissionsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/user.rb', line 181

def update_repo_permissions
  self.update_column(:currently_syncing, true)
  download_orgs
  r = github_client.repos

  current_repo_ids = []

  existing_permissions = repository_permissions.all
  new_repo_ids = r.map(&:id)
  existing_repos = GithubRepository.where(github_id: new_repo_ids).select(:id, :github_id)

  r.each do |repo|
    unless github_repo = existing_repos.find{|r| r.github_id == repo.id}
      github_repo = GithubRepository.find_by('lower(full_name) = ?', repo.full_name.downcase) || GithubRepository.create_from_hash(repo)
    end
    next if github_repo.nil?
    current_repo_ids << github_repo.id

    unless rp = existing_permissions.find{|p| p.github_repository_id == github_repo.id}
      rp = repository_permissions.build(github_repository_id: github_repo.id)
    end
    rp.admin = repo.permissions.admin
    rp.push = repo.permissions.push
    rp.pull = repo.permissions.pull
    rp.save! if rp.changed?
  end

  # delete missing permissions
  existing_repo_ids = repository_permissions.pluck(:github_repository_id)
  remove_ids = existing_repo_ids - current_repo_ids
  repository_permissions.where(github_repository_id: remove_ids).delete_all if remove_ids.any?

rescue Octokit::Unauthorized, Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
ensure
  self.update_columns(last_synced_at: Time.now, currently_syncing: false)
end

#update_repo_permissions_asyncObject



177
178
179
# File 'app/models/user.rb', line 177

def update_repo_permissions_async
  SyncPermissionsWorker.perform_async(self.id)
end

#your_dependent_repos(project) ⇒ Object



91
92
93
94
# File 'app/models/user.rb', line 91

def your_dependent_repos(project)
  ids = really_all_dependencies.where(project_id: project.id).includes(:manifest).map{|dep| dep.manifest.github_repository_id }
  all_github_repositories.where(id: ids).order('fork ASC, pushed_at DESC, stargazers_count DESC')
end