-
-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathSubscriptionController.ts
More file actions
143 lines (119 loc) · 4.26 KB
/
Copy pathSubscriptionController.ts
File metadata and controls
143 lines (119 loc) · 4.26 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
import { Subscription } from '@standardnotes/responses'
import { destroyAllObjectProperties } from '@/Utils'
import {
ApplicationEvent,
FeaturesClientInterface,
InternalEventBusInterface,
InternalEventHandlerInterface,
InternalEventInterface,
Invitation,
InvitationStatus,
SessionsClientInterface,
SubscriptionManagerEvent,
SubscriptionManagerInterface,
} from '@standardnotes/snjs'
import { computed, makeObservable, observable, runInAction } from 'mobx'
import { AbstractViewController } from '../Abstract/AbstractViewController'
export class SubscriptionController extends AbstractViewController implements InternalEventHandlerInterface {
private readonly ALLOWED_SUBSCRIPTION_INVITATIONS = 5
subscriptionInvitations: Invitation[] | undefined = undefined
hasAccount: boolean
onlineSubscription: Subscription | undefined = undefined
constructor(
private subscriptions: SubscriptionManagerInterface,
private sessions: SessionsClientInterface,
private features: FeaturesClientInterface,
eventBus: InternalEventBusInterface,
) {
super(eventBus)
this.hasAccount = sessions.isSignedIn()
makeObservable(this, {
subscriptionInvitations: observable,
hasAccount: observable,
onlineSubscription: observable,
isSharedSubscription: computed,
usedInvitationsCount: computed,
allowedInvitationsCount: computed,
allInvitationsUsed: computed,
})
eventBus.addEventHandler(this, ApplicationEvent.Launched)
eventBus.addEventHandler(this, ApplicationEvent.SignedIn)
eventBus.addEventHandler(this, ApplicationEvent.UserRolesChanged)
eventBus.addEventHandler(this, SubscriptionManagerEvent.DidFetchSubscription)
}
override deinit() {
super.deinit()
;(this.subscriptionInvitations as unknown) = undefined
destroyAllObjectProperties(this)
}
async handleEvent(event: InternalEventInterface): Promise<void> {
switch (event.type) {
case ApplicationEvent.Launched: {
if (this.sessions.isSignedIn()) {
this.reloadSubscriptionInvitations().catch(console.error)
}
runInAction(() => {
this.hasAccount = this.sessions.isSignedIn()
})
break
}
case ApplicationEvent.SignedIn: {
this.reloadSubscriptionInvitations().catch(console.error)
runInAction(() => {
this.hasAccount = this.sessions.isSignedIn()
})
break
}
case SubscriptionManagerEvent.DidFetchSubscription: {
runInAction(() => {
this.onlineSubscription = this.subscriptions.getOnlineSubscription()
})
break
}
case ApplicationEvent.UserRolesChanged: {
this.reloadSubscriptionInvitations().catch(console.error)
break
}
}
}
hasFirstPartyOnlineOrOfflineSubscription(): boolean {
const offline = this.features.hasFirstPartyOfflineSubscription()
if (!this.sessions.isSignedIn() || !this.sessions.isSignedIntoFirstPartyServer()) {
return offline
}
return !!this.subscriptions.getOnlineSubscription() || offline
}
get isSharedSubscription(): boolean {
return this.onlineSubscription?.subscriptionType === 'shared'
}
get usedInvitationsCount(): number {
return (
this.subscriptionInvitations?.filter((invitation) =>
[InvitationStatus.Accepted, InvitationStatus.Sent].includes(invitation.status),
).length ?? 0
)
}
get allowedInvitationsCount(): number {
return this.ALLOWED_SUBSCRIPTION_INVITATIONS
}
get allInvitationsUsed(): boolean {
return this.usedInvitationsCount === this.ALLOWED_SUBSCRIPTION_INVITATIONS
}
async sendSubscriptionInvitation(inviteeEmail: string): Promise<boolean> {
const success = await this.subscriptions.inviteToSubscription(inviteeEmail)
if (success) {
await this.reloadSubscriptionInvitations()
}
return success
}
async cancelSubscriptionInvitation(invitationUuid: string): Promise<boolean> {
const success = await this.subscriptions.cancelInvitation(invitationUuid)
if (success) {
await this.reloadSubscriptionInvitations()
}
return success
}
private async reloadSubscriptionInvitations(): Promise<void> {
this.subscriptionInvitations = await this.subscriptions.listSubscriptionInvitations()
}
}