parent
22c2b9f665
commit
3142d0a4be
@ -0,0 +1,8 @@
|
||||
import {env, RedisOptions} from '@extollo/lib'
|
||||
|
||||
export default {
|
||||
connection: {
|
||||
port: env('REDIS_PORT', 6379),
|
||||
host: env('REDIS_HOST', '127.0.0.1'),
|
||||
} as RedisOptions
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
import {Controller, view, Injectable, Collection} from '@extollo/lib'
|
||||
import {FeedPost} from '../../models/FeedPost.model'
|
||||
|
||||
/**
|
||||
* Feed Controller
|
||||
* ------------------------------------
|
||||
* Backend for routes related to my post feed.
|
||||
*/
|
||||
@Injectable()
|
||||
export class Feed extends Controller {
|
||||
public async feed(feedPosts: Collection<FeedPost>) {
|
||||
return view('feed', {
|
||||
feedPosts: feedPosts.toArray(),
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
import {Controller, view, Inject, Injectable, HTTPStatus, http, redirect} from '@extollo/lib'
|
||||
import {GoLink} from '../../models/GoLink.model'
|
||||
|
||||
/**
|
||||
* GoLinks Controller
|
||||
*/
|
||||
@Injectable()
|
||||
export class GoLinks extends Controller {
|
||||
async launch() {
|
||||
const short = this.request.safe('short').string()
|
||||
const link = await GoLink.query<GoLink>()
|
||||
.where('active', '=', true)
|
||||
.where('short', '=', short)
|
||||
.first()
|
||||
|
||||
if ( !link ) {
|
||||
return http(HTTPStatus.http404, 'Invalid or expired link.')
|
||||
}
|
||||
|
||||
return redirect(link.url)
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
import {Injectable, ParameterMiddleware, Collection, SecurityContext, Either, ResponseObject, right, Inject} from '@extollo/lib'
|
||||
import {FeedPost} from '../../../models/FeedPost.model'
|
||||
|
||||
/**
|
||||
* LoadFeedPosts Middleware
|
||||
* --------------------------------------------
|
||||
* Load feed posts visible to the user as a handler parameter.
|
||||
*/
|
||||
@Injectable()
|
||||
export class LoadFeedPosts extends ParameterMiddleware<Collection<FeedPost>, [] | [{ all: boolean }]> {
|
||||
@Inject()
|
||||
protected readonly security!: SecurityContext
|
||||
|
||||
async handle({ all = false } = {}): Promise<Either<ResponseObject, Collection<FeedPost>>> {
|
||||
const query = FeedPost.query<FeedPost>()
|
||||
.orderByDescending('posted_at')
|
||||
|
||||
if ( !all ) {
|
||||
query.limit(6)
|
||||
}
|
||||
|
||||
if ( !this.security.hasUser() ) {
|
||||
query.where('visible', '=', true)
|
||||
}
|
||||
|
||||
return right(await query.get().collect())
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
import {
|
||||
Either,
|
||||
http,
|
||||
HTTPStatus,
|
||||
Inject,
|
||||
Injectable,
|
||||
left,
|
||||
ParameterMiddleware,
|
||||
ResponseObject, right,
|
||||
SecurityContext,
|
||||
} from '@extollo/lib'
|
||||
import {Snippet} from '../../../models/Snippet.model'
|
||||
|
||||
/**
|
||||
* LoadSnippet Middleware
|
||||
* --------------------------------------------
|
||||
* Look up the Snippet instance from the request parameters.
|
||||
*/
|
||||
@Injectable()
|
||||
export class LoadSnippet extends ParameterMiddleware<Snippet> {
|
||||
@Inject()
|
||||
protected readonly security!: SecurityContext
|
||||
|
||||
async handle(): Promise<Either<ResponseObject, Snippet>> {
|
||||
const slug = String(this.request.input('slug') || '')
|
||||
if ( !slug ) {
|
||||
return left(http(HTTPStatus.http404))
|
||||
}
|
||||
|
||||
const snippet = await Snippet.query<Snippet>()
|
||||
.where('slug', '=', slug)
|
||||
.first()
|
||||
|
||||
if ( !snippet || snippet.usersOnly && !this.security.hasUser() ) {
|
||||
return left(http(HTTPStatus.http404))
|
||||
}
|
||||
|
||||
return right(snippet)
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
import {DatabaseService, FieldType, Inject, Injectable, Migration} from '@extollo/lib'
|
||||
|
||||
/**
|
||||
* CreateGolinksTableMigration
|
||||
* ----------------------------------
|
||||
* Create Table to store URL redirections
|
||||
*/
|
||||
@Injectable()
|
||||
export default class CreateGolinksTableMigration extends Migration {
|
||||
@Inject()
|
||||
protected readonly db!: DatabaseService
|
||||
|
||||
/**
|
||||
* Apply the migration.
|
||||
*/
|
||||
async up(): Promise<void> {
|
||||
const schema = this.db.get().schema()
|
||||
const table = await schema.table('go_links')
|
||||
|
||||
table.primaryKey('go_link_id').required()
|
||||
|
||||
table.column('active')
|
||||
.type(FieldType.bool)
|
||||
.default(true)
|
||||
|
||||
table.column('short')
|
||||
.type(FieldType.varchar)
|
||||
.required()
|
||||
.unique()
|
||||
|
||||
table.column('url')
|
||||
.type(FieldType.text)
|
||||
.required()
|
||||
|
||||
await schema.commit(table)
|
||||
}
|
||||
|
||||
/**
|
||||
* Undo the migration.
|
||||
*/
|
||||
async down(): Promise<void> {
|
||||
const schema = this.db.get().schema()
|
||||
const table = await schema.table('go_links')
|
||||
|
||||
table.dropIfExists()
|
||||
|
||||
await schema.commit(table)
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
import {Field, FieldType, Injectable, Model} from '@extollo/lib'
|
||||
|
||||
/**
|
||||
* GoLink Model
|
||||
* -----------------------------------
|
||||
* A shortened URL.
|
||||
*/
|
||||
@Injectable()
|
||||
export class GoLink extends Model<GoLink> {
|
||||
protected static table = 'go_links'
|
||||
protected static key = 'go_link_id'
|
||||
|
||||
@Field(FieldType.serial, 'go_link_id')
|
||||
protected id!: number
|
||||
|
||||
@Field(FieldType.bool)
|
||||
public active = true
|
||||
|
||||
@Field(FieldType.varchar)
|
||||
public short!: string
|
||||
|
||||
@Field(FieldType.text)
|
||||
public url!: string
|
||||
}
|
Loading…
Reference in new issue