@trap_stevo/ventry
Version:
The universal engine for creating, tracking, and evolving interactive content — from posts, comments, and likes to offers, auctions, events, and beyond. Define, extend, and analyze content objects in real time. Turn anything into user-driven content.
120 lines (119 loc) • 2.67 kB
JavaScript
"use strict";
const {
newID
} = require("../HUDComponents/ID.cjs");
class CommentsManager {
constructor(vaultManager, metricsManager, eventsManager) {
this.metrics = metricsManager;
this.events = eventsManager;
this.vault = vaultManager;
}
addComment(itemID, ownerID, payload = {}) {
const item = this.vault.getItem(itemID);
if (!item) {
console.warn("Item not found.");
return null;
}
if (item.data.commentsEnabled !== true) {
console.warn("Comments disabled.");
return null;
}
const record = {
parentID: payload.parentID || null,
ownerID,
itemID,
id: newID(),
data: payload.data || {},
status: "active"
};
const saved = this.vault.createComment(record);
this.metrics.track("comment", 1, {
commentID: saved.id,
ownerID,
itemID
});
if (this.events) {
this.events.emit("comment.created", {
parentID: record.parentID,
commentID: saved.id,
ownerID,
itemID,
data: record.data
});
}
return {
id: saved.id
};
}
listComments(itemID, params = {}) {
const sort = params.sort === "old" ? {
timestamp: 1
} : {
timestamp: -1
};
const qb = this.vault.queryComments({
itemID,
parentID: params.parentID || null
}, sort, {
limit: params.limit || 50
});
const comments = qb.execute(true);
return {
comments,
nextCursor: undefined
};
}
reactToComment(commentID, actorID, reactionType) {
const comment = this.vault.getComment(commentID);
if (!comment) {
console.warn("Comment not found.");
return null;
}
this.metrics.track("comment.like", 1, {
itemID: comment.data.itemID,
commentID,
actorID,
reactionType
});
if (this.events) {
this.events.emit("comment.reacted", {
itemID: comment.data.itemID,
commentID,
actorID,
reactionType
});
}
return {
ok: true
};
}
reportComment(commentID, reporterID, reason, meta) {
const comment = this.vault.getComment(commentID);
if (!comment) {
console.warn("Comment not found.");
return null;
}
this.metrics.track("comment.report", 1, {
itemID: comment.data.itemID,
commentID,
reporterID,
reason
}, meta);
if (this.events) {
this.events.emit("comment.reported", {
itemID: comment.data.itemID,
reporterID,
commentID,
reason,
meta
});
}
return {
ok: true
};
}
}
;
module.exports = {
CommentsManager
};