Skip to content

Commit

Permalink
Store entry source. #276
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Nov 22, 2024
1 parent 2e1547a commit 3cd7daa
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/reader/_storage/_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..types import Entry
from ..types import EntryCounts
from ..types import EntrySort
from ..types import EntrySource
from ._base import wrap_exceptions
from ._feeds import feed_factory
from ._sql_utils import Query
Expand Down Expand Up @@ -251,6 +252,7 @@ def _insert_entry(self, db: sqlite3.Connection, intent: EntryUpdateIntent) -> No
summary,
content,
enclosures,
source,
read,
last_updated,
first_updated,
Expand All @@ -271,6 +273,7 @@ def _insert_entry(self, db: sqlite3.Connection, intent: EntryUpdateIntent) -> No
:summary,
:content,
:enclosures,
:source,
0, -- read (should be not null in the schema, but isn't)
:last_updated,
:first_updated,
Expand All @@ -296,6 +299,7 @@ def _update_entry(self, db: sqlite3.Connection, intent: EntryUpdateIntent) -> No
summary = :summary,
content = :content,
enclosures = :enclosures,
source = :source,
last_updated = :last_updated,
feed_order = :feed_order,
recent_sort = :recent_sort,
Expand Down Expand Up @@ -425,6 +429,7 @@ def get_entries_query(
entries.summary
entries.content
entries.enclosures
entries.source
entries.read
entries.read_modified
entries.important
Expand Down Expand Up @@ -456,6 +461,7 @@ def entry_factory(row: tuple[Any, ...]) -> Entry:
summary,
content,
enclosures,
source,
read,
read_modified,
important,
Expand All @@ -465,7 +471,15 @@ def entry_factory(row: tuple[Any, ...]) -> Entry:
last_updated,
original_feed,
sequence,
) = row[14:32]
) = row[14:33]

source_obj = None
if source:
source_dict = json.loads(source)
if source_dict['updated']:
source_dict['updated'] = convert_timestamp(source_dict['updated'])
source_obj = EntrySource(**source_dict)

return Entry(
id,
convert_timestamp(updated) if updated else None,
Expand All @@ -476,6 +490,7 @@ def entry_factory(row: tuple[Any, ...]) -> Entry:
summary,
tuple(Content(**d) for d in json.loads(content)) if content else (),
tuple(Enclosure(**d) for d in json.loads(enclosures)) if enclosures else (),
source_obj,
read == 1,
convert_timestamp(read_modified) if read_modified else None,
important == 1 if important is not None else None,
Expand Down Expand Up @@ -663,4 +678,11 @@ def entry_update_intent_to_dict(intent: EntryUpdateIntent) -> dict[str, Any]:
data_hash=entry.hash,
data_hash_changed=context.pop('hash_changed'),
)

if entry.source:
source_dict = entry.source._asdict()
if entry.source.updated:
source_dict['updated'] = adapt_datetime(entry.source.updated)
context['source'] = json.dumps(source_dict)

return context
9 changes: 8 additions & 1 deletion src/reader/_storage/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
summary TEXT,
content TEXT,
enclosures TEXT,
source TEXT,
original_feed TEXT, -- null if the feed was never moved
data_hash BLOB, -- derived from entry data
data_hash_changed INTEGER, -- metadata about data_hash
Expand Down Expand Up @@ -318,7 +319,12 @@ def update_from_41_to_42(db: sqlite3.Connection, /) -> None: # pragma: no cover
entry_tags_by_key_index.create(db)


VERSION = 42
def update_from_42_to_43(db: sqlite3.Connection, /) -> None: # pragma: no cover
# https://github.com/lemon24/reader/issues/276
db.execute("ALTER TABLE entries ADD COLUMN source TEXT;")


VERSION = 43

MIGRATIONS = {
# 1-9 removed before 0.1 (last in e4769d8ba77c61ec1fe2fbe99839e1826c17ace7)
Expand All @@ -331,6 +337,7 @@ def update_from_41_to_42(db: sqlite3.Connection, /) -> None: # pragma: no cover
39: update_from_39_to_40,
40: update_from_40_to_41,
41: update_from_41_to_42,
42: update_from_42_to_43,
}
MISSING_SUFFIX = (
"; you may have skipped some required migrations, see "
Expand Down
2 changes: 0 additions & 2 deletions src/reader/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ def as_entry(self, **kwargs: object) -> Entry:
attrs.update(kwargs)
attrs.setdefault('original_feed_url', feed_url)
attrs.setdefault('added_by', 'feed')
# FIXME: temporary during #276 development
attrs.pop('source')
return Entry(**attrs)

@property
Expand Down
7 changes: 6 additions & 1 deletion src/reader/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def feed_url(self) -> str:
#: A sequence of :class:`Enclosure` objects.
enclosures: Sequence[Enclosure] = ()

#: Metadata of the source feed if the entry is a copy.
#:
#: .. versionadded:: 3.16
source: EntrySource | None = None

#: Whether the entry was read or not.
read: bool = False

Expand Down Expand Up @@ -436,7 +441,7 @@ class Enclosure(_namedtuple_compat):


@dataclass(frozen=True)
class EntrySource:
class EntrySource(_namedtuple_compat):
"""Metadata of a source feed (used with :attr:`Entry.source`).
.. versionadded:: 3.16
Expand Down
8 changes: 4 additions & 4 deletions tests/test_reader_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class datetime_mock(datetime):
monkeypatch.undo()

(feed,) = reader.get_feeds()
entries = set(reader.get_entries())
entries = sorted(reader.get_entries(), key=lambda e: e.id)

url_base, rel_base = make_url_base(feed_url)
expected = {'url_base': url_base, 'rel_base': rel_base}
Expand All @@ -139,14 +139,14 @@ class datetime_mock(datetime):
)

assert feed == expected_feed
assert entries == {
assert entries == [
e.as_entry(
feed=feed,
added=utc_datetime(2010, 1, 2),
last_updated=utc_datetime(2010, 1, 2),
)
for e in expected['entries']
}
for e in sorted(expected['entries'], key=lambda e: e.id)
]

# TODO: same tests, but with http server

Expand Down

0 comments on commit 3cd7daa

Please sign in to comment.