Some checks failed
CI / Lint, typecheck, test (push) Successful in 3m41s
CD / Build and push images (push) Successful in 3m44s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m18s
CD / Promote to Int (push) Successful in 11s
CI / Auth e2e pack (push) Failing after 2m53s
CI / Import/export fidelity gate (push) Has been skipped
New per-user digestFrequency (hourly default | daily | off) on the profile and in the settings UI. A scheduler job (15 min cadence) mails a user once their oldest unread, unmailed notification exceeds the cadence window: one localized mail per batch, grouped per pond then per page with actor names and change/comment counts, enqueued through the mail outbox. Sending marks the batch mailed — never read — and re-checks page read permission per entry at send time; entries the user can no longer read are dropped from the mail but still marked handled, so revoked content cannot queue forever. Every mail carries a signed, single-purpose unsubscribe link: it only flips the setting to off, renders a session-free confirmation page, and sets no cookie. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import { ConflictException, Injectable } from '@nestjs/common';
|
|
import { Prisma, User } from '@prisma/client';
|
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { hashPassword, verifyPassword } from './password';
|
|
|
|
export const PASSWORD_PROVIDER = 'password';
|
|
|
|
export interface CreateUserInput {
|
|
username: string;
|
|
email: string;
|
|
displayName: string;
|
|
password: string;
|
|
locale: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class UsersService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
/**
|
|
* Creates the account plus its password identity in one transaction.
|
|
* Uniqueness violations surface as field-level conflicts so the client
|
|
* can highlight the right input.
|
|
*/
|
|
async createUser(input: CreateUserInput): Promise<User> {
|
|
try {
|
|
return await this.prisma.$transaction(async (tx) => {
|
|
const user = await tx.user.create({
|
|
data: {
|
|
username: input.username,
|
|
email: input.email.toLowerCase(),
|
|
displayName: input.displayName,
|
|
locale: input.locale,
|
|
},
|
|
});
|
|
await tx.userIdentity.create({
|
|
data: {
|
|
userId: user.id,
|
|
provider: PASSWORD_PROVIDER,
|
|
// The user id (not the username) is the stable subject: a
|
|
// future username change must not orphan the identity.
|
|
subject: user.id,
|
|
credential: await hashPassword(input.password),
|
|
},
|
|
});
|
|
return user;
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
|
|
const target = (error.meta?.target as string[] | undefined)?.[0] ?? 'username';
|
|
throw new ConflictException({ details: { [target]: ['validation.taken'] } });
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
findById(id: string): Promise<User | null> {
|
|
return this.prisma.user.findUnique({ where: { id } });
|
|
}
|
|
|
|
findByUsernameOrEmail(usernameOrEmail: string): Promise<User | null> {
|
|
const value = usernameOrEmail.trim();
|
|
return this.prisma.user.findFirst({
|
|
where: value.includes('@') ? { email: value.toLowerCase() } : { username: value },
|
|
});
|
|
}
|
|
|
|
findByEmail(email: string): Promise<User | null> {
|
|
return this.prisma.user.findUnique({ where: { email: email.toLowerCase() } });
|
|
}
|
|
|
|
async checkPassword(userId: string, password: string): Promise<boolean> {
|
|
const identity = await this.prisma.userIdentity.findUnique({
|
|
where: { provider_subject: { provider: PASSWORD_PROVIDER, subject: userId } },
|
|
});
|
|
if (!identity?.credential) return false;
|
|
return verifyPassword(identity.credential, password);
|
|
}
|
|
|
|
async setPassword(userId: string, password: string): Promise<void> {
|
|
await this.prisma.userIdentity.update({
|
|
where: { provider_subject: { provider: PASSWORD_PROVIDER, subject: userId } },
|
|
data: { credential: await hashPassword(password) },
|
|
});
|
|
}
|
|
|
|
async markEmailVerified(userId: string): Promise<User> {
|
|
return this.prisma.user.update({
|
|
where: { id: userId },
|
|
data: { status: 'ACTIVE', emailVerifiedAt: new Date() },
|
|
});
|
|
}
|
|
|
|
async updateProfile(
|
|
userId: string,
|
|
data: {
|
|
displayName?: string;
|
|
locale?: string;
|
|
autoWatchOwnPages?: boolean;
|
|
autoWatchOnComment?: boolean;
|
|
digestFrequency?: string;
|
|
},
|
|
): Promise<User> {
|
|
return this.prisma.user.update({ where: { id: userId }, data });
|
|
}
|
|
}
|