Skip to content

Commit a1f9453

Browse files
committed
Add batch delete message event
1 parent fc9ae27 commit a1f9453

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/common/ClientEventNames.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const MESSAGE_CREATED = 'message:created';
4545
export const MESSAGE_UPDATED = 'message:updated';
4646
export const CHANNEL_TYPING = 'channel:typing';
4747
export const MESSAGE_DELETED = 'message:deleted';
48+
export const MESSAGE_DELETED_BATCH = 'message:deleted_batch';
4849
export const MESSAGE_REACTION_ADDED = 'message:reaction_added';
4950
export const MESSAGE_REACTION_REMOVED = 'message:reaction_removed';
5051

src/emits/Server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MESSAGE_CREATED, MESSAGE_DELETED, MESSAGE_REACTION_ADDED, MESSAGE_REACTION_REMOVED, MESSAGE_UPDATED, SERVER_CHANNEL_ORDER_UPDATED, SERVER_EMOJI_ADD, SERVER_EMOJI_REMOVE, SERVER_EMOJI_UPDATE, SERVER_JOINED, SERVER_LEFT, SERVER_MEMBER_JOINED, SERVER_MEMBER_LEFT, SERVER_MEMBER_UPDATED, SERVER_ORDER_UPDATED, SERVER_ROLE_CREATED, SERVER_ROLE_DELETED, SERVER_ROLE_ORDER_UPDATED, SERVER_ROLE_UPDATED, SERVER_UPDATED } from '../common/ClientEventNames';
1+
import { MESSAGE_CREATED, MESSAGE_DELETED, MESSAGE_DELETED_BATCH, MESSAGE_REACTION_ADDED, MESSAGE_REACTION_REMOVED, MESSAGE_UPDATED, SERVER_CHANNEL_ORDER_UPDATED, SERVER_EMOJI_ADD, SERVER_EMOJI_REMOVE, SERVER_EMOJI_UPDATE, SERVER_JOINED, SERVER_LEFT, SERVER_MEMBER_JOINED, SERVER_MEMBER_LEFT, SERVER_MEMBER_UPDATED, SERVER_ORDER_UPDATED, SERVER_ROLE_CREATED, SERVER_ROLE_DELETED, SERVER_ROLE_ORDER_UPDATED, SERVER_ROLE_UPDATED, SERVER_UPDATED } from '../common/ClientEventNames';
22
import { getIO } from '../socket/socket';
33
import { Presence, UserCache } from '../cache/UserCache';
44
import { UpdateServerOptions } from '../services/Server';
@@ -138,6 +138,17 @@ export const emitServerMessageDeleted = (data: { channelId: string; messageId: s
138138
io.in(data.channelId).emit(MESSAGE_DELETED, data);
139139
};
140140

141+
export const emitServerMessageDeletedBatch = (data: { userId: string, serverId: string; fromTime: Date, toTime: Date }) => {
142+
const io = getIO();
143+
144+
io.in(data.serverId).emit(MESSAGE_DELETED_BATCH, {
145+
userId: data.userId,
146+
fromTime: data.fromTime.getTime(),
147+
serverId: data.serverId,
148+
toTime: data.toTime.getTime(),
149+
});
150+
}
151+
141152
export const emitServerUpdated = (serverId: string, updated: UpdateServerOptions) => {
142153
const io = getIO();
143154

src/routes/posts/postCreate.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ async function route(req: Request, res: Response) {
7171
body.poll!.choices = body.poll!.choices.map(choice => choice.trim()).filter(choice => choice);
7272

7373
}
74-
console.log(body.poll)
7574

7675
const validateError = customExpressValidatorResult(req);
7776

src/services/Message.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChannelCache, getChannelCache } from '../cache/ChannelCache';
22
import { emitDMMessageCreated, emitDMMessageDeleted, emitDMMessageReactionAdded, emitDMMessageReactionRemoved, emitDMMessageUpdated } from '../emits/Channel';
3-
import { emitServerMessageCreated, emitServerMessageDeleted, emitServerMessageReactionAdded, emitServerMessageReactionRemoved, emitServerMessageUpdated } from '../emits/Server';
3+
import { emitServerMessageCreated, emitServerMessageDeleted, emitServerMessageDeletedBatch, emitServerMessageReactionAdded, emitServerMessageReactionRemoved, emitServerMessageUpdated } from '../emits/Server';
44
import { MessageType } from '../types/Message';
55
import { dismissChannelNotification } from './Channel';
66
import { dateToDateTime, exists, getMessageReactedUserIds, prisma, publicUserExcludeFields } from '../common/database';
@@ -190,18 +190,26 @@ export const getMessagesByChannelId = async (channelId: string, opts?: GetMessag
190190

191191
// delete messages sent in the last 7 hours
192192
export const deleteRecentMessages = async (userId: string, serverId: string) => {
193-
const date = new Date();
194-
date.setHours(date.getHours() + 7);
193+
const fromTime = new Date();
194+
const toTime = new Date();
195+
toTime.setHours(toTime.getHours() + 7);
195196

196197
await prisma.message.deleteMany({
197198
where: {
198199
createdById: userId,
199200
channel: { serverId },
200201
createdAt: {
201-
lt: dateToDateTime(date),
202+
lt: dateToDateTime(toTime),
202203
},
203204
},
204205
});
206+
207+
emitServerMessageDeletedBatch({
208+
userId,
209+
serverId,
210+
fromTime,
211+
toTime,
212+
})
205213
};
206214

207215
interface EditMessageOptions {

0 commit comments

Comments
 (0)