-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get rid of playerbans collection (#111)
- Loading branch information
1 parent
2f85d55
commit 85129b1
Showing
14 changed files
with
152 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { ObjectId } from 'mongodb' | ||
import { database } from '../database/database' | ||
import { collections } from '../database/collections' | ||
import { logger } from '../logger' | ||
|
||
interface PlayerBanModel { | ||
player: ObjectId | ||
admin: ObjectId | ||
start: Date | ||
end: Date | ||
reason: string | ||
} | ||
|
||
export async function up() { | ||
const collection = database.collection<PlayerBanModel>('playerbans') | ||
const bans = await collection.find().toArray() | ||
for (const ban of bans) { | ||
const player = await collections.players.findOne({ _id: ban.player }) | ||
if (player === null) { | ||
logger.warn(`actor ${ban.player.toString()} not found`) | ||
continue | ||
} | ||
|
||
const actor = await collections.players.findOne({ _id: ban.admin }) | ||
if (actor === null) { | ||
logger.warn(`actor ${ban.admin.toString()} not found; was this bot?`) | ||
} | ||
|
||
await collections.players.updateOne( | ||
{ steamId: player.steamId }, | ||
{ | ||
$push: { | ||
bans: { | ||
actor: actor?.steamId ?? 'bot', | ||
start: ban.start, | ||
end: ban.end, | ||
reason: ban.reason, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
await collection.deleteOne({ _id: ban._id }) | ||
} | ||
|
||
await collection.drop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
import { collections } from '../database/collections' | ||
import type { PlayerBanModel } from '../database/models/player-ban.model' | ||
import type { PlayerBan } from '../database/models/player.model' | ||
import { events } from '../events' | ||
import type { SteamId64 } from '../shared/types/steam-id-64' | ||
import { PlayerNotFoundError } from './errors' | ||
import { update } from './update' | ||
|
||
export async function addBan(props: { | ||
player: SteamId64 | ||
admin: SteamId64 | ||
end: Date | ||
reason: string | ||
}): Promise<PlayerBanModel> { | ||
const player = await collections.players.findOne({ steamId: props.player }) | ||
if (!player) { | ||
throw new PlayerNotFoundError(props.player) | ||
} | ||
|
||
}): Promise<PlayerBan> { | ||
const admin = await collections.players.findOne({ steamId: props.admin }) | ||
if (!admin) { | ||
throw new PlayerNotFoundError(props.admin) | ||
} | ||
|
||
const { insertedId } = await collections.playerBans.insertOne({ | ||
player: player._id, | ||
admin: admin._id, | ||
const ban: PlayerBan = { | ||
actor: admin.steamId, | ||
start: new Date(), | ||
end: props.end, | ||
reason: props.reason, | ||
}) | ||
const ban = (await collections.playerBans.findOne({ _id: insertedId }))! | ||
events.emit('player/ban:added', { ban }) | ||
} | ||
|
||
await update(props.player, { $push: { bans: ban } }) | ||
events.emit('player/ban:added', { player: props.player, ban }) | ||
return ban | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
import type { ObjectId } from 'mongodb' | ||
import { collections } from '../database/collections' | ||
import type { SteamId64 } from '../shared/types/steam-id-64' | ||
import { events } from '../events' | ||
import { update } from './update' | ||
import type { PlayerBan } from '../database/models/player.model' | ||
|
||
export async function revokeBan(banId: ObjectId, adminId: SteamId64) { | ||
let ban = await collections.playerBans.findOne({ _id: banId }) | ||
if (!ban) { | ||
throw new Error(`ban not found: ${banId}`) | ||
} | ||
export async function revokeBan(props: { | ||
player: SteamId64 | ||
banStart: Date | ||
admin: SteamId64 | ||
}): Promise<PlayerBan> { | ||
const after = await update( | ||
props.player, | ||
{ | ||
$set: { | ||
'bans.$[ban].end': new Date(), | ||
}, | ||
}, | ||
{ | ||
arrayFilters: [{ 'ban.start': { $eq: props.banStart } }], | ||
}, | ||
) | ||
|
||
if (ban.end < new Date()) { | ||
throw new Error(`ban already expired: ${banId}`) | ||
const ban = after.bans?.find(b => b.start.getTime() === props.banStart.getTime()) | ||
if (!ban) { | ||
throw new Error(`ban not found`) | ||
} | ||
|
||
const after = (await collections.playerBans.findOneAndUpdate( | ||
{ _id: banId }, | ||
{ $set: { end: new Date() } }, | ||
{ | ||
returnDocument: 'after', | ||
}, | ||
))! | ||
events.emit('player/ban:revoked', { ban: after, admin: adminId }) | ||
return after | ||
events.emit('player/ban:revoked', { player: after.steamId, ban, admin: props.admin }) | ||
return ban | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.