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.

40 lines
1.1 KiB

import {make, Middleware, Injectable, Inject, SecurityContext, Logging, Config} from '@extollo/lib'
import * as PageViewModule from '../../models/PageView.model'
/**
* PageView Middleware
* --------------------------------------------
* Record the page view to the analytics table.
*/
@Injectable()
export class PageView extends Middleware {
@Inject()
protected readonly security!: SecurityContext
@Inject()
protected readonly logging!: Logging
@Inject()
protected readonly config!: Config
public async apply() {
if ( this.request.cookies.has(this.config.get('app.analytics.optOutCookie')) ) {
return
}
const view = make<PageViewModule.PageView>(PageViewModule.PageView)
const user = this.security.getUser()
// hostname
view.ip = `${this.request.address.address}:${this.request.address.port}`
view.method = this.request.method
view.endpoint = this.request.path
view.userId = user ? (user as any).userId : undefined
view.xhr = this.request.isXHR
this.logging.debug(this.request)
await view.save()
}
}