To complete this task, you will be constructing a simulated comments section. Focus
Our attention will be on two key elements:
Users
There are three types of users in this scenario - regular users, moderators, and admins. Regular users can only add new comments and edit their own. Moderators have the ability to delete comments (to get rid of trolls), while admins can edit or delete any comment. Users can log in and out, with their last login time being tracked.
Comments
Comments consist of a message, a timestamp, and the author. Comments can also be replies, so we will keep track of the parent comment.
Below is my code:
class Admin extends Moderator {
constructor(name) {
super(name);
}
canEdit(comment) {
return true;
}
}
class Comment {
constructor(author, message, repliedTo) {
this.createdAt = new Date();
this._author = author;
this._message = message;
this.repliedTo = repliedTo || null;
}
getMessage() {
return this._message;
}
setMessage(message) {
this._message = message;
}
getCreatedAt() {
return this.createdAt;
}
getAuthor() {
return this._author;
}
getRepliedTo() {
return this.repliedTo;
}
getString(comment) {
const authorName = comment.getAuthor().getName();
if (!comment.getRepliedTo()) return authorName;
return `${comment.getMessage()} by ${authorName} (replied to ${this.getString(comment.getRepliedTo())})`;
}
toString() {
const authorName = this.getAuthor().getName();
if (!this.getRepliedTo()) {
return `${this._message} by ${authorName}`;
}
return this.getString(this);
}
}
I encountered an issue
The toString method should display the accurate hierarchy (including nested replies)