patx/gitman

Add signed-in profile activity tab and account navigation menu

Commit a495d78 · patx · Nov 3 at 11:00PM

Changeset
a495d7858702f082f0660affa0a76b9e0c928f12
Parents
61e83079e0ccf63d689f34ae7b4ae498b487cafc

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/app.py b/app.py
index 8620ec5..68b08da 100644
--- a/app.py
+++ b/app.py
@@ -5273,28 +5273,36 @@ def user_profile(username):
 def render_user_profile(profile_user, **context):
     user = current_user()
     active_tab = request.query.get("tab", "owned")
-    if active_tab not in {"owned", "stars"}:
+    if active_tab not in {"owned", "stars", "activity"} or (active_tab == "activity" and not user):
         active_tab = "owned"
     page = request_page()
-    per_page = PROFILE_REPO_PAGE_SIZE
+    per_page = min(PROFILE_REPO_PAGE_SIZE, 50) if active_tab == "activity" else PROFILE_REPO_PAGE_SIZE
     offset = page_offset(page, per_page)
     owned_count = owned_repo_count(profile_user["id"])
     starred_count = starred_repo_count(profile_user["id"])
-    repos = (
-        list_starred_repos(profile_user["id"], limit=per_page, offset=offset)
-        if active_tab == "stars"
-        else list_owned_repos(profile_user["id"], limit=per_page, offset=offset)
-    )
-    total = starred_count if active_tab == "stars" else owned_count
+    actions = []
+    repos = []
+    if active_tab == "activity":
+        actions = list_recent_actions(per_page + 1, offset=offset)
+        pagination = pagination_metadata(page, per_page, has_next=len(actions) > per_page)
+        actions = actions[:per_page]
+    else:
+        repos = (
+            list_starred_repos(profile_user["id"], limit=per_page, offset=offset)
+            if active_tab == "stars"
+            else list_owned_repos(profile_user["id"], limit=per_page, offset=offset)
+        )
+        total = starred_count if active_tab == "stars" else owned_count
+        pagination = pagination_metadata(page, per_page, total=total)
     return render(
         "profile.tpl",
         profile_user=profile_user,
         owned_count=owned_count,
         starred_count=starred_count,
         repos=repos,
+        actions=actions,
         active_tab=active_tab,
-        is_self=bool(user and user["id"] == profile_user["id"]),
-        pagination=pagination_metadata(page, per_page, total=total),
+        pagination=pagination,
         current_url_with_page=lambda next_page: f"/{profile_user['username']}?" + urlencode({"tab": active_tab, "page": next_page}),
         **context,
     )
diff --git a/static/styles.css b/static/styles.css
index 9b95221..1592516 100644
--- a/static/styles.css
+++ b/static/styles.css
@@ -270,6 +270,14 @@ th { color: var(--ink); font-weight: 700; }
 }
 .nav-icons:hover { color: var(--ink); text-decoration: none; }
 .nav-icons svg { display: block; width: 20px; height: 20px; flex-shrink: 0; }
