Skip to content

Feat/persist channel icons - #3

Merged
yazdipour merged 2 commits into
masterfrom
feat/persist-channel-icons
Nov 25, 2026
Merged

yazdipour merged 2 commits into
masterfrom
feat/persist-channel-icons

Conversation

@yazdipour

@yazdipour yazdipour commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Closes #IssueNr

📑 Description

✅ Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

ℹ Additional Information

Summary by Sourcery

Persist successfully fetched channel icons to configurable local storage and serve them from memory or disk on subsequent requests.

New Features:

  • Persist channel icons on disk and reuse them across application restarts, with successful fetches cached indefinitely.

Enhancements:

  • Add configurable icon cache storage with an option to disable disk persistence and retry failed fetches.

Documentation:

  • Document the channel icon cache setting and its default behavior.

Chores:

  • Update the example channel configuration and include the icon cache directory setting.

yazdipour and others added 2 commits November 25, 2026 23:03
Fetch each channel icon once and keep it for the life of the process
and on disk under server.icon_cache_dir (default /data/icon-cache).
Only successful fetches are stored; failures are retried on the next
request. Icons are stored outside the IPA cache dir so the size-based
eviction sweep cannot delete them.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Replace expiring in-memory icon caching with permanent memory-and-disk caching, including cache reads, writes, and refresh behavior.

  • src/main.py L32, L67, L437-484

Configuration

Add the icon-cache directory setting and configure it in the example alongside the updated channel list.

  • config.example.yml L18-36
  • src/settings.py L154, L227

Documentation

Document the icon-cache configuration and its permanent caching and retry behavior.

  • README.md L59, L100-103

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T22:12:55.458072Z 1d1dd37 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@yazdipour
yazdipour merged commit 80a6d69 into master Nov 25, 2026
2 checks passed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 8 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/main.py" line_range="441" />
<code_context>
+    if not settings.icon_cache_dir:
+        return None
+    # Slugs are sanitised in settings to [A-Za-z0-9_-], so they are safe as filenames.
+    return Path(settings.icon_cache_dir) / f"{source_slug}.img"
+
+
</code_context>
<issue_to_address>
**Previous channel icon is served**

When a configuration change assigns an existing slug to a different channel, `_source_channel_icon` reads the memory or disk cache by `source.slug` without checking channel identity, so the replacement channel receives the previous channel’s icon indefinitely.

Key both memory and disk icon caches by a stable channel identity rather than the slug alone.

Also at `src/main.py:474-476`, `src/main.py:483`.
</issue_to_address>

### Comment 2
<location path="config.example.yml" line_range="32" />
<code_context>
+- blatants
+- eeveespotifyrevived
+- the_public_archive
+- ethModsGroup
+- compiledipas
+- ciesIPAs
</code_context>
<issue_to_address>
**Duplicate channel sources are published**

When a user starts from the example channel list, `_load_sources` creates a source for each duplicate channel entry and only deduplicates slugs, so the same Telegram channel appears as two sources with duplicate content.

Remove the duplicate channel entry from the example configuration or deduplicate sources by channel identity.

Also at `config.example.yml:24`, `config.example.yml:33`.
</issue_to_address>

### Comment 3
<location path="README.md" line_range="59" />
<code_context>
   ipa_cache_global_workers: 8
   ipa_cache_part_size: 8388608
   ipa_cache_max_mb: 1024
+icon_cache_dir: /data/icon-cache

 channels:
</code_context>
<issue_to_address>
**Documented cache path is ignored**

When a user follows either README configuration example to customize or disable icon storage, `load_settings` reads `server.icon_cache_dir`, but the examples place `icon_cache_dir` at the YAML root, so the custom path or empty value is ignored and the default directory is used.

Place `icon_cache_dir` under `server` in both README configuration examples.

Also at `README.md:100`.
</issue_to_address>

