Skip to content

Commit 2c359fd

Browse files
authored
feat(filters): reduce api calls for filtered detailed notifications (#2158)
* feat(filter): avoid further enrichment if state not visible Signed-off-by: Adam Setch <[email protected]> * feat(filter): avoid further enrichment if state not visible Signed-off-by: Adam Setch <[email protected]> * feat(filter): avoid further enrichment if state not visible Signed-off-by: Adam Setch <[email protected]> * feat(filter): avoid further enrichment if state not visible Signed-off-by: Adam Setch <[email protected]> * feat(filter): avoid further enrichment if state not visible Signed-off-by: Adam Setch <[email protected]> * feat(filter): avoid further enrichment if user not visible Signed-off-by: Adam Setch <[email protected]> * feat(filter): avoid further enrichment if user not visible Signed-off-by: Adam Setch <[email protected]> --------- Signed-off-by: Adam Setch <[email protected]>
1 parent 3da45b9 commit 2c359fd

File tree

4 files changed

+408
-82
lines changed

4 files changed

+408
-82
lines changed

src/renderer/utils/notifications/filters/filter.ts

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { SettingsState } from '../../../types';
2-
import type { Notification } from '../../../typesGitHub';
2+
import type {
3+
Notification,
4+
StateType,
5+
SubjectUser,
6+
} from '../../../typesGitHub';
37
import {
48
filterNotificationByHandle,
59
hasExcludeHandleFilters,
@@ -45,37 +49,11 @@ export function filterDetailedNotifications(
4549
let passesFilters = true;
4650

4751
if (settings.detailedNotifications) {
48-
if (userTypeFilter.hasFilters(settings)) {
49-
passesFilters =
50-
passesFilters &&
51-
settings.filterUserTypes.some((userType) =>
52-
userTypeFilter.filterNotification(notification, userType),
53-
);
54-
}
55-
56-
if (hasIncludeHandleFilters(settings)) {
57-
passesFilters =
58-
passesFilters &&
59-
settings.filterIncludeHandles.some((handle) =>
60-
filterNotificationByHandle(notification, handle),
61-
);
62-
}
63-
64-
if (hasExcludeHandleFilters(settings)) {
65-
passesFilters =
66-
passesFilters &&
67-
!settings.filterExcludeHandles.some((handle) =>
68-
filterNotificationByHandle(notification, handle),
69-
);
70-
}
71-
72-
if (stateFilter.hasFilters(settings)) {
73-
passesFilters =
74-
passesFilters &&
75-
settings.filterStates.some((state) =>
76-
stateFilter.filterNotification(notification, state),
77-
);
78-
}
52+
passesFilters =
53+
passesFilters && passesUserFilters(notification, settings);
54+
55+
passesFilters =
56+
passesFilters && passesStateFilter(notification, settings);
7957
}
8058

8159
return passesFilters;
@@ -92,3 +70,67 @@ export function hasAnyFiltersSet(settings: SettingsState): boolean {
9270
reasonFilter.hasFilters(settings)
9371
);
9472
}
73+
74+
function passesUserFilters(
75+
notification: Notification,
76+
settings: SettingsState,
77+
): boolean {
78+
let passesFilters = true;
79+
80+
if (userTypeFilter.hasFilters(settings)) {
81+
passesFilters =
82+
passesFilters &&
83+
settings.filterUserTypes.some((userType) =>
84+
userTypeFilter.filterNotification(notification, userType),
85+
);
86+
}
87+
88+
if (hasIncludeHandleFilters(settings)) {
89+
passesFilters =
90+
passesFilters &&
91+
settings.filterIncludeHandles.some((handle) =>
92+
filterNotificationByHandle(notification, handle),
93+
);
94+
}
95+
96+
if (hasExcludeHandleFilters(settings)) {
97+
passesFilters =
98+
passesFilters &&
99+
!settings.filterExcludeHandles.some((handle) =>
100+
filterNotificationByHandle(notification, handle),
101+
);
102+
}
103+
104+
return passesFilters;
105+
}
106+
107+
function passesStateFilter(
108+
notification: Notification,
109+
settings: SettingsState,
110+
): boolean {
111+
if (stateFilter.hasFilters(settings)) {
112+
return settings.filterStates.some((state) =>
113+
stateFilter.filterNotification(notification, state),
114+
);
115+
}
116+
117+
return true;
118+
}
119+
120+
export function isStateFilteredOut(
121+
state: StateType,
122+
settings: SettingsState,
123+
): boolean {
124+
const notification = { subject: { state: state } } as Notification;
125+
126+
return !passesStateFilter(notification, settings);
127+
}
128+
129+
export function isUserFilteredOut(
130+
user: SubjectUser,
131+
settings: SettingsState,
132+
): boolean {
133+
const notification = { subject: { user: user } } as Notification;
134+
135+
return !passesUserFilters(notification, settings);
136+
}

src/renderer/utils/notifications/notifications.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export async function enrichNotifications(
108108
let additionalSubjectDetails: GitifySubject = {};
109109

110110
try {
111-
additionalSubjectDetails = await getGitifySubjectDetails(notification);
111+
additionalSubjectDetails = await getGitifySubjectDetails(
112+
notification,
113+
settings,
114+
);
112115
} catch (err) {
113116
logError(
114117
'enrichNotifications',

0 commit comments

Comments
 (0)