Skip to content

Commit

Permalink
feat: announce - return a 202 on torrent not completed (cross-seed#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa authored Oct 31, 2023
1 parent cb52821 commit 50c2868
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,18 @@ export async function searchForLocalTorrentByCriteria(

export async function checkNewCandidateMatch(
candidate: Candidate
): Promise<boolean> {
): Promise<InjectionResult | SaveResult> {
const meta = await getTorrentByFuzzyName(candidate.name);
if (meta === null) {
logger.verbose({
label: Label.REVERSE_LOOKUP,
message: `Did not find an existing entry for ${candidate.name}`,
});
return false;
return null;
}

const hashesToExclude = await getInfoHashesToExclude();
if (!filterByContent(meta)) return false;
if (!filterByContent(meta)) return null;
const searchee = createSearcheeFromMetafile(meta);

// make sure searchee is in database
Expand All @@ -232,7 +232,7 @@ export async function checkNewCandidateMatch(
assessment.decision !== Decision.MATCH &&
assessment.decision !== Decision.MATCH_SIZE_ONLY
)
return false;
return null;

const result = await performAction(
assessment.metafile,
Expand All @@ -245,7 +245,7 @@ export async function checkNewCandidateMatch(
[[assessment, candidate.tracker, result]],
Label.REVERSE_LOOKUP
);
return result === InjectionResult.SUCCESS || result === SaveResult.SAVED;
return result;
}

async function findSearchableTorrents() {
Expand Down
27 changes: 16 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
checkNewCandidateMatch,
searchForLocalTorrentByCriteria,
} from "./pipeline.js";
import { InjectionResult, SaveResult } from "./constants.js";
import { indexNewTorrents, TorrentLocator } from "./torrent.js";

function getData(req): Promise<string> {
Expand Down Expand Up @@ -152,18 +153,22 @@ async function announce(
try {
await indexNewTorrents();
const result = await checkNewCandidateMatch(candidate);
if (!result) {
res.writeHead(204);
res.end();
return;
const isOk =
result === InjectionResult.SUCCESS || result === SaveResult.SAVED;
if (!isOk) {
if (result === InjectionResult.TORRENT_NOT_COMPLETE) {
res.writeHead(202);
} else {
res.writeHead(204);
}
} else {
logger.info({
label: Label.SERVER,
message: `Added announce from ${candidate.tracker}: ${candidate.name}`,
});
res.writeHead(200);
}
logger.info({
label: Label.SERVER,
message: `Added announce from ${candidate.tracker}: ${candidate.name}`,
});
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(JSON.stringify(result));
res.end();
} catch (e) {
logger.error(e);
res.writeHead(500);
Expand Down

0 comments on commit 50c2868

Please sign in to comment.