mirror of
https://github.com/immich-app/immich.git
synced 2025-03-11 02:23:09 -05:00
fix: memories off by one (#16434)
This commit is contained in:
parent
5c0538e52c
commit
e684062569
5 changed files with 24 additions and 24 deletions
|
@ -55,9 +55,10 @@ with
|
|||
inner join "exif" on "a"."id" = "exif"."assetId"
|
||||
)
|
||||
select
|
||||
(
|
||||
(now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date
|
||||
) / 365 as "yearsAgo",
|
||||
date_part(
|
||||
'year',
|
||||
("localDateTime" at time zone 'UTC')::date
|
||||
)::int as "year",
|
||||
json_agg("res") as "assets"
|
||||
from
|
||||
"res"
|
||||
|
|
|
@ -192,7 +192,7 @@ export class AssetRepository {
|
|||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID, { day: 1, month: 1 }] })
|
||||
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay): Promise<DayOfYearAssets[]> {
|
||||
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay) {
|
||||
return this.db
|
||||
.with('res', (qb) =>
|
||||
qb
|
||||
|
@ -239,16 +239,12 @@ export class AssetRepository {
|
|||
.select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')),
|
||||
)
|
||||
.selectFrom('res')
|
||||
.select(
|
||||
sql<number>`((now() at time zone 'UTC')::date - ("localDateTime" at time zone 'UTC')::date) / 365`.as(
|
||||
'yearsAgo',
|
||||
),
|
||||
)
|
||||
.select(sql<number>`date_part('year', ("localDateTime" at time zone 'UTC')::date)::int`.as('year'))
|
||||
.select((eb) => eb.fn.jsonAgg(eb.table('res')).as('assets'))
|
||||
.groupBy(sql`("localDateTime" at time zone 'UTC')::date`)
|
||||
.orderBy(sql`("localDateTime" at time zone 'UTC')::date`, 'desc')
|
||||
.limit(10)
|
||||
.execute() as any as Promise<DayOfYearAssets[]>;
|
||||
.execute();
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [[DummyValue.UUID]] })
|
||||
|
|
|
@ -64,18 +64,18 @@ describe(AssetService.name, () => {
|
|||
mocks.partner.getAll.mockResolvedValue([]);
|
||||
mocks.asset.getByDayOfYear.mockResolvedValue([
|
||||
{
|
||||
yearsAgo: 1,
|
||||
year: 2023,
|
||||
assets: [image1, image2],
|
||||
},
|
||||
{
|
||||
yearsAgo: 9,
|
||||
year: 2015,
|
||||
assets: [image3],
|
||||
},
|
||||
{
|
||||
yearsAgo: 15,
|
||||
year: 2009,
|
||||
assets: [image4],
|
||||
},
|
||||
]);
|
||||
] as any);
|
||||
|
||||
await expect(sut.getMemoryLane(authStub.admin, { day: 15, month: 1 })).resolves.toEqual([
|
||||
{ yearsAgo: 1, title: '1 year ago', assets: [mapAsset(image1), mapAsset(image2)] },
|
||||
|
|
|
@ -38,12 +38,15 @@ export class AssetService extends BaseService {
|
|||
const userIds = [auth.user.id, ...partnerIds];
|
||||
|
||||
const groups = await this.assetRepository.getByDayOfYear(userIds, dto);
|
||||
return groups.map(({ yearsAgo, assets }) => ({
|
||||
yearsAgo,
|
||||
// TODO move this to clients
|
||||
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`,
|
||||
assets: assets.map((asset) => mapAsset(asset, { auth })),
|
||||
}));
|
||||
return groups.map(({ year, assets }) => {
|
||||
const yearsAgo = DateTime.utc().year - year;
|
||||
return {
|
||||
yearsAgo,
|
||||
// TODO move this to clients
|
||||
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`,
|
||||
assets: assets.map((asset) => mapAsset(asset as AssetEntity, { auth })),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async getStatistics(auth: AuthDto, dto: AssetStatsDto) {
|
||||
|
|
|
@ -45,18 +45,18 @@ export class MemoryService extends BaseService {
|
|||
for (const [userId, userIds] of Object.entries(userMap)) {
|
||||
const memories = await this.assetRepository.getByDayOfYear(userIds, target);
|
||||
|
||||
for (const memory of memories) {
|
||||
const data: OnThisDayData = { year: target.year - memory.yearsAgo };
|
||||
for (const { year, assets } of memories) {
|
||||
const data: OnThisDayData = { year };
|
||||
await this.memoryRepository.create(
|
||||
{
|
||||
ownerId: userId,
|
||||
type: MemoryType.ON_THIS_DAY,
|
||||
data,
|
||||
memoryAt: target.minus({ years: memory.yearsAgo }).toISO(),
|
||||
memoryAt: target.set({ year }).toISO(),
|
||||
showAt,
|
||||
hideAt,
|
||||
},
|
||||
new Set(memory.assets.map(({ id }) => id)),
|
||||
new Set(assets.map(({ id }) => id)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue