diff --git a/lib/Mangrove-reviews.ts b/lib/Mangrove-reviews.ts index beb10ca..6565cbb 100644 --- a/lib/Mangrove-reviews.ts +++ b/lib/Mangrove-reviews.ts @@ -77,8 +77,7 @@ export class MangroveReviews { * @param {Payload} payload - Base {@link Payload} to be cleaned, it will be mutated. * @returns {string} Mangrove Review encoded as JWT. */ - public static async signReview(keypair: CryptoKeyPair, payload: Review): Promise { - payload = MangroveReviews.cleanPayload(payload) + public static async signReview(keypair: CryptoKeyPair, payload: Readonly): Promise { const algo = 'ES256' const kid = await MangroveReviews.publicToPem(keypair.publicKey) const jwk = JSON.stringify(await crypto.subtle.exportKey('jwk', keypair.publicKey)) @@ -105,10 +104,22 @@ export class MangroveReviews { /** * Composition of `signReview` and `submitReview`. * @param keypair WebCrypto keypair, can be generated with `generateKeypair`. - * @param {Payload} payload Base {@link Payload} to be cleaned, it will be mutated. + * @param payload Base {@link Payload} to be cleaned, it will be mutated. * @param {string} [api=ORIGINAL_API] - API endpoint used to fetch the data. */ static async signAndSubmitReview(keypair: CryptoKeyPair, payload: Review, api: string = MangroveReviews.ORIGINAL_API) { + payload = MangroveReviews.cleanPayload(payload) + return await this.signSubmitDirectly(keypair, payload, api) + } + + /** + * Does not clean, used for e.g. deletion + * @param keypair + * @param payload + * @param api + * @private + */ + private static async signSubmitDirectly(keypair: CryptoKeyPair, payload: Review, api: string = MangroveReviews.ORIGINAL_API) { const jwt = await MangroveReviews.signReview(keypair, payload) return MangroveReviews.submitReview(jwt, api) } @@ -334,6 +345,83 @@ export class MangroveReviews { return payload } + /** + * Report abuse for an existing review. + * @param {Keypair} keypair WebCrypto keypair can be any valid keypair. + * @param {string} reviewSignature Signature of the review to report. + * @param {string} [reason] Optional reason for the abuse report. + * @param {string} [api=ORIGINAL_API] API endpoint used for the submission. + * @returns {Promise} Result of the submission. + */ + static async reportAbuseReview( + keypair: CryptoKeyPair, + reviewSignature: string | { signature: string }, + reason: string | undefined = undefined, + api = this.ORIGINAL_API + ) { + if (typeof reviewSignature !== "string") { + reviewSignature = reviewSignature.signature + } + const payload = { + sub: `urn:maresi:${reviewSignature}`, + action: 'report_abuse', + reason + } + + return this.signAndSubmitReview(keypair, payload, api) + } + + /** + * Edit an existing review with updated content. + * @param {Keypair} keypair WebCrypto keypair must be the same keypair used for the original review. + * @param {string} reviewSignature Signature of the review to edit. + * @param {Object} updates Object containing fields to update (rating, opinion, images, metadata). + * @param {string} [api=ORIGINAL_API] API endpoint used for the submission. + * @returns {Promise} Result of the submission. + */ + static async editReview( + keypair: CryptoKeyPair, + reviewSignature: string | { signature: string }, + updates: Partial>, + api = this.ORIGINAL_API + ) { + if(updates["sub"]){ + throw "Invalid: when changing a review, the sub may not be embedded" + } + if (typeof reviewSignature !== "string") { + reviewSignature = reviewSignature.signature + } + const payload = { + sub: `urn:maresi:${reviewSignature}`, + action: 'edit', + ...updates + } + return this.signSubmitDirectly(keypair, payload, api) + } + + /** + * Delete an existing review. + * @param {Keypair} keypair WebCrypto keypair must be the same keypair used for the original review. + * @param {string} reviewSignature Signature of the review to delete. + * @param {string} [api=ORIGINAL_API] API endpoint used for the submission. + * @returns {Promise} Result of the submission. + */ + public static async deleteReview(keypair: CryptoKeyPair, + reviewSignature: string | { signature: string }, api = this.ORIGINAL_API) { + if (typeof reviewSignature !== "string") { + reviewSignature = reviewSignature.signature + } + const payload = { + sub: `urn:maresi:${reviewSignature}`, + action: 'delete', + iat : Math.floor(Date.now() / 1000) + } + + return this.signSubmitDirectly(keypair, payload, api) + } + + + } diff --git a/package.json b/package.json index 83fcb25..810971e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mangrove-reviews-typescript", - "version": "1.3.1", + "version": "1.4.6", "description": "A library to interface with Mangrove.reviews", "main": "dist/index.js", "types": "dist/index.d.ts",