Skip to content

Commit 6aad387

Browse files
Document Community Highlights workflow (#231)
## 💸 TL;DR <!-- What's the three sentence summary of purpose of the PR --> Shows how to check, add, remove, and reorder highlighted posts.
1 parent 2fa2f9f commit 6aad387

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

‎docs/capabilities/server/reddit-api.mdx‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,69 @@ export const createPost = async () => {
133133
};
134134
```
135135

136+
### Managing Community Highlights
137+
138+
Community Highlights display selected posts at the top of a subreddit. Use `post.isHighlighted()`, `post.highlight()`, and `post.unhighlight()` to inspect or change a post's highlight. Use the subreddit methods to retrieve the highlighted posts and change their display order.
139+
140+
These operations use the app's Reddit access. If you expose them through an app endpoint, check that the current user has permission to manage the subreddit's posts.
141+
142+
This example checks the caller's moderator permissions, highlights the current post for seven days, and moves it to the first position:
143+
144+
```ts title="server/index.ts"
145+
import { context, reddit } from "@devvit/web/server";
146+
147+
export async function highlightCurrentPost() {
148+
if (!context.userId || !context.postId) {
149+
throw new Error("Signed-in post interaction required.");
150+
}
151+
152+
const subreddit = await reddit.getCurrentSubreddit();
153+
const user = await reddit.getUserById(context.userId);
154+
const permissions = await user?.getModPermissionsForSubreddit(subreddit.name);
155+
if (
156+
!permissions?.some(
157+
(permission) => permission === "all" || permission === "posts",
158+
)
159+
) {
160+
throw new Error("Moderator post permissions required.");
161+
}
162+
163+
const post = await reddit.getPostById(context.postId);
164+
if (post.subredditId !== subreddit.id) {
165+
throw new Error("Post must belong to this subreddit.");
166+
}
167+
168+
const wasHighlighted = await post.isHighlighted();
169+
await post.highlight({
170+
highlightLabelType: "ANNOUNCEMENT",
171+
highlightUntil: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
172+
});
173+
174+
const highlights = await subreddit.getHighlightedPosts();
175+
await subreddit.reorderHighlightedPosts([
176+
post.id,
177+
...highlights
178+
.filter((item) => item.postId !== post.id)
179+
.map((item) => item.postId),
180+
]);
181+
182+
return {
183+
wasHighlighted,
184+
highlights: await subreddit.getHighlightedPosts(),
185+
};
186+
}
187+
```
188+
189+
`post.highlight()` updates the settings if the post is already highlighted. Omit `highlightUntil` to keep a highlight until it is removed. A subreddit can have six highlights; highlighting a seventh post removes the last highlight in display order.
190+
191+
To remove a highlight, call `await post.unhighlight()` from an authorized action. Removing a highlight from a post that is not highlighted has no effect. These methods do not accept `runAs`.
192+
193+
`subreddit.getHighlightedPosts()` returns an ordered array of [HighlightedPostInfo](../../api/redditapi/models/type-aliases/HighlightedPostInfo.md), rather than `Post` objects. Each item has a `postId` and optional `highlightedUntil` and `highlightLabel` fields. Use `reddit.getPostById(postId)` when you need the post's content. Note the different names for the input options: `highlightUntil` and `highlightLabelType`.
194+
195+
Fetch the current highlights before reordering and pass the complete intended order. Another moderator can change the list between requests, so handle failures and refresh the list before trying again.
196+
197+
See the [Post highlight methods](../../api/redditapi/models/classes/Post.md#highlight), [HighlightPostOptions](../../api/redditapi/models/type-aliases/HighlightPostOptions.md), and [Subreddit reorder method](../../api/redditapi/models/classes/Subreddit.md#reorderhighlightedposts) for the API references.
198+
136199
### Submitting a comment
137200

138201
:::note

‎versioned_docs/version-0.14/capabilities/server/reddit-api.mdx‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,69 @@ export const createPost = async () => {
133133
};
134134
```
135135

136+
### Managing Community Highlights
137+
138+
Community Highlights display selected posts at the top of a subreddit. Use `post.isHighlighted()`, `post.highlight()`, and `post.unhighlight()` to inspect or change a post's highlight. Use the subreddit methods to retrieve the highlighted posts and change their display order.
139+
140+
These operations use the app's Reddit access. If you expose them through an app endpoint, check that the current user has permission to manage the subreddit's posts.
141+
142+
This example checks the caller's moderator permissions, highlights the current post for seven days, and moves it to the first position:
143+
144+
```ts title="server/index.ts"
145+
import { context, reddit } from "@devvit/web/server";
146+
147+
export async function highlightCurrentPost() {
148+
if (!context.userId || !context.postId) {
149+
throw new Error("Signed-in post interaction required.");
150+
}
151+
152+
const subreddit = await reddit.getCurrentSubreddit();
153+
const user = await reddit.getUserById(context.userId);
154+
const permissions = await user?.getModPermissionsForSubreddit(subreddit.name);
155+
if (
156+
!permissions?.some(
157+
(permission) => permission === "all" || permission === "posts",
158+
)
159+
) {
160+
throw new Error("Moderator post permissions required.");
161+
}
162+
163+
const post = await reddit.getPostById(context.postId);
164+
if (post.subredditId !== subreddit.id) {
165+
throw new Error("Post must belong to this subreddit.");
166+
}
167+
168+
const wasHighlighted = await post.isHighlighted();
169+
await post.highlight({
170+
highlightLabelType: "ANNOUNCEMENT",
171+
highlightUntil: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
172+
});
173+
174+
const highlights = await subreddit.getHighlightedPosts();
175+
await subreddit.reorderHighlightedPosts([
176+
post.id,
177+
...highlights
178+
.filter((item) => item.postId !== post.id)
179+
.map((item) => item.postId),
180+
]);
181+
182+
return {
183+
wasHighlighted,
184+
highlights: await subreddit.getHighlightedPosts(),
185+
};
186+
}
187+
```
188+
189+
`post.highlight()` updates the settings if the post is already highlighted. Omit `highlightUntil` to keep a highlight until it is removed. A subreddit can have six highlights; highlighting a seventh post removes the last highlight in display order.
190+
191+
To remove a highlight, call `await post.unhighlight()` from an authorized action. Removing a highlight from a post that is not highlighted has no effect. These methods do not accept `runAs`.
192+
193+
`subreddit.getHighlightedPosts()` returns an ordered array of [HighlightedPostInfo](../../api/redditapi/models/type-aliases/HighlightedPostInfo.md), rather than `Post` objects. Each item has a `postId` and optional `highlightedUntil` and `highlightLabel` fields. Use `reddit.getPostById(postId)` when you need the post's content. Note the different names for the input options: `highlightUntil` and `highlightLabelType`.
194+
195+
Fetch the current highlights before reordering and pass the complete intended order. Another moderator can change the list between requests, so handle failures and refresh the list before trying again.
196+
197+
See the [Post highlight methods](../../api/redditapi/models/classes/Post.md#highlight), [HighlightPostOptions](../../api/redditapi/models/type-aliases/HighlightPostOptions.md), and [Subreddit reorder method](../../api/redditapi/models/classes/Subreddit.md#reorderhighlightedposts) for the API references.
198+
136199
### Submitting a comment
137200

138201
:::note

0 commit comments

Comments
 (0)