Skip to content

Commit

Permalink
Merge pull request #92 from curveball/user-agent
Browse files Browse the repository at this point in the history
User agent
  • Loading branch information
juhangsin authored Nov 11, 2019
2 parents f2d831f + de0bc4d commit 649a99a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
9 changes: 9 additions & 0 deletions mysql-schema/027-userlog3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SET NAMES utf8mb4;
START TRANSACTION;

INSERT INTO changelog VALUES (27, UNIX_TIMESTAMP());

ALTER TABLE user_log ADD
user_agent TEXT NULL;

COMMIT;
5 changes: 3 additions & 2 deletions src/log/formats/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { eventTypeString, LogEntry } from '../types';

export default function csv(log: LogEntry[]): string {

const header = 'time,eventType,ip\n';
const header = 'time,eventType,ip,userAgent\n';
return header + log.map( entry => {

return [
entry.time.toISOString(),
eventTypeString.get(entry.eventType),
entry.ip
entry.ip,
entry.userAgent
];

}).join('\n');
Expand Down
17 changes: 11 additions & 6 deletions src/log/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@ import { User } from '../user/types';
import { EventType, LogEntry } from './types';

export function log(eventType: EventType, ctx: Context): Promise<void>;
export function log(eventType: EventType, ip: string, userId: number): Promise<void>;
export function log(eventType: EventType, ip: string, userId: number, userAgent: string): Promise<void>;
export default async function log(
eventType: EventType,
arg1: string | Context,
arg2?: number,
arg3?: string
) {

if (isContext(arg1)) {
addLogEntry(
await addLogEntry(
eventType,
arg1.ip(),
arg1.state.session.user && arg1.state.session.user.id ? arg1.state.session.user.id : null
arg1.state.session.user && arg1.state.session.user.id ? arg1.state.session.user.id : null,
arg1.request.headers.get('User-Agent'),
);
} else {
addLogEntry(eventType, arg1, arg2);
await addLogEntry(eventType, arg1, arg2, arg3);
}

}

export async function addLogEntry(eventType: EventType, ip: string, userId: number): Promise<void> {
export async function addLogEntry(eventType: EventType, ip: string, userId: number, userAgent: string): Promise<void> {

await db.query('INSERT INTO user_log SET time = UNIX_TIMESTAMP(), ?', {
user_id: userId,
event_type: eventType,
ip: ip
ip: ip,
user_agent: userAgent
});

}
Expand All @@ -39,6 +42,7 @@ type LogRow = {
ip: string,
time: number,
event_type: EventType,
user_agent: string
};

export async function findByUser(user: User): Promise<LogEntry[]> {
Expand All @@ -52,6 +56,7 @@ export async function findByUser(user: User): Promise<LogEntry[]> {
time: new Date(row.time * 1000),
ip: row.ip,
eventType: row.event_type,
userAgent: row.user_agent
};
});

Expand Down
1 change: 1 addition & 0 deletions src/log/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type LogEntry = {
userId: number,
ip: string,
eventType: EventType,
userAgent: string
};

export const eventTypeString = new Map<EventType, string>([
Expand Down

0 comments on commit 649a99a

Please sign in to comment.