-
Notifications
You must be signed in to change notification settings - Fork 103
Feat/activity log backend #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Abhash-Chakraborty
merged 13 commits into
Abhash-Chakraborty:canary
from
payalrvs3:feat/activity-log-backend
Aug 1, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b5e18a0
feat: add privacy-safe local activity log (backend)
payalrvs3 7b90490
fix: restore legacy vault migration call dropped in unlock_vault
payalrvs3 731d5fb
Merge branch 'canary' into feat/activity-log-backend
Abhash-Chakraborty 9970c40
fix: close open dependency advisories across all three ecosystems (#377)
Abhash-Chakraborty e19a2c2
fix: collapse postcss onto a single patched version (#379)
Abhash-Chakraborty 274aed4
fix: attribute vault lock to the acting user, like unlock
payalrvs3 8b256c2
Merge branch 'canary' into feat/activity-log-backend
Abhash-Chakraborty 47e8f9c
fix(vault): keep lock working in shared mode
Abhash-Chakraborty acaf8d1
Merge branch 'canary' into feat/activity-log-backend
Abhash-Chakraborty 35ecb32
Merge branch 'canary' into feat/activity-log-backend
Abhash-Chakraborty a29e015
Merge branch 'canary' into feat/activity-log-backend
Abhash-Chakraborty 1121670
fix(activity): reject negative retention and thin the router
Abhash-Chakraborty 1148520
Merge branch 'canary' into feat/activity-log-backend
Abhash-Chakraborty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Add activity table (append-only local activity log). | ||
|
|
||
| Revision ID: 20260715activitylog | ||
| Revises: 20260714_vault_credentials | ||
| Create Date: 2026-07-15 | ||
| """ | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "20260715activitylog" | ||
| down_revision = "20260714_vault_credentials" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "activity", | ||
| sa.Column("id", sa.Integer(), primary_key=True), | ||
| sa.Column("category", sa.String(length=32), nullable=False), | ||
| sa.Column("action", sa.String(length=32), nullable=False), | ||
| sa.Column( | ||
| "user_id", | ||
| sa.Integer(), | ||
| sa.ForeignKey("users.id", ondelete="SET NULL"), | ||
| nullable=True, | ||
| ), | ||
| sa.Column( | ||
| "media_id", | ||
| sa.Integer(), | ||
| sa.ForeignKey("media.id", ondelete="SET NULL"), | ||
| nullable=True, | ||
| ), | ||
| sa.Column("payload", sa.JSON(), nullable=True), | ||
| sa.Column( | ||
| "created_at", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.func.now(), | ||
| nullable=True, | ||
| ), | ||
| ) | ||
| op.create_index("ix_activity_category", "activity", ["category"]) | ||
| op.create_index("ix_activity_action", "activity", ["action"]) | ||
| op.create_index("ix_activity_user_id", "activity", ["user_id"]) | ||
| op.create_index("ix_activity_media_id", "activity", ["media_id"]) | ||
| op.create_index("ix_activity_created_at", "activity", ["created_at"]) | ||
| op.create_index("ix_activity_user_created", "activity", ["user_id", "created_at"]) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_index("ix_activity_user_created", table_name="activity") | ||
| op.drop_index("ix_activity_created_at", table_name="activity") | ||
| op.drop_index("ix_activity_media_id", table_name="activity") | ||
| op.drop_index("ix_activity_user_id", table_name="activity") | ||
| op.drop_index("ix_activity_action", table_name="activity") | ||
| op.drop_index("ix_activity_category", table_name="activity") | ||
| op.drop_table("activity") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """Append-only local activity log. | ||
|
|
||
| Records privacy-safe events for diagnosing and understanding what changed: | ||
| upload/processing outcomes, archive/trash/restore, vault lock/unlock, and | ||
| settings updates. ``payload`` must stay small and typed — never image bytes, | ||
| embeddings, OCR text, captions, secrets, or raw session tokens (see | ||
| services/activity_log.py for the writer that enforces this at call sites). | ||
|
|
||
| Rows are written once and never mutated. Both foreign keys use SET NULL (not | ||
| CASCADE) so the log outlives the user/media it references, keeping the | ||
| history readable after a delete. | ||
| """ | ||
|
|
||
| from sqlalchemy import Column, DateTime, ForeignKey, Index, Integer, JSON, String | ||
| from sqlalchemy.sql import func | ||
|
|
||
| from find_api.core.database import Base | ||
|
|
||
|
|
||
| class Activity(Base): | ||
| """One append-only activity log entry.""" | ||
|
|
||
| __tablename__ = "activity" | ||
|
|
||
| id = Column(Integer, primary_key=True, index=True) | ||
|
|
||
| # Broad grouping for the category filter, e.g. "upload", "media", "vault", | ||
| # "settings". | ||
| category = Column(String(32), nullable=False, index=True) | ||
| # Specific event within the category, e.g. "completed", "failed", | ||
| # "archived", "trashed", "restored", "locked", "unlocked", "updated". | ||
| action = Column(String(32), nullable=False, index=True) | ||
|
|
||
| # Owner scoping (populated in shared mode, null in local mode) — mirrors | ||
| # Media.uploader_user_id. | ||
| user_id = Column( | ||
| Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True | ||
| ) | ||
| # Related media, when applicable. Kept even after the media row is gone. | ||
| media_id = Column( | ||
| Integer, ForeignKey("media.id", ondelete="SET NULL"), nullable=True, index=True | ||
| ) | ||
|
|
||
| # Small, privacy-safe details (e.g. filename, sanitized error, changed | ||
| # setting key). See module docstring for what must never go here. | ||
| payload = Column(JSON, nullable=True) | ||
|
|
||
| created_at = Column(DateTime(timezone=True), server_default=func.now(), index=True) | ||
|
|
||
| __table_args__ = (Index("ix_activity_user_created", "user_id", "created_at"),) | ||
|
|
||
| def __repr__(self): | ||
| return ( | ||
| f"<Activity(id={self.id}, category={self.category}, action={self.action})>" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """Local activity log — read history and manage retention. | ||
|
|
||
| Rows are written elsewhere (see services/activity_log.py) at the point of | ||
| each state change; this router only reads and prunes them. | ||
| """ | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from fastapi import APIRouter, Depends, Query | ||
| from pydantic import BaseModel | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from find_api.core.config import settings | ||
| from find_api.core.database import get_db | ||
| from find_api.core.dependencies import get_required_user | ||
| from find_api.models.activity import Activity | ||
| from find_api.models.user import User | ||
| from find_api.services import activity_log | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| class ActivityPurgeResponse(BaseModel): | ||
| """Summary of a clear/purge request.""" | ||
|
|
||
| message: str | ||
| deleted_count: int | ||
|
|
||
|
|
||
| def _serialize_activity(entry: Activity) -> dict: | ||
| return { | ||
| "id": entry.id, | ||
| "category": entry.category, | ||
| "action": entry.action, | ||
| "user_id": entry.user_id, | ||
| "media_id": entry.media_id, | ||
| "payload": entry.payload, | ||
| "created_at": entry.created_at.isoformat() if entry.created_at else None, | ||
| } | ||
|
|
||
|
|
||
| @router.get("/activity") | ||
| def list_activity( | ||
| skip: int = Query(0, ge=0), | ||
| limit: int = Query(50, ge=1, le=100), | ||
| category: Optional[str] = Query(None, description="e.g. 'upload', 'vault'"), | ||
| action: Optional[str] = Query(None, description="e.g. 'completed', 'failed'"), | ||
| media_id: Optional[int] = Query(None, description="Filter to one asset's history"), | ||
| db: Session = Depends(get_db), | ||
| user: Optional[User] = Depends(get_required_user), | ||
| ): | ||
| """List activity log entries, newest first.""" | ||
| rows, total = activity_log.list_activity( | ||
| db, | ||
| user, | ||
| skip=skip, | ||
| limit=limit, | ||
| category=category, | ||
| action=action, | ||
| media_id=media_id, | ||
| ) | ||
| items = [_serialize_activity(row) for row in rows] | ||
| page = (skip // limit) + 1 if limit else 1 | ||
| return {"items": items, "total": total, "skip": skip, "page": page, "limit": limit} | ||
|
|
||
|
|
||
| @router.post("/activity/clear", response_model=ActivityPurgeResponse) | ||
| def clear_activity( | ||
| db: Session = Depends(get_db), | ||
| user: Optional[User] = Depends(get_required_user), | ||
| ): | ||
| """Delete every activity row the current user can see.""" | ||
| deleted = activity_log.clear_activity(db, user) | ||
| return {"message": "Activity cleared", "deleted_count": deleted} | ||
|
|
||
|
|
||
| @router.post("/activity/purge", response_model=ActivityPurgeResponse) | ||
| def purge_expired_activity( | ||
| db: Session = Depends(get_db), | ||
| user: Optional[User] = Depends(get_required_user), | ||
| ): | ||
| """Delete activity rows older than the retention window. | ||
|
|
||
| Mirrors ``POST /trash/purge``: age-bounded, intended for a scheduled or | ||
| manual auto-purge. ``ACTIVITY_RETENTION_DAYS=0`` disables it (no-op). | ||
| """ | ||
| retention_days = settings.ACTIVITY_RETENTION_DAYS | ||
| if retention_days <= 0: | ||
| return { | ||
| "message": "Auto-purge disabled (ACTIVITY_RETENTION_DAYS=0)", | ||
| "deleted_count": 0, | ||
| } | ||
|
|
||
| deleted = activity_log.purge_expired_activity(db, user, retention_days) | ||
| return { | ||
| "message": f"Purged activity older than {retention_days} days", | ||
| "deleted_count": deleted, | ||
| } | ||
|
Abhash-Chakraborty marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.