Split sort event

The sort event bundled networks and channels for no reason at all.
They share none of the actual logic, so combining them just makes
the typing poor but serves no benefit.
This commit is contained in:
Reto Brunner 2024-03-24 16:39:26 +01:00
commit 0067c30273
5 changed files with 72 additions and 84 deletions

View file

@ -22,10 +22,8 @@ import ClientManager from "./clientManager";
import {MessageStorage} from "./plugins/messageStorage/types";
import {StorageCleaner} from "./storageCleaner";
import {SearchQuery, SearchResponse} from "../shared/types/storage";
import {ChanType} from "../shared/types/chan";
type OrderItem = Chan["id"] | Network["uuid"];
type Order = OrderItem[];
import {SharedChan, ChanType} from "../shared/types/chan";
import {SharedNetwork} from "../shared/types/network";
const events = [
"away",
@ -692,56 +690,39 @@ class Client {
this.emit("open", targetNetChan.chan.id);
}
sort(data: {order: Order; type: "networks" | "channels"; target: string}) {
const order = data.order;
sortChannels(netid: SharedNetwork["uuid"], order: SharedChan["id"][]) {
const network = _.find(this.networks, {uuid: netid});
if (!_.isArray(order)) {
if (!network) {
return;
}
switch (data.type) {
case "networks":
this.networks.sort((a, b) => order.indexOf(a.uuid) - order.indexOf(b.uuid));
// Sync order to connected clients
this.emit("sync_sort", {
order: this.networks.map((obj) => obj.uuid),
type: data.type,
});
break;
case "channels": {
const network = _.find(this.networks, {uuid: data.target});
if (!network) {
return;
}
network.channels.sort((a, b) => {
// Always sort lobby to the top regardless of what the client has sent
// Because there's a lot of code that presumes channels[0] is the lobby
if (a.type === ChanType.LOBBY) {
return -1;
} else if (b.type === ChanType.LOBBY) {
return 1;
}
return order.indexOf(a.id) - order.indexOf(b.id);
});
// Sync order to connected clients
this.emit("sync_sort", {
order: network.channels.map((obj) => obj.id),
type: data.type,
target: network.uuid,
});
break;
network.channels.sort((a, b) => {
// Always sort lobby to the top regardless of what the client has sent
// Because there's a lot of code that presumes channels[0] is the lobby
if (a.type === ChanType.LOBBY) {
return -1;
} else if (b.type === ChanType.LOBBY) {
return 1;
}
}
return order.indexOf(a.id) - order.indexOf(b.id);
});
this.save();
// Sync order to connected clients
this.emit("sync_sort:channels", {
target: network.uuid,
order: network.channels.map((obj) => obj.id),
});
}
sortNetworks(order: SharedNetwork["uuid"][]) {
this.networks.sort((a, b) => order.indexOf(a.uuid) - order.indexOf(b.uuid));
this.save();
// Sync order to connected clients
this.emit("sync_sort:networks", {
order: this.networks.map((obj) => obj.uuid),
});
}
names(data: {target: number}) {

View file

@ -569,10 +569,28 @@ function initializeClient(
client.open(socket.id, data);
});
socket.on("sort", (data) => {
if (_.isPlainObject(data)) {
client.sort(data);
socket.on("sort:networks", (data) => {
if (!_.isPlainObject(data)) {
return;
}
if (!Array.isArray(data.order)) {
return;
}
client.sortNetworks(data.order);
});
socket.on("sort:channels", (data) => {
if (!_.isPlainObject(data)) {
return;
}
if (!Array.isArray(data.order) || typeof data.network !== "string") {
return;
}
client.sortChannels(data.network, data.order);
});
socket.on("names", (data) => {