-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.es7
139 lines (116 loc) · 4.36 KB
/
index.es7
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const slugify = require('slugify')
const firestore = admin.firestore()
const algoliasearch = require('algoliasearch')
const client = algoliasearch(functions.config().algolia.app_id, functions.config().algolia.api_key)
// Name fo the algolia index for Blog posts content.
const ALGOLIA_INDEX_NAME = 'documents'
exports.register = functions.auth.user().onCreate(async (event) => {
const user = event.data // The Firebase user.
const {email, displayName, photoURL, uid} = user // Details of the user.
const isAdmin = false
let slug = slugify(displayName, {
replacement: ' ',
lower: true
}).split(' ').join('')
// Check user slug
let userSnapshot = await firestore.collection('user').where('slug', '==', slug).get()
if (userSnapshot.docs.length > 0) {
slug = `${slug}-${userSnapshot.docs.length}`
}
return firestore.doc(`user/${uid}`).set({
email,
displayName,
photoURL,
uid,
isAdmin,
slug
})
})
exports.onDocumentUpdate = functions.firestore.document('document/{slug}')
.onUpdate(event => {
const document = event.data.data()
document.objectID = event.params.slug
if (document.hasPreview && !document.doc && document.text) {
let {slug, description, date, displayName, title, text, thumbnail, userSlug, objectID} = document
const index = client.initIndex(ALGOLIA_INDEX_NAME)
index.saveObject({objectID, slug, description, date, displayName, title, text, thumbnail, userSlug})
return event.data.ref.update({
text: null,
doc: null
})
} else {
return true
}
})
exports.onDocumentDelete = functions.firestore.document('document/{slug}')
.onDelete(event => {
const document = event.data.previous.data()
document.objectID = event.params.slug
firestore.doc(`pdf/${document.slug}`).delete().catch(console.log)
return client.deleteObject(document.slug, err => {
if (err) {
console.log(err)
} else {
console.log('Remove success', document.slug)
}
})
})
exports.onDocumentCreate = functions.firestore.document('document/{slug}')
.onCreate(async (event) => {
const document = event.data.data()
// Get document author followers
let authorFollowers = await firestore.collection(`user/${document.uid}/follower/`).get().then(async (snap) => snap.docs)
if (authorFollowers.length > 0) {
let followers = []
authorFollowers.forEach((value, key) => {
followers.push(firestore.doc(`user/${value.data().uid}`).get().then(snap => snap.data()))
})
return Promise.all(followers).then(users => {
console.log(users[0].displayName)
let tokens = users.filter(user => user.token)
const payload = {
notification: {
title: `${document.displayName} Yeni bir döküman yükledi!`,
body: `${document.title} adlı bir döküman henüz ders.im'e yüklendi.`,
icon: document.photoURL
}
}
if (tokens.length > 0) {
return admin.messaging().sendToDevice(tokens, payload).then(response => {
console.log(response)
})
}
})
}
})
exports.onSyllabusCreate = functions.firestore.document('syllabus/{slug}')
.onCreate(async (event) => {
const document = event.data.data()
// Get document author followers
let authorFollowers = await firestore.collection(`user/${document.uid}/follower/`).get().then(async (snap) => snap.docs)
if (authorFollowers.length > 0) {
let followers = []
authorFollowers.forEach((value, key) => {
followers.push(firestore.doc(`user/${value.data().uid}`).get().then(snap => snap.data()))
})
return Promise.all(followers).then(users => {
console.log(users[0].displayName)
let tokens = users.filter(user => user.token)
const payload = {
notification: {
title: `${document.displayName} Yeni bir döküman yükledi!`,
body: `${document.title} adlı bir döküman henüz ders.im'e yüklendi.`,
icon: document.photoURL
}
}
if (tokens.length > 0) {
return admin.messaging().sendToDevice(tokens, payload).then(response => {
console.log(response)
})
}
})
}
})