Leaflet Blog in Deno Fresh
1import { type AppBskyFeedDefs, AppBskyFeedPost } from "npm:@atproto/api";
2
3const MinLikeCountFilter = (
4 min: number,
5): (comment: AppBskyFeedDefs.ThreadViewPost) => boolean => {
6 return (comment: AppBskyFeedDefs.ThreadViewPost) => {
7 return (comment.post.likeCount ?? 0) < min;
8 };
9};
10
11const MinCharacterCountFilter = (
12 min: number,
13): (comment: AppBskyFeedDefs.ThreadViewPost) => boolean => {
14 return (comment: AppBskyFeedDefs.ThreadViewPost) => {
15 if (!AppBskyFeedPost.isRecord(comment.post.record)) {
16 return false;
17 }
18 const record = comment.post.record as AppBskyFeedPost.Record;
19 return record.text.length < min;
20 };
21};
22
23const TextContainsFilter = (
24 text: string,
25): (comment: AppBskyFeedDefs.ThreadViewPost) => boolean => {
26 return (comment: AppBskyFeedDefs.ThreadViewPost) => {
27 if (!AppBskyFeedPost.isRecord(comment.post.record)) {
28 return false;
29 }
30 const record = comment.post.record as AppBskyFeedPost.Record;
31 return record.text.toLowerCase().includes(text.toLowerCase());
32 };
33};
34
35const ExactMatchFilter = (
36 text: string,
37): (comment: AppBskyFeedDefs.ThreadViewPost) => boolean => {
38 return (comment: AppBskyFeedDefs.ThreadViewPost) => {
39 if (!AppBskyFeedPost.isRecord(comment.post.record)) {
40 return false;
41 }
42 const record = comment.post.record as AppBskyFeedPost.Record;
43 return record.text.toLowerCase() === text.toLowerCase();
44 };
45};
46
47/*
48 * This function allows you to filter out comments based on likes,
49 * characters, text, pins, or exact matches.
50 */
51export const Filters: {
52 MinLikeCountFilter: (
53 min: number,
54 ) => (comment: AppBskyFeedDefs.ThreadViewPost) => boolean;
55 MinCharacterCountFilter: (
56 min: number,
57 ) => (comment: AppBskyFeedDefs.ThreadViewPost) => boolean;
58 TextContainsFilter: (
59 text: string,
60 ) => (comment: AppBskyFeedDefs.ThreadViewPost) => boolean;
61 ExactMatchFilter: (
62 text: string,
63 ) => (comment: AppBskyFeedDefs.ThreadViewPost) => boolean;
64 NoLikes: (comment: AppBskyFeedDefs.ThreadViewPost) => boolean;
65 NoPins: (comment: AppBskyFeedDefs.ThreadViewPost) => boolean;
66} = {
67 MinLikeCountFilter,
68 MinCharacterCountFilter,
69 TextContainsFilter,
70 ExactMatchFilter,
71 NoLikes: MinLikeCountFilter(0),
72 NoPins: ExactMatchFilter("📌"),
73};
74
75export default Filters;