feat: add cross-album search, auto-rules, album UI redesign, editorial archiving and pdf download
This commit is contained in:
1 parent
9c0d43b1ec
commit
375e5b8b7d
23 files changed
+1153
-43
No files matched your search
@@ -0,0 +1,121 @@
|
||||
import "server-only";
|
||||
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
|
||||
import { db } from "@/db/client";
|
||||
import { albumAutoRules } from "@/db/schema";
|
||||
import type * as schema from "@/db/schema";
|
||||
import { albumGrantService } from "./grants";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichClient } from "@/modules/immich/types";
|
||||
|
||||
export type AutoRule = {
|
||||
id: string;
|
||||
editorialAccountId: string;
|
||||
keyword: string;
|
||||
createdAt: Date;
|
||||
createdByUserId: string | null;
|
||||
};
|
||||
|
||||
export type AutoRuleRepository = {
|
||||
listRules(editorialId: string): Promise<AutoRule[]>;
|
||||
createRule(input: { editorialId: string; keyword: string; adminUserId: string }): Promise<void>;
|
||||
deleteRule(input: { ruleId: string; adminUserId: string }): Promise<void>;
|
||||
};
|
||||
|
||||
export function createAutoRuleService({
|
||||
repository,
|
||||
grantService,
|
||||
immich,
|
||||
}: {
|
||||
repository: AutoRuleRepository;
|
||||
grantService: Pick<typeof albumGrantService, "getGrantedAlbumIds" | "grantAlbum">;
|
||||
immich: Pick<ImmichClient, "listAlbums">;
|
||||
}) {
|
||||
return {
|
||||
async listRules(editorialId: string): Promise<AutoRule[]> {
|
||||
return repository.listRules(editorialId);
|
||||
},
|
||||
|
||||
async createRule(input: { editorialId: string; keyword: string; adminUserId: string }): Promise<void> {
|
||||
await repository.createRule(input);
|
||||
},
|
||||
|
||||
async deleteRule(input: { ruleId: string; adminUserId: string }): Promise<void> {
|
||||
await repository.deleteRule(input);
|
||||
},
|
||||
|
||||
async applyRules(editorialId: string, adminUserId: string): Promise<{ granted: number }> {
|
||||
const rules = await repository.listRules(editorialId);
|
||||
if (rules.length === 0) {
|
||||
return { granted: 0 };
|
||||
}
|
||||
|
||||
let albums;
|
||||
try {
|
||||
albums = await immich.listAlbums();
|
||||
} catch (error) {
|
||||
return { granted: 0 };
|
||||
}
|
||||
|
||||
const grantedAlbumIds = new Set(await grantService.getGrantedAlbumIds(editorialId));
|
||||
let newlyGranted = 0;
|
||||
|
||||
for (const album of albums) {
|
||||
if (grantedAlbumIds.has(album.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const albumNameLower = album.albumName.toLowerCase();
|
||||
const matchesRule = rules.some((rule) => albumNameLower.includes(rule.keyword.toLowerCase()));
|
||||
|
||||
if (matchesRule) {
|
||||
try {
|
||||
await grantService.grantAlbum({ editorialId, immichAlbumId: album.id, adminUserId });
|
||||
newlyGranted++;
|
||||
grantedAlbumIds.add(album.id);
|
||||
} catch (error) {
|
||||
// Ignore individual album grant failures
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { granted: newlyGranted };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createAutoRuleRepository(
|
||||
database: NodePgDatabase<typeof schema>,
|
||||
): AutoRuleRepository {
|
||||
return {
|
||||
async listRules(editorialId: string) {
|
||||
return database
|
||||
.select()
|
||||
.from(albumAutoRules)
|
||||
.where(eq(albumAutoRules.editorialAccountId, editorialId));
|
||||
},
|
||||
|
||||
async createRule({ editorialId, keyword, adminUserId }) {
|
||||
await database
|
||||
.insert(albumAutoRules)
|
||||
.values({
|
||||
editorialAccountId: editorialId,
|
||||
keyword,
|
||||
createdByUserId: adminUserId,
|
||||
})
|
||||
.onConflictDoNothing();
|
||||
},
|
||||
|
||||
async deleteRule({ ruleId, adminUserId }) {
|
||||
await database.delete(albumAutoRules).where(eq(albumAutoRules.id, ruleId));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const autoRuleService = createAutoRuleService({
|
||||
repository: createAutoRuleRepository(db),
|
||||
grantService: albumGrantService,
|
||||
immich: immichClient,
|
||||
});
|
||||
Reference in new issue
Block a user