Skip to content

Commit effd59b

Browse files
authored
feat: add shareLink command (#297)
* chore: update schema * feat: add shareLink command * chore: add mock * fix: shareLink jsdoc * fix: share link command response * feat: add response to schema
1 parent 91c298c commit effd59b

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

src/commands/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {openShareMomentDialog} from './openShareMomentDialog';
1515
import {setActivity, SetActivity} from './setActivity';
1616
import {setConfig} from './setConfig';
1717
import {setOrientationLockState} from './setOrientationLockState';
18+
import {shareLink} from './shareLink';
1819
import {startPurchase} from './startPurchase';
1920
import {userSettingsGetLocale} from './userSettingsGetLocale';
2021
import {initiateImageUpload} from './initiateImageUpload';
@@ -40,6 +41,7 @@ function commands(sendCommand: TSendCommand) {
4041
setActivity: setActivity(sendCommand),
4142
setConfig: setConfig(sendCommand),
4243
setOrientationLockState: setOrientationLockState(sendCommand),
44+
shareLink: shareLink(sendCommand),
4345
startPurchase: startPurchase(sendCommand),
4446
userSettingsGetLocale: userSettingsGetLocale(sendCommand),
4547
initiateImageUpload: initiateImageUpload(sendCommand),

src/commands/shareLink.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Command} from '../generated/schemas';
2+
import {schemaCommandFactory} from '../utils/commandFactory';
3+
4+
/**
5+
* Opens a modal in the user's client to share the Activity link.
6+
*
7+
* @param {string} referrer_id
8+
* @param {string} custom_id
9+
* @param {string} message - message sent alongside link when shared.
10+
* @returns {Promise<{success: boolean>} whether or not the user shared the link to someone
11+
*/
12+
export const shareLink = schemaCommandFactory(Command.SHARE_LINK);

src/generated/schema.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,71 @@
148148
"required": ["participants"],
149149
"additionalProperties": false
150150
}
151+
},
152+
"SHARE_INTERACTION": {
153+
"request": {
154+
"type": "object",
155+
"properties": {
156+
"command": {"type": "string"},
157+
"content": {"type": "string", "maxLength": 2000},
158+
"preview_image": {
159+
"type": "object",
160+
"properties": {"height": {"type": "number"}, "url": {"type": "string"}, "width": {"type": "number"}},
161+
"required": ["height", "url", "width"],
162+
"additionalProperties": false
163+
},
164+
"components": {
165+
"type": "array",
166+
"items": {
167+
"type": "object",
168+
"properties": {
169+
"type": {"const": 1},
170+
"components": {
171+
"type": "array",
172+
"maxItems": 5,
173+
"items": {
174+
"type": "object",
175+
"properties": {
176+
"type": {"const": 2},
177+
"style": {"type": "number", "minimum": 1, "maximum": 5},
178+
"label": {"type": "string", "description": "Text that appears on the button", "maxLength": 80},
179+
"custom_id": {
180+
"type": "string",
181+
"description": "Developer-defined identifier for the button; max 100 characters",
182+
"maxLength": 100
183+
}
184+
},
185+
"required": ["type", "style"],
186+
"additionalProperties": false
187+
}
188+
}
189+
},
190+
"required": ["type"],
191+
"additionalProperties": false
192+
}
193+
}
194+
},
195+
"required": ["command"],
196+
"additionalProperties": false
197+
},
198+
"response": null
199+
},
200+
"SHARE_LINK": {
201+
"request": {
202+
"type": "object",
203+
"properties": {
204+
"referrer_id": {"type": "string", "maxLength": 64},
205+
"custom_id": {"type": "string", "maxLength": 64},
206+
"message": {"type": "string", "maxLength": 1000}
207+
},
208+
"required": ["message"],
209+
"additionalProperties": false
210+
},
211+
"response": {
212+
"type": "object",
213+
"properties": {"success": {"type": "boolean"}},
214+
"required": ["success"],
215+
"additionalProperties": false
216+
}
151217
}
152218
}

