events.ts raw
1 import { DomainEvent } from '../shared/events'
2 import { Pubkey } from '../shared/value-objects/Pubkey'
3 import { RelayUrl } from '../shared/value-objects/RelayUrl'
4 import { SignerType } from './SignerType'
5
6 // ============================================================================
7 // Profile Events
8 // ============================================================================
9
10 /**
11 * Profile field values
12 */
13 export interface ProfileFields {
14 name?: string | null
15 about?: string | null
16 picture?: string | null
17 banner?: string | null
18 nip05?: string | null
19 lud16?: string | null
20 website?: string | null
21 }
22
23 /**
24 * Changes to profile fields
25 */
26 export interface ProfileChanges {
27 name?: { from: string | null; to: string | null }
28 about?: { from: string | null; to: string | null }
29 picture?: { from: string | null; to: string | null }
30 banner?: { from: string | null; to: string | null }
31 nip05?: { from: string | null; to: string | null }
32 lud16?: { from: string | null; to: string | null }
33 website?: { from: string | null; to: string | null }
34 }
35
36 /**
37 * Raised when a profile is published (new or update)
38 */
39 export class ProfilePublished extends DomainEvent {
40 get eventType(): string {
41 return 'identity.profile_published'
42 }
43
44 constructor(
45 readonly pubkey: Pubkey,
46 readonly name: string | null,
47 readonly about: string | null,
48 readonly picture: string | null,
49 readonly nip05: string | null
50 ) {
51 super()
52 }
53 }
54
55 /**
56 * Raised when specific profile fields are updated
57 */
58 export class ProfileUpdated extends DomainEvent {
59 get eventType(): string {
60 return 'identity.profile_updated'
61 }
62
63 constructor(
64 readonly pubkey: Pubkey,
65 readonly changes: ProfileChanges
66 ) {
67 super()
68 }
69
70 get changedFields(): string[] {
71 return Object.keys(this.changes).filter(
72 (key) => this.changes[key as keyof ProfileChanges] !== undefined
73 )
74 }
75 }
76
77 // ============================================================================
78 // Relay List Events (User's NIP-65 mailbox relays)
79 // ============================================================================
80
81 /**
82 * Raised when a user's relay list changes
83 */
84 export class RelayListChanged extends DomainEvent {
85 get eventType(): string {
86 return 'identity.relay_list_changed'
87 }
88
89 constructor(
90 readonly pubkey: Pubkey,
91 readonly addedRelays: readonly RelayUrl[],
92 readonly removedRelays: readonly RelayUrl[]
93 ) {
94 super()
95 }
96
97 get hasAdditions(): boolean {
98 return this.addedRelays.length > 0
99 }
100
101 get hasRemovals(): boolean {
102 return this.removedRelays.length > 0
103 }
104 }
105
106 // ============================================================================
107 // Account Events
108 // ============================================================================
109
110 /**
111 * Raised when a new account is created/added
112 */
113 export class AccountCreated extends DomainEvent {
114 get eventType(): string {
115 return 'identity.account_created'
116 }
117
118 constructor(
119 readonly pubkey: Pubkey,
120 readonly signerType: SignerType
121 ) {
122 super()
123 }
124 }
125
126 /**
127 * Raised when switching between accounts
128 */
129 export class AccountSwitched extends DomainEvent {
130 get eventType(): string {
131 return 'identity.account_switched'
132 }
133
134 constructor(
135 readonly fromPubkey: Pubkey | null,
136 readonly toPubkey: Pubkey | null
137 ) {
138 super()
139 }
140
141 get isLogin(): boolean {
142 return this.fromPubkey === null && this.toPubkey !== null
143 }
144
145 get isLogout(): boolean {
146 return this.fromPubkey !== null && this.toPubkey === null
147 }
148 }
149
150 /**
151 * Raised when an account is removed
152 */
153 export class AccountRemoved extends DomainEvent {
154 get eventType(): string {
155 return 'identity.account_removed'
156 }
157
158 constructor(readonly pubkey: Pubkey) {
159 super()
160 }
161 }
162
163 /**
164 * Raised when the signer type for an account changes
165 */
166 export class SignerTypeChanged extends DomainEvent {
167 get eventType(): string {
168 return 'identity.signer_type_changed'
169 }
170
171 constructor(
172 readonly pubkey: Pubkey,
173 readonly fromType: SignerType,
174 readonly toType: SignerType
175 ) {
176 super()
177 }
178 }
179