-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
vsls.ts
221 lines (182 loc) · 5.81 KB
/
vsls.ts
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
'use strict';
import { Disposable, extensions, workspace } from 'vscode';
import type { LiveShare, LiveShareExtension, SessionChangeEvent } from '../@types/vsls';
import { ContextKeys, DocumentSchemes, setContext } from '../constants';
import { Container } from '../container';
import { Logger } from '../logger';
import { debug, timeout } from '../system';
import { VslsGuestService } from './guest';
import { VslsHostService } from './host';
export const vslsUriPrefixRegex = /^[/|\\]~(?:\d+?|external)(?:[/|\\]|$)/;
export const vslsUriRootRegex = /^[/|\\]~(?:\d+?|external)$/;
export interface ContactPresence {
status: ContactPresenceStatus;
statusText: string;
}
export type ContactPresenceStatus = 'online' | 'away' | 'busy' | 'dnd' | 'offline';
function contactStatusToPresence(status: string | undefined): ContactPresence {
switch (status) {
case 'available':
return { status: 'online', statusText: 'Available' };
case 'away':
return { status: 'away', statusText: 'Away' };
case 'busy':
return { status: 'busy', statusText: 'Busy' };
case 'doNotDisturb':
return { status: 'dnd', statusText: 'DND' };
default:
return { status: 'offline', statusText: 'Offline' };
}
}
export class VslsController implements Disposable {
private _disposable: Disposable | undefined;
private _guest: VslsGuestService | undefined;
private _host: VslsHostService | undefined;
private _onReady: (() => void) | undefined;
private _waitForReady: Promise<void> | undefined;
private _api: Promise<LiveShare | undefined> | undefined;
constructor() {
void this.initialize();
}
dispose() {
this._disposable?.dispose();
this._host?.dispose();
this._guest?.dispose();
}
private async initialize() {
try {
// If we have a vsls: workspace open, we might be a guest, so wait until live share transitions into a mode
if (workspace.workspaceFolders?.some(f => f.uri.scheme === DocumentSchemes.Vsls)) {
this.setReadonly(true);
this._waitForReady = new Promise(resolve => (this._onReady = resolve));
}
this._api = this.getLiveShareApi();
const api = await this._api;
if (api == null) {
void setContext(ContextKeys.Vsls, false);
// Tear it down if we can't talk to live share
if (this._onReady !== undefined) {
this._onReady();
this._waitForReady = undefined;
}
return;
}
void setContext(ContextKeys.Vsls, true);
this._disposable = Disposable.from(
api.onDidChangeSession(e => this.onLiveShareSessionChanged(api, e), this),
);
} catch (ex) {
Logger.error(ex);
}
}
private async getLiveShareApi(): Promise<LiveShare | undefined> {
try {
const extension = extensions.getExtension<LiveShareExtension>('ms-vsliveshare.vsliveshare');
if (extension != null) {
const liveshareExtension = extension.isActive ? extension.exports : await extension.activate();
return (await liveshareExtension.getApi('1.0.3015')) ?? undefined;
}
} catch {}
return undefined;
}
get isMaybeGuest() {
return this._guest !== undefined || this._waitForReady !== undefined;
}
private _readonly: boolean = false;
get readonly() {
return this._readonly;
}
private setReadonly(value: boolean) {
this._readonly = value;
void setContext(ContextKeys.Readonly, value ? true : undefined);
}
@debug()
async getContact(email: string | undefined) {
if (email === undefined) return undefined;
const api = await this._api;
if (api == null) return undefined;
const contacts = await api.getContacts([email]);
return contacts.contacts[email];
}
@debug({
args: {
0: (emails: string[]) => `length=${emails.length}`,
},
})
private async getContacts(emails: string[]) {
const api = await this._api;
if (api == null) return undefined;
const contacts = await api.getContacts(emails);
return Object.values(contacts.contacts);
}
@debug()
async getContactPresence(email: string | undefined): Promise<ContactPresence | undefined> {
const contact = await this.getContact(email);
if (contact == null) return undefined;
return contactStatusToPresence(contact.status);
}
@debug({
args: {
0: (emails: string[]) => `length=${emails.length}`,
},
})
async getContactsPresence(emails: string[]): Promise<Map<string, ContactPresence> | undefined> {
const contacts = await this.getContacts(emails);
if (contacts == null) return undefined;
return new Map<string, ContactPresence>(
Object.values(contacts).map(c => [c.email, contactStatusToPresence(c.status)]),
);
}
@debug()
@timeout(250)
maybeGetPresence(email: string | undefined): Promise<ContactPresence | undefined> {
return Container.vsls.getContactPresence(email);
}
async invite(email: string | undefined) {
if (email == null) return undefined;
const contact = await this.getContact(email);
if (contact == null) return undefined;
return contact.invite();
}
async startSession() {
const api = await this._api;
if (api == null) return undefined;
return api.share();
}
async guest() {
if (this._waitForReady !== undefined) {
await this._waitForReady;
this._waitForReady = undefined;
}
return this._guest;
}
host() {
return this._host;
}
private async onLiveShareSessionChanged(api: LiveShare, e: SessionChangeEvent) {
this._host?.dispose();
this._guest?.dispose();
switch (e.session.role) {
case 1 /*Role.Host*/:
this.setReadonly(false);
void setContext(ContextKeys.Vsls, 'host');
if (Container.config.liveshare.allowGuestAccess) {
this._host = await VslsHostService.share(api);
}
break;
case 2 /*Role.Guest*/:
this.setReadonly(true);
void setContext(ContextKeys.Vsls, 'guest');
this._guest = await VslsGuestService.connect(api);
break;
default:
this.setReadonly(false);
void setContext(ContextKeys.Vsls, true);
break;
}
if (this._onReady !== undefined) {
this._onReady();
this._onReady = undefined;
}
}
}