Adds the ability for an owner to share a computer build via a public, unauthenticated link, inspired by a feature request from the original pcbuilder repo (no such feature actually existed there — implemented from scratch here). - New columns on computers: share_token (unique, nullable), share_enabled, share_show_sell_price, share_show_cost - computer_share_service.ts: generates a non-guessable share token (crypto.randomBytes, base64url), enables/disables sharing, and supports rotating the token to invalidate previously distributed links - ComputerShareController (authenticated, owner-scoped like the rest of the app): PUT /computers/:id/share to update settings, POST /computers/:id/share/rotate to rotate the token - PublicComputerController + GET /share/:token (outside any auth/guest group, reachable anonymously): resolves only computers with share_enabled = true, 404 otherwise - ComputerPublicTransformer: dedicated read-only projection that never leaks userId, and only includes sellPrice/cost when the owner opted in via share_show_sell_price / share_show_cost — everything else (title, scores, state, components, pictures) is always visible on a shared link - ComputerShareModal.vue: owner-facing UI (toggle sharing, choose which prices to expose, copy the public link, revoke/rotate it) wired into computers/show.vue via a new 'Share' button - share/show.vue: public page rendered with EmptyLayout (no authenticated header/sidebar) - Boolean columns use a cast since MySQL returns tinyint(1), not native booleans, through Lucid - 6 new functional tests covering: enabling sharing, price visibility toggles, disabling sharing revokes the link, cross-user protection on the share settings endpoint (404), token rotation invalidating the old link, and anonymous access to a shared computer
77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import { DateTime } from 'luxon'
|
|
import { BaseModel, belongsTo, column, computed, hasMany } from '@adonisjs/lucid/orm'
|
|
import type { BelongsTo, HasMany } from '@adonisjs/lucid/types/relations'
|
|
import logger from '@adonisjs/core/services/logger'
|
|
import ComputerState from '#models/computer_state'
|
|
import ComputerPicture from '#models/computer_picture'
|
|
import Component from '#models/component'
|
|
|
|
export default class Computer extends BaseModel {
|
|
@column({ isPrimary: true })
|
|
declare id: number
|
|
|
|
@column()
|
|
declare title: string
|
|
|
|
@column()
|
|
declare sellPrice: number
|
|
|
|
@column()
|
|
declare gpuScore: number
|
|
|
|
@column()
|
|
declare cpuScore: number
|
|
|
|
@column()
|
|
declare globalScore: number
|
|
|
|
@column({ serializeAs: null })
|
|
declare stateId: number
|
|
|
|
@belongsTo(() => ComputerState, {
|
|
foreignKey: 'stateId',
|
|
})
|
|
declare state: BelongsTo<typeof ComputerState>
|
|
|
|
@hasMany(() => Component)
|
|
declare components: HasMany<typeof Component>
|
|
|
|
@hasMany(() => ComputerPicture, {
|
|
foreignKey: 'computerId',
|
|
})
|
|
declare pictures: HasMany<typeof ComputerPicture>
|
|
|
|
@column({ serializeAs: null })
|
|
declare userId: number
|
|
|
|
@column({ serializeAs: null })
|
|
declare shareToken: string | null
|
|
|
|
@column({ consume: (value: unknown) => Boolean(value) })
|
|
declare shareEnabled: boolean
|
|
|
|
@column({ consume: (value: unknown) => Boolean(value) })
|
|
declare shareShowSellPrice: boolean
|
|
|
|
@column({ consume: (value: unknown) => Boolean(value) })
|
|
declare shareShowCost: boolean
|
|
|
|
@column.dateTime({ autoCreate: true })
|
|
declare createdAt: DateTime
|
|
|
|
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
|
declare updatedAt: DateTime | null
|
|
|
|
@computed()
|
|
get cost(): number {
|
|
if (!this.$preloaded.components) {
|
|
logger.warn(
|
|
{ computerId: this.id },
|
|
'Computer.cost accessed without preloading "components"; returning 0'
|
|
)
|
|
return 0
|
|
}
|
|
return this.components.reduce((total, component) => total + Number(component.price), 0)
|
|
}
|
|
}
|