This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
/
intents.js
119 lines (104 loc) · 4.08 KB
/
intents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var boom = require('boom')
var braveHapi = require('../brave-hapi')
var bson = require('bson')
var helper = require('./helper')
var Joi = require('joi')
var underscore = require('underscore')
var v1 = {}
/*
POST /v1/users/{userId}/intents
{ "sessionID": "...", "type": "...", "timestamp": "...", "data": "..." }
always creates
*/
var intentSchema = Joi.object().keys({
sessionId: Joi.string().guid().required().description('the identity of the session'),
type: Joi.string().min(6).required().description('e.g., "browser.site.visit"'),
timestamp: Joi.number().positive().required().description('opaque number identifying a instance of time'),
data: Joi.object().required().description('an opaque JSON object')
})
var resultSchema = Joi.object().keys({
replacements: Joi.number().min(0).optional().description('the number of ad replacements for this session')
})
v1.post =
{ handler: function (runtime) {
return async function (request, reply) {
var intent, result, user
var debug = braveHapi.debug(module, request)
var userId = request.params.userId.toUpperCase()
var container = request.payload.sessionId ? request.payload : request.payload.payload
var sessionId = container.sessionId.toUpperCase()
var type = container.type
var timestamp = container.timestamp
var payload = container.payload
var users = runtime.db.get('users')
var intents = runtime.db.get('intents')
user = await users.findOne({ userId: userId })
if (!user) { return reply(boom.notFound('user entry does not exist: ' + userId)) }
result = await helper.verify(debug, user, request.payload)
if (result) return reply(result)
result = await helper.sessionId2stats(runtime, userId, sessionId)
// NB: alternatives is temporary
reply(user.version ? helper.add_nonce_data(result) : result)
intent = { userId: userId,
sessionID: sessionId,
timestamp: bson.Timestamp(),
type: type,
payload: underscore.extend(payload, { timestamp: timestamp })
}
try {
await intents.insert(intent)
} catch (ex) {
debug('insert failed for intents', ex)
}
}
},
description: 'Records user activity',
notes: 'The browser uses this to indicate <a href="https://github.com/brave/vault/wiki/Intents" target="_blank">user activity</a>, such as clicking on a link.',
tags: ['api'],
validate:
{ params: { userId: Joi.string().guid().required().description('the identity of the user entry') },
payload: Joi.alternatives(intentSchema, helper.add_header_schema(intentSchema)) // NB: alternatives is temporary
},
response: {
schema: Joi.alternatives(resultSchema, helper.add_nonce_schema(resultSchema)) // NB: alternatives is temporary
/*
status: {
400: Joi.object({
boomlet: Joi.string().required().description('payload is not cryptographically-signed')
}),
400: Joi.object({
boomlet: Joi.string().required().description('unknown user entry cryptography version')
}),
400: Joi.object({
boomlet: Joi.string().required().description('header.nonce is invalid')
}),
404: Joi.object({
boomlet: Joi.string().required().description('user entry does not exist')
}),
422: Joi.object({
boomlet: Joi.string().required().description('user entry is not cryptographically-enabled')
}),
422: Joi.object({
boomlet: Joi.string().required().description('header.nonce is untimely')
}),
422: Joi.object({
boomlet: Joi.string().required().description('signature error')
})
}
*/
}
}
module.exports.routes =
[
braveHapi.routes.async().post().path('/v1/users/{userId}/intents').config(v1.post)
]
module.exports.initialize = async function (debug, runtime) {
runtime.db.checkIndices(debug,
[ { category: runtime.db.get('intents'),
name: 'intents',
property: 'userId',
empty: { userId: '', sessionId: '', timestamp: bson.Timestamp.ZERO, type: '', payload: {} },
others: [ { userId: 1 }, { sessionId: 1 }, { timestamp: 1 } ]
}
])
}