### Comment 4
<location path="src/main.py" line_range="465" />
<code_context>
+        temp_path.replace(path)
+    except OSError as exc:
+        logger.warning("Channel icon cache write failed for %s: %s", path.name, exc)
+        temp_path.unlink(missing_ok=True)
+
+
</code_context>
<issue_to_address>
**Cache write failure aborts icon request**

When writing the cache fails and removing the temporary file also fails, `_write_icon_cache` catches the initial filesystem error but lets `temp_path.unlink` raise another `OSError`, so the request fails instead of returning the fetched icon.

Guard or suppress errors from temporary-file cleanup in the write-error handler.
</issue_to_address>

### Comment 5
<location path="src/main.py" line_range="481" />
<code_context>
+        return from_disk
     if not await telegram.is_authorized():
         return None
     icon = await telegram.download_channel_photo(source)
-    cache["value"] = icon
-    cache["expires_at"] = now + 86400
</code_context>
<issue_to_address>
**Concurrent requests fetch icons repeatedly**

When multiple requests for the same uncached source arrive before its first Telegram download finishes, `_source_channel_icon` lets each cache-missing request call `telegram.download_channel_photo`, so concurrent requests duplicate Telegram downloads and writes; callers can receive different bytes and a later write can replace the cached icon.

Coalesce concurrent misses for each source behind a shared in-flight download.

Also at `src/main.py:471`.
</issue_to_address>

### Comment 6
<location path="src/main.py" line_range="484" />
<code_context>
-    cache["expires_at"] = now + 86400
+    if icon:
+        source_icon_caches[source.slug] = icon
+        _write_icon_cache(path, icon)
     return icon

</code_context>
<issue_to_address>
**Icon cache can fill shared disk**

When many configured channels have their icons requested over time, `_write_icon_cache` stores successful icons without a size limit or eviction, so the cache can fill `/data`, which is also used for the Telegram session and IPA cache, and disrupt the service.

Bound the icon cache with a size limit and eviction policy.
</issue_to_address>

### Comment 7
<location path="src/main.py" line_range="482" />
<code_context>
     icon = await telegram.download_channel_photo(source)
-    cache["value"] = icon
-    cache["expires_at"] = now + 86400
+    if icon:
+        source_icon_caches[source.slug] = icon
+        _write_icon_cache(path, icon)
</code_context>
<issue_to_address>
**Failed icon fetches retry on every request**

When a channel has no downloadable photo or Telegram keeps failing while clients request its icon, `_source_channel_icon()` records only truthy results, so a missing photo or failed Telegram download leaves no cache entry; each subsequent icon request repeats the upstream call, while `download_channel_photo()` suppresses download exceptions and the route returns the fallback icon.

Apply a bounded negative-cache or retry-backoff interval to failed icon fetches.
</issue_to_address>

### Comment 8
<location path="src/main.py" line_range="483-484" />
<code_context>
-    cache["value"] = icon
-    cache["expires_at"] = now + 86400
+    if icon:
+        source_icon_caches[source.slug] = icon
+        _write_icon_cache(path, icon)
     return icon

</code_context>
<issue_to_address>
**Refresh stores an old channel icon**

When the config UI replaces a channel with another channel using the same slug while the old channel’s icon request is awaiting Telegram, the in-flight request retains its old `source`, but after `_runtime_refresh` replaces the global cache and settings, the request stores that old icon under the slug in the new cache and may write it to the new cache directory. A replacement channel reusing that slug is served the old channel’s icon.

Before caching a fetched icon, verify that the current source for the slug still has the same channel identity as the request’s source.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 8 findings to address first, and a successfully fetched icon is written to disk and then reused indefinitely, so a stale or incorrect icon can keep being served after a restart. Reverting stops the new code from using these files, but the cached files remain on disk and can be cleared to repair the issue.

Blocking findings: src/main.py:441, config.example.yml:32, README.md:59, src/main.py:465, src/main.py:481, and 3 more


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/main.py
if not settings.icon_cache_dir:
return None
# Slugs are sanitised in settings to [A-Za-z0-9_-], so they are safe as filenames.
return Path(settings.icon_cache_dir) / f"{source_slug}.img"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Previous channel icon is served

