Skip to content

Commit 6ff12ce

Browse files
authored
Merge pull request #1759 from kiminkim724/notifications-readme
Update README for Notifications
2 parents cbc7399 + 2e469dc commit 6ff12ce

9 files changed

+38
-17
lines changed

functions/src/notifications/README.md

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,59 @@ The notification feature is responsible for sending notifications to users based
66

77
## Features
88

9-
- **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.
9+
- **Topic-Based Notifications**: Users receive notifications based on topics they are subscribed to.
10+
- Each user has a list of topics created when subscribing to a Bill, User, or Organization.
11+
- Topics can be categorized as either Bill Topics or Organization Topics:
12+
- `bill-[courtId]-[billId]`: Triggers when a subscribed Bill has a history update or when testimony is posted on the Bill.
13+
- `testimony-[userId]`: Triggers when a subscribed User or Organization posts testimony.
14+
- **Notification Events**
15+
- A BillHistoryNotificationEvent is created when a subscribed Bill has an updated history
16+
- A TestimonySubmissionNotificationEvent is created when a subscribed Bill has testimony published and when a Organization/User publishes testimony
17+
- **Event Triggers**: Functions are triggered based on changes in the `notificationEvents` collection
1218

1319
## Architecture
1420

15-
### Events
21+
### Notification Events
1622

1723
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.
1824

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

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)
28+
Example event regarding an testimony:
29+
![example-testimony-notification-event](/functions/src/notifications/images/example-testimony-notification-event.png)
2730

2831
### Cloud Functions
2932

3033
The following cloud functions are involved in the notification process:
3134

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

34-
- Creates a notification document from an event.
41+
2. **publishNotifications**:
42+
43+
- Creates a notification document from an notification event.
3544
- Populates the user's `userNotificationFeed` collection with a notification document.
3645
- Populates the newsfeed.
3746

3847
For example, here is a notification document in a given user's feed:
3948

40-
![example-org-notification](/functions/src/notifications/images/example-org-notification.png)
49+
![example-bill-history-update-notification](/functions/src/notifications/images/example-bill-history-update-notification.png)
50+
51+
- There are two key fields to differentiate whether a notification came from following a bill or following a user/organization:
52+
- `isBillMatch` and `isUserMatch` indicate the source of the notification.
53+
- 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.
54+
- If a user follows only the bill and not the organization, `isBillMatch` will be true and `isUserMatch` will be false, and vice versa.
4155

42-
1. **deliverNotifications**:
56+
3. **deliverNotifications**:
4357

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

47-
2. **cleanUpNotifications**:
61+
4. **cleanUpNotifications**:
4862
- Removes notifications from the users' userNotificationFeed collection that are older than 60 days.
4963
- Removes notifications from the notificationEvents collection that are older than 60 days.
5064
- Removes notifications from the emails collection that are older than 60 days.
@@ -54,6 +68,7 @@ The following cloud functions are involved in the notification process:
5468
- `activeTopicSubscriptions`: Stores the active topic subscriptions for users.
5569
- `emails`: Stores the notification mails sent to users.
5670
- `notificationEvents`: Stores the events that trigger notifications.
71+
- `userNotificationFeed`: Stores the notifications for each user
5772

5873
### Query Logic
5974

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

85+
or to test the notifications as a whole
86+
87+
```bash
88+
yarn test:integration notifications.test.ts
89+
```
90+
7091
## Future Considerations
7192

7293
- `publishNotifications` currently listens to the `notificationEvents` collection but could be extended to include other collections.
Binary file not shown.
Loading
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
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)