UNPKG

@wepublish/api

Version:
104 lines 5.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updatePublicComment = exports.addPublicComment = void 0; const tslib_1 = require("tslib"); const client_1 = require("@prisma/client"); const api_1 = require("../../../../settings-api/src"); const error_1 = require("../../error"); const utility_1 = require("../../utility"); const api_2 = require("../../../../permissions-api/src"); const addPublicComment = (input, optionalAuthenticateUser, challenge, settingsClient, commentClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; const user = optionalAuthenticateUser(); let authorType = client_1.CommentAuthorType.verifiedUser; const commentLength = (0, utility_1.countRichtextChars)(0, input.text); const maxCommentLength = (_a = (yield settingsClient.findUnique({ where: { name: api_1.SettingName.COMMENT_CHAR_LIMIT } }))) === null || _a === void 0 ? void 0 : _a.value; if (commentLength > maxCommentLength) { throw new error_1.CommentLengthError(+maxCommentLength); } const canSkipApproval = (0, api_2.hasPermission)(api_2.CanCreateApprovedComment, (_b = user === null || user === void 0 ? void 0 : user.roles) !== null && _b !== void 0 ? _b : []); // Challenge if (!user) { authorType = client_1.CommentAuthorType.guestUser; const guestCanCommentSetting = yield settingsClient.findUnique({ where: { name: api_1.SettingName.ALLOW_GUEST_COMMENTING } }); const guestCanComment = (_c = guestCanCommentSetting === null || guestCanCommentSetting === void 0 ? void 0 : guestCanCommentSetting.value) !== null && _c !== void 0 ? _c : process.env.ENABLE_ANONYMOUS_COMMENTS === 'true'; if (!guestCanComment) { throw new error_1.AnonymousCommentsDisabledError(); } if (!input.guestUsername) throw new error_1.AnonymousCommentError(); if (!input.challenge) throw new error_1.ChallengeMissingCommentError(); const challengeValidationResult = yield challenge.validateChallenge({ challengeID: input.challenge.challengeID, solution: input.challenge.challengeSolution }); if (!challengeValidationResult.valid) throw new error_1.CommentAuthenticationError(challengeValidationResult.message); } if (input.itemType === client_1.CommentItemType.peerArticle && !input.peerId) { throw new error_1.PeerIdMissingCommentError(); } // Cleanup const { challenge: _, title, text } = input, commentInput = tslib_1.__rest(input, ["challenge", "title", "text"]); const comment = yield commentClient.create({ data: Object.assign(Object.assign({}, commentInput), { revisions: { create: { text, title } }, userID: user === null || user === void 0 ? void 0 : user.user.id, authorType, state: canSkipApproval ? client_1.CommentState.approved : client_1.CommentState.pendingApproval }), include: { revisions: { orderBy: { createdAt: 'asc' } }, overriddenRatings: true } }); return Object.assign(Object.assign({}, comment), { title, text }); }); exports.addPublicComment = addPublicComment; const updatePublicComment = (input, authenticateUser, commentClient, settingsClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _d, _e, _f, _g, _h, _j; const { user, roles } = authenticateUser(); const canSkipApproval = (0, api_2.hasPermission)(api_2.CanCreateApprovedComment, roles); const comment = yield commentClient.findUnique({ where: { id: input.id }, include: { revisions: { orderBy: { createdAt: 'asc' } } } }); if (!comment) { throw new error_1.NotFound('comment', input.id); } if (user.id !== (comment === null || comment === void 0 ? void 0 : comment.userID)) { throw new error_1.NotAuthorisedError(); } const commentEditingSetting = yield settingsClient.findUnique({ where: { name: api_1.SettingName.ALLOW_COMMENT_EDITING } }); if (!(commentEditingSetting === null || commentEditingSetting === void 0 ? void 0 : commentEditingSetting.value) && comment.state !== client_1.CommentState.pendingUserChanges) { throw new error_1.UserInputError('Comment state must be pending user changes'); } const { id, text, title, lead } = input; const updatedComment = yield commentClient.update({ where: { id }, data: { revisions: { create: { text: text !== null && text !== void 0 ? text : (_d = comment === null || comment === void 0 ? void 0 : comment.revisions.at(-1)) === null || _d === void 0 ? void 0 : _d.text, title: title !== null && title !== void 0 ? title : (_e = comment === null || comment === void 0 ? void 0 : comment.revisions.at(-1)) === null || _e === void 0 ? void 0 : _e.title, lead: lead !== null && lead !== void 0 ? lead : (_f = comment === null || comment === void 0 ? void 0 : comment.revisions.at(-1)) === null || _f === void 0 ? void 0 : _f.lead } }, state: canSkipApproval ? client_1.CommentState.approved : client_1.CommentState.pendingApproval }, include: { revisions: { orderBy: { createdAt: 'asc' } }, overriddenRatings: true } }); return Object.assign(Object.assign({}, updatedComment), { text: (_g = updatedComment.revisions.at(-1)) === null || _g === void 0 ? void 0 : _g.text, title: (_h = updatedComment.revisions.at(-1)) === null || _h === void 0 ? void 0 : _h.title, lead: (_j = updatedComment.revisions.at(-1)) === null || _j === void 0 ? void 0 : _j.lead }); }); exports.updatePublicComment = updatePublicComment; //# sourceMappingURL=comment.public-mutation.js.map