-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
327 lines (271 loc) · 10.1 KB
/
Copy pathmodels.py
File metadata and controls
327 lines (271 loc) · 10.1 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""Typed result models for the overlay.social read API.
These mirror @overlay-social/sdk (TypeScript) one-to-one. Each dataclass reads
the EXACT keys the live overlay returns — note the API itself mixes camelCase
(``displayName``, ``avatarRef``, ``stateRoot``) and snake_case (``minted_at``);
the ``from_dict`` helpers preserve that verbatim rather than silently
normalizing a real contract difference. Open-ended rows (``PeckRow``) stay
plain dicts because the indexer rides extra MAP keys.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
# A peck row (/v1/feed, /v1/post/:txid) is intentionally an open dict — the
# indexer carries arbitrary extra MAP keys, so we do not lock its shape.
PeckRow = dict[str, Any]
@dataclass(frozen=True)
class ResolvedIdentity:
"""One entry from POST /v1/identities/resolve, keyed by the exact input."""
pubkey: str
address: str
handle: str | None = None
display_name: str | None = None
avatar_ref: str | None = None
profile_outpoint: str | None = None
minted_at: str | None = None # set by GET /v1/identities (discovery); None from resolve
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "ResolvedIdentity":
return cls(
pubkey=d.get("pubkey", ""),
address=d.get("address", ""),
handle=d.get("handle"),
display_name=d.get("displayName"),
avatar_ref=d.get("avatarRef"),
profile_outpoint=d.get("profileOutpoint"),
minted_at=d.get("mintedAt"),
)
@dataclass(frozen=True)
class IdentityProfile:
outpoint: str
version: int
display_name: str | None = None
bio: str | None = None
avatar_url: str | None = None # NB: resolved https here, not raw uhrp ref
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "IdentityProfile":
return cls(
outpoint=d.get("outpoint", ""),
version=int(d.get("version", 0)),
display_name=d.get("displayName"),
bio=d.get("bio"),
avatar_url=d.get("avatarUrl"),
)
@dataclass(frozen=True)
class IdentityBundle:
"""Full bundle from GET /identity/:pubkey (profile + handles + certs)."""
pubkey: str
profile: IdentityProfile | None = None
handles: list[str] = field(default_factory=list)
certs: list[Any] = field(default_factory=list)
as_of: dict[str, Any] | None = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "IdentityBundle":
prof = d.get("profile")
return cls(
pubkey=d.get("pubkey", ""),
profile=IdentityProfile.from_dict(prof) if prof else None,
handles=list(d.get("handles") or []),
certs=list(d.get("certs") or []),
as_of=d.get("asOf"),
)
@dataclass(frozen=True)
class HandleResolution:
"""GET /resolve/:handle — handle -> {pubkey, deepLinks}."""
handle: str
pubkey: str
deep_links: dict[str, str] = field(default_factory=dict)
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "HandleResolution":
return cls(
handle=d.get("handle", ""),
pubkey=d.get("pubkey", ""),
deep_links=dict(d.get("deepLinks") or {}),
)
@dataclass(frozen=True)
class ProfileState:
subject: str
version: int
display_name: str | None = None
bio: str | None = None
avatar_ref: str | None = None
cert_refs: list[str] = field(default_factory=list)
handle: str | None = None
nickname: str | None = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "ProfileState":
return cls(
subject=d.get("subject", ""),
version=int(d.get("version", 0)),
display_name=d.get("displayName"),
bio=d.get("bio"),
avatar_ref=d.get("avatarRef"),
cert_refs=list(d.get("certRefs") or []),
handle=d.get("handle"),
nickname=d.get("nickname"),
)
@dataclass(frozen=True)
class ProfileRow:
"""One canonical ProfileToken row from GET /v1/bio/profile."""
outpoint: str
txid: str
vout: int
subject: str
owner: str
version: int
state: ProfileState
minted_at: int | None = None
updated_at: int | None = None
canonical: bool = False
spent: bool = False
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "ProfileRow":
return cls(
outpoint=d.get("outpoint", ""),
txid=d.get("txid", ""),
vout=int(d.get("vout", 0)),
subject=d.get("subject", ""),
owner=d.get("owner", ""),
version=int(d.get("version", 0)),
state=ProfileState.from_dict(d.get("state") or {}),
minted_at=d.get("minted_at"),
updated_at=d.get("updated_at"),
canonical=bool(d.get("canonical", False)),
spent=bool(d.get("spent", False)),
)
@dataclass(frozen=True)
class TopicAnchor:
"""Latest on-chain anchor for a topic's state-root (when present in /state).
`matches_live` is True when the served state_root equals the anchored root."""
root: str
txid: str
vout: int
outpoint: str
block_height: int | None = None
ts: int | None = None
anchored_at: str | None = None
matches_live: bool = False
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "TopicAnchor":
return cls(
root=d.get("root", ""),
txid=d.get("txid", ""),
vout=int(d.get("vout", 0)),
outpoint=d.get("outpoint", ""),
block_height=d.get("blockHeight"),
ts=d.get("ts"),
anchored_at=d.get("anchoredAt"),
matches_live=bool(d.get("matchesLive", False)),
)
@dataclass(frozen=True)
class TopicState:
topic: str
count: int
state_root: str
source: str
anchor: TopicAnchor | None = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "TopicState":
a = d.get("anchor")
return cls(
topic=d.get("topic", ""),
count=int(d.get("count", 0)),
state_root=d.get("stateRoot", ""),
source=d.get("source", ""),
anchor=TopicAnchor.from_dict(a) if isinstance(a, dict) else None,
)
@dataclass(frozen=True)
class OverlayState:
"""GET /state — overlay topic state-roots + counts."""
status: str
service: str
domain: str
network: str
managers: list[str] = field(default_factory=list)
topics: list[TopicState] = field(default_factory=list)
computed_at: str | None = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "OverlayState":
return cls(
status=d.get("status", ""),
service=d.get("service", ""),
domain=d.get("domain", ""),
network=d.get("network", ""),
managers=list(d.get("managers") or []),
topics=[TopicState.from_dict(t) for t in (d.get("topics") or [])],
computed_at=d.get("computedAt"),
)
@dataclass(frozen=True)
class FeedResponse:
status: str
total: int
offset: int
limit: int
count: int
data: list[PeckRow] = field(default_factory=list)
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "FeedResponse":
return cls(
status=d.get("status", ""),
total=int(d.get("total", 0)),
offset=int(d.get("offset", 0)),
limit=int(d.get("limit", 0)),
count=int(d.get("count", 0)),
data=list(d.get("data") or []),
)
@dataclass(frozen=True)
class ThreadResponse:
post: PeckRow | None = None
replies: list[PeckRow] = field(default_factory=list)
@dataclass(frozen=True)
class FriendEntry:
"""One side of the friend graph, hydrated with the peer's canonical handle."""
peer: str # peer identity ROOT pubkey (66-hex compressed)
handle: str | None = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "FriendEntry":
return cls(peer=d.get("peer", ""), handle=d.get("handle"))
@dataclass(frozen=True)
class FriendsGraph:
"""GET /v1/friends/:subject — mutual-consent friendship layer.
``mutual`` = both directions attested + unrevoked (two one-way BRC-3
records form a pair). ``pending_in``/``pending_out`` are one-way.
``legacy_outgoing``/``legacy_incoming`` mirror the BAP-era Bitcoin Schema
rows (display-only; never count toward mutual).
"""
mutual: list[FriendEntry] = field(default_factory=list)
pending_in: list[FriendEntry] = field(default_factory=list)
pending_out: list[FriendEntry] = field(default_factory=list)
legacy_outgoing: list[dict[str, Any]] = field(default_factory=list)
legacy_incoming: list[dict[str, Any]] = field(default_factory=list)
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "FriendsGraph":
light = d.get("light") or {}
data = d.get("data") or {}
return cls(
mutual=[FriendEntry.from_dict(x) for x in (light.get("mutual") or [])],
pending_in=[FriendEntry.from_dict(x) for x in (light.get("pending_in") or [])],
pending_out=[FriendEntry.from_dict(x) for x in (light.get("pending_out") or [])],
legacy_outgoing=list(data.get("outgoing") or []),
legacy_incoming=list(data.get("incoming") or []),
)
@dataclass(frozen=True)
class NotificationItem:
"""One row from GET /v1/notifications/:address.
``actor`` is an address/username for like/reply/follow/mention and an
identity ROOT pubkey for friend_request — resolve via resolve_identities
(bound posting keys collapse to their identity).
"""
type: str
actor: str
target_txid: str | None = None
subject_txid: str | None = None
timestamp: str | None = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> "NotificationItem":
ts = d.get("timestamp")
return cls(
type=d.get("type", ""),
actor=d.get("actor", ""),
target_txid=d.get("target_txid"),
subject_txid=d.get("subject_txid"),
timestamp=str(ts) if ts is not None else None,
)