Make testing for moderator state centralized in User class
This commit is contained in:
@@ -28,8 +28,25 @@ export interface SocketEvent {
|
||||
export interface ConnectedClientInfoEvent extends SocketEvent {
|
||||
user: User;
|
||||
}
|
||||
export interface ChatEvent extends SocketEvent {
|
||||
export class ChatEvent implements SocketEvent {
|
||||
constructor(message) {
|
||||
this.id = message.id;
|
||||
this.timestamp = message.timestamp;
|
||||
this.type = message.type;
|
||||
this.body = message.body;
|
||||
if (message.user) {
|
||||
this.user = new User(message.user);
|
||||
}
|
||||
}
|
||||
|
||||
timestamp: Date;
|
||||
|
||||
type: MessageType;
|
||||
|
||||
id: string;
|
||||
|
||||
user: User;
|
||||
|
||||
body: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ export const createUser = (name: string, color: number, createdAt: Date): User =
|
||||
nameChangedAt: createdAt,
|
||||
previousNames: [],
|
||||
scopes: [],
|
||||
|
||||
isModerator: () => false,
|
||||
});
|
||||
|
||||
export const spidermanUser = createUser('Spiderman', 1, new Date(2020, 1, 2));
|
||||
|
||||
@@ -1,10 +1,37 @@
|
||||
export interface User {
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
export class User {
|
||||
constructor(u) {
|
||||
this.id = u.id;
|
||||
this.displayName = u.displayName;
|
||||
this.displayColor = u.displayColor;
|
||||
this.createdAt = u.createdAt;
|
||||
this.previousNames = u.previousNames;
|
||||
this.nameChangedAt = u.nameChangedAt;
|
||||
this.scopes = u.scopes;
|
||||
this.authenticated = u.authenticated;
|
||||
}
|
||||
|
||||
id: string;
|
||||
|
||||
displayName: string;
|
||||
|
||||
displayColor: number;
|
||||
|
||||
createdAt: Date;
|
||||
|
||||
previousNames: string[];
|
||||
|
||||
nameChangedAt: Date;
|
||||
|
||||
scopes: string[];
|
||||
|
||||
authenticated: boolean;
|
||||
|
||||
public isModerator = (): boolean => {
|
||||
if (!this.scopes || this.scopes.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.scopes.includes('moderator');
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user