owltide/utils/client-user.ts
Hornwitser e52972853d License under AGPL version 3 or later
I firmly believe in free software.

The application I'm making here have capabilities that I've not seen in
any system.  It presents itself as an opportunity to collaborate on a
tool that serves the people rather than corporations.  Whose incentives
are to help people rather, not make the most money.  And whose terms
ensure that these freedoms and incentives cannot be taken back or
subverted.

I license this software under the AGPL.
2025-06-30 18:58:24 +02:00

88 lines
1.9 KiB
TypeScript

/*
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { ApiUser, ApiUserType } from "~/shared/types/api";
import type { Id } from "~/shared/types/common";
import { DateTime, Zone } from "~/shared/utils/luxon";
import { ClientEntity } from "~/utils/client-entity";
export class ClientUser extends ClientEntity<ApiUser> {
serverName: string | undefined;
serverType: ApiUserType
constructor(
id: Id,
updatedAt: DateTime,
deleted: boolean,
public name: string | undefined,
public type: ApiUserType,
) {
super(id, updatedAt, deleted);
this.serverName = name;
this.serverType = type;
}
override isModified() {
return (
super.isModified()
|| this.name !== this.serverName
|| this.type !== this.serverType
);
}
override discard() {
if (this.isNew()) {
throw new Error("ClientUser.discard: Cannot discard new entity.")
}
this.updatedAt = this.serverUpdatedAt;
this.deleted = this.serverDeleted;
this.name = this.serverName;
this.type = this.serverType;
}
static create(
id: Id,
name: string | undefined,
type: ApiUserType,
opts: { zone: Zone, locale: string },
) {
return new this(
id,
DateTime.fromMillis(ClientEntity.newEntityMillis, opts),
false,
name,
type,
)
}
static fromApi(api: ApiUser, opts: { zone: Zone, locale: string }) {
return new this(
api.id,
DateTime.fromISO(api.updatedAt, opts),
false,
api.name,
api.type,
);
}
override apiUpdate(api: ApiUser, opts: { zone: Zone, locale: string }) {
const wasModified = this.isModified();
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
this.serverDeleted = false;
this.serverName = api.name;
this.serverType = api.type;
if (!wasModified || !this.isModified()) {
this.discard();
}
}
toApi(): ApiUser {
return {
id: this.id,
updatedAt: toIso(this.updatedAt),
name: this.name,
type: this.type,
};
}
}