Archive of the USPol Labeler's automatic labeling script. Out-of-date. Must run alongside Ozone. DO NOT OPEN ISSUES OR PULLS -- THEY WILL BE IGNORED/CLOSED
at tip 98 lines 4.2 kB view raw
1import { Agent, AppBskyActorGetProfile } from "@atproto/api"; 2import { retry } from "@atproto/common"; 3import { auditPostForKeywords } from "./handler.ts"; 4import { DRY_RUN, INITIAL_PAGES_LIMIT, PAGE_SIZE, PAGES_LIMIT, redis, VERBOSE } from "../main.ts"; 5import { hasAlreadyHandled } from "./redis.ts"; 6import { clamp } from "./utils.ts"; 7 8const listMostRecent: Record<string, string> = {} 9async function setListCursor(list: string, cursor: string) { 10 if (redis) { 11 return await retry(async () => await redis!.sendCommand(["HSET", "listCursors", list, cursor]), {maxRetries: 3}); 12 } else { 13 return listMostRecent[list] = cursor; 14 } 15} 16async function getListCursor(list: string): Promise<string|undefined> { 17 if (redis) { 18 const reply = await retry(async () => await redis!.sendCommand(["HGET", "listCursors", list]), {maxRetries: 3}); 19 if (typeof reply == "string") return reply; 20 return undefined; 21 } else { 22 return listMostRecent[list]; 23 } 24} 25 26export async function pollBlueskyList(agent: Agent, me: AppBskyActorGetProfile.Response, listDid: string, cursor?: string, depth: number = 0) { 27 // deno-lint-ignore no-var 28 var anyPostFailed = false; 29 try { 30 const feedEntries = await agent.app.bsky.feed.getListFeed({ 31 list: listDid, 32 limit: clamp(PAGE_SIZE, 1, 100), 33 cursor: cursor ? cursor : undefined, 34 }, {headers: {"atproto-proxy": ""}}); 35 if (!feedEntries.success) return; 36 if (feedEntries.data.feed.length == 0) return; 37 const mostRecent = await getListCursor(listDid); 38 if (INITIAL_PAGES_LIMIT === 0 && !mostRecent) { 39 // can maybe be a zero-like value (undefined), 40 // so we have to strict type match. 41 // If INITIAL_PAGES_LIMIT is 0, don't do any initial labeling at all. 42 return; 43 } 44 for (const post of feedEntries.data.feed) { 45 if (post.post.uri == mostRecent) { 46 if (VERBOSE) console.log("Finishing with list feed early") 47 if (depth == 0 && !anyPostFailed) { 48 if (VERBOSE) console.log("Most recent before:", mostRecent) 49 await setListCursor(listDid, feedEntries.data.feed[0].post.uri); 50 if (VERBOSE) console.log("Most recent now:", await getListCursor(listDid)) 51 } 52 return; // out of pollFeed entirely 53 } 54 // // deno-lint-ignore no-explicit-any 55 // const record = post.post.record as any 56 // record.uri = post.post.uri; 57 // record.cid = post.post.cid; 58 try { 59 if (!mostRecent) { 60 const alreadyHandled = await hasAlreadyHandled(post.post.uri); 61 if (alreadyHandled) { 62 if (VERBOSE) console.log("Already handled this one:", post.post.uri); 63 continue; 64 } else if (alreadyHandled === null) { 65 const recResponse = await agent.tools.ozone.moderation.queryStatuses({ 66 subject: post.post.uri, 67 }); 68 if (recResponse.success && recResponse.data.subjectStatuses.length != 0) { 69 if (recResponse.data.subjectStatuses[0].tags?.includes("auto-handled") || recResponse.data.subjectStatuses[0].reviewState != "lex:tools.ozone.moderation.defs#reviewNone") { 70 continue; // skip this one, it's already been checked 71 } 72 } 73 } 74 } 75 if (VERBOSE || DRY_RUN) console.log(post.post.uri, `(From: ${listDid})`); 76 try { 77 await auditPostForKeywords(post.post, agent, me, {}) 78 } catch(e) { 79 console.error("Getting keywords for %s failed:", post.post.uri, e) 80 anyPostFailed = true; 81 } 82 } catch(e) { 83 console.error("Invalid post in list feed:", post) 84 console.error("Cause:", e) 85 anyPostFailed = true; 86 } 87 } 88 if (depth == 0 && !anyPostFailed) { 89 if (VERBOSE) console.log("Most recent before:", mostRecent) 90 await setListCursor(listDid, feedEntries.data.feed[0].post.uri); 91 if (VERBOSE) console.log("Most recent now:", await getListCursor(listDid)) 92 } 93 const limit = mostRecent ? PAGES_LIMIT : INITIAL_PAGES_LIMIT || 0 94 if (depth < limit && feedEntries.data.cursor) await pollBlueskyList(agent, me, listDid, feedEntries.data.cursor, depth+1) 95 } catch(e) { 96 console.error("Error when polling feed:",e) 97 } 98}