1defmodule Drinkup.Event.Account do
2 @moduledoc """
3 Struct for account events from the ATProto Firehose.
4 """
5
6 use TypedStruct
7
8 @type status() ::
9 :takendown
10 | :suspended
11 | :deleted
12 | :deactivated
13 | :desynchronized
14 | :throttled
15 | String.t()
16
17 typedstruct enforce: true do
18 field :seq, integer()
19 field :did, String.t()
20 field :time, NaiveDateTime.t()
21 field :active, bool()
22 field :status, status(), enforce: false
23 end
24
25 @spec from(map()) :: t()
26 def from(%{"seq" => seq, "did" => did, "time" => time, "active" => active} = msg) do
27 status = recognise_status(Map.get(msg, "status"))
28 time = NaiveDateTime.from_iso8601!(time)
29
30 %__MODULE__{
31 seq: seq,
32 did: did,
33 time: time,
34 active: active,
35 status: status
36 }
37 end
38
39 @spec recognise_status(String.t()) :: status()
40 defp recognise_status(status)
41 when status in [
42 "takendown",
43 "suspended",
44 "deleted",
45 "deactivated",
46 "desynchronized",
47 "throttled"
48 ],
49 do: String.to_atom(status)
50
51 defp recognise_status(status) when is_binary(status), do: status
52 defp recognise_status(nil), do: nil
53end