import { accessToken } from './auth';
import { DEVICE_NAME, deviceId } from './device';
export type Me = {
username: string;
defaultJitsiUrl: string;
ntfyServer?: string;
ntfyTopic?: string;
};
export type Contact = { username: string; online: boolean };
export type Invite = { from: string; meetingId: string; jitsiUrl: string; createdAt: string };
* `notified`: the callee's app was closed, so they got a notification instead.
* `joined`: they were calling you at the same time, so this is their call,
* already answered: go straight in.
*/
export type Call = { meetingId: string; jitsiUrl: string; notified?: boolean; joined?: boolean };
export type CallStatus = {
peer: string;
peerSeenMsAgo: number | null;
endedBy: string;
* The other side hung up or declined. In a call to yourself, endedBy is
* you either way, so this says which side it was.
*/
peerEnded?: boolean;
answeredOn?: string;
* Set when the call has moved to another of your devices (its name): mute
* now, and leave the room (without hanging up) once `movedJoined`.
*/
movedTo?: string;
movedJoined?: boolean;
};
export type ActiveCall = { meetingId: string; peer: string; jitsiUrl: string; device: string };
export class Api {
constructor(private serverUrl: string) {}
private async request<T>(path: string, init: RequestInit = {}, retried = false): Promise<T> {
const token = await accessToken(retried);
const res = await fetch(this.serverUrl + path, {
...init,
headers: {
...(init.body ? { 'Content-Type': 'application/json' } : {}),
Authorization: `Bearer ${token}`,
},
});
if (res.status === 401 && !retried) return this.request<T>(path, init, true);
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(body.error || `HTTP ${res.status}`);
return body as T;
}
me() {
return this.request<Me>('/me');
}
async poll() {
const device = await deviceId();
const r = await this.request<{ invites?: Invite[]; calls?: ActiveCall[] }>(`/poll?device=${encodeURIComponent(device)}`);
return { invites: r.invites ?? [], calls: r.calls ?? [] };
}
async call(to: string, jitsiUrl?: string) {
const body = JSON.stringify({ to, jitsiUrl, device: await deviceId(), name: DEVICE_NAME });
return this.request<Call>('/call', { method: 'POST', body });
}
async callStatus(meetingId: string) {
const device = encodeURIComponent(await deviceId());
return this.request<CallStatus>(`/calls/${encodeURIComponent(meetingId)}?device=${device}`);
}
async heartbeat(meetingId: string, joined = false) {
const body = JSON.stringify({ device: await deviceId(), name: DEVICE_NAME, joined });
return this.request<CallStatus>(`/calls/${encodeURIComponent(meetingId)}/heartbeat`, { method: 'POST', body });
}
async move(meetingId: string) {
const body = JSON.stringify({ device: await deviceId(), name: DEVICE_NAME });
return this.request<ActiveCall>(`/calls/${encodeURIComponent(meetingId)}/move`, { method: 'POST', body });
}
async hangUp(meetingId: string) {
const body = JSON.stringify({ device: await deviceId() });
return this.request<{ ok: boolean }>(`/calls/${encodeURIComponent(meetingId)}/hangup`, { method: 'POST', body });
}
contacts() {
return this.request<{ contacts: Contact[] }>('/contacts').then(r => r.contacts ?? []);
}
addContact(username: string) {
return this.request<Contact>('/contacts', { method: 'POST', body: JSON.stringify({ username }) });
}
removeContact(username: string) {
return this.request<{ ok: boolean }>(`/contacts/${encodeURIComponent(username)}`, { method: 'DELETE' });
}
}