| 1 | class UsersController < ApplicationController |
| 2 | before_action :require_sign_in!, only: [ :settings, :update_settings ] |
| 3 | before_action :noindex!, only: [ :settings, :update_settings ] |
| 4 | |
| 5 | # Username whose profile shows the curated showcase feed instead of a |
| 6 | # real activity feed. |
| 7 | DEMO_FEED_USERNAME = "sigit" |
| 8 | |
| 9 | def show |
| 10 | @profile_user = User.find_by!(username: params[:username]) |
| 11 | @repositories = if signed_in? && current_user == @profile_user |
| 12 | @profile_user.repositories.order(updated_at: :desc) |
| 13 | else |
| 14 | @profile_user.repositories.where(is_private: false).order(updated_at: :desc) |
| 15 | end.to_a |
| 16 | |
| 17 | # The profile mirrors the site nav: code repositories and models are shown |
| 18 | # on separate tabs, so split the owner's repos by kind. |
| 19 | @code_repositories = @repositories.select(&:code?) |
| 20 | @model_repositories = @repositories.select(&:model?) |
| 21 | |
| 22 | if @profile_user.username == DEMO_FEED_USERNAME |
| 23 | # Curated showcase feed for the demo profile. |
| 24 | feed_repo = @repositories.find { |r| r.name == "sigit" } || @repositories.first |
| 25 | if feed_repo&.initialized? |
| 26 | @feed_repo = feed_repo |
| 27 | @feed_commits = GitRepositoryService.commits( |
| 28 | feed_repo.disk_path, |
| 29 | feed_repo.default_branch, |
| 30 | limit: 3 |
| 31 | ) |
| 32 | end |
| 33 | else |
| 34 | @activity_items = build_activity_feed(@repositories) |
| 35 | end |
| 36 | rescue ActiveRecord::RecordNotFound |
| 37 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 38 | end |
| 39 | |
| 40 | def settings |
| 41 | @user = current_user |
| 42 | end |
| 43 | |
| 44 | def update_settings |
| 45 | @user = current_user |
| 46 | if @user.update(user_settings_params) |
| 47 | redirect_to settings_path, notice: "Settings updated." |
| 48 | else |
| 49 | render :settings, status: :unprocessable_entity |
| 50 | end |
| 51 | end |
| 52 | |
| 53 | private |
| 54 | |
| 55 | def user_settings_params |
| 56 | params.require(:user).permit(:display_name, :avatar_url) |
| 57 | end |
| 58 | |
| 59 | # Real activity feed for a profile, newest first: repository creations and |
| 60 | # recent pushes (latest commits per repo) merged and sorted by time. |
| 61 | def build_activity_feed(repositories, limit: 10) |
| 62 | items = [] |
| 63 | |
| 64 | repositories.each do |repo| |
| 65 | items << { kind: :created_repo, at: repo.created_at, repo: repo } |
| 66 | |
| 67 | next unless repo.initialized? |
| 68 | |
| 69 | commits = GitRepositoryService.commits(repo.disk_path, repo.default_branch, limit: 3) |
| 70 | next if commits.blank? |
| 71 | |
| 72 | items << { |
| 73 | kind: :pushed, |
| 74 | at: commits.first[:authored_date] || repo.updated_at, |
| 75 | repo: repo, |
| 76 | commits: commits |
| 77 | } |
| 78 | end |
| 79 | |
| 80 | items.sort_by { |item| item[:at] || Time.at(0) }.reverse.first(limit) |
| 81 | end |
| 82 | end |