mirror of
https://github.com/spacebarchat/server.git
synced 2026-04-01 20:05:43 +00:00
remove distinct
This commit is contained in:
@@ -17,15 +17,7 @@
|
||||
*/
|
||||
|
||||
import { route } from "@spacebar/api";
|
||||
import {
|
||||
Channel,
|
||||
ChannelRecipientAddEvent,
|
||||
DiscordApiErrors,
|
||||
DmChannelDTO,
|
||||
emitEvent,
|
||||
Recipient,
|
||||
User,
|
||||
} from "@spacebar/util";
|
||||
import { Channel, ChannelRecipientAddEvent, DiscordApiErrors, DmChannelDTO, emitEvent, Recipient, User } from "@spacebar/util";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { ChannelType, PublicUserProjection } from "@spacebar/schemas";
|
||||
|
||||
@@ -47,24 +39,16 @@ router.put(
|
||||
});
|
||||
|
||||
if (channel.type !== ChannelType.GROUP_DM) {
|
||||
const recipients = [
|
||||
...(channel.recipients?.map((r) => r.user_id) || []),
|
||||
user_id,
|
||||
].distinct();
|
||||
const recipients = [...new Set([...(channel.recipients?.map((r) => r.user_id) || []), user_id])];
|
||||
|
||||
const new_channel = await Channel.createDMChannel(
|
||||
recipients,
|
||||
req.user_id,
|
||||
);
|
||||
const new_channel = await Channel.createDMChannel(recipients, req.user_id);
|
||||
return res.status(201).json(new_channel);
|
||||
} else {
|
||||
if (channel.recipients?.map((r) => r.user_id).includes(user_id)) {
|
||||
throw DiscordApiErrors.INVALID_RECIPIENT; //TODO is this the right error?
|
||||
}
|
||||
|
||||
channel.recipients?.push(
|
||||
Recipient.create({ channel_id: channel_id, user_id: user_id }),
|
||||
);
|
||||
channel.recipients?.push(Recipient.create({ channel_id: channel_id, user_id: user_id }));
|
||||
await channel.save();
|
||||
|
||||
await emitEvent({
|
||||
@@ -103,13 +87,7 @@ router.delete(
|
||||
where: { id: channel_id },
|
||||
relations: ["recipients"],
|
||||
});
|
||||
if (
|
||||
!(
|
||||
channel.type === ChannelType.GROUP_DM &&
|
||||
(channel.owner_id === req.user_id || user_id === req.user_id)
|
||||
)
|
||||
)
|
||||
throw DiscordApiErrors.MISSING_PERMISSIONS;
|
||||
if (!(channel.type === ChannelType.GROUP_DM && (channel.owner_id === req.user_id || user_id === req.user_id))) throw DiscordApiErrors.MISSING_PERMISSIONS;
|
||||
|
||||
if (!channel.recipients?.map((r) => r.user_id).includes(user_id)) {
|
||||
throw DiscordApiErrors.INVALID_RECIPIENT; //TODO is this the right error?
|
||||
|
||||
@@ -254,10 +254,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
|
||||
});
|
||||
});
|
||||
|
||||
const groups = ops
|
||||
.map((x) => x.groups)
|
||||
.flat()
|
||||
.distinct();
|
||||
const groups = [...new Set(ops.map((x) => x.groups).flat())];
|
||||
|
||||
await Send(this, {
|
||||
op: OPCODES.Dispatch,
|
||||
|
||||
@@ -255,7 +255,7 @@ export class Channel extends BaseClass {
|
||||
}
|
||||
|
||||
static async createDMChannel(recipients: string[], creator_user_id: string, name?: string) {
|
||||
recipients = recipients.distinct().filter((x) => x !== creator_user_id);
|
||||
recipients = [...new Set(recipients.distinct().filter((x) => x !== creator_user_id))];
|
||||
// TODO: check config for max number of recipients
|
||||
/** if you want to disallow note to self channels, uncomment the conditional below
|
||||
|
||||
|
||||
@@ -19,10 +19,4 @@ describe("Array extensions", () => {
|
||||
arr.remove(6);
|
||||
assert.deepEqual(arr, [1, 2, 4, 5]);
|
||||
});
|
||||
|
||||
it("distinct", () => {
|
||||
const arr = [1, 2, 2, 3, 3, 3];
|
||||
assert.deepEqual(arr.distinct(), [1, 2, 3]);
|
||||
assert.deepEqual([].distinct(), []);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,7 +20,6 @@ declare global {
|
||||
interface Array<T> {
|
||||
partition(filter: (elem: T) => boolean): [T[], T[]];
|
||||
remove(item: T): void;
|
||||
distinct(): T[];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,10 +38,6 @@ export function arrayRemove<T>(this: T[], item: T): void {
|
||||
}
|
||||
}
|
||||
|
||||
export function arrayDistinct<T>(this: T[]): T[] {
|
||||
return Array.from(new Set(this));
|
||||
}
|
||||
|
||||
// register extensions
|
||||
if (!Array.prototype.partition)
|
||||
Array.prototype.partition = function <T>(this: T[], filter: (elem: T) => boolean) {
|
||||
@@ -53,8 +48,3 @@ if (!Array.prototype.remove)
|
||||
Array.prototype.remove = function <T>(this: T[], item: T) {
|
||||
return arrayRemove.call(this, item);
|
||||
};
|
||||
|
||||
if (!Array.prototype.distinct)
|
||||
Array.prototype.distinct = function <T>(this: T[]) {
|
||||
return arrayDistinct.call(this);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user