Skip to content

Commit 12d47b9

Browse files
committed
Update docs
1 parent 8bd39c7 commit 12d47b9

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

doc/firebase-cloud-functions.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,55 @@ id: firebase-cloud-functions
33
title: Cloud Functions
44
---
55

6-
This is an example of a cloud function that sets a message's status to delivered once the message is received on Firebase.
6+
This is an example of a cloud function that sets a message's status to `delivered` once the message is received on Firebase.
77

88
```js
9-
const functions = require("firebase-functions");
9+
const functions = require('firebase-functions')
1010

1111
exports.changeMessageStatus = functions.firestore
12-
.document("rooms/{roomId}/messages/{messageId}")
13-
.onWrite((change) => {
14-
const message = change.after.data();
15-
if (message) {
16-
if (["delivered", "read"].includes(message.status)) {
17-
return null;
18-
} else {
19-
return change.after.ref.update({
20-
status: "delivered",
21-
});
22-
}
12+
.document('rooms/{roomId}/messages/{messageId}')
13+
.onWrite((change) => {
14+
const message = change.after.data()
15+
if (message) {
16+
if (['delivered', 'seen', 'sent'].includes(message.status)) {
17+
return null
2318
} else {
24-
return null;
19+
return change.after.ref.update({
20+
status: 'delivered',
21+
})
2522
}
26-
});
23+
} else {
24+
return null
25+
}
26+
})
2727
```
2828

29-
This is an example of a cloud function that sets a room's `lastMessage` to the most recent message sent once recieved in Firestore.
29+
This is an example of a cloud function that sets room's `lastMessages` to the most recent message sent once recieved in Firestore.
3030

3131
```js
32-
const functions = require("firebase-functions");
33-
const admin = require("firebase-admin");
32+
const admin = require('firebase-admin')
33+
const functions = require('firebase-functions')
3434

35-
admin.initializeApp();
35+
admin.initializeApp()
3636

37-
const db = admin.firestore();
37+
const db = admin.firestore()
3838

3939
exports.changeLastMessage = functions.firestore
40-
.document("rooms/{roomId}/messages/{messageId}")
41-
.onUpdate((change, context) => {
42-
const message = change.after.data();
43-
if (message) {
44-
return db.doc("rooms/" + context.params.roomId).update({
45-
lastMessage: message,
46-
});
47-
} else {
48-
return null;
49-
}
50-
});
40+
.document('rooms/{roomId}/messages/{messageId}')
41+
.onUpdate((change, context) => {
42+
const message = change.after.data()
43+
if (message) {
44+
return db.doc('rooms/' + context.params.roomId).update({
45+
lastMessages: [message],
46+
})
47+
} else {
48+
return null
49+
}
50+
})
5151
```
5252

5353
:::important
5454

55-
Starting from March 15, 2021, you will need a paid plan to use Cloud Functions, so there might be a better way to do it. We will explore options to create more examples, including `read` status and push notifications.
55+
Starting from March 15, 2021, you will need a paid plan to use Cloud Functions, so there might be a better way to do it. We might explore options to create more examples, including `seen` status and push notifications.
5656

5757
:::

0 commit comments

Comments
 (0)