auron
Version:
Interact with your ATProto labeler from your terminal
105 lines (104 loc) • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRecords = exports.getRepos = exports.getQueueItems = exports.getAgent = void 0;
const api_1 = require("@atproto/api");
const common_1 = require("@atproto/common");
let agent;
const getAgent = async () => {
if (agent)
return agent;
if (!process.env.SERVICE_URL) {
throw new Error("SERVICE_URL env var is required");
}
if (!process.env.SERVICE_DID) {
throw new Error("SERVICE_DID env var is required");
}
agent = new api_1.AtpAgent({ service: process.env.SERVICE_URL });
// @ts-ignore
agent.configureProxy(process.env.SERVICE_DID);
await agent.login({
identifier: process.env.USERNAME,
password: process.env.PASSWORD,
});
return agent;
};
exports.getAgent = getAgent;
const getQueueItems = async ({ maxCount = 500, cursor, }, emitter) => {
const agent = await (0, exports.getAgent)();
let nextCursor = cursor;
let counter = 0;
const result = {
cursor,
subjectStatuses: [],
};
do {
try {
const { data } = await agent.tools.ozone.moderation.queryStatuses({
cursor: nextCursor,
limit: Math.min(100, maxCount),
reviewState: api_1.ToolsOzoneModerationDefs.REVIEWOPEN,
});
nextCursor = data.cursor;
result.subjectStatuses.push(...data.subjectStatuses);
emitter.emit("update", {
maxCount,
nextCursor,
subjectCount: result.subjectStatuses.length,
});
}
catch (err) {
console.error(err);
break;
}
// Every 5th request, wait for 500ms to avoid potential rate limiting
if (counter % 5) {
await new Promise((resolve) => setTimeout(resolve, 500));
}
counter++;
} while (nextCursor &&
(!maxCount || result.subjectStatuses.length < maxCount));
return { ...result, cursor: nextCursor };
};
exports.getQueueItems = getQueueItems;
const getRepos = async ({ dids }, emitter) => {
const agent = await (0, exports.getAgent)();
const repos = [];
for (const chunk of (0, common_1.chunkArray)(dids, 100)) {
try {
const { data } = await agent.tools.ozone.moderation.getRepos({
dids: chunk,
});
repos.push(...data.repos.filter((r) => api_1.ToolsOzoneModerationDefs.isRepoViewDetail(r)));
emitter.emit("update", {
total: dids.length,
repoCount: repos.length,
});
}
catch (err) {
console.log(`Error fetching repos for ${chunk}`);
}
}
return repos;
};
exports.getRepos = getRepos;
const getRecords = async ({ uris }, emitter) => {
const agent = await (0, exports.getAgent)();
const records = [];
for (const chunk of (0, common_1.chunkArray)(uris, 100)) {
try {
const { data } = await agent.tools.ozone.moderation.getRecords({
uris: chunk,
});
records.push(...data.records.filter((r) => api_1.ToolsOzoneModerationDefs.isRecordViewDetail(r)));
emitter.emit("update", {
total: uris.length,
recordCount: records.length,
});
}
catch (err) {
console.log(`Error fetching records for ${chunk}`);
}
}
return records;
};
exports.getRecords = getRecords;