You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
39 lines
1.2 KiB
2 years ago
|
import {Controller, http, HTTPStatus, Inject, Injectable, Logging, view} from '@extollo/lib'
|
||
|
import {Snippet} from '../../models/Snippet.model'
|
||
|
|
||
|
/**
|
||
|
* Snippets Controller
|
||
|
* ------------------------------------
|
||
|
* Put some description here.
|
||
|
*/
|
||
|
@Injectable()
|
||
|
export class Snippets extends Controller {
|
||
|
@Inject()
|
||
|
protected readonly logging!: Logging
|
||
|
|
||
|
public async viewSnippet() {
|
||
|
const slug = this.request.safe('slug').string()
|
||
|
const snippet = await Snippet.query<Snippet>()
|
||
|
.where('slug', '=', slug)
|
||
|
.first()
|
||
|
|
||
|
if ( !snippet ) {
|
||
|
return http(HTTPStatus.http404, 'Snippet URL is invalid.')
|
||
|
}
|
||
|
|
||
|
const needsAccessKey = snippet?.accessKey && snippet.accessKey !== this.request.input('accessKey')
|
||
|
const needsConfirm = snippet?.singleAccessOnly && !this.request.input('confirmSingleAccess')
|
||
|
|
||
|
if ( snippet?.singleAccessOnly && !needsAccessKey && !needsConfirm ) {
|
||
|
await snippet.delete()
|
||
|
}
|
||
|
|
||
|
return view('snippet', {
|
||
|
snippet,
|
||
|
needsAccessKey,
|
||
|
needsConfirm,
|
||
|
accessKey: this.request.input('accessKey'),
|
||
|
})
|
||
|
}
|
||
|
}
|