-
-
Notifications
You must be signed in to change notification settings - Fork 66
feat: serve an appversions.json index file to clients via IMAP metadata #1038
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
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| include src/chatmaild/defaults/*.json | ||
| include src/chatmaild/ini/*.ini.f | ||
| include src/chatmaild/ini/*.ini | ||
| include src/chatmaild/tests/mail-data/* |
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,15 @@ | ||
| { | ||
| "clients": [ | ||
| { | ||
| "clientId": "deltachat", | ||
|
hpk42 marked this conversation as resolved.
|
||
| "sources": [ | ||
| { | ||
| "sourceId": "gplay", | ||
| "versionInteger": 754, | ||
| "versionString": "2.57.0", | ||
| "downloadUrl": "https://github.com/deltachat/deltachat-android/releases/download/v2.57.0/deltachat-gplay-release-2.57.0.apk" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
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 |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import json | ||
| import logging | ||
| import socket | ||
| import sys | ||
| import time | ||
| from contextlib import contextmanager | ||
| from importlib.resources import files | ||
|
|
||
| from .config import read_config | ||
| from .dictproxy import DictProxy | ||
|
|
@@ -18,6 +20,18 @@ def turn_credentials(turn_socket_path): | |
| return file.readline().decode("utf-8").strip() | ||
|
|
||
|
|
||
| def read_appversions(path): | ||
| try: | ||
| data = json.loads(path.read_bytes()) | ||
| except FileNotFoundError: | ||
| return None | ||
| except (OSError, ValueError): | ||
| logging.exception(f"failed to read {path}") | ||
| return None | ||
| # the dict protocol is line-based, keep the value single-line | ||
| return json.dumps(data, separators=(",", ":")) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the separators are just to minimize the generated string. they would be ", " and ": " otherwise :) |
||
|
|
||
|
|
||
| def _is_valid_token_timestamp(timestamp, now): | ||
| # Token if invalid after 90 days | ||
| # or if the timestamp is in the future. | ||
|
|
@@ -101,6 +115,7 @@ def __init__( | |
| self.iroh_relay = iroh_relay | ||
| self.turn_hostname = turn_hostname | ||
| self.turn_socket_path = turn_socket_path | ||
| self.appversions_path = files(__package__).joinpath("defaults/appversions.json") | ||
|
|
||
| def handle_lookup(self, parts): | ||
| # Lpriv/43f5f508a7ea0366dff30200c15250e3/devicetoken\tlkj123poi@c2.testrun.org | ||
|
|
@@ -125,6 +140,9 @@ def handle_lookup(self, parts): | |
| case "maxsmtprecipients": | ||
| # postfix default (see "postconf smtpd_recipient_limit") | ||
| return "O1000\n" | ||
| case "appversions": | ||
| value = read_appversions(self.appversions_path) | ||
| return f"O{value}\n" if value else "N\n" | ||
|
|
||
| logging.warning(f"lookup ignored: {parts!r}") | ||
| return "N\n" | ||
|
|
||
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,96 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| from chatmaild.metadata import MetadataDictProxy | ||
|
|
||
| ALLOWED_URL_PREFIXES = ( | ||
| "https://github.com/deltachat/", | ||
| "https://download.delta.chat/", | ||
| ) | ||
|
|
||
|
|
||
| def check_string(value): | ||
| assert isinstance(value, str), value | ||
| assert value | ||
|
|
||
|
|
||
| def check_version_integer(value): | ||
| # core parses this as u32, see https://github.com/chatmail/core/pull/8557 | ||
| assert isinstance(value, int) and not isinstance(value, bool), value | ||
| assert 0 <= value < 2**32, value | ||
|
|
||
|
|
||
| def check_appversions(data): | ||
| """Verifies the file the way core parses it. | ||
|
|
||
| core deserializes into typed structs and drops the whole payload | ||
| of a relay if a single value has an unexpected type, | ||
| while missing or misspelled keys silently turn into defaults. | ||
| """ | ||
| assert set(data) == {"clients"}, data | ||
| assert isinstance(data["clients"], list) | ||
| assert data["clients"] | ||
| client_ids = [] | ||
| for client in data["clients"]: | ||
| assert set(client) == {"clientId", "sources"}, client | ||
| check_string(client["clientId"]) | ||
| client_ids.append(client["clientId"]) | ||
| assert isinstance(client["sources"], list) | ||
| assert client["sources"] | ||
| source_ids = [] | ||
| for source in client["sources"]: | ||
| assert set(source) == { | ||
| "sourceId", | ||
| "versionInteger", | ||
| "versionString", | ||
| "downloadUrl", | ||
| }, source | ||
| check_string(source["sourceId"]) | ||
| source_ids.append(source["sourceId"]) | ||
| check_version_integer(source["versionInteger"]) | ||
| check_string(source["versionString"]) | ||
| check_string(source["downloadUrl"]) | ||
| assert source["downloadUrl"].startswith(ALLOWED_URL_PREFIXES) | ||
| # core takes the first matching source, later duplicates never surface | ||
| assert len(set(source_ids)) == len(source_ids), source_ids | ||
| assert len(set(client_ids)) == len(client_ids), client_ids | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def appversions(): | ||
| # check the file which chatmail-metadata actually serves | ||
| path = MetadataDictProxy(notifier=None, metadata=None).appversions_path | ||
| return json.loads(path.read_text()) | ||
|
|
||
|
|
||
| def test_appversions_schema(appversions): | ||
| check_appversions(appversions) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [True, -1, 2**32, "754", 754.0, None]) | ||
| def test_version_integer_rejected(appversions, value): | ||
| appversions["clients"][0]["sources"][0]["versionInteger"] = value | ||
| with pytest.raises(AssertionError): | ||
| check_appversions(appversions) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("key", ["clientId", "sources"]) | ||
| def test_misspelled_client_key_rejected(appversions, key): | ||
| client = appversions["clients"][0] | ||
| client[key + "s"] = client.pop(key) | ||
| with pytest.raises(AssertionError): | ||
| check_appversions(appversions) | ||
|
|
||
|
|
||
| def test_duplicate_source_id_rejected(appversions): | ||
| sources = appversions["clients"][0]["sources"] | ||
| sources.append(dict(sources[0])) | ||
| with pytest.raises(AssertionError): | ||
| check_appversions(appversions) | ||
|
|
||
|
|
||
| def test_foreign_download_url_rejected(appversions): | ||
| appversions["clients"][0]["sources"][0]["downloadUrl"] = "https://example.org/x.apk" | ||
| with pytest.raises(AssertionError): | ||
| check_appversions(appversions) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this file maybe have a timestamp?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it wouldn't help much currently with freshness of app versions to users. Freshness pipe from relay-repo -> relay-deployment -> core reading new metadata -> UI showing "update available" rather depends on core currently. it's not clear what checks/automatizations we want to do, so let's stick with the minimum data for now.