-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Expand file tree
/
Copy pathmemory.service.ts
More file actions
169 lines (143 loc) · 5.84 KB
/
Copy pathmemory.service.ts
File metadata and controls
169 lines (143 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { BadRequestException, Injectable } from '@nestjs/common';
import { DateTime } from 'luxon';
import { OnJob } from 'src/decorators';
import { BulkIdResponseDto, BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
import { AuthDto } from 'src/dtos/auth.dto';
import { MemoryCreateDto, MemoryResponseDto, MemorySearchDto, MemoryUpdateDto, mapMemory } from 'src/dtos/memory.dto';
import { DatabaseLock, JobName, MemoryType, Permission, QueueName, SystemMetadataKey } from 'src/enum';
import { BaseService } from 'src/services/base.service';
import { addAssets, removeAssets } from 'src/utils/asset.util';
const DAYS = 3;
@Injectable()
export class MemoryService extends BaseService {
@OnJob({ name: JobName.MemoryGenerate, queue: QueueName.BackgroundTask })
async onMemoriesCreate() {
const users = await this.userRepository.getList({ withDeleted: false });
await this.databaseRepository.withLock(DatabaseLock.MemoryCreation, async () => {
const state = await this.systemMetadataRepository.get(SystemMetadataKey.MemoriesState);
const start = DateTime.utc().startOf('day').minus({ days: DAYS });
const lastOnThisDayDate = state?.lastOnThisDayDate ? DateTime.fromISO(state.lastOnThisDayDate) : start;
// generate a memory +/- X days from today
for (let i = 0; i <= DAYS * 2; i++) {
const target = start.plus({ days: i });
if (lastOnThisDayDate >= target) {
continue;
}
this.logger.log(`Creating memories for ${target.toISO()}`);
try {
await Promise.all(users.map((owner) => this.createOnThisDayMemories(owner.id, target)));
} catch (error) {
this.logger.error(`Failed to create memories for ${target.toISO()}: ${error}`);
}
// update system metadata even when there is an error to minimize the chance of duplicates
await this.systemMetadataRepository.set(SystemMetadataKey.MemoriesState, {
...state,
lastOnThisDayDate: target.toISO(),
});
}
});
}
private async createOnThisDayMemories(ownerId: string, target: DateTime) {
const showAt = target.startOf('day').toISO();
const hideAt = target.endOf('day').toISO();
const memories = await this.assetRepository.getByDayOfYear([ownerId], target);
await Promise.all(
memories.map(({ year, assets }) =>
this.memoryRepository.create(
{
ownerId,
type: MemoryType.OnThisDay,
data: { year },
memoryAt: target.set({ year }).toISO()!,
showAt,
hideAt,
},
new Set(assets.map(({ id }) => id)),
),
),
);
}
@OnJob({ name: JobName.MemoryCleanup, queue: QueueName.BackgroundTask })
async onMemoriesCleanup() {
await this.memoryRepository.cleanup();
}
async search(auth: AuthDto, dto: MemorySearchDto) {
const memories = await this.memoryRepository.search(auth.user.id, dto);
return memories.map((memory) => mapMemory(memory, auth));
}
statistics(auth: AuthDto, dto: MemorySearchDto) {
return this.memoryRepository.statistics(auth.user.id, dto);
}
async get(auth: AuthDto, id: string): Promise<MemoryResponseDto> {
await this.requireAccess({ auth, permission: Permission.MemoryRead, ids: [id] });
const memory = await this.findOrFail(id);
return mapMemory(memory, auth);
}
async create(auth: AuthDto, dto: MemoryCreateDto) {
// TODO validate type/data combination
const assetIds = dto.assetIds || [];
const allowedAssetIds = await this.checkAccess({
auth,
permission: Permission.AssetShare,
ids: assetIds,
});
const memory = await this.memoryRepository.create(
{
ownerId: auth.user.id,
type: dto.type,
data: dto.data,
isSaved: dto.isSaved,
memoryAt: dto.memoryAt,
showAt: dto.showAt,
hideAt: dto.hideAt,
seenAt: dto.seenAt,
},
allowedAssetIds,
);
return mapMemory(memory, auth);
}
async update(auth: AuthDto, id: string, dto: MemoryUpdateDto): Promise<MemoryResponseDto> {
await this.requireAccess({ auth, permission: Permission.MemoryUpdate, ids: [id] });
const memory = await this.memoryRepository.update(id, {
isSaved: dto.isSaved,
memoryAt: dto.memoryAt,
seenAt: dto.seenAt,
});
return mapMemory(memory, auth);
}
async remove(auth: AuthDto, id: string): Promise<void> {
await this.requireAccess({ auth, permission: Permission.MemoryDelete, ids: [id] });
await this.memoryRepository.delete(id);
}
async addAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
await this.requireAccess({ auth, permission: Permission.MemoryRead, ids: [id] });
const repos = { access: this.accessRepository, bulk: this.memoryRepository };
const results = await addAssets(auth, repos, { parentId: id, assetIds: dto.ids });
const hasSuccess = results.find(({ success }) => success);
if (hasSuccess) {
await this.memoryRepository.update(id, { updatedAt: new Date() });
}
return results;
}
async removeAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
await this.requireAccess({ auth, permission: Permission.MemoryUpdate, ids: [id] });
const repos = { access: this.accessRepository, bulk: this.memoryRepository };
const results = await removeAssets(auth, repos, {
parentId: id,
assetIds: dto.ids,
canAlwaysRemove: Permission.MemoryDelete,
});
const hasSuccess = results.find(({ success }) => success);
if (hasSuccess) {
await this.memoryRepository.update(id, { id, updatedAt: new Date() });
}
return results;
}
private async findOrFail(id: string) {
const memory = await this.memoryRepository.get(id);
if (!memory) {
throw new BadRequestException('Memory not found');
}
return memory;
}
}