-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (59 loc) · 2.17 KB
/
index.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
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const compose = require('lodash.compose')
const cors = require('cors')({ origin: true });
const promisify = func => compose(Promise.resolve.bind(Promise), func)
module.exports = config => {
const { messages='messages', chats='chats', members='members' } = config.paths || {}
admin.initializeApp(Object.assign({}, functions.config().firebase, {
credential: admin.credential.cert(config.serviceAccount),
}))
// create an authentication endpoint.
// calls our backend to validate JWT
// if it passes, generate firebase token for the user
const authenticate = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const { method, query } = req
if (method !== 'GET')
return res.status(405).end()
const authPromise = promisify(config.authenticate) // in case it returns a non-promise
authPromise(query)
.then(({ uid, additionalClaims }) => admin.auth().createCustomToken(uid, additionalClaims))
.then(token => {
res.set('Cache-Control', 'public, max-age=600, s-maxage=1800');
res.status(200).send({ token })
})
.catch(e => res.status(e.status || 500).send(e))
})
})
// listen for new messages and notify our backend
const onNewMessage = functions.database.ref(`${messages}/{roomId}/{messageId}`)
.onWrite(({ data, params }) => {
const { roomId, messageId } = params
const payload = data.val()
return Promise.all([
admin.database().ref(chats).child(roomId).set(payload),
typeof config.onNewMessage === 'function'
&& config.onNewMessage(roomId, messageId, payload),
])
})
const onAttachment = functions.storage.object().onChange(({ data }) => {
const { name, mediaLink, timeCreated } = data
const userId = name.split('/')[1]
const at = new Date(timeCreated).getTime()
return admin.database().ref(messages).child(userId).child(at).set({
userId,
at,
attachments: [{
path: name,
url: mediaLink,
}],
})
})
return {
authenticate,
onNewMessage,
onAttachment,
}
}