When a configuration change assigns an existing slug to a different channel, _source_channel_icon reads the memory or disk cache by source.slug without checking channel identity, so the replacement channel receives the previous channel’s icon indefinitely.

Key both memory and disk icon caches by a stable channel identity rather than the slug alone.

Also at src/main.py:474-476, src/main.py:483.

Prompt for AI agents
In `src/main.py` at line 441:

**Previous channel icon is served**

When a configuration change assigns an existing slug to a different channel, `_source_channel_icon` reads the memory or disk cache by `source.slug` without checking channel identity, so the replacement channel receives the previous channel’s icon indefinitely.

Key both memory and disk icon caches by a stable channel identity rather than the slug alone.

Also at `src/main.py:474-476`, `src/main.py:483`.

Comment thread config.example.yml
- Sa1y0miii
- IAppsBestLibrary
- ASJTweaks
- ethModsGroup

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Duplicate channel sources are published

When a user starts from the example channel list, _load_sources creates a source for each duplicate channel entry and only deduplicates slugs, so the same Telegram channel appears as two sources with duplicate content.

Remove the duplicate channel entry from the example configuration or deduplicate sources by channel identity.

Also at config.example.yml:24, config.example.yml:33.

Prompt for AI agents
In `config.example.yml` at line 32:

**Duplicate channel sources are published**

When a user starts from the example channel list, `_load_sources` creates a source for each duplicate channel entry and only deduplicates slugs, so the same Telegram channel appears as two sources with duplicate content.

Remove the duplicate channel entry from the example configuration or deduplicate sources by channel identity.

Also at `config.example.yml:24`, `config.example.yml:33`.

Comment thread README.md
ipa_cache_global_workers: 8
ipa_cache_part_size: 8388608
ipa_cache_max_mb: 1024
icon_cache_dir: /data/icon-cache

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Documented cache path is ignored

When a user follows either README configuration example to customize or disable icon storage, load_settings reads server.icon_cache_dir, but the examples place icon_cache_dir at the YAML root, so the custom path or empty value is ignored and the default directory is used.

Place icon_cache_dir under server in both README configuration examples.

Also at README.md:100.

Prompt for AI agents
In `README.md` at line 59:

**Documented cache path is ignored**

When a user follows either README configuration example to customize or disable icon storage, `load_settings` reads `server.icon_cache_dir`, but the examples place `icon_cache_dir` at the YAML root, so the custom path or empty value is ignored and the default directory is used.

Place `icon_cache_dir` under `server` in both README configuration examples.

Also at `README.md:100`.

