diff options
| author | Dennis Fink | 2026-11-17 17:09:59 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 17:09:59 +0100 |
| commit | 6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch) | |
| tree | 33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /webmentions_ssg/models/utils.py | |
| parent | 227092b57acc886469e16bcc4c0d3256e86d3d1e (diff) | |
| download | webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip | |
refactor(core)!: split forms and models by feature
Replace monolithic form and model modules with focused packages for
receiver, moderation, source, user, and shared validation concerns.
Rename source policy concepts to domain moderation, including routes,
models, tables, configuration, templates, and tests.
BREAKING CHANGE: form and model import paths, moderation routes, and
`SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the
new feature-specific modules.
Diffstat (limited to 'webmentions_ssg/models/utils.py')
| -rw-r--r-- | webmentions_ssg/models/utils.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/webmentions_ssg/models/utils.py b/webmentions_ssg/models/utils.py new file mode 100644 index 0000000..e961088 --- /dev/null +++ b/webmentions_ssg/models/utils.py @@ -0,0 +1,41 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""URL fingerprints and UUID timestamp utilities.""" + +import json +from datetime import UTC, datetime +from hashlib import sha256 +from uuid import UUID + + +def uuid7_to_datetime(identifier: UUID) -> datetime: + """Convert a UUID version 7 timestamp to a datetime. + + :param identifier: UUID whose embedded timestamp should be converted. + :returns: Timestamp as a timezone-aware UTC datetime. + """ + return datetime.fromtimestamp(identifier.time / 1000, tz=UTC) + + +def source_target_digest(source: str, target: str) -> str: + """Fingerprint an unambiguous, ordered source/target pair. + + :param source: Webmention source URL. + :param target: Webmention target URL. + :returns: SHA-256 hexadecimal digest of the ordered URL pair. + """ + encoded = json.dumps( + [source, target], ensure_ascii=False, separators=(",", ":") + ).encode("utf-8") + return sha256(encoded).hexdigest() + + +def target_digest(target: str) -> str: + """Fingerprint an outgoing target (unique within each source ID). + + :param target: Outgoing Webmention target URL. + :returns: SHA-256 hexadecimal digest of the target URL. + """ + return sha256(target.encode("utf-8")).hexdigest() |
