patx/gitman

Keep starred repository history read and include own starred activity

Commit 87ea1b2 · patx · Nov 4 at 10:57PM

Changeset
87ea1b28700db4ef8d685b4dcc0b9cd3f2d98efc
Parents
0c60339c392dc2bccec9276313a7f485d2dc5c21

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/app.py b/app.py
index 23277f8..e27503f 100644
--- a/app.py
+++ b/app.py
@@ -638,6 +638,8 @@ def init_db():
         )
         conn.execute("BEGIN IMMEDIATE")
         ensure_user_profile_columns(conn)
+        if 'notification_snapshot' not in {row['name'] for row in conn.execute('PRAGMA table_info(repo_stars)')}:
+            conn.execute('ALTER TABLE repo_stars ADD COLUMN notification_snapshot TEXT')
         ensure_repository_collaboration_columns(conn)
         ensure_repository_pages_columns(conn)
         ensure_custom_domains_repo_scope(conn)
@@ -1504,7 +1506,7 @@ def notification_read_state(user_id):
 
 
 def notification_is_read(action, read_before, read_keys):
-    return action["event_key"] in read_keys or bool(
+    return bool(action.get('star_history_read') or action.get('is_self_activity')) or action["event_key"] in read_keys or bool(
         action.get("kind") != "commit_created" and read_before
         and parse_activity_time(action["occurred_at"]) < parse_activity_time(read_before)
     )
@@ -1537,13 +1539,12 @@ def list_activity_repositories(limit=ACTIVITY_REPO_SCAN_LIMIT, user_id=None):
     with db_connect() as conn:
         return conn.execute(
             """
-            SELECT repositories.*, users.username AS owner_username
+            SELECT repositories.*, users.username AS owner_username,
+                followed.created_at AS viewer_starred_at, followed.notification_snapshot AS star_notification_snapshot
             FROM repositories
             JOIN users ON users.id = repositories.owner_id
-            WHERE (? IS NULL OR repositories.owner_id = ? OR EXISTS (
-                SELECT 1 FROM repo_stars
-                WHERE repo_stars.repo_id = repositories.id AND repo_stars.user_id = ?
-            ))
+            LEFT JOIN repo_stars AS followed ON followed.repo_id = repositories.id AND followed.user_id = ?
+            WHERE (? IS NULL OR repositories.owner_id = ? OR followed.user_id IS NOT NULL)
             ORDER BY CASE WHEN ? IS NULL THEN repositories.created_at ELSE repositories.updated_at END DESC,
                 repositories.id DESC
             LIMIT ?
@@ -1558,11 +1559,23 @@ def iter_commit_activity_actions(limit, user_id=None, *, read_before=None, read_
     repositories = list_activity_repositories() if user_id is None else list_activity_repositories(user_id=user_id)
     for repo in repositories:
         path = repo_path(repo["owner_username"], repo["name"])
+        own_starred = bool(user and repo['owner_id'] == user['id'] and repo['viewer_starred_at'])
+        snapshot = json.loads(repo['star_notification_snapshot'] or '{}')
+        pending = set(snapshot.get('pending', []))
+        new_nodes = None
         batch_limit = max(100, limit or 100) if own_names or unread_only or since or limit is None else limit
         offset = 0
         emitted = 0
         try:
-            commits = commit_log(path, limit=batch_limit)
+            revision = revision_or_default(path, None)
+            baseline = snapshot.get('head')
+            if baseline and not is_null_revision(revision):
+                delta = run_git(['rev-list', revision, '^' + baseline], cwd=path, check=False)
+                if delta.returncode == 0:
+                    new_nodes = set(delta.stdout.splitlines())
+                    if unread_only and not new_nodes and not pending:
+                        continue
+            commits = commit_log(path, limit=batch_limit, revision=revision)
             while commits:
                 for commit in commits:
                     action = {
@@ -1579,8 +1592,11 @@ def iter_commit_activity_actions(limit, user_id=None, *, read_before=None, read_
                         "summary": "committed",
                         "detail": text_preview(commit["summary"]),
                     }
-                    if commit["author"].strip().casefold() in own_names:
+                    is_self = commit["author"].strip().casefold() in own_names
+                    if is_self and (not own_starred or unread_only):
                         continue
+                    action['is_self_activity'] = is_self
+                    action['star_history_read'] = new_nodes is not None and commit['node'] not in new_nodes and commit['node'] not in pending
                     if since and parse_activity_time(commit["date"]) < parse_activity_time(since):
                         continue
                     if unread_only and notification_is_read(action, read_before, read_keys or set()):
@@ -1592,7 +1608,7 @@ def iter_commit_activity_actions(limit, user_id=None, *, read_before=None, read_
                 if (limit is not None and emitted >= limit) or len(commits) < batch_limit:
                     break
                 offset += len(commits)
-                commits = commit_log(path, limit=batch_limit, offset=offset)
+                commits = commit_log(path, limit=batch_limit, offset=offset, revision=revision)
         except (GitCommandError, OSError):
             continue
 
@@ -1604,6 +1620,8 @@ def list_commit_activity_actions(limit, user_id=None, **filters):
 def list_recent_actions(limit=50, user_id=None, offset=0, *, unread_only=False, since=None):
     limit = max(1, int(limit)) if user_id is not None else max(1, min(int(limit), 100))
     fetch_limit = offset + limit
+    if user_id is not None:
+        initialize_star_notification_snapshots(user_id)
     read_before, read_keys = notification_read_state(user_id)
     with db_connect() as conn:
         conn.create_function("notification_key", 2, notification_key, deterministic=True)
@@ -1836,6 +1854,8 @@ def list_recent_actions(limit=50, user_id=None, offset=0, *, unread_only=False,
             WHERE occurred_at IS NOT NULL
                 AND (:since IS NULL OR occurred_at >= :since)
                 AND (NOT :unread_only OR (
+                    actor_username != (SELECT username FROM users WHERE id = :user_id)
+                    AND
                     (:read_before IS NULL OR occurred_at >= :read_before)
                     AND NOT EXISTS (SELECT 1 FROM notification_reads
                         WHERE notification_reads.user_id = :user_id
@@ -1852,6 +1872,12 @@ def list_recent_actions(limit=50, user_id=None, offset=0, *, unread_only=False,
                 ))
                 AND (:user_id IS NULL OR actor_username != (
                     SELECT username FROM users WHERE id = :user_id
+                ) OR EXISTS (
+                    SELECT 1 FROM repo_stars
+                    JOIN repositories AS own_starred ON own_starred.id = repo_stars.repo_id
+                    JOIN users AS own_owner ON own_owner.id = own_starred.owner_id
+                    WHERE repo_stars.user_id = :user_id AND own_starred.owner_id = :user_id
+                        AND own_owner.username = repo_owner_username AND own_starred.name = repo_name
                 )) AND (
                 :user_id IS NULL
                 OR EXISTS (
@@ -1895,6 +1921,8 @@ def list_recent_actions(limit=50, user_id=None, offset=0, *, unread_only=False,
         actions.extend(list_commit_activity_actions(fetch_limit, user_id=user_id,
             read_before=read_before, read_keys=read_keys, unread_only=unread_only, since=since))
         for action in actions:
+            if action.get('actor_username') == user['username']:
+                action['is_self_activity'] = True
             action["is_read"] = notification_is_read(action, read_before, read_keys)
             action["read_signature"] = notification_signature(user_id, action["event_key"] + ":" + action["target_url"])
     actions.sort(key=activity_sort_key, reverse=True)
@@ -2035,15 +2063,71 @@ def user_starred_repo(user, repo):
     return bool(row)
 
 
+def repository_notification_history(conn, repo, user_id, before=None):
+    """Snapshot existing database events without relying on timestamp resolution."""
+    sources = [
+        ("CASE WHEN forked_from_repo_id IS NULL THEN 'repo_created' ELSE 'repo_forked' END", 'id', 'created_at', 'repositories', 'id'),
+        ("'repo_starred'", "repo_id || ':' || user_id || ':' || created_at", 'created_at', 'repo_stars', 'repo_id'),
+        ("'contributor_added'", "repo_id || ':' || user_id || ':' || created_at", 'created_at', 'repo_contributors', 'repo_id'),
+        ("'issue_opened'", 'id', 'created_at', 'issues', 'repo_id'),
+        ("'issue_commented'", 'c.id', 'c.created_at', 'issue_comments c JOIN issues p ON p.id=c.issue_id', 'p.repo_id'),
+        ("'issue_' || c.status", 'c.id', 'c.created_at', 'issue_status_events c JOIN issues p ON p.id=c.issue_id', 'p.repo_id'),
+        ("'pull_request_opened'", 'id', 'created_at', 'pull_requests', 'target_repo_id'),
+        ("'pull_request_commented'", 'c.id', 'c.created_at', 'pull_request_comments c JOIN pull_requests p ON p.id=c.pull_request_id', 'p.target_repo_id'),
+        ("'pull_request_closed'", 'c.id', 'c.created_at', 'pull_request_status_events c JOIN pull_requests p ON p.id=c.pull_request_id', 'p.target_repo_id'),
+        ("'pull_request_merged'", 'id', 'merged_at', 'pull_requests', 'target_repo_id'),
+        ("'commit_commented'", 'id', 'created_at', 'commit_comments', 'repo_id'),
+    ]
+    for kind, event_id, date, table, scope in sources:
+        for row in conn.execute(f'SELECT {kind} AS kind, {event_id} AS event_id, {date} AS occurred_at FROM {table} WHERE {scope}=?', (repo['id'],)):
+            if row['occurred_at'] and (before is None or parse_activity_time(row['occurred_at']) < parse_activity_time(before)):
+                conn.execute('INSERT OR IGNORE INTO notification_reads (user_id, event_key, read_at) VALUES (?, ?, ?)',
+                    (user_id, notification_key(row['kind'], row['event_id']), utcnow()))
+
+
+def star_notification_snapshot(repo, legacy_since=None):
+    path = repo_path(repo['owner_username'], repo['name'])
+    head = revision_or_default(path, None)
+    pending = []
+    if legacy_since and not is_null_revision(head):
+        history = run_git(['log', '--format=%H %aI', head], cwd=path, check=False)
+        if history.returncode:
+            raise GitCommandError('Unable to snapshot starred repository history.', history.returncode)
+        pending = [node for line in history.stdout.splitlines() for node, date in [line.split(' ', 1)]
+            if parse_activity_time(date) >= parse_activity_time(legacy_since)]
+    return json.dumps({'head': '' if is_null_revision(head) else head, 'pending': pending})
+
+
+def initialize_star_notification_snapshots(user_id):
+    with db_connect() as conn:
+        legacy = conn.execute("""SELECT repositories.*, users.username AS owner_username, repo_stars.created_at AS starred_at
+            FROM repo_stars JOIN repositories ON repositories.id=repo_stars.repo_id
+            JOIN users ON users.id=repositories.owner_id
+            WHERE repo_stars.user_id=? AND repo_stars.notification_snapshot IS NULL""", (user_id,)).fetchall()
+    for repo in legacy:
+        try:
+            snapshot = star_notification_snapshot(repo, repo['starred_at'])
+        except (GitCommandError, OSError):
+            continue
+        with db_connect() as conn:
+            changed = conn.execute('UPDATE repo_stars SET notification_snapshot=? WHERE repo_id=? AND user_id=? AND notification_snapshot IS NULL AND created_at=?',
+                (snapshot, repo['id'], user_id, repo['starred_at'])).rowcount
+            if changed:
+                repository_notification_history(conn, repo, user_id, before=repo['starred_at'])
+
+
 def star_repo(user, repo):
+    snapshot = star_notification_snapshot(repo)
     with db_connect() as conn:
-        conn.execute(
+        inserted = conn.execute(
             """
-            INSERT OR IGNORE INTO repo_stars (repo_id, user_id, created_at)
-            VALUES (?, ?, ?)
+            INSERT OR IGNORE INTO repo_stars (repo_id, user_id, created_at, notification_snapshot)
+            VALUES (?, ?, ?, ?)
             """,
-            (repo["id"], user["id"], utcnow()),
-        )
+            (repo["id"], user["id"], utcnow(), snapshot),
+        ).rowcount
+        if inserted:
+            repository_notification_history(conn, repo, user["id"])
 
 
 def unstar_repo(user, repo):
diff --git a/static/styles.css b/static/styles.css
index 813dcec..94fd87c 100644
--- a/static/styles.css
+++ b/static/styles.css
@@ -338,6 +338,7 @@ th { color: var(--ink); font-weight: 700; }
 }
 
 .eyebrow, .muted, .empty, .issue-list .muted { color: var(--muted); }
+.empty a { color: var(--ink); text-decoration: underline; text-underline-offset: .15em; }
 .eyebrow { font-size: .875rem; }
 
 .repo-list, .clean-list, .commit-list, .file-list, .issue-list, .comment-list, .activity-feed {
diff --git a/templates/base.tpl b/templates/base.tpl
index abace2c..391f214 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=26">
+  <link rel="stylesheet" href="/static/styles.css?v=27">
 </head>
 <body class="{{'landing-page' if get('landing', False) else ''}}">
   % if not get("landing", False):
diff --git a/templates/profile.tpl b/templates/profile.tpl
index 60edfbd..a24ef26 100644
--- a/templates/profile.tpl
+++ b/templates/profile.tpl
@@ -22,7 +22,7 @@
 
 <section class="panel">
   <div class="panel-heading">
-    <h2>{{'Global activity' if active_tab == 'activity' else 'Repositories'}}</h2>
+    <h2>{{'Feed' 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>
@@ -53,7 +53,12 @@
     % if pagination["page"] > 1:
       <p class="empty">No repositories on this page.</p>
     % else:
-      <p class="empty">{{"No starred repositories yet." if active_tab == "stars" else "No repositories yet."}}</p>
+      <p class="empty">
+        {{"No starred repositories yet." if active_tab == "stars" else "No repositories yet."}}
+        % if active_tab == 'owned' and owned_count == 0 and user and user['id'] == profile_user['id']:
+          <a href="/new">Create your first repo now</a>
+        % end
+      </p>
     % end
     % include("pagination.tpl", pagination=pagination)
   % end
diff --git a/tests/test_app.py b/tests/test_app.py
index 484a777..384bddc 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -642,7 +642,7 @@ def test_commit_activity_scans_bounded_repo_set(isolated_app, monkeypatch):
 
     scanned = []
 
-    def fake_commit_log(path, limit):
+    def fake_commit_log(path, limit, revision=None):
         scanned.append(path.name)
         return []
 
@@ -1632,6 +1632,21 @@ def test_repo_branch_and_tag_pages_paginate_refs(isolated_app, monkeypatch):
     assert "Showing" not in tags_first.text
 
 
+def test_empty_own_profile_links_to_first_repository_creation(isolated_app):
+    owner = create_user('alice')
+    create_user('bob')
+    client = WsgiClient(isolated_app.app)
+    assert 'Create your first repo now' not in client.get('/alice').text
+    login_client(client, 'alice')
+    profile = client.get('/alice')
+    assert 'No repositories yet.' in profile.text
+    assert 'href="/new">Create your first repo now</a>' in profile.text
+    for path in ['/bob', '/alice?tab=stars', '/alice?page=2']:
+        assert 'Create your first repo now' not in client.get(path).text
+    isolated_app.create_repository(owner, 'demo', '')
+    assert 'Create your first repo now' not in client.get('/alice').text
+
+
 def test_profile_repositories_paginate_owned_and_starred_tabs(isolated_app, monkeypatch):
     monkeypatch.setattr(gitman, "PROFILE_REPO_PAGE_SIZE", 2)
     owner = create_user("alice")
@@ -1685,7 +1700,7 @@ def test_profile_global_activity_requires_login_and_includes_everyones_work(isol
     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 '<h2>Feed</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)
@@ -3348,11 +3363,93 @@ def test_notifications_from_other_peoples_forks_require_starring_the_fork(isolat
     fork = isolated_app.get_repo('bob', 'forked')
     isolated_app.star_repo(alice, fork)
     assert 'Fork issue' in a.get('/-/notifications').text
-    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] > 0
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 0
     isolated_app.unstar_repo(alice, fork)
     assert 'Fork issue' not in a.get('/-/notifications').text
 
 
+def test_starring_reads_existing_history_but_preserves_new_and_unrelated_activity(isolated_app, monkeypatch):
+    alice, bob = create_user('alice'), create_user('bob')
+    isolated_app.create_repository(alice, 'owned', '')
+    isolated_app.create_repository(bob, 'demo', '')
+    path = isolated_app.repo_path('bob', 'demo')
+    commit_file(path, 'old.txt', 'old', message='Historical commit', user='bob')
+    a, b = WsgiClient(isolated_app.app), WsgiClient(isolated_app.app)
+    login_client(a, 'alice')
+    login_client(b, 'bob')
+    monkeypatch.setattr(isolated_app, 'utcnow', lambda: '2026-11-04T12:00:00Z')
+    b.post('/bob/demo/issues/new', {'title': 'Historical issue', 'body': 'Old body'})
+    b.post('/bob/demo/issues/1', {'action': 'comment', 'body': 'Historical reply'})
+    b.post('/alice/owned/issues/new', {'title': 'Unrelated unread issue', 'body': ''})
+    assert a.post('/bob/demo/star').status_code == 303
+    feed = a.get('/-/notifications').text
+    assert all(text in feed for text in ('Historical issue', 'Historical reply', 'Historical commit'))
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 1
+    # These arrive in the same second as the star, and the Git author date is older.
+    b.post('/bob/demo/issues/1', {'action': 'comment', 'body': 'New reply'})
+    monkeypatch.setenv('GIT_AUTHOR_DATE', '2001-01-01T00:00:00Z')
+    monkeypatch.setenv('GIT_COMMITTER_DATE', '2001-01-01T00:00:00Z')
+    commit_file(path, 'new.txt', 'new', message='New backdated work', user='bob')
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 3
+    a.post('/bob/demo/star')  # duplicate submissions must not clear new activity
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 3
+    a.post('/bob/demo/star', {'action': 'unstar'})
+    a.post('/bob/demo/star')
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 1
+
+
+def test_own_activity_is_visible_and_read_only_when_own_repo_is_starred(isolated_app):
+    alice = create_user('alice')
+    create_user('bob')
+    isolated_app.create_repository(alice, 'demo', '')
+    path = isolated_app.repo_path('alice', 'demo')
+    commit_file(path, 'old.txt', 'old', message='My historical commit', user='alice')
+    a, b = WsgiClient(isolated_app.app), WsgiClient(isolated_app.app)
+    login_client(a, 'alice')
+    login_client(b, 'bob')
+    a.post('/alice/demo/issues/new', {'title': 'My issue', 'body': ''})
+    feed = a.get('/-/notifications').text
+    assert 'My issue' not in feed and 'My historical commit' not in feed
+    assert a.post('/alice/demo/star').status_code == 303
+    a.post('/alice/demo/issues/1', {'action': 'comment', 'body': 'My new comment'})
+    commit_file(path, 'new.txt', 'new', message='My new commit', user='alice')
+    feed = a.get('/-/notifications?per_page=100').text
+    assert all(text in feed for text in ('My issue', 'My historical commit', 'My new comment', 'My new commit'))
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 0
+    b.post('/alice/demo/issues/1', {'action': 'comment', 'body': 'Someone else replied'})
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 1
+    a.post('/alice/demo/star', {'action': 'unstar'})
+    feed = a.get('/-/notifications').text
+    assert 'My new comment' not in feed and 'My new commit' not in feed
+    assert 'Someone else replied' in feed
+
+
+def test_existing_stars_read_pre_star_history_without_clearing_later_activity(isolated_app, monkeypatch):
+    alice, bob = create_user('alice'), create_user('bob')
+    monkeypatch.setattr(isolated_app, 'utcnow', lambda: '2026-01-01T00:00:00Z')
+    isolated_app.create_repository(bob, 'demo', '')
+    path = isolated_app.repo_path('bob', 'demo')
+    monkeypatch.setenv('GIT_AUTHOR_DATE', '2026-01-01T00:00:00Z')
+    monkeypatch.setenv('GIT_COMMITTER_DATE', '2026-01-01T00:00:00Z')
+    commit_file(path, 'old.txt', 'old', message='Before legacy star', user='bob')
+    a, b = WsgiClient(isolated_app.app), WsgiClient(isolated_app.app)
+    login_client(a, 'alice')
+    login_client(b, 'bob')
+    b.post('/bob/demo/issues/new', {'title': 'Before legacy star issue', 'body': ''})
+    repo = isolated_app.get_repo('bob', 'demo')
+    with isolated_app.db_connect() as conn:
+        conn.execute('INSERT INTO repo_stars (repo_id,user_id,created_at) VALUES (?,?,?)',
+            (repo['id'], alice['id'], '2026-01-02T00:00:00Z'))
+    monkeypatch.setattr(isolated_app, 'utcnow', lambda: '2026-01-03T00:00:00Z')
+    b.post('/bob/demo/issues/1', {'action': 'comment', 'body': 'After legacy star reply'})
+    monkeypatch.setenv('GIT_AUTHOR_DATE', '2026-01-03T00:00:00Z')
+    monkeypatch.setenv('GIT_COMMITTER_DATE', '2026-01-03T00:00:00Z')
+    commit_file(path, 'later.txt', 'later', message='After legacy star', user='bob')
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 2
+    assert 'Before legacy star' in a.get('/-/notifications').text
+    assert json.loads(a.get('/-/notifications/unread').text)['unread_count'] == 2
+
+
 def test_new_backdated_commits_are_unread_after_mark_all_read(isolated_app, monkeypatch):
     alice = create_user('alice')
     isolated_app.create_repository(alice, 'demo', '')