Comment thread src/main.py
temp_path.replace(path)
except OSError as exc:
logger.warning("Channel icon cache write failed for %s: %s", path.name, exc)
temp_path.unlink(missing_ok=True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Cache write failure aborts icon request

When writing the cache fails and removing the temporary file also fails, _write_icon_cache catches the initial filesystem error but lets temp_path.unlink raise another OSError, so the request fails instead of returning the fetched icon.

Guard or suppress errors from temporary-file cleanup in the write-error handler.

Prompt for AI agents
In `src/main.py` at line 465:

**Cache write failure aborts icon request**

When writing the cache fails and removing the temporary file also fails, `_write_icon_cache` catches the initial filesystem error but lets `temp_path.unlink` raise another `OSError`, so the request fails instead of returning the fetched icon.

Guard or suppress errors from temporary-file cleanup in the write-error handler.

Comment thread src/main.py
return from_disk
if not await telegram.is_authorized():
return None
icon = await telegram.download_channel_photo(source)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Concurrent requests fetch icons repeatedly

When multiple requests for the same uncached source arrive before its first Telegram download finishes, _source_channel_icon lets each cache-missing request call telegram.download_channel_photo, so concurrent requests duplicate Telegram downloads and writes; callers can receive different bytes and a later write can replace the cached icon.

Coalesce concurrent misses for each source behind a shared in-flight download.

Also at src/main.py:471.

Prompt for AI agents
In `src/main.py` at line 481:

**Concurrent requests fetch icons repeatedly**

When multiple requests for the same uncached source arrive before its first Telegram download finishes, `_source_channel_icon` lets each cache-missing request call `telegram.download_channel_photo`, so concurrent requests duplicate Telegram downloads and writes; callers can receive different bytes and a later write can replace the cached icon.

Coalesce concurrent misses for each source behind a shared in-flight download.

Also at `src/main.py:471`.

Comment thread src/main.py
cache["expires_at"] = now + 86400
if icon:
source_icon_caches[source.slug] = icon
_write_icon_cache(path, icon)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Icon cache can fill shared disk

When many configured channels have their icons requested over time, _write_icon_cache stores successful icons without a size limit or eviction, so the cache can fill /data, which is also used for the Telegram session and IPA cache, and disrupt the service.

Bound the icon cache with a size limit and eviction policy.

Prompt for AI agents
In `src/main.py` at line 484:

**Icon cache can fill shared disk**

When many configured channels have their icons requested over time, `_write_icon_cache` stores successful icons without a size limit or eviction, so the cache can fill `/data`, which is also used for the Telegram session and IPA cache, and disrupt the service.

Bound the icon cache with a size limit and eviction policy.

Comment thread src/main.py
icon = await telegram.download_channel_photo(source)
cache["value"] = icon
cache["expires_at"] = now + 86400
if icon:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Failed icon fetches retry on every request

When a channel has no downloadable photo or Telegram keeps failing while clients request its icon, _source_channel_icon() records only truthy results, so a missing photo or failed Telegram download leaves no cache entry; each subsequent icon request repeats the upstream call, while download_channel_photo() suppresses download exceptions and the route returns the fallback icon.

Apply a bounded negative-cache or retry-backoff interval to failed icon fetches.

Prompt for AI agents
In `src/main.py` at line 482:

**Failed icon fetches retry on every request**

When a channel has no downloadable photo or Telegram keeps failing while clients request its icon, `_source_channel_icon()` records only truthy results, so a missing photo or failed Telegram download leaves no cache entry; each subsequent icon request repeats the upstream call, while `download_channel_photo()` suppresses download exceptions and the route returns the fallback icon.

Apply a bounded negative-cache or retry-backoff interval to failed icon fetches.

Comment thread src/main.py
Comment on lines +483 to +484
source_icon_caches[source.slug] = icon
_write_icon_cache(path, icon)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Refresh stores an old channel icon

When the config UI replaces a channel with another channel using the same slug while the old channel’s icon request is awaiting Telegram, the in-flight request retains its old source, but after _runtime_refresh replaces the global cache and settings, the request stores that old icon under the slug in the new cache and may write it to the new cache directory. A replacement channel reusing that slug is served the old channel’s icon.

Before caching a fetched icon, verify that the current source for the slug still has the same channel identity as the request’s source.

Prompt for AI agents
In `src/main.py` at lines 483-484:

**Refresh stores an old channel icon**

When the config UI replaces a channel with another channel using the same slug while the old channel’s icon request is awaiting Telegram, the in-flight request retains its old `source`, but after `_runtime_refresh` replaces the global cache and settings, the request stores that old icon under the slug in the new cache and may write it to the new cache directory. A replacement channel reusing that slug is served the old channel’s icon.

Before caching a fetched icon, verify that the current source for the slug still has the same channel identity as the request’s source.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1d1dd3749d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread README.md
ipa_cache_global_workers: 8
ipa_cache_part_size: 8388608
ipa_cache_max_mb: 1024
icon_cache_dir: /data/icon-cache

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Nest the cache option under server

When a user follows either README YAML snippet and changes icon_cache_dir to customize or disable storage, this root-level key is ignored: load_settings() reads only server.icon_cache_dir, so the app silently keeps using the default. Indent the option under server: in both snippets.

Useful? React with 👍 / 👎.

yazdipour added a commit that referenced this pull request Nov 25, 2026
fix(icons): address PR #3 review findings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant