Skip to content

Commit

Permalink
feat(bsky): mute/unmute user
Browse files Browse the repository at this point in the history
  • Loading branch information
swkatmask committed Feb 4, 2025
1 parent 9c217a7 commit 51af864
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
29 changes: 28 additions & 1 deletion src/helpers/formatBskyPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AppBskyFeedDefs,
} from '@atproto/api';
import { isViewRecord } from '@atproto/api/dist/client/types/app/bsky/embed/record.js';
import { isPostView, isThreadViewPost } from '@atproto/api/dist/client/types/app/bsky/feed/defs.js';
import { type FeedViewPost,isPostView, isThreadViewPost } from '@atproto/api/dist/client/types/app/bsky/feed/defs.js';
import { produce } from 'immer';
import { first, isUndefined, omitBy } from 'lodash-es';

Expand Down Expand Up @@ -143,3 +143,30 @@ export function formatBskyThreadPosts(thread: AppBskyFeedDefs.ThreadViewPost, po
}
return formatBskyThreadPosts(thread.parent, [post, ...posts]);
}

export function formatBskyFeed(feed: FeedViewPost): Post {
const parent = feed.reply?.parent as PostView | undefined;

Check failure on line 148 in src/helpers/formatBskyPost.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 'PostView'.
return {
publicationId: feed.post.cid,
type: feed.reply ? 'Comment' : 'Post',
postId: feed.post.cid,
parentPostId: parent?.cid,
parentAuthor: parent?.author ? formatBskyProfile(parent.author) : undefined,
timestamp: new Date(feed.post.indexedAt).getTime(),
author: formatBskyProfile(feed.post.author),
metadata: {
locale: '',
content: {
content: `TODO: ${JSON.parse(feed.post.embed as any)}`,
},
},
mentions: [],
hasLiked: !!feed.post.viewer?.like,
hasMirrored: !!feed.post.viewer?.repost,
hasBookmarked: false,
source: Source.Bsky,
canComment: true,
commentOn: feed.reply?.parent ? formatBskyPost(feed.reply.parent as PostView) : undefined,

Check failure on line 169 in src/helpers/formatBskyPost.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 'PostView'.
__original__: feed,
};
}
3 changes: 2 additions & 1 deletion src/helpers/formatBskyProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function formatBskyProfile(profile: AppBskyActorDefs.ProfileViewDetailed)
viewerContext: {
following: !!profile.viewer?.following,
followedBy: !!profile.viewer?.followedBy,
blocking: profile.viewer?.blockedBy,
// .blockedBy will block data request as well.
blocking: profile.viewer?.muted,
},
};
}
36 changes: 23 additions & 13 deletions src/providers/bsky/SocialMedia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AppBskyActorProfile, RichText } from '@atproto/api';
import { isThreadViewPost, type PostView } from '@atproto/api/dist/client/types/app/bsky/feed/defs.js';
import { BlockedActorError } from '@atproto/api/dist/client/types/app/bsky/feed/getAuthorFeed.js';
import { safeUnreachable } from '@masknet/kit';
import { compact } from 'lodash-es';

Expand Down Expand Up @@ -235,17 +236,24 @@ export class BskySocialMedia implements Provider {
);
}
async getPostsByProfileId(profileId: string, indicator?: PageIndicator): Promise<Pageable<Post, PageIndicator>> {
const res = await bskySessionHolder.agent.getAuthorFeed({
actor: profileId,
filter: 'posts_and_author_threads',
cursor: indicator?.id,
});
if (!res.success) throw new Error(`Failed to get post by profile id = ${profileId}.`);
return createPageable(
res.data.feed.map(formatBskyPost),
createIndicator(indicator),
res.data.cursor ? createNextIndicator(indicator, res.data.cursor) : undefined,
);
try {
const res = await bskySessionHolder.agent.getAuthorFeed({
actor: profileId,
filter: 'posts_and_author_threads',
cursor: indicator?.id,
});
if (!res.success) throw new Error(`Failed to get post by profile id = ${profileId}.`);
return createPageable(
res.data.feed.map(formatBskyPost),
createIndicator(indicator),
res.data.cursor ? createNextIndicator(indicator, res.data.cursor) : undefined,
);
} catch (err) {
if (err instanceof BlockedActorError) {
return createPageable([], createIndicator(indicator), undefined);
}
throw err;
}
}
async getLikedPostsByProfileId(
profileId: string,
Expand Down Expand Up @@ -558,10 +566,12 @@ export class BskySocialMedia implements Provider {
throw new NotImplementedError();
}
async blockProfile(profileId: string): Promise<boolean> {
throw new NotImplementedError();
const res = await bskySessionHolder.agent.mute(profileId);
return res.success;
}
async unblockProfile(profileId: string): Promise<boolean> {
throw new NotImplementedError();
const res = await bskySessionHolder.agent.unmute(profileId);
return res.success;
}
async getBlockedProfiles(indicator?: PageIndicator): Promise<Pageable<Profile, PageIndicator>> {
throw new NotImplementedError();
Expand Down

0 comments on commit 51af864

Please sign in to comment.