+.account-menu { position: relative; }
+.account-menu > summary { list-style: none; cursor: pointer; border-radius: 8px; }
+.account-menu > summary::-webkit-details-marker { display: none; }
+.account-menu > summary:hover, .account-menu[open] > summary { background: var(--chip); color: var(--ink); }
+.account-menu > summary:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
+.account-menu-links { position: absolute; top: calc(100% + .5rem); right: 0; z-index: 30; min-width: 12rem; padding: .5rem; border: 1px solid var(--surface-border); border-radius: 8px; background: var(--surface); }
+.account-menu-links a { display: block; padding: .65rem .75rem; border-radius: 4px; color: var(--ink); white-space: nowrap; }
+.account-menu-links a:hover, .account-menu-links a:focus-visible { background: var(--chip); text-decoration: none; }
 .nav form, .repo-tabs form, .inline-form { display: inline; }
 
 .colophon {
diff --git a/templates/base.tpl b/templates/base.tpl
index f0b8343..9f7d40e 100644
--- a/templates/base.tpl
+++ b/templates/base.tpl
@@ -20,7 +20,7 @@
   % if not get('landing', False) and theme != 'light':
     <link rel="stylesheet" href="/static/vendor/highlight.js/github-dark.min.css"{{!' media="(prefers-color-scheme: dark)"' if theme == 'auto' else ''}}>
   % end
-  <link rel="stylesheet" href="/static/styles.css?v=18">
+  <link rel="stylesheet" href="/static/styles.css?v=19">
 </head>
 <body class="{{'landing-page' if get('landing', False) else ''}}">
   % if not get("landing", False):
@@ -44,17 +44,16 @@
           </svg>
           <span class="notification-count" data-notification-count aria-live="polite" hidden></span>
         </a>
-        <a class="nav-icons" href="/new" aria-label="New repository" title="New repository">
-            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-plus-square" viewBox="0 0 16 16" aria-hidden="true">
-              <path d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2z"/>
-              <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4"/>
-            </svg>
-        </a>
-        <a class="nav-icons" href="/{{user['username']}}" aria-label="Profile" title="Profile">
-            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-person" viewBox="2 2 12 12" aria-hidden="true">
-                <path d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z"/>
-            </svg>
-        </a>
+        <details class="account-menu" data-account-menu>
+          <summary class="nav-icons" aria-label="Account menu" title="Account menu">
+            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M4 6h16M4 12h16M4 18h16"/></svg>
+          </summary>
+          <div class="account-menu-links">
+            <a href="/{{user['username']}}">View Profile</a>
+            <a href="/new">New Repository</a>
+            <a href="/settings">Settings</a>
+          </div>
+        </details>
       % else:
         <a href="/login">Log in</a>
         <a class="button" href="/signup">Sign up</a>
@@ -95,6 +94,24 @@
   % if user:
     <script src="/static/notifications.js?v=2" defer></script>
     <script src="/static/repo-search.js?v=1" defer></script>
+    <script>
+      (() => {
+        const menu = document.querySelector('[data-account-menu]');
+        if (!menu) return;
+        document.addEventListener('click', (event) => {
+          if (!menu.contains(event.target)) menu.open = false;
+        });
+        menu.addEventListener('keydown', (event) => {
+          if (event.key !== 'Escape' || !menu.open) return;
+          event.preventDefault();
+          menu.open = false;
+          menu.querySelector('summary').focus();
+        });
+        menu.addEventListener('focusout', (event) => {
+          if (event.relatedTarget && !menu.contains(event.relatedTarget)) menu.open = false;
+        });
+      })();
+    </script>
   % end
   <script src="/static/vendor/highlight.js/highlight.min.js"></script>
   <script>
diff --git a/templates/profile.tpl b/templates/profile.tpl
index 19d0633..26d551b 100644
--- a/templates/profile.tpl
+++ b/templates/profile.tpl
@@ -11,9 +11,6 @@
   </h1>
   <p class="muted"><small>
     Joined {{profile_user["created_at"]}}
-    % if is_self:
-      <a class="button-link" href="/settings">Settings</a>
-    % end
   </small></p>
   % if profile_user["bio"]:
     <p>{{!render_markdown_links(profile_user["bio"])}}</p>
@@ -25,13 +22,23 @@
 
 <section class="panel">
   <div class="panel-heading">
-    <h2>Repositories</h2>
+    <h2>{{'Global activity' if active_tab == 'activity' else 'Repositories'}}</h2>
     <nav class="tabs">
       <a class="{{'active' if active_tab == 'owned' else ''}}" href="/{{profile_user['username']}}">Owned ({{owned_count}})</a>
       <a class="{{'active' if active_tab == 'stars' else ''}}" href="/{{profile_user['username']}}?tab=stars">Starred ({{starred_count}})</a>
+      % if user:
+        <a class="{{'active' if active_tab == 'activity' else ''}}" href="/{{profile_user['username']}}?tab=activity">Global activity</a>
+      % end
     </nav>
   </div>
-  % if repos:
+  % if active_tab == 'activity':
+    % if actions:
+      % include("activity_feed.tpl", actions=actions)
+    % else:
+      <p class="empty">No activity yet.</p>
+    % end
+    % include("pagination.tpl", pagination=pagination)
+  % elif repos:
     <div class="repo-list" data-paginated-list>
       % for repo in repos:
         <a class="feed-card repo-card" href="/{{repo['owner_username']}}/{{repo['name']}}">
diff --git a/tests/test_app.py b/tests/test_app.py
index b22a044..fe880a4 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -1663,6 +1663,43 @@ def test_profile_repositories_paginate_owned_and_starred_tabs(isolated_app, monk
     assert "demo-0" in starred_second.text
 
 
+def test_profile_global_activity_requires_login_and_includes_everyones_work(isolated_app, monkeypatch):
+    monkeypatch.setattr(gitman, "PROFILE_REPO_PAGE_SIZE", 2)
+    alice = create_user("alice")
+    bob = create_user("bob")
+    isolated_app.create_repository(alice, "alice-work", "")
+    isolated_app.create_repository(bob, "bob-work", "")
+    commit_file(isolated_app.repo_path("bob", "bob-work"), "README.md", "Hello\n", message="Global feed commit", user="bob")
+    client = WsgiClient(isolated_app.app)
+
+    anonymous = client.get('/alice?tab=activity')
+    assert anonymous.status_code == 200
+    assert 'Global activity' not in anonymous.text
+    assert 'bob-work' not in anonymous.text
+    assert 'alice-work' in anonymous.text
+
+    login_client(client, "alice")
+    assert 'href="/alice?tab=activity">Global activity</a>' in client.get('/alice').text
+    first = client.get('/alice?tab=activity')
+    second = client.get('/alice?tab=activity&page=2')
+    third = client.get('/alice?tab=activity&page=3')
+    assert re.search(r'<h1>\s*alice\s*</h1>', first.text)
+    assert '<h2>Global activity</h2>' in first.text
+    assert 'data-next-url="/alice?tab=activity&amp;page=2"' in first.text
+    first_cards = re.findall(r'<a class="feed-card" href="([^"]+)"', first.text)
+    second_cards = re.findall(r'<a class="feed-card" href="([^"]+)"', second.text)
+    assert len(first_cards) == len(second_cards) == 2
+    assert first_cards != second_cards
+    feed = first.text + second.text + third.text
+    for target in ['/alice', '/bob', '/alice/alice-work', '/bob/bob-work']:
+        assert f'href="{target}"' in feed
+    assert 'joined' in feed
+    assert 'created repository' in feed
+    assert 'Global feed commit' in feed
+    assert 'repo-card' not in first.text
+    assert client.get('/').location_path == '/alice'
+
+
 def test_pages_host_serves_user_site_and_enabled_project_docs(isolated_app):
     owner = create_user("alice")
     isolated_app.create_repository(owner, "alice.gitman.io", "")
@@ -2727,7 +2764,12 @@ def test_change_password_revokes_other_sessions_and_git_credentials(isolated_app
     profile = client.get('/alice').text
     assert 'href="/settings">Settings</a>' in profile
     assert 'href="/logout"' not in profile
-    assert profile.index('aria-label="Notifications"') < profile.index('aria-label="New repository"') < profile.index('aria-label="Profile"')
+    assert profile.index('aria-label="Notifications"') < profile.index('aria-label="Account menu"')
+    assert 'href="/alice">View Profile</a>' in profile
+    assert 'href="/new">New Repository</a>' in profile
+    assert 'aria-label="New repository"' not in profile
+    profile_content = profile.split('<main class="page">', 1)[1]
+    assert 'href="/settings"' not in profile_content
     assert 'href="/settings">Settings</a>' not in WsgiClient(isolated_app.app).get('/alice').text
     settings = client.get('/settings').text
     assert '<h1>Settings</h1>' in settings