mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-17 17:44:09 +01:00
feat: notify owners when repositories expire
This commit is contained in:
1 parent
511dcd7067
commit
104af7d261
5 files changed
+142
-2
No files matched your search
@@ -4,6 +4,7 @@ import { hasAutomaticUpdates } from "./billing/expiration";
|
||||
import { FILE_TREE_BATCH_SIZE, FILE_LISTING_LIMIT, deleteRowsInBatches, clearStagedRowsInBatches } from "./file-tree";
|
||||
import { SourceBase, sourceStorageSize } from "./source/Source";
|
||||
import { notifyOwnerAccessProblem, withOwnerAccessRecovery } from "./owner-notifications";
|
||||
import { notifyRepositoryExpired } from "./repository-expiration-email";
|
||||
import AnonymizedPathModel from "./model/anonymized-path";
|
||||
import storage from "./storage";
|
||||
import { contentGenerationPrefix, contentRetirementMarker } from "./content-generation";
|
||||
@@ -1045,6 +1046,7 @@ export default class Repository {
|
||||
statusDate: new Date(), isReseted: true, size: { storage: 0, file: 0 } },
|
||||
$unset: { cleanupToken: "", cleanupUntil: "", pathIndexKey: "", pathIndexBuiltAt: "", sizeComputedAt: "", emptyTreeGeneration: "", retiredTreeGenerations: "", retiredPathIndexKeys: "", retiredContentPrefixes: "", legacyContentCleanupPending: "", stagedFileTrees: "" } }).exec();
|
||||
if (result.matchedCount) { this.model.status = RepositoryStatus.EXPIRED; this.model.isReseted = true; this.model.size = { storage: 0, file: 0 }; this.model.sizeComputedAt = undefined; this.model.emptyTreeGeneration = undefined; }
|
||||
if (result.matchedCount) await notifyRepositoryExpired(this.model._id);
|
||||
} finally {
|
||||
clearInterval(heartbeat);
|
||||
await AnonymizedRepositoryModel.updateOne({ _id: this.model._id, cleanupToken: token }, { $unset: { cleanupToken: "", cleanupUntil: "" } }).exec();
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Schema } from "mongoose";
|
||||
const AnonymizedRepositorySchema = new Schema({
|
||||
billingManaged: Boolean,
|
||||
billingNeverExpiresUntil: Date,
|
||||
expirationEmailClaimedFor: { type: Date, select: false },
|
||||
accessAlertGeneration: { type: String, select: false },
|
||||
accessAlertClaimedAt: { type: Date, select: false },
|
||||
accessAlertAttemptId: { type: String, select: false },
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import config from "../config";
|
||||
import RepositoryModel from "./model/anonymizedRepositories/anonymizedRepositories.model";
|
||||
import UserModel from "./model/users/users.model";
|
||||
import { notificationEmail } from "./owner-notifications";
|
||||
import { createLogger } from "./logger";
|
||||
import { RepositoryStatus } from "./types";
|
||||
|
||||
const logger = createLogger("repository-expiration-email");
|
||||
|
||||
/** Attempt delivery once per repository expiration date, across all workers. */
|
||||
export async function notifyRepositoryExpired(id: unknown): Promise<void> {
|
||||
if (!config.RESEND_API_KEY || !config.EMAIL_FROM) return;
|
||||
try {
|
||||
const repo = await RepositoryModel.findOne({ _id: id, status: RepositoryStatus.EXPIRED }).lean();
|
||||
const expirationDate = repo?.options.expirationDate;
|
||||
if (!repo || repo.options.expirationMode === "never" || !expirationDate || expirationDate > new Date()) return;
|
||||
const owner = await UserModel.findOne({ _id: repo.owner, status: { $nin: ["removed", "banned"] } }).lean();
|
||||
const to = owner && notificationEmail(owner.emails, owner.notificationEmail);
|
||||
if (!to) return;
|
||||
const base = /^https?:\/\//.test(config.APP_HOSTNAME) ? config.APP_HOSTNAME : `https://${config.APP_HOSTNAME}`;
|
||||
const url = new URL("/dashboard", base).href;
|
||||
const claim = await RepositoryModel.updateOne({
|
||||
_id: repo._id, owner: repo.owner, status: RepositoryStatus.EXPIRED, statusDate: repo.statusDate,
|
||||
"options.expirationMode": repo.options.expirationMode,
|
||||
"options.expirationDate": expirationDate,
|
||||
expirationEmailClaimedFor: { $ne: expirationDate },
|
||||
}, { $set: { expirationEmailClaimedFor: expirationDate } }).exec();
|
||||
if (!claim.matchedCount) return;
|
||||
// Keep the claim even after a timeout: the provider may have accepted the email.
|
||||
const response = await fetch("https://api.resend.com/emails", {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${config.RESEND_API_KEY}`, "Content-Type": "application/json",
|
||||
"Idempotency-Key": `repository-expired/${repo._id}/${expirationDate.getTime()}` },
|
||||
signal: AbortSignal.timeout(15000),
|
||||
body: JSON.stringify({
|
||||
from: config.EMAIL_FROM, to: [to], subject: "Your Anonymous GitHub repository has expired",
|
||||
text: `Your anonymized repository "${repo.repoId}" expired on ${expirationDate.toISOString().slice(0, 10)} (UTC).\n\nIts anonymized content is no longer available.${repo.options.expirationMode === "redirect" ? " Its link now redirects to the original repository." : ""}\n\nOpen your dashboard to manage your repositories:\n${url}\n\nYou are receiving this email because you own this repository on Anonymous GitHub. You can change your alert address on the connections page.\n`,
|
||||
}),
|
||||
});
|
||||
if (response.ok) logger.info("repository expiration email accepted", { repoId: repo.repoId });
|
||||
else logger.warn("repository expiration email delivery failed", { repoId: repo.repoId, httpStatus: response.status });
|
||||
} catch {
|
||||
// Email delivery must not interrupt expiration or expose addresses in logs.
|
||||
logger.warn("repository expiration email unavailable");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user