| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: users.sql |
| 5 | |
| 6 | package usersdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const bumpUserSessionEpoch = `-- name: BumpUserSessionEpoch :exec |
| 15 | UPDATE users SET session_epoch = session_epoch + 1 WHERE id = $1 |
| 16 | ` |
| 17 | |
| 18 | func (q *Queries) BumpUserSessionEpoch(ctx context.Context, db DBTX, id int64) error { |
| 19 | _, err := db.Exec(ctx, bumpUserSessionEpoch, id) |
| 20 | return err |
| 21 | } |
| 22 | |
| 23 | const countRecentUsernameChanges = `-- name: CountRecentUsernameChanges :one |
| 24 | SELECT count(*) FROM username_redirects |
| 25 | WHERE user_id = $1 AND changed_at > $2 |
| 26 | ` |
| 27 | |
| 28 | type CountRecentUsernameChangesParams struct { |
| 29 | UserID int64 |
| 30 | ChangedAt pgtype.Timestamptz |
| 31 | } |
| 32 | |
| 33 | // Drives the 3-changes-per-60d cap. |
| 34 | func (q *Queries) CountRecentUsernameChanges(ctx context.Context, db DBTX, arg CountRecentUsernameChangesParams) (int64, error) { |
| 35 | row := db.QueryRow(ctx, countRecentUsernameChanges, arg.UserID, arg.ChangedAt) |
| 36 | var count int64 |
| 37 | err := row.Scan(&count) |
| 38 | return count, err |
| 39 | } |
| 40 | |
| 41 | const countUsers = `-- name: CountUsers :one |
| 42 | SELECT count(*) FROM users WHERE deleted_at IS NULL |
| 43 | ` |
| 44 | |
| 45 | func (q *Queries) CountUsers(ctx context.Context, db DBTX) (int64, error) { |
| 46 | row := db.QueryRow(ctx, countUsers) |
| 47 | var count int64 |
| 48 | err := row.Scan(&count) |
| 49 | return count, err |
| 50 | } |
| 51 | |
| 52 | const createUser = `-- name: CreateUser :one |
| 53 | |
| 54 | INSERT INTO users (username, display_name, password_hash) |
| 55 | VALUES ($1, $2, $3) |
| 56 | RETURNING id, username, display_name, primary_email_id, password_hash, password_algo, password_updated_at, email_verified, last_login_at, suspended_at, suspended_reason, deleted_at, created_at, updated_at, bio, location, website, company, pronouns, avatar_object_key, theme, session_epoch, is_site_admin, include_private_contributions, plan |
| 57 | ` |
| 58 | |
| 59 | type CreateUserParams struct { |
| 60 | Username string |
| 61 | DisplayName string |
| 62 | PasswordHash string |
| 63 | } |
| 64 | |
| 65 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 66 | func (q *Queries) CreateUser(ctx context.Context, db DBTX, arg CreateUserParams) (User, error) { |
| 67 | row := db.QueryRow(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash) |
| 68 | var i User |
| 69 | err := row.Scan( |
| 70 | &i.ID, |
| 71 | &i.Username, |
| 72 | &i.DisplayName, |
| 73 | &i.PrimaryEmailID, |
| 74 | &i.PasswordHash, |
| 75 | &i.PasswordAlgo, |
| 76 | &i.PasswordUpdatedAt, |
| 77 | &i.EmailVerified, |
| 78 | &i.LastLoginAt, |
| 79 | &i.SuspendedAt, |
| 80 | &i.SuspendedReason, |
| 81 | &i.DeletedAt, |
| 82 | &i.CreatedAt, |
| 83 | &i.UpdatedAt, |
| 84 | &i.Bio, |
| 85 | &i.Location, |
| 86 | &i.Website, |
| 87 | &i.Company, |
| 88 | &i.Pronouns, |
| 89 | &i.AvatarObjectKey, |
| 90 | &i.Theme, |
| 91 | &i.SessionEpoch, |
| 92 | &i.IsSiteAdmin, |
| 93 | &i.IncludePrivateContributions, |
| 94 | &i.Plan, |
| 95 | ) |
| 96 | return i, err |
| 97 | } |
| 98 | |
| 99 | const getUserByID = `-- name: GetUserByID :one |
| 100 | SELECT id, username, display_name, primary_email_id, password_hash, password_algo, password_updated_at, email_verified, last_login_at, suspended_at, suspended_reason, deleted_at, created_at, updated_at, bio, location, website, company, pronouns, avatar_object_key, theme, session_epoch, is_site_admin, include_private_contributions, plan |
| 101 | FROM users |
| 102 | WHERE id = $1 AND deleted_at IS NULL |
| 103 | ` |
| 104 | |
| 105 | func (q *Queries) GetUserByID(ctx context.Context, db DBTX, id int64) (User, error) { |
| 106 | row := db.QueryRow(ctx, getUserByID, id) |
| 107 | var i User |
| 108 | err := row.Scan( |
| 109 | &i.ID, |
| 110 | &i.Username, |
| 111 | &i.DisplayName, |
| 112 | &i.PrimaryEmailID, |
| 113 | &i.PasswordHash, |
| 114 | &i.PasswordAlgo, |
| 115 | &i.PasswordUpdatedAt, |
| 116 | &i.EmailVerified, |
| 117 | &i.LastLoginAt, |
| 118 | &i.SuspendedAt, |
| 119 | &i.SuspendedReason, |
| 120 | &i.DeletedAt, |
| 121 | &i.CreatedAt, |
| 122 | &i.UpdatedAt, |
| 123 | &i.Bio, |
| 124 | &i.Location, |
| 125 | &i.Website, |
| 126 | &i.Company, |
| 127 | &i.Pronouns, |
| 128 | &i.AvatarObjectKey, |
| 129 | &i.Theme, |
| 130 | &i.SessionEpoch, |
| 131 | &i.IsSiteAdmin, |
| 132 | &i.IncludePrivateContributions, |
| 133 | &i.Plan, |
| 134 | ) |
| 135 | return i, err |
| 136 | } |
| 137 | |
| 138 | const getUserByUsername = `-- name: GetUserByUsername :one |
| 139 | SELECT id, username, display_name, primary_email_id, password_hash, password_algo, password_updated_at, email_verified, last_login_at, suspended_at, suspended_reason, deleted_at, created_at, updated_at, bio, location, website, company, pronouns, avatar_object_key, theme, session_epoch, is_site_admin, include_private_contributions, plan |
| 140 | FROM users |
| 141 | WHERE username = $1 AND deleted_at IS NULL |
| 142 | ` |
| 143 | |
| 144 | func (q *Queries) GetUserByUsername(ctx context.Context, db DBTX, username string) (User, error) { |
| 145 | row := db.QueryRow(ctx, getUserByUsername, username) |
| 146 | var i User |
| 147 | err := row.Scan( |
| 148 | &i.ID, |
| 149 | &i.Username, |
| 150 | &i.DisplayName, |
| 151 | &i.PrimaryEmailID, |
| 152 | &i.PasswordHash, |
| 153 | &i.PasswordAlgo, |
| 154 | &i.PasswordUpdatedAt, |
| 155 | &i.EmailVerified, |
| 156 | &i.LastLoginAt, |
| 157 | &i.SuspendedAt, |
| 158 | &i.SuspendedReason, |
| 159 | &i.DeletedAt, |
| 160 | &i.CreatedAt, |
| 161 | &i.UpdatedAt, |
| 162 | &i.Bio, |
| 163 | &i.Location, |
| 164 | &i.Website, |
| 165 | &i.Company, |
| 166 | &i.Pronouns, |
| 167 | &i.AvatarObjectKey, |
| 168 | &i.Theme, |
| 169 | &i.SessionEpoch, |
| 170 | &i.IsSiteAdmin, |
| 171 | &i.IncludePrivateContributions, |
| 172 | &i.Plan, |
| 173 | ) |
| 174 | return i, err |
| 175 | } |
| 176 | |
| 177 | const getUserByUsernameIncludingDeleted = `-- name: GetUserByUsernameIncludingDeleted :one |
| 178 | SELECT id, username, display_name, primary_email_id, password_hash, password_algo, password_updated_at, email_verified, last_login_at, suspended_at, suspended_reason, deleted_at, created_at, updated_at, bio, location, website, company, pronouns, avatar_object_key, theme, session_epoch, is_site_admin, include_private_contributions, plan FROM users WHERE username = $1 |
| 179 | ` |
| 180 | |
| 181 | func (q *Queries) GetUserByUsernameIncludingDeleted(ctx context.Context, db DBTX, username string) (User, error) { |
| 182 | row := db.QueryRow(ctx, getUserByUsernameIncludingDeleted, username) |
| 183 | var i User |
| 184 | err := row.Scan( |
| 185 | &i.ID, |
| 186 | &i.Username, |
| 187 | &i.DisplayName, |
| 188 | &i.PrimaryEmailID, |
| 189 | &i.PasswordHash, |
| 190 | &i.PasswordAlgo, |
| 191 | &i.PasswordUpdatedAt, |
| 192 | &i.EmailVerified, |
| 193 | &i.LastLoginAt, |
| 194 | &i.SuspendedAt, |
| 195 | &i.SuspendedReason, |
| 196 | &i.DeletedAt, |
| 197 | &i.CreatedAt, |
| 198 | &i.UpdatedAt, |
| 199 | &i.Bio, |
| 200 | &i.Location, |
| 201 | &i.Website, |
| 202 | &i.Company, |
| 203 | &i.Pronouns, |
| 204 | &i.AvatarObjectKey, |
| 205 | &i.Theme, |
| 206 | &i.SessionEpoch, |
| 207 | &i.IsSiteAdmin, |
| 208 | &i.IncludePrivateContributions, |
| 209 | &i.Plan, |
| 210 | ) |
| 211 | return i, err |
| 212 | } |
| 213 | |
| 214 | const getUserIncludingDeleted = `-- name: GetUserIncludingDeleted :one |
| 215 | SELECT id, username, display_name, primary_email_id, password_hash, password_algo, password_updated_at, email_verified, last_login_at, suspended_at, suspended_reason, deleted_at, created_at, updated_at, bio, location, website, company, pronouns, avatar_object_key, theme, session_epoch, is_site_admin, include_private_contributions, plan FROM users WHERE id = $1 |
| 216 | ` |
| 217 | |
| 218 | // Like GetUserByID but returns the row even when deleted_at IS NOT NULL. |
| 219 | func (q *Queries) GetUserIncludingDeleted(ctx context.Context, db DBTX, id int64) (User, error) { |
| 220 | row := db.QueryRow(ctx, getUserIncludingDeleted, id) |
| 221 | var i User |
| 222 | err := row.Scan( |
| 223 | &i.ID, |
| 224 | &i.Username, |
| 225 | &i.DisplayName, |
| 226 | &i.PrimaryEmailID, |
| 227 | &i.PasswordHash, |
| 228 | &i.PasswordAlgo, |
| 229 | &i.PasswordUpdatedAt, |
| 230 | &i.EmailVerified, |
| 231 | &i.LastLoginAt, |
| 232 | &i.SuspendedAt, |
| 233 | &i.SuspendedReason, |
| 234 | &i.DeletedAt, |
| 235 | &i.CreatedAt, |
| 236 | &i.UpdatedAt, |
| 237 | &i.Bio, |
| 238 | &i.Location, |
| 239 | &i.Website, |
| 240 | &i.Company, |
| 241 | &i.Pronouns, |
| 242 | &i.AvatarObjectKey, |
| 243 | &i.Theme, |
| 244 | &i.SessionEpoch, |
| 245 | &i.IsSiteAdmin, |
| 246 | &i.IncludePrivateContributions, |
| 247 | &i.Plan, |
| 248 | ) |
| 249 | return i, err |
| 250 | } |
| 251 | |
| 252 | const getUserSessionEpoch = `-- name: GetUserSessionEpoch :one |
| 253 | SELECT session_epoch FROM users WHERE id = $1 |
| 254 | ` |
| 255 | |
| 256 | func (q *Queries) GetUserSessionEpoch(ctx context.Context, db DBTX, id int64) (int32, error) { |
| 257 | row := db.QueryRow(ctx, getUserSessionEpoch, id) |
| 258 | var session_epoch int32 |
| 259 | err := row.Scan(&session_epoch) |
| 260 | return session_epoch, err |
| 261 | } |
| 262 | |
| 263 | const linkUserPrimaryEmail = `-- name: LinkUserPrimaryEmail :exec |
| 264 | UPDATE users |
| 265 | SET primary_email_id = $2 |
| 266 | WHERE id = $1 |
| 267 | ` |
| 268 | |
| 269 | type LinkUserPrimaryEmailParams struct { |
| 270 | ID int64 |
| 271 | PrimaryEmailID pgtype.Int8 |
| 272 | } |
| 273 | |
| 274 | // Sets the FK only. Does NOT flip users.email_verified — that happens via |
| 275 | // MarkUserEmailPrimaryVerified after the user clicks the verification link. |
| 276 | func (q *Queries) LinkUserPrimaryEmail(ctx context.Context, db DBTX, arg LinkUserPrimaryEmailParams) error { |
| 277 | _, err := db.Exec(ctx, linkUserPrimaryEmail, arg.ID, arg.PrimaryEmailID) |
| 278 | return err |
| 279 | } |
| 280 | |
| 281 | const markUserEmailPrimaryVerified = `-- name: MarkUserEmailPrimaryVerified :exec |
| 282 | UPDATE users |
| 283 | SET email_verified = true |
| 284 | WHERE id = $1 |
| 285 | ` |
| 286 | |
| 287 | // Called after MarkUserEmailVerified for the primary email, to flip the |
| 288 | // denormalized users.email_verified flag. |
| 289 | func (q *Queries) MarkUserEmailPrimaryVerified(ctx context.Context, db DBTX, id int64) error { |
| 290 | _, err := db.Exec(ctx, markUserEmailPrimaryVerified, id) |
| 291 | return err |
| 292 | } |
| 293 | |
| 294 | const renameUser = `-- name: RenameUser :exec |
| 295 | UPDATE users |
| 296 | SET username = $2 |
| 297 | WHERE id = $1 |
| 298 | ` |
| 299 | |
| 300 | type RenameUserParams struct { |
| 301 | ID int64 |
| 302 | Username string |
| 303 | } |
| 304 | |
| 305 | // Wrapped by the username-change flow inside a tx that also writes |
| 306 | // username_redirects, so the old name becomes a redirect target atomically. |
| 307 | func (q *Queries) RenameUser(ctx context.Context, db DBTX, arg RenameUserParams) error { |
| 308 | _, err := db.Exec(ctx, renameUser, arg.ID, arg.Username) |
| 309 | return err |
| 310 | } |
| 311 | |
| 312 | const restoreUserAccount = `-- name: RestoreUserAccount :exec |
| 313 | UPDATE users SET deleted_at = NULL WHERE id = $1 |
| 314 | ` |
| 315 | |
| 316 | // Clears deleted_at; called when a user logs in within the 14-day grace |
| 317 | // window. The login handler enforces the window check before calling. |
| 318 | func (q *Queries) RestoreUserAccount(ctx context.Context, db DBTX, id int64) error { |
| 319 | _, err := db.Exec(ctx, restoreUserAccount, id) |
| 320 | return err |
| 321 | } |
| 322 | |
| 323 | const softDeleteUser = `-- name: SoftDeleteUser :exec |
| 324 | UPDATE users |
| 325 | SET deleted_at = now() |
| 326 | WHERE id = $1 |
| 327 | ` |
| 328 | |
| 329 | func (q *Queries) SoftDeleteUser(ctx context.Context, db DBTX, id int64) error { |
| 330 | _, err := db.Exec(ctx, softDeleteUser, id) |
| 331 | return err |
| 332 | } |
| 333 | |
| 334 | const suspendUser = `-- name: SuspendUser :exec |
| 335 | UPDATE users |
| 336 | SET suspended_at = now(), |
| 337 | suspended_reason = $2 |
| 338 | WHERE id = $1 |
| 339 | ` |
| 340 | |
| 341 | type SuspendUserParams struct { |
| 342 | ID int64 |
| 343 | SuspendedReason pgtype.Text |
| 344 | } |
| 345 | |
| 346 | func (q *Queries) SuspendUser(ctx context.Context, db DBTX, arg SuspendUserParams) error { |
| 347 | _, err := db.Exec(ctx, suspendUser, arg.ID, arg.SuspendedReason) |
| 348 | return err |
| 349 | } |
| 350 | |
| 351 | const touchUserLastLogin = `-- name: TouchUserLastLogin :exec |
| 352 | UPDATE users |
| 353 | SET last_login_at = now() |
| 354 | WHERE id = $1 |
| 355 | ` |
| 356 | |
| 357 | func (q *Queries) TouchUserLastLogin(ctx context.Context, db DBTX, id int64) error { |
| 358 | _, err := db.Exec(ctx, touchUserLastLogin, id) |
| 359 | return err |
| 360 | } |
| 361 | |
| 362 | const unsuspendUser = `-- name: UnsuspendUser :exec |
| 363 | UPDATE users |
| 364 | SET suspended_at = NULL, |
| 365 | suspended_reason = NULL |
| 366 | WHERE id = $1 |
| 367 | ` |
| 368 | |
| 369 | // Clears the suspended state. Mirrors SuspendUser; used by the |
| 370 | // /admin/users/{id}/unsuspend handler. Replaces an inline UPDATE |
| 371 | // in admin/users.go (SR2 M2). |
| 372 | func (q *Queries) UnsuspendUser(ctx context.Context, db DBTX, id int64) error { |
| 373 | _, err := db.Exec(ctx, unsuspendUser, id) |
| 374 | return err |
| 375 | } |
| 376 | |
| 377 | const updateUserAvatarKey = `-- name: UpdateUserAvatarKey :exec |
| 378 | UPDATE users |
| 379 | SET avatar_object_key = $2 |
| 380 | WHERE id = $1 |
| 381 | ` |
| 382 | |
| 383 | type UpdateUserAvatarKeyParams struct { |
| 384 | ID int64 |
| 385 | AvatarObjectKey pgtype.Text |
| 386 | } |
| 387 | |
| 388 | func (q *Queries) UpdateUserAvatarKey(ctx context.Context, db DBTX, arg UpdateUserAvatarKeyParams) error { |
| 389 | _, err := db.Exec(ctx, updateUserAvatarKey, arg.ID, arg.AvatarObjectKey) |
| 390 | return err |
| 391 | } |
| 392 | |
| 393 | const updateUserPassword = `-- name: UpdateUserPassword :exec |
| 394 | UPDATE users |
| 395 | SET password_hash = $2, |
| 396 | password_algo = $3, |
| 397 | password_updated_at = now() |
| 398 | WHERE id = $1 |
| 399 | ` |
| 400 | |
| 401 | type UpdateUserPasswordParams struct { |
| 402 | ID int64 |
| 403 | PasswordHash string |
| 404 | PasswordAlgo string |
| 405 | } |
| 406 | |
| 407 | func (q *Queries) UpdateUserPassword(ctx context.Context, db DBTX, arg UpdateUserPasswordParams) error { |
| 408 | _, err := db.Exec(ctx, updateUserPassword, arg.ID, arg.PasswordHash, arg.PasswordAlgo) |
| 409 | return err |
| 410 | } |
| 411 | |
| 412 | const updateUserPrivateContributions = `-- name: UpdateUserPrivateContributions :exec |
| 413 | UPDATE users SET include_private_contributions = $2 WHERE id = $1 |
| 414 | ` |
| 415 | |
| 416 | type UpdateUserPrivateContributionsParams struct { |
| 417 | ID int64 |
| 418 | IncludePrivateContributions bool |
| 419 | } |
| 420 | |
| 421 | func (q *Queries) UpdateUserPrivateContributions(ctx context.Context, db DBTX, arg UpdateUserPrivateContributionsParams) error { |
| 422 | _, err := db.Exec(ctx, updateUserPrivateContributions, arg.ID, arg.IncludePrivateContributions) |
| 423 | return err |
| 424 | } |
| 425 | |
| 426 | const updateUserProfile = `-- name: UpdateUserProfile :exec |
| 427 | UPDATE users |
| 428 | SET display_name = $2, |
| 429 | bio = $3, |
| 430 | location = $4, |
| 431 | website = $5, |
| 432 | company = $6, |
| 433 | pronouns = $7 |
| 434 | WHERE id = $1 |
| 435 | ` |
| 436 | |
| 437 | type UpdateUserProfileParams struct { |
| 438 | ID int64 |
| 439 | DisplayName string |
| 440 | Bio string |
| 441 | Location string |
| 442 | Website string |
| 443 | Company string |
| 444 | Pronouns string |
| 445 | } |
| 446 | |
| 447 | func (q *Queries) UpdateUserProfile(ctx context.Context, db DBTX, arg UpdateUserProfileParams) error { |
| 448 | _, err := db.Exec(ctx, updateUserProfile, |
| 449 | arg.ID, |
| 450 | arg.DisplayName, |
| 451 | arg.Bio, |
| 452 | arg.Location, |
| 453 | arg.Website, |
| 454 | arg.Company, |
| 455 | arg.Pronouns, |
| 456 | ) |
| 457 | return err |
| 458 | } |
| 459 | |
| 460 | const updateUserTheme = `-- name: UpdateUserTheme :exec |
| 461 | UPDATE users SET theme = $2 WHERE id = $1 |
| 462 | ` |
| 463 | |
| 464 | type UpdateUserThemeParams struct { |
| 465 | ID int64 |
| 466 | Theme string |
| 467 | } |
| 468 | |
| 469 | func (q *Queries) UpdateUserTheme(ctx context.Context, db DBTX, arg UpdateUserThemeParams) error { |
| 470 | _, err := db.Exec(ctx, updateUserTheme, arg.ID, arg.Theme) |
| 471 | return err |
| 472 | } |
| 473 |