mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-24 14:35:27 +00:00
rid of forEachAsync
This commit is contained in:
@@ -244,24 +244,28 @@ router.get(
|
||||
return x;
|
||||
});
|
||||
|
||||
await ret
|
||||
.filter((x: MessageCreateSchema) => x.interaction_metadata && !x.interaction_metadata.user)
|
||||
.forEachAsync(async (x: MessageCreateSchema) => {
|
||||
x.interaction_metadata!.user = x.interaction!.user = await User.findOneOrFail({ where: { id: (x as Message).interaction_metadata!.user_id } });
|
||||
});
|
||||
await Promise.all(
|
||||
ret
|
||||
.filter((x: MessageCreateSchema) => x.interaction_metadata && !x.interaction_metadata.user)
|
||||
.map(async (x: MessageCreateSchema) => {
|
||||
x.interaction_metadata!.user = x.interaction!.user = await User.findOneOrFail({ where: { id: (x as Message).interaction_metadata!.user_id } });
|
||||
}),
|
||||
);
|
||||
|
||||
// polyfill message references for old messages
|
||||
await ret
|
||||
.filter((msg) => msg.message_reference && !msg.referenced_message?.id)
|
||||
.forEachAsync(async (msg) => {
|
||||
const whereOptions: { id: string; guild_id?: string; channel_id?: string } = {
|
||||
id: msg.message_reference!.message_id,
|
||||
};
|
||||
if (msg.message_reference!.guild_id) whereOptions.guild_id = msg.message_reference!.guild_id;
|
||||
if (msg.message_reference!.channel_id) whereOptions.channel_id = msg.message_reference!.channel_id;
|
||||
await Promise.all(
|
||||
ret
|
||||
.filter((msg) => msg.message_reference && !msg.referenced_message?.id)
|
||||
.map(async (msg) => {
|
||||
const whereOptions: { id: string; guild_id?: string; channel_id?: string } = {
|
||||
id: msg.message_reference!.message_id,
|
||||
};
|
||||
if (msg.message_reference!.guild_id) whereOptions.guild_id = msg.message_reference!.guild_id;
|
||||
if (msg.message_reference!.channel_id) whereOptions.channel_id = msg.message_reference!.channel_id;
|
||||
|
||||
msg.referenced_message = await Message.findOne({ where: whereOptions, relations: ["author", "mentions", "mention_roles", "mention_channels"] });
|
||||
});
|
||||
msg.referenced_message = await Message.findOne({ where: whereOptions, relations: ["author", "mentions", "mention_roles", "mention_channels"] });
|
||||
}),
|
||||
);
|
||||
|
||||
return res.json(ret);
|
||||
},
|
||||
|
||||
@@ -40,11 +40,13 @@ router.get(
|
||||
relations: PublicInviteRelation,
|
||||
});
|
||||
|
||||
await invites
|
||||
.filter((i) => i.isExpired())
|
||||
.forEachAsync(async (i) => {
|
||||
await Invite.delete({ code: i.code });
|
||||
});
|
||||
await Promise.all(
|
||||
invites
|
||||
.filter((i) => i.isExpired())
|
||||
.map(async (i) => {
|
||||
await Invite.delete({ code: i.code });
|
||||
}),
|
||||
);
|
||||
|
||||
return res.json(invites.filter((i) => !i.isExpired()));
|
||||
},
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
declare global {
|
||||
interface Array<T> {
|
||||
partition(filter: (elem: T) => boolean): [T[], T[]];
|
||||
forEachAsync(callback: (elem: T, index: number, array: T[]) => Promise<void>): Promise<void>;
|
||||
remove(item: T): void;
|
||||
distinct(): T[];
|
||||
}
|
||||
@@ -33,10 +32,6 @@ export function arrayPartition<T>(array: T[], filter: (elem: T) => boolean): [T[
|
||||
return [pass, fail];
|
||||
}
|
||||
|
||||
export async function arrayForEachAsync<T>(array: T[], callback: (elem: T, index: number, array: T[]) => Promise<void>): Promise<void> {
|
||||
await Promise.all(array.map(callback));
|
||||
}
|
||||
|
||||
export function arrayRemove<T>(this: T[], item: T): void {
|
||||
const index = this.indexOf(item);
|
||||
if (index > -1) {
|
||||
@@ -54,11 +49,6 @@ if (!Array.prototype.partition)
|
||||
return arrayPartition(this, filter);
|
||||
};
|
||||
|
||||
if (!Array.prototype.forEachAsync)
|
||||
Array.prototype.forEachAsync = function <T>(this: T[], callback: (elem: T, index: number, array: T[]) => Promise<void>) {
|
||||
return arrayForEachAsync(this, callback);
|
||||
};
|
||||
|
||||
if (!Array.prototype.remove)
|
||||
Array.prototype.remove = function <T>(this: T[], item: T) {
|
||||
return arrayRemove.call(this, item);
|
||||
|
||||
Reference in New Issue
Block a user