src/generated/schemas.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,46 @@ export type GetActivityInstanceConnectedParticipantsResponse = zInfer<
113113
typeof GetActivityInstanceConnectedParticipantsResponseSchema
114114
>;
115115

116+
// SHARE_INTERACTION
117+
export const ShareInteractionRequestSchema = z.object({
118+
command: z.string(),
119+
content: z.string().max(2000).optional(),
120+
preview_image: z.object({height: z.number(), url: z.string(), width: z.number()}).optional(),
121+
components: z
122+
.array(
123+
z.object({
124+
type: z.literal(1),
125+
components: z
126+
.array(
127+
z.object({
128+
type: z.literal(2),
129+
style: z.number().gte(1).lte(5),
130+
label: z.string().max(80).optional(),
131+
custom_id: z
132+
.string()
133+
.max(100)
134+
.describe('Developer-defined identifier for the button; max 100 characters')
135+
.optional(),
136+
}),
137+
)
138+
.max(5)
139+
.optional(),
140+
}),
141+
)
142+
.optional(),
143+
});
144+
export type ShareInteractionRequest = zInfer<typeof ShareInteractionRequestSchema>;
145+
146+
// SHARE_LINK
147+
export const ShareLinkRequestSchema = z.object({
148+
referrer_id: z.string().max(64).optional(),
149+
custom_id: z.string().max(64).optional(),
150+
message: z.string().max(1000),
151+
});
152+
export type ShareLinkRequest = zInfer<typeof ShareLinkRequestSchema>;
153+
export const ShareLinkResponseSchema = z.object({success: z.boolean()});
154+
export type ShareLinkResponse = zInfer<typeof ShareLinkResponseSchema>;
155+
116156
/**
117157
* RPC Commands which support schemas.
118158
*/
@@ -121,6 +161,8 @@ export enum Command {
121161
OPEN_SHARE_MOMENT_DIALOG = 'OPEN_SHARE_MOMENT_DIALOG',
122162
AUTHENTICATE = 'AUTHENTICATE',
123163
GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS = 'GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS',
164+
SHARE_INTERACTION = 'SHARE_INTERACTION',
165+
SHARE_LINK = 'SHARE_LINK',
124166
}
125167

126168
const emptyResponseSchema = z.object({}).optional().nullable();
@@ -146,4 +188,12 @@ export const Schemas = {
146188
request: emptyRequestSchema,
147189
response: GetActivityInstanceConnectedParticipantsResponseSchema,
148190
},
191+
[Command.SHARE_INTERACTION]: {
192+
request: ShareInteractionRequestSchema,
193+
response: emptyResponseSchema,
194+
},
195+
[Command.SHARE_LINK]: {
196+
request: ShareLinkRequestSchema,
197+
response: ShareLinkResponseSchema,
198+
},
149199
} as const;

src/mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export const commandsMockDefault: IDiscordSDK['commands'] = {
134134
}),
135135
getChannelPermissions: () => Promise.resolve({permissions: bigInt(1234567890) as unknown as bigint}),
136136
openShareMomentDialog: () => Promise.resolve(null),
137+
shareLink: () => Promise.resolve({success: false}),
137138
initiateImageUpload: () =>
138139
Promise.resolve({
139140
image_url:

src/schema/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export enum Commands {
3636
OPEN_SHARE_MOMENT_DIALOG = 'OPEN_SHARE_MOMENT_DIALOG',
3737
INITIATE_IMAGE_UPLOAD = 'INITIATE_IMAGE_UPLOAD',
3838
GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS = 'GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS',
39+
SHARE_LINK = 'SHARE_LINK',
3940
}
4041

4142
export const ReceiveFramePayload = zod

src/schema/responses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ function parseResponseData({cmd, data}: zod.infer<typeof ResponseFrame>) {
182182
case Commands.INITIATE_IMAGE_UPLOAD:
183183
case Commands.OPEN_SHARE_MOMENT_DIALOG:
184184
case Commands.GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS:
185+
case Commands.SHARE_LINK:
185186
const {response} = Schemas[cmd];
186187
return response.parse(data);
187188
default:

0 commit comments

Comments
 (0)