@signalapp/mock-server
Version:
Mock Signal Server for writing tests
166 lines (165 loc) • 5.8 kB
JavaScript
"use strict";
// Copyright 2026 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
Object.defineProperty(exports, "__esModule", { value: true });
exports.SfuCall = exports.SfuClientStatus = void 0;
const call_1 = require("../data/call");
var SfuClientStatus;
(function (SfuClientStatus) {
SfuClientStatus["Active"] = "ACTIVE";
SfuClientStatus["Pending"] = "PENDING";
SfuClientStatus["Blocked"] = "BLOCKED";
SfuClientStatus["Rejected"] = "REJECTED";
})(SfuClientStatus || (exports.SfuClientStatus = SfuClientStatus = {}));
function toCallInfoClients(clients) {
return clients.map((client) => {
return {
demuxId: client.demuxId,
opaqueUserId: client.userId,
};
});
}
function takeClients(existing, demuxId) {
const found = existing.find((client) => client.demuxId === demuxId);
if (found == null) {
return null;
}
const { userId } = found;
const matches = [];
const remaining = [];
for (const client of existing) {
if (client.demuxId === demuxId) {
matches.push(client);
}
else {
remaining.push(client);
}
}
return { userId, matches, remaining };
}
class SfuCall extends call_1.CallData {
#eraId;
#creatorUserId;
#maxClients;
#newClientsRequireApproval;
#persistApprovalForAllUsersWhoJoin;
#activeClients = [];
#pendingClients = [];
#removedClients = [];
#blockedUsers = new Set();
#deniedUsers = new Set();
#approvedUsers;
constructor(options) {
super(options);
this.#eraId = options.eraId;
this.#creatorUserId = options.creatorUserId;
this.#maxClients = options.maxClients;
this.#newClientsRequireApproval = options.newClientsRequireApproval;
this.#persistApprovalForAllUsersWhoJoin =
options.persistApprovalForAllUsersWhoJoin;
this.#approvedUsers = new Set(options.approvedUsers);
}
get eraId() {
return this.#eraId;
}
getInfo(includePendingClients) {
const activeClients = toCallInfoClients(this.#activeClients);
const pendingClients = includePendingClients
? toCallInfoClients(this.#pendingClients)
: null;
return {
eraId: this.eraId,
maxClients: this.#maxClients,
creatorUserId: this.#creatorUserId,
activeClients,
pendingClients,
};
}
isAdmin(userId) {
return this.#activeClients.some((client) => {
return client.userId === userId && client.isAdmin;
});
}
addClient(client) {
const count = this.#activeClients.length + this.#pendingClients.length;
if (count >= this.#maxClients) {
return SfuClientStatus.Rejected;
}
if (this.#blockedUsers.has(client.userId)) {
this.#removedClients.push(client);
return SfuClientStatus.Blocked;
}
const canAutoJoin = client.isAdmin ||
!this.#newClientsRequireApproval ||
this.#approvedUsers.has(client.userId);
if (canAutoJoin) {
if (this.#persistApprovalForAllUsersWhoJoin) {
this.#approvedUsers.add(client.userId);
}
this.#activeClients.push(client);
return SfuClientStatus.Active;
}
else {
this.#pendingClients.push(client);
return SfuClientStatus.Pending;
}
}
hasClient(demuxId) {
return (this.#activeClients.some((client) => client.demuxId === demuxId) ||
this.#pendingClients.some((client) => client.demuxId === demuxId) ||
this.#removedClients.some((client) => client.demuxId === demuxId));
}
approvePendingDemuxId(demuxId) {
const result = takeClients(this.#pendingClients, demuxId);
if (result != null) {
this.#pendingClients = result.remaining;
for (const client of result.matches) {
this.#activeClients.push(client);
}
this.#deniedUsers.delete(result.userId);
this.#approvedUsers.add(result.userId);
}
}
denyPendingDemuxId(demuxId) {
const result = takeClients(this.#pendingClients, demuxId);
if (result != null) {
this.#pendingClients = result.remaining;
for (const client of result.matches) {
this.#removedClients.push(client);
}
const isDenied = this.#deniedUsers.has(result.userId);
if (isDenied) {
this.#blockedUsers.add(result.userId);
}
else {
this.#deniedUsers.add(result.userId);
}
}
}
dropDemuxId(demuxId) {
const activeClients = takeClients(this.#activeClients, demuxId);
const pendingClients = takeClients(this.#pendingClients, demuxId);
const removedClients = takeClients(this.#removedClients, demuxId);
if (activeClients != null) {
this.#activeClients = activeClients.remaining;
}
if (pendingClients != null) {
this.#pendingClients = pendingClients.remaining;
}
if (removedClients != null) {
this.#removedClients = removedClients.remaining;
}
}
blockDemuxId(demuxId) {
const result = takeClients(this.#activeClients, demuxId);
if (result != null) {
this.#activeClients = result.remaining;
for (const client of result.matches) {
this.#removedClients.push(client);
}
this.#approvedUsers.delete(result.userId);
this.#blockedUsers.add(result.userId);
}
}
}
exports.SfuCall = SfuCall;