2026-04-01 11:37:55 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-23 17:03:52 +02:00
|
|
|
"context"
|
2026-04-01 11:37:55 +02:00
|
|
|
"fmt"
|
2026-07-19 15:34:27 +00:00
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/db/dbgen"
|
2026-04-01 11:37:55 +02:00
|
|
|
)
|
|
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
// UpdateUserProfile updates the username, avatar, display name and about text
|
|
|
|
|
// for the given user. All four are written unconditionally, so the caller is
|
|
|
|
|
// responsible for merging a partial PATCH against the current row.
|
2026-04-01 11:37:55 +02:00
|
|
|
// Returns ErrNotFound if the user does not exist. Returns an error wrapping
|
|
|
|
|
// a UNIQUE constraint violation if the username is already taken.
|
2026-08-01 22:06:14 +02:00
|
|
|
func (d *DB) UpdateUserProfile(ctx context.Context, userID int64, username string, avatar, displayName, about *string) error {
|
2026-07-23 17:03:52 +02:00
|
|
|
result, err := d.q.UpdateUserProfile(ctx, dbgen.UpdateUserProfileParams{
|
2026-08-01 22:06:14 +02:00
|
|
|
Username: username,
|
|
|
|
|
Avatar: avatar,
|
|
|
|
|
DisplayName: displayName,
|
|
|
|
|
About: about,
|
|
|
|
|
ID: userID,
|
2026-07-19 15:34:27 +00:00
|
|
|
})
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("UpdateUserProfile: %w", err)
|
|
|
|
|
}
|
|
|
|
|
rows, err := result.RowsAffected()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("UpdateUserProfile rows: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if rows == 0 {
|
|
|
|
|
return fmt.Errorf("UpdateUserProfile: %w", ErrNotFound)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
// UpdateUserCustomStatus sets (or clears, with nil) the user's custom status
|
|
|
|
|
// line. Kept separate from UpdateUserProfile because it arrives on the
|
|
|
|
|
// presence path and must not overwrite a concurrent profile edit.
|
|
|
|
|
func (d *DB) UpdateUserCustomStatus(ctx context.Context, userID int64, customStatus *string) error {
|
|
|
|
|
if err := d.q.UpdateUserCustomStatus(ctx, dbgen.UpdateUserCustomStatusParams{
|
|
|
|
|
CustomStatus: customStatus,
|
|
|
|
|
ID: userID,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return fmt.Errorf("UpdateUserCustomStatus: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsAvatarFileURL reports whether url is currently some user's avatar. It is
|
|
|
|
|
// the authorization check that lets an uploaded avatar — an attachment with no
|
|
|
|
|
// channel, and therefore private to its uploader by default — be served to
|
|
|
|
|
// every authenticated user for exactly as long as it is in use.
|
|
|
|
|
func (d *DB) IsAvatarFileURL(ctx context.Context, url string) (bool, error) {
|
|
|
|
|
n, err := d.q.CountUsersWithAvatar(ctx, &url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("IsAvatarFileURL: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return n > 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 11:37:55 +02:00
|
|
|
// UpdateUserPassword sets a new password hash for the given user.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UpdateUserPassword(ctx context.Context, userID int64, newPasswordHash string) error {
|
|
|
|
|
if err := d.q.UpdateUserPassword(ctx, dbgen.UpdateUserPasswordParams{
|
2026-07-19 15:34:27 +00:00
|
|
|
Password: newPasswordHash,
|
|
|
|
|
ID: userID,
|
|
|
|
|
}); err != nil {
|
2026-04-01 11:37:55 +02:00
|
|
|
return fmt.Errorf("UpdateUserPassword: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListUserSessions returns all sessions for the given user in a single query.
|
|
|
|
|
// Results are ordered by created_at descending (newest first).
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) ListUserSessions(ctx context.Context, userID int64) ([]Session, error) {
|
|
|
|
|
rows, err := d.q.ListUserSessions(ctx, userID)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("ListUserSessions: %w", err)
|
|
|
|
|
}
|
2026-07-19 15:34:27 +00:00
|
|
|
sessions := make([]Session, 0, len(rows))
|
|
|
|
|
for _, s := range rows {
|
|
|
|
|
sessions = append(sessions, sessionFromGen(s))
|
2026-04-01 11:37:55 +02:00
|
|
|
}
|
|
|
|
|
return sessions, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteSessionByID removes a session by its ID, but only if it belongs to
|
|
|
|
|
// the specified user. Returns ErrNotFound if the session does not exist or
|
|
|
|
|
// does not belong to the user.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) DeleteSessionByID(ctx context.Context, sessionID, userID int64) error {
|
|
|
|
|
result, err := d.q.DeleteSessionByID(ctx, dbgen.DeleteSessionByIDParams{
|
2026-07-19 15:34:27 +00:00
|
|
|
ID: sessionID,
|
|
|
|
|
UserID: userID,
|
|
|
|
|
})
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("DeleteSessionByID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
rows, err := result.RowsAffected()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("DeleteSessionByID rows: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if rows == 0 {
|
|
|
|
|
return fmt.Errorf("DeleteSessionByID: %w", ErrNotFound)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|