Skip to content

Commit

Permalink
fix: skip empty admin notifications (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Mar 3, 2021
1 parent 32d3d44 commit 2a4fddc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/discord/services/admin-notifications.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ describe('AdminNotificationsService', () => {

events.playerUpdates.next({ oldPlayer: player, newPlayer: { ...player, name: 'NEW_PLAYER_NAME' }, adminId: admin.id });
}));

describe('when the update doesn\'t change anything', () => {
it('should not send any messages', async () => new Promise<void>(resolve => {
events.playerUpdates.next({ oldPlayer: player, newPlayer: player, adminId: admin.id });
setTimeout(() => {
expect(sendSpy).not.toHaveBeenCalled();
resolve();
}, 1000);
}));
});
});

describe('when the playerBanAdded event emits', () => {
Expand Down Expand Up @@ -170,5 +180,16 @@ describe('AdminNotificationsService', () => {
const newSkill = new Map([[Tf2ClassName.soldier, 4]]);
events.playerSkillChanged.next({ playerId: player.id, oldSkill, newSkill, adminId: admin.id });
}));

describe('when the skill doesn\'t really change', () => {
it('should not send any message', async () => new Promise<void>(resolve => {
const oldSkill = new Map([[Tf2ClassName.soldier, 2]]);
events.playerSkillChanged.next({ playerId: player.id, oldSkill, newSkill: oldSkill, adminId: admin.id });
setTimeout(() => {
expect(sendSpy).not.toHaveBeenCalled();
resolve();
}, 1000);
}));
});
});
});
23 changes: 23 additions & 0 deletions src/discord/services/admin-notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import { isRefType } from '@typegoose/typegoose';
import { newPlayer, playerBanAdded, playerBanRevoked, playerSkillChanged, playerProfileUpdated } from '../notifications';
import { DiscordService } from './discord.service';

const playerSkillEqual = (oldSkill: PlayerSkillType, newSkill: PlayerSkillType) => {
if (oldSkill.size !== newSkill.size) {
return false;
}

for (const [key, value] of oldSkill) {
const currentSkill = newSkill.get(key);
if (currentSkill !== value || (currentSkill === undefined && !newSkill.has(key))) {
return false;
}
}

return true;
}

@Injectable()
export class AdminNotificationsService implements OnModuleInit {

Expand Down Expand Up @@ -53,6 +68,10 @@ export class AdminNotificationsService implements OnModuleInit {
changes.role = { old: oldPlayer.role, new: newPlayer.role };
}

if (Object.keys(changes).length === 0) {
return; // skip empty notification
}

this.discordService.getAdminsChannel()?.send({
embed: playerProfileUpdated({
player: {
Expand Down Expand Up @@ -130,6 +149,10 @@ export class AdminNotificationsService implements OnModuleInit {
return;
}

if (playerSkillEqual(oldSkill, newSkill)) {
return;
}

const player = await this.playersService.getById(playerId);
const admin = await this.playersService.getById(adminId);

Expand Down

0 comments on commit 2a4fddc

Please sign in to comment.