File size: 1,504 Bytes
31c93b1
 
 
4aaae80
 
31c93b1
 
 
 
 
 
 
 
 
 
 
 
 
 
4aaae80
31c93b1
 
 
 
 
4aaae80
 
31c93b1
4aaae80
31c93b1
 
4aaae80
31c93b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import annotations

from dataclasses import dataclass
from datetime import UTC, datetime
from typing import Literal

Category = Literal["offer", "request", "info", "emergency"]


@dataclass(frozen=True)
class Location:
    lat: float
    lon: float
    label: str


@dataclass(frozen=True)
class Post:
    event_id: str
    author: str  # full node_id
    category: Category
    title: str
    body: str
    location: Location | None
    tags: list[str]
    created_at: str  # RFC 3339 UTC
    expires_at: str  # RFC 3339 UTC
    lamport: int
    client_id: str  # for idempotency

    def is_expired(self, now: datetime | None = None) -> bool:
        now = now or datetime.now(UTC)
        try:
            exp = datetime.fromisoformat(self.expires_at.replace("Z", "+00:00"))
            return now > exp
        except Exception:
            return False

    def as_dict(self) -> dict:
        return {
            "event_id": self.event_id,
            "author": self.author,
            "category": self.category,
            "title": self.title,
            "body": self.body,
            "location": (
                {"lat": self.location.lat, "lon": self.location.lon, "label": self.location.label}
                if self.location
                else None
            ),
            "tags": self.tags,
            "created_at": self.created_at,
            "expires_at": self.expires_at,
            "lamport": self.lamport,
            "client_id": self.client_id,
        }