Files
immich/web/src/lib/managers/activity-manager.svelte.ts
Robin Brisa a26d703335 feat(web): display number of likes in asset viewer (#18911)
* feat: display number of likes

* fix: properly decrement like count on unlike

Co-authored-by: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com>

* chore: pr feedback

* chore: updated related test

* chore: formatter run

* chore: force numberOfLikes to null in album context to pass lint

* chore: open-api updated

* fix: use undefined, not null

* styling tweaks

* chore: updated sql

---------

Co-authored-by: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2025-06-04 12:41:50 -05:00

129 lines
3.0 KiB
TypeScript

import { user } from '$lib/stores/user.store';
import { handlePromiseError } from '$lib/utils';
import {
createActivity,
deleteActivity,
getActivities,
getActivityStatistics,
ReactionLevel,
ReactionType,
type ActivityCreateDto,
type ActivityResponseDto,
} from '@immich/sdk';
import { get } from 'svelte/store';
class ActivityManager {
#albumId = $state<string | undefined>();
#assetId = $state<string | undefined>();
#activities = $state<ActivityResponseDto[]>([]);
#commentCount = $state(0);
#likeCount = $state(0);
#isLiked = $state<ActivityResponseDto | null>(null);
get activities() {
return this.#activities;
}
get commentCount() {
return this.#commentCount;
}
get likeCount() {
return this.#likeCount;
}
get isLiked() {
return this.#isLiked;
}
init(albumId: string, assetId?: string) {
this.#albumId = albumId;
this.#assetId = assetId;
}
async addActivity(dto: ActivityCreateDto) {
if (this.#albumId === undefined) {
return;
}
const activity = await createActivity({ activityCreateDto: dto });
this.#activities = [...this.#activities, activity];
if (activity.type === ReactionType.Comment) {
this.#commentCount++;
}
if (activity.type === ReactionType.Like) {
this.#likeCount++;
}
handlePromiseError(this.refreshActivities(this.#albumId, this.#assetId));
return activity;
}
async deleteActivity(activity: ActivityResponseDto, index?: number) {
if (!this.#albumId) {
return;
}
if (activity.type === ReactionType.Comment) {
this.#commentCount--;
}
if (activity.type === ReactionType.Like) {
this.#likeCount--;
}
this.#activities = index
? this.#activities.splice(index, 1)
: this.#activities.filter(({ id }) => id !== activity.id);
await deleteActivity({ id: activity.id });
handlePromiseError(this.refreshActivities(this.#albumId, this.#assetId));
}
async toggleLike() {
if (!this.#albumId) {
return;
}
if (this.#isLiked) {
await this.deleteActivity(this.#isLiked);
this.#isLiked = null;
} else {
this.#isLiked = (await this.addActivity({
albumId: this.#albumId,
assetId: this.#assetId,
type: ReactionType.Like,
}))!;
}
}
async refreshActivities(albumId: string, assetId?: string) {
this.#activities = await getActivities({ albumId, assetId });
const [liked] = await getActivities({
albumId,
assetId,
userId: get(user).id,
$type: ReactionType.Like,
level: assetId ? undefined : ReactionLevel.Album,
});
this.#isLiked = liked ?? null;
const { comments, likes } = await getActivityStatistics({ albumId, assetId });
this.#commentCount = comments;
this.#likeCount = likes;
}
reset() {
this.#albumId = undefined;
this.#assetId = undefined;
this.#activities = [];
this.#commentCount = 0;
this.#likeCount = 0;
}
}
export const activityManager = new ActivityManager();