diff --git a/src/resources/universes.ts b/src/resources/universes.ts index c8e7a0c..ad1b7f7 100644 --- a/src/resources/universes.ts +++ b/src/resources/universes.ts @@ -1,7 +1,6 @@ import { HttpClient } from "../http"; -import { ListOptions } from "../types"; +import { ListOptions, UpdateUserRestrictionOptions } from "../types"; import { - GameJoinRestriction, PublishMessageBody, SpeechAssetBody, SpeechAssetOperation, @@ -157,20 +156,26 @@ export class Universes { * *Requires `universe.user-restriction:write` scope.* * * @param universeId - The universe ID (numeric string) - * @param userRestrictionId - The user ID (numeric string) - * @param body - The user restriction data to update + * @param options - The options for updating the user restriction + * @param options.userRestrictionId - The user ID (numeric string) + * @param options.placeId - The place ID (optional) (numeric string) + * @param options.body - The user restriction data to update * @returns Promise resolving to the user restriction response * @throws {AuthError} If API key is invalid * @throws {OpenCloudError} If an API error occurs * * @example * ```typescript - * const userRestriction = await client.universes.updateUserRestriction('123456789', '123456789', { - * active: true, - * duration: "3s", - * privateReason: "some private reason", - * displayReason: "some display reason", - * excludeAltAccounts: true + * const userRestriction = await client.universes.updateUserRestriction('123456789', { + * userRestrictionId: '1210019099', + * placeId: '5678', + * body: { + * active: true, + * duration: "3s", + * privateReason: "some private reason", + * displayReason: "some display reason", + * excludeAltAccounts: true + * } * }); * ``` * @@ -178,11 +183,12 @@ export class Universes { */ async updateUserRestriction( universeId: string, - userRestrictionId: string, - body: GameJoinRestriction, + options: UpdateUserRestrictionOptions, ): Promise { - const searchParams = new URLSearchParams(); + const { body, userRestrictionId, placeId } = options; + body.duration = body.duration === "" ? undefined : body.duration; + const searchParams = new URLSearchParams(); searchParams.set("updateMask", "game_join_restriction"); const idempotencyKey = generateIdempotencyKey(); @@ -191,14 +197,16 @@ export class Universes { searchParams.set("idempotencyKey.key", idempotencyKey); searchParams.set("idempotencyKey.firstSent", firstSent); - return this.http.request( - `/cloud/v2/universes/${universeId}/user-restrictions/${userRestrictionId}`, - { - method: "PATCH", - body: JSON.stringify({ gameJoinRestriction: body }), - searchParams, - }, - ); + let resourcePath = `/cloud/v2/universes/${universeId}`; + // This resource has two different paths, one for universe-level restrictions, and another for place-level restrictions + if (placeId) resourcePath += `/places/${placeId}`; + resourcePath += `/user-restrictions/${userRestrictionId}`; + + return this.http.request(resourcePath, { + method: "PATCH", + body: JSON.stringify({ gameJoinRestriction: body }), + searchParams, + }); } /** diff --git a/src/types/universes.ts b/src/types/universes.ts index c1e4567..6144e97 100644 --- a/src/types/universes.ts +++ b/src/types/universes.ts @@ -84,6 +84,12 @@ export interface SpeechAssetResponse extends SpeechAssetOperation { }; } +export interface UpdateUserRestrictionOptions { + userRestrictionId: string; + placeId?: string; + body: GameJoinRestriction; +} + export interface GameJoinRestriction { active: boolean; duration?: string; diff --git a/test/universes.test.ts b/test/universes.test.ts index 9ded4c1..9327c66 100644 --- a/test/universes.test.ts +++ b/test/universes.test.ts @@ -267,7 +267,6 @@ describe("Universes", () => { user: "users/111111111", gameJoinRestriction: { active: true, - duration: "7200s", privateReason: "Updated reason", displayReason: "Updated display reason", excludeAltAccounts: false, @@ -287,7 +286,7 @@ describe("Universes", () => { const body: GameJoinRestriction = { active: true, - duration: "7200s", + duration: "", privateReason: "Updated reason", displayReason: "Updated display reason", excludeAltAccounts: false, @@ -295,17 +294,20 @@ describe("Universes", () => { const result = await openCloud.universes.updateUserRestriction( "123456789", - "111111111", - body, + { + userRestrictionId: "111111111", + placeId: "987654321", + body, + }, ); expect(result.user).toBe("users/111111111"); expect(result.gameJoinRestriction.active).toBe(true); - expect(result.gameJoinRestriction.duration).toBe("7200s"); + expect(result.gameJoinRestriction.duration).toBe(undefined); const url = calls[0]?.url.toString(); expect(url).toContain( - `${baseUrl}/cloud/v2/universes/123456789/user-restrictions/111111111`, + `${baseUrl}/cloud/v2/universes/123456789/places/987654321/user-restrictions/111111111`, ); expect(url).toContain("updateMask=game_join_restriction"); expect(url).toContain("idempotencyKey.key=");