Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions src/resources/universes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { HttpClient } from "../http";
import { ListOptions } from "../types";
import { ListOptions, UpdateUserRestrictionOptions } from "../types";
import {
GameJoinRestriction,
PublishMessageBody,
SpeechAssetBody,
SpeechAssetOperation,
Expand Down Expand Up @@ -156,33 +155,47 @@ 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.universeId - The universe 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('1210019099', {
* universeId: '1234',
* placeId: '5678',
* body: {
* active: true,
* duration: "3s",
* privateReason: "some private reason",
* displayReason: "some display reason",
* excludeAltAccounts: true
* }
* });
* ```
*
* @see https://create.roblox.com/docs/cloud/reference/UserRestriction#Cloud_UpdateUserRestriction__Using_Universes_Places
*/
async updateUserRestriction(
universeId: string,
userRestrictionId: string,
Comment thread
marinofranz marked this conversation as resolved.
Outdated
body: GameJoinRestriction,
options: UpdateUserRestrictionOptions,
): Promise<UserRestriction> {
const searchParams = new URLSearchParams();
const { body, universeId, placeId } = options;
const payload = { ...body };
Comment thread
marinofranz marked this conversation as resolved.
Outdated

// API returns 400 if duration is set as an empty string
if (payload.duration == "") {
console.log(payload);
payload.duration = undefined;
console.log(payload);
}
Comment thread
marinofranz marked this conversation as resolved.
Outdated

const searchParams = new URLSearchParams();
searchParams.set("updateMask", "game_join_restriction");

const idempotencyKey = generateIdempotencyKey();
Expand All @@ -191,14 +204,20 @@ export class Universes {
searchParams.set("idempotencyKey.key", idempotencyKey);
searchParams.set("idempotencyKey.firstSent", firstSent);

return this.http.request<UserRestriction>(
`/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}`;
Comment thread
marinofranz marked this conversation as resolved.

return this.http.request<UserRestriction>(resourcePath.toString(), {
Comment thread
marinofranz marked this conversation as resolved.
Outdated
method: "PATCH",
body: JSON.stringify({ gameJoinRestriction: payload }),
searchParams,
});
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/types/universes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export interface SpeechAssetResponse extends SpeechAssetOperation {
};
}

export interface UpdateUserRestrictionOptions {
universeId: string;
placeId?: string;
body: GameJoinRestriction;
}

export interface GameJoinRestriction {
active: boolean;
duration?: string;
Expand Down
14 changes: 8 additions & 6 deletions test/universes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ describe("Universes", () => {
user: "users/111111111",
gameJoinRestriction: {
active: true,
duration: "7200s",
privateReason: "Updated reason",
displayReason: "Updated display reason",
excludeAltAccounts: false,
Expand All @@ -287,25 +286,28 @@ describe("Universes", () => {

const body: GameJoinRestriction = {
active: true,
duration: "7200s",
duration: "",
privateReason: "Updated reason",
displayReason: "Updated display reason",
excludeAltAccounts: false,
};

const result = await openCloud.universes.updateUserRestriction(
"123456789",
"111111111",
body,
{
universeId: "123456789",
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=");
Expand Down