Skip to content

Commit

Permalink
store IP address, timezone result, and useragent JSON in Promise model
Browse files Browse the repository at this point in the history
…closes #170
  • Loading branch information
chrisbutler committed Apr 7, 2018
1 parent 4a6fc6a commit 34659e4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/express.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express'
import expressHandlebars from 'express-handlebars'
import sassMiddleware from 'node-sass-middleware'
import useragent from 'express-useragent'

import { PORT } from './config'
import log from '../lib/logger'
Expand All @@ -12,6 +13,8 @@ import '../helpers/utils'

const app = express()

app.use(useragent.express())

app.use(sassMiddleware({
src: 'styles',
dest: 'public',
Expand Down
12 changes: 9 additions & 3 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import app from './express'
import log from '../lib/logger'
import sendMail from '../lib/mail'

import _ from 'lodash'

import { Sequelize } from '../db/sequelize'
import { promiseGallerySort } from '../models/promise'
import { Promises, Users } from '../models'
Expand Down Expand Up @@ -36,12 +38,14 @@ app.get('/_s/:user/:urtext(*)', (req, res, next) => {
const { ip, originalUrl: urtext, user: { username } = {} } = req

let parsedPromise = parsePromise({ username, urtext })
let foundPromise = undefined

return Promises.find({
where: {
id: parsedPromise.id
},
}).then(async(foundPromise) => {
}).then(async(p) => {
foundPromise = p
let toLog = { level: 'debug', state: 'exists' }

if (!foundPromise) {
Expand All @@ -52,7 +56,9 @@ app.get('/_s/:user/:urtext(*)', (req, res, next) => {
})

if (parsedPromise) {
foundPromise = await Promises.create({ ...parsedPromise })
const useragent = JSON.stringify(_.pickBy(req.useragent))
foundPromise = await Promises
.create({ ...parsedPromise, ip, useragent })
.catch((reason) => { // creating promise failed
log.error('promise creation error', reason)
return res.render('404') // FIXME?
Expand All @@ -76,7 +82,7 @@ app.get('/_s/:user/:urtext(*)', (req, res, next) => {

return next()
})
.catch((reason) => { // couldn't handle this promise
.catch((...reason) => { // couldn't handle this promise
log.error('promise finding error', reason)
return res.render('404') // FIXME?
})
Expand Down
1 change: 1 addition & 0 deletions lib/parse/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const parsePromise = ({
...promise,
id,
slug,
timezone,
what,
urtext,
cred: parseCredit({ dueDate: tdue, finishDate: tfin }),
Expand Down
4 changes: 4 additions & 0 deletions models/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export default sequelize.define('promises', { // sequelize needs the doublequote

clix: { type: Sequelize.INTEGER, defaultValue: 1 }, // number of clicks a promise has gotten
note: { type: Sequelize.STRING }, // optional additional notes or context for the promise

ip: { type: Sequelize.STRING, defaultValue: null }, //
timezone: { type: Sequelize.STRING }, //
useragent: { type: Sequelize.JSON }, //
}, {
indexes: [
{
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"express": "^4.16.3",
"express-handlebars": "^3.0.0",
"express-subdomain-handler": "^0.1.0",
"express-useragent": "^1.0.12",
"ipapi.co": "^0.2.3",
"lodash": "^4.17.5",
"mailgun-js": "^0.16.0",
Expand Down

0 comments on commit 34659e4

Please sign in to comment.