Skip to content

Commit

Permalink
feat: add shareLink command (#297)
Browse files Browse the repository at this point in the history
* chore: update schema

* feat: add shareLink command

* chore: add mock

* fix: shareLink jsdoc

* fix: share link command response

* feat: add response to schema
  • Loading branch information
afgiel authored Jan 9, 2025
1 parent 91c298c commit effd59b
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {openShareMomentDialog} from './openShareMomentDialog';
import {setActivity, SetActivity} from './setActivity';
import {setConfig} from './setConfig';
import {setOrientationLockState} from './setOrientationLockState';
import {shareLink} from './shareLink';
import {startPurchase} from './startPurchase';
import {userSettingsGetLocale} from './userSettingsGetLocale';
import {initiateImageUpload} from './initiateImageUpload';
Expand All @@ -40,6 +41,7 @@ function commands(sendCommand: TSendCommand) {
setActivity: setActivity(sendCommand),
setConfig: setConfig(sendCommand),
setOrientationLockState: setOrientationLockState(sendCommand),
shareLink: shareLink(sendCommand),
startPurchase: startPurchase(sendCommand),
userSettingsGetLocale: userSettingsGetLocale(sendCommand),
initiateImageUpload: initiateImageUpload(sendCommand),
Expand Down
12 changes: 12 additions & 0 deletions src/commands/shareLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Command} from '../generated/schemas';
import {schemaCommandFactory} from '../utils/commandFactory';

/**
* Opens a modal in the user's client to share the Activity link.
*
* @param {string} referrer_id
* @param {string} custom_id
* @param {string} message - message sent alongside link when shared.
* @returns {Promise<{success: boolean>} whether or not the user shared the link to someone
*/
export const shareLink = schemaCommandFactory(Command.SHARE_LINK);
66 changes: 66 additions & 0 deletions src/generated/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,71 @@
"required": ["participants"],
"additionalProperties": false
}
},
"SHARE_INTERACTION": {
"request": {
"type": "object",
"properties": {
"command": {"type": "string"},
"content": {"type": "string", "maxLength": 2000},
"preview_image": {
"type": "object",
"properties": {"height": {"type": "number"}, "url": {"type": "string"}, "width": {"type": "number"}},
"required": ["height", "url", "width"],
"additionalProperties": false
},
"components": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"const": 1},
"components": {
"type": "array",
"maxItems": 5,
"items": {
"type": "object",
"properties": {
"type": {"const": 2},
"style": {"type": "number", "minimum": 1, "maximum": 5},
"label": {"type": "string", "description": "Text that appears on the button", "maxLength": 80},
"custom_id": {
"type": "string",
"description": "Developer-defined identifier for the button; max 100 characters",
"maxLength": 100
}
},
"required": ["type", "style"],
"additionalProperties": false
}
}
},
"required": ["type"],
"additionalProperties": false
}
}
},
"required": ["command"],
"additionalProperties": false
},
"response": null
},
"SHARE_LINK": {
"request": {
"type": "object",
"properties": {
"referrer_id": {"type": "string", "maxLength": 64},
"custom_id": {"type": "string", "maxLength": 64},
"message": {"type": "string", "maxLength": 1000}
},
"required": ["message"],
"additionalProperties": false
},
"response": {
"type": "object",
"properties": {"success": {"type": "boolean"}},
"required": ["success"],
"additionalProperties": false
}
}
}
50 changes: 50 additions & 0 deletions src/generated/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,46 @@ export type GetActivityInstanceConnectedParticipantsResponse = zInfer<
typeof GetActivityInstanceConnectedParticipantsResponseSchema
>;

// SHARE_INTERACTION
export const ShareInteractionRequestSchema = z.object({
command: z.string(),
content: z.string().max(2000).optional(),
preview_image: z.object({height: z.number(), url: z.string(), width: z.number()}).optional(),
components: z
.array(
z.object({
type: z.literal(1),
components: z
.array(
z.object({
type: z.literal(2),
style: z.number().gte(1).lte(5),
label: z.string().max(80).optional(),
custom_id: z
.string()
.max(100)
.describe('Developer-defined identifier for the button; max 100 characters')
.optional(),
}),
)
.max(5)
.optional(),
}),
)
.optional(),
});
export type ShareInteractionRequest = zInfer<typeof ShareInteractionRequestSchema>;

// SHARE_LINK
export const ShareLinkRequestSchema = z.object({
referrer_id: z.string().max(64).optional(),
custom_id: z.string().max(64).optional(),
message: z.string().max(1000),
});
export type ShareLinkRequest = zInfer<typeof ShareLinkRequestSchema>;
export const ShareLinkResponseSchema = z.object({success: z.boolean()});
export type ShareLinkResponse = zInfer<typeof ShareLinkResponseSchema>;

/**
* RPC Commands which support schemas.
*/
Expand All @@ -121,6 +161,8 @@ export enum Command {
OPEN_SHARE_MOMENT_DIALOG = 'OPEN_SHARE_MOMENT_DIALOG',
AUTHENTICATE = 'AUTHENTICATE',
GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS = 'GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS',
SHARE_INTERACTION = 'SHARE_INTERACTION',
SHARE_LINK = 'SHARE_LINK',
}

const emptyResponseSchema = z.object({}).optional().nullable();
Expand All @@ -146,4 +188,12 @@ export const Schemas = {
request: emptyRequestSchema,
response: GetActivityInstanceConnectedParticipantsResponseSchema,
},
[Command.SHARE_INTERACTION]: {
request: ShareInteractionRequestSchema,
response: emptyResponseSchema,
},
[Command.SHARE_LINK]: {
request: ShareLinkRequestSchema,
response: ShareLinkResponseSchema,
},
} as const;
1 change: 1 addition & 0 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const commandsMockDefault: IDiscordSDK['commands'] = {
}),
getChannelPermissions: () => Promise.resolve({permissions: bigInt(1234567890) as unknown as bigint}),
openShareMomentDialog: () => Promise.resolve(null),
shareLink: () => Promise.resolve({success: false}),
initiateImageUpload: () =>
Promise.resolve({
image_url:
Expand Down
1 change: 1 addition & 0 deletions src/schema/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export enum Commands {
OPEN_SHARE_MOMENT_DIALOG = 'OPEN_SHARE_MOMENT_DIALOG',
INITIATE_IMAGE_UPLOAD = 'INITIATE_IMAGE_UPLOAD',
GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS = 'GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS',
SHARE_LINK = 'SHARE_LINK',
}

export const ReceiveFramePayload = zod
Expand Down
1 change: 1 addition & 0 deletions src/schema/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function parseResponseData({cmd, data}: zod.infer<typeof ResponseFrame>) {
case Commands.INITIATE_IMAGE_UPLOAD:
case Commands.OPEN_SHARE_MOMENT_DIALOG:
case Commands.GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS:
case Commands.SHARE_LINK:
const {response} = Schemas[cmd];
return response.parse(data);
default:
Expand Down

0 comments on commit effd59b

Please sign in to comment.