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
parent 843db1727b
commit 0067c30273
5 changed files with 75 additions and 87 deletions

View file

@ -309,8 +309,7 @@ export default defineComponent({
moveItemInArray(store.state.networks, oldIndex, newIndex);
socket.emit("sort", {
type: "networks",
socket.emit("sort:networks", {
order: store.state.networks.map((n) => n.uuid),
});
};
@ -341,9 +340,8 @@ export default defineComponent({
moveItemInArray(netChan.network.channels, oldIndex, newIndex);
socket.emit("sort", {
type: "channels",
target: netChan.network.uuid,
socket.emit("sort:channel", {
network: netChan.network.uuid,
order: netChan.network.channels.map((c) => c.id),
});
};

View file

@ -1,30 +1,16 @@
import socket from "../socket";
import {store} from "../store";
socket.on("sync_sort", function (data) {
const order = data.order;
switch (data.type) {
case "networks":
store.commit(
"sortNetworks",
(a, b) => (order as string[]).indexOf(a.uuid) - (order as string[]).indexOf(b.uuid)
);
break;
case "channels": {
const network = store.getters.findNetwork(data.target);
if (!network) {
return;
}
network.channels.sort(
(a, b) => (order as number[]).indexOf(a.id) - (order as number[]).indexOf(b.id)
);
break;
}
}
socket.on("sync_sort:networks", function (data) {
store.commit("sortNetworks", (a, b) => data.order.indexOf(a.uuid) - data.order.indexOf(b.uuid));
});
socket.on("sync_sort:channels", function (data) {
const network = store.getters.findNetwork(data.network);
if (!network) {
return;
}
network.channels.sort((a, b) => data.order.indexOf(a.id) - data.order.indexOf(b.id));
});

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) => {

View file

@ -1,5 +1,5 @@
import {SharedMention} from "./mention";
import {ChanState, InitClientChan} from "./chan";
import {ChanState, InitClientChan, SharedChan} from "./chan";
import {SharedNetwork} from "./network";
import {SharedMsg, ClientMessage, LinkPreview} from "./msg";
import {SharedUser} from "./user";
@ -67,10 +67,11 @@ interface ServerToClientEvents {
"sign-out": NoPayloadEventHandler;
sync_sort: EventHandler<
| {type: "networks"; order: string[]; target: string}
| {type: "channels"; order: number[]; target: string}
>;
"sync_sort:networks": EventHandler<{order: SharedNetwork["uuid"][]}>;
"sync_sort:channels": EventHandler<{
network: SharedNetwork["uuid"];
order: SharedChan["id"][];
}>;
topic: EventHandler<{chan: number; topic: string}>;
@ -134,7 +135,11 @@ interface ClientToServerEvents {
"sessions:get": NoPayloadEventHandler;
sort: EventHandler<{type: string; order: any; target?: string}>;
"sort:networks": EventHandler<{order: SharedNetwork["uuid"][]}>;
"sort:channels": EventHandler<{
network: SharedNetwork["uuid"];
order: SharedChan["id"][];
}>;
"mentions:dismiss": (msgId: number) => void;
"mentions:dismiss_all": NoPayloadEventHandler;