Skip to content

Commit

Permalink
reading-list-updater: Consume readinglist.update.requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed Oct 12, 2024
1 parent f1973e7 commit af5c24b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def from_email_bytes(cls, data: bytes) -> ReadingListEntry:
email = EmailMessage.from_bytes(data)
return cls.from_email_summary(email.summary)

@classmethod
def from_email_summary_bytes(cls, data: bytes) -> ReadingListEntry:
return cls.from_email_summary(json.loads(data))

@classmethod
def from_email_summary(cls, email: dict[str, str]) -> ReadingListEntry:
for header in ("to", "cc", "bcc"):
Expand Down
20 changes: 20 additions & 0 deletions services/reading-list-updater/hive/reading_list_updater/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,27 @@
class Service:
email_queue: str = "readinglist.emails.received"
append_request_queue: str = "readinglist.append.requests"
update_request_queue: str = "readinglist.update.requests"
on_channel_open: Optional[Callable[[Channel], None]] = None

@cached_property
def wiki(self):
return HiveWiki()

def on_update_request(
self,
channel: Channel,
method: Basic.Deliver,
properties: BasicProperties,
body: bytes,
):
content_type = properties.content_type
if content_type != "application/json":
raise ValueError(content_type)
entry = ReadingListEntry.from_email_summary_bytes(body)
wikitext = entry.as_wikitext()
self.wiki.page("Reading list").append(f"* {wikitext}")

def on_append_request(
self,
channel: Channel,
Expand Down Expand Up @@ -69,4 +84,9 @@ def run(self):
on_message_callback=self.on_append_request,
dead_letter=True,
)
channel.consume_requests(
queue=self.update_request_queue,
on_message_callback=self.on_update_request,
dead_letter=True,
)
channel.start_consuming()

0 comments on commit af5c24b

Please sign in to comment.