Skip to content

Commit f586af9

Browse files
committed
Update README for notifications
1 parent 6f6b7d0 commit f586af9

9 files changed

+34
-16
lines changed

functions/src/notifications/README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,55 @@ The notification feature is responsible for sending notifications to users based
77
## Features
88

99
- **Topic-Based Notifications**: Users receive notifications based on topics they are interested in.
10-
- **Scheduled Triggers**: Functions are triggered based on a publication schedule.
11-
- **Multi-Environment Support**: Both HTTP callable and pubSub versions of functions are available for testing and production, respectively.
10+
- It can be either a Bill Topic or an Org Topic
11+
- **Notification Events**
12+
- A BillHistoryNotificationEvent is created when a subscribed Bill has an updated history
13+
- A TestimonySubmissionNotificationEvent is created when a subscribed Bill has testimony published and when a Organization/User publishes testimony
14+
- **Event Triggers**: Functions are triggered based on changes in the `notificationEvents` collection
1215

1316
## Architecture
1417

15-
### Events
18+
### Notification Events
1619

1720
A user could be subscribed to a given topic, and receive a notification of a bill or profile (such as an organization) that fall under that topic have an event.
1821

19-
Example event regarding a bill:
20-
![example-bill-event](/functions/src/notifications/images/example-bill-event.png)
22+
Example event regarding a bill history update:
23+
![example-bill-notification-event](/functions/src/notifications/images/example-bill-notification-event.png)
2124

22-
Example event regarding an organization:
23-
![example-org-event](/functions/src/notifications/images/example-bill-event.png)
24-
25-
Example event created from the web scraper (notice the difference in fields):
26-
![example-scraped_hearing-event](/functions/src/notifications/images/example-scraped_hearing-event.png)
25+
Example event regarding an testimony:
26+
![example-testimony-notification-event](/functions/src/notifications/images/example-testimony-notification-event.png)
2727

2828
### Cloud Functions
2929

3030
The following cloud functions are involved in the notification process:
3131

32-
1. **publishNotifications**:
32+
1. **populateBillHistoryNotifictionEvents** and **populateTestimonySubmissionNotificationEvents**:
33+
34+
- Creates/Updates a notificationEvent document when:
35+
- A Bill is created or updated, if the history is updated, it will update the notificationEvent history and not create a new one.
36+
- Testimony is published or updated, if the testimony content is updated, it will update the notificationEvent testimony content an not create a new one.
3337

34-
- Creates a notification document from an event.
38+
2. **publishNotifications**:
39+
40+
- Creates a notification document from an notification event.
3541
- Populates the user's `userNotificationFeed` collection with a notification document.
3642
- Populates the newsfeed.
3743

3844
For example, here is a notification document in a given user's feed:
3945

40-
![example-org-notification](/functions/src/notifications/images/example-org-notification.png)
46+
![example-bill-history-update-notification](/functions/src/notifications/images/example-bill-history-update-notification.png)
47+
48+
- There are two key fields to differentiate whether a notification came from following a bill or following a user/organization:
49+
- `isBillMatch` and `isUserMatch` indicate the source of the notification.
50+
- For example, if a user follows both a bill and an organization, and the organization posts testimony to that same bill, both `isBillMatch` and `isUserMatch` will be true.
51+
- If a user follows only the bill and not the organization, `isBillMatch` will be true and `isUserMatch` will be false, and vice versa.
4152

42-
1. **deliverNotifications**:
53+
3. **deliverNotifications**:
4354

4455
- Sends notifications to users who have a `notificationFrequency` of 'daily' and whose `nextDigestAt` is less than or equal to the current time.
4556
- Populates the `emails` collection with a notification document.
4657

47-
2. **cleanUpNotifications**:
58+
4. **cleanUpNotifications**:
4859
- Removes notifications from the users' userNotificationFeed collection that are older than 60 days.
4960
- Removes notifications from the notificationEvents collection that are older than 60 days.
5061
- Removes notifications from the emails collection that are older than 60 days.
@@ -54,6 +65,7 @@ The following cloud functions are involved in the notification process:
5465
- `activeTopicSubscriptions`: Stores the active topic subscriptions for users.
5566
- `emails`: Stores the notification mails sent to users.
5667
- `notificationEvents`: Stores the events that trigger notifications.
68+
- `userNotificationFeed`: Stores the notifications for each user
5769

5870
### Query Logic
5971

@@ -67,6 +79,12 @@ To test these functions in a container environment, use the following command:
6779
yarn firebase-admin -e local run-script <name-of-script>
6880
```
6981

82+
or to test the notifications as a whole
83+
84+
```bash
85+
yarn test:integration notifications.test.ts
86+
```
87+
7088
## Future Considerations
7189

7290
- `publishNotifications` currently listens to the `notificationEvents` collection but could be extended to include other collections.
Binary file not shown.
711 KB
Loading
639 KB
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
623 KB
Loading

functions/src/notifications/publishNotifications.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sets up a document trigger for /events and queries the activeTopicSubscriptions collection group in Firestore
22
// for all subscriptions for the given topic event, then creates a notification document in the user's notification feed.
3-
// This function runs every time a new topic event is created in the /events collection.
3+
// This function runs every time a new topic event is created in the /notificationEvents collection.
44
// Creates a notification document in the user's notification feed for each active subscription.
55

66
// Import necessary Firebase modules

0 commit comments

Comments
 (0)