Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.0 KB
Raw Blame
"""
Zotero integration models.

These tables live in each user's encrypted per-user database and track the
state needed to sync a Zotero library/collection into the document library:

- ``ZoteroSyncState``: one row per configured (library, collection) pair. It
  remembers which LDR :class:`Collection` the Zotero collection maps to and
  the Zotero *library version* reached by the last successful sync
  (``last_version`` — informational / shown in the status UI; see below).
- ``ZoteroItemMap``: maps an individual Zotero item to the LDR
  :class:`Document` it was imported as, plus the item's Zotero version. This
  enables skipping unchanged items, re-importing changed ones, and detecting
  items removed from the collection so they can be removed locally.

Sync compares the *full* current ``{itemKey: version}`` map of the
collection's top-level items against ``ZoteroItemMap`` — additions/changes by
version, removals by absence. This deliberately does NOT use a ``?since=``
delta: a delta (or the library-level ``/deleted`` feed) would not catch items
*removed from the collection* but still present in the library, whereas the
full-map comparison does.

"""

from datetime import datetime

from sqlalchemy import (
    Boolean,
    Column,
    ForeignKey,
    Integer,
    String,
    Text,
    UniqueConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy_utc import UtcDateTime, utcnow

from .base import Base


class ZoteroSyncState(Base):
    """Per-collection sync bookkeeping for the Zotero integration."""

    __tablename__ = "zotero_sync_state"
    __table_args__ = (
        UniqueConstraint(
            "library_type",
            "library_id",
            "collection_key",
            name="uq_zotero_sync_state_library_collection",
        ),
    )

    id = Column(Integer, primary_key=True, autoincrement=True)

    # Zotero library identity
    library_type = Column(String(10), nullable=False)  # 'user' | 'group'
    library_id = Column(String(64), nullable=False)
    # Empty string means "the entire library" (no specific collection).
    # Stored as "" rather than NULL so the UNIQUE constraint is effective on
    # SQLite (which treats NULLs as distinct).
    collection_key = Column(String(16), nullable=False, default="")

    # LDR collection these items are imported into
    ldr_collection_id = Column(
        String(36),
        ForeignKey("collections.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    # Zotero library version reached by the last successful sync.
    # Informational (surfaced in the status UI) — NOT used as a ``?since=``
    # cursor; each sync fetches the full current version map (see module
    # docstring for why). 0 means "never synced".
    last_version = Column(Integer, nullable=False, default=0)

    last_synced_at = Column(UtcDateTime, nullable=True)
    last_status = Column(
        String(20), nullable=False, default="pending"
    )  # pending | syncing | completed | failed
    last_error = Column(Text, nullable=True)
    item_count = Column(Integer, nullable=False, default=0)

    # Counts from the last sync that completed for this collection. NULL until
    # one completes; a later failed attempt leaves them unchanged.
    last_imported_count = Column(Integer, nullable=True)
    last_updated_count = Column(Integer, nullable=True)
    last_removed_count = Column(Integer, nullable=True)
    last_skipped_count = Column(Integer, nullable=True)
    last_error_count = Column(Integer, nullable=True)

    created_at = Column(UtcDateTime, default=utcnow(), nullable=False)
    updated_at = Column(
        UtcDateTime, default=utcnow(), onupdate=utcnow(), nullable=False
    )

    def __repr__(self) -> str:
        return (
            f"<ZoteroSyncState(library={self.library_type}:{self.library_id}, "
            f"collection={self.collection_key or 'ALL'}, "
            f"version={self.last_version})>"
        )


class ZoteroItemMap(Base):
    """Maps a single Zotero item to the LDR document it was imported as."""

    __tablename__ = "zotero_item_map"
    __table_args__ = (
        UniqueConstraint(
            "ldr_collection_id",
            "zotero_item_key",
            name="uq_zotero_item_map_collection_item",
        ),
    )

    id: Mapped[int] = mapped_column(
        Integer, primary_key=True, autoincrement=True
    )

    ldr_collection_id: Mapped[str] = mapped_column(
        String(36),
        ForeignKey("collections.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    # The Zotero (parent) item key this row tracks.
    zotero_item_key: Mapped[str] = mapped_column(
        String(16), nullable=False, index=True
    )
    # Zotero item version at last import — used for change detection.
    zotero_version: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0
    )

    # The LDR document this item was imported as. SET NULL on document
    # deletion so a removed document doesn't dangle. A null document_id marks
    # an item deliberately not imported (skipped, or its document was deleted
    # locally); the sync service re-imports it only when the item's Zotero
    # version changes. A non-null document_id whose Document row went missing
    # IS re-imported on the next sync.
    document_id: Mapped[str | None] = mapped_column(
        String(36),
        ForeignKey("documents.id", ondelete="SET NULL"),
        nullable=True,
        index=True,
    )
    has_pdf: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False
    )

    last_synced_at: Mapped[datetime | None] = mapped_column(
        UtcDateTime, nullable=True
    )
    created_at: Mapped[datetime] = mapped_column(
        UtcDateTime, default=utcnow(), nullable=False
    )
    updated_at: Mapped[datetime] = mapped_column(
        UtcDateTime, default=utcnow(), onupdate=utcnow(), nullable=False
    )

    def __repr__(self) -> str:
        return (
            f"<ZoteroItemMap(item={self.zotero_item_key}, "
            f"document={self.document_id}, version={self.zotero_version})>"
        )