|
1 | 1 | import {Injectable, inject} from '@angular/core'; |
2 | | -import { |
3 | | - updateDoc, |
4 | | - collection, |
5 | | - QueryDocumentSnapshot, |
6 | | - FirestoreDataConverter, |
7 | | - collectionSnapshots, |
8 | | - Firestore, |
9 | | - doc, |
10 | | - getDoc, |
11 | | -} from '@angular/fire/firestore'; |
| 2 | +import {QueryDocumentSnapshot, FirestoreDataConverter} from '@angular/fire/firestore'; |
12 | 3 | import {httpsCallable, Functions} from '@angular/fire/functions'; |
13 | | -import {map, shareReplay} from 'rxjs'; |
| 4 | +import {map, shareReplay, from, switchMap, BehaviorSubject} from 'rxjs'; |
| 5 | +import {MatSnackBar} from '@angular/material/snack-bar'; |
14 | 6 |
|
15 | 7 | export interface BlockUserParams { |
16 | 8 | /** The username of the user being blocked. */ |
@@ -46,55 +38,64 @@ export type BlockedUserFromFirestore = BlockedUser & { |
46 | 38 | export class BlockService { |
47 | 39 | /** Firebase functions instance, provided from the root. */ |
48 | 40 | private functions = inject(Functions); |
49 | | - /** Firebase firestore instance, provided from the root. */ |
50 | | - private firestore = inject(Firestore); |
| 41 | + /** Snackbar for displaying failure alerts. */ |
| 42 | + private snackBar = inject(MatSnackBar); |
| 43 | + /** Subject to trigger refreshing the blocked users list. */ |
| 44 | + private refreshBlockedUsers$ = new BehaviorSubject<void>(undefined); |
51 | 45 |
|
52 | | - /** Request a user to be blocked by the blocking service. */ |
53 | | - readonly block = httpsCallable(this.functions, 'blockUser'); |
54 | | - |
55 | | - /** Request a user to be unblocked by the blocking service. */ |
56 | | - readonly unblock = httpsCallable<UnblockUserParams>(this.functions, 'unblockUser'); |
57 | | - |
58 | | - /** Request a sync of all blocked users with the current Github blockings. */ |
59 | | - readonly syncUsersFromGithub = httpsCallable<void>(this.functions, 'syncUsersFromGithub'); |
| 46 | + /** Request all blocked users. */ |
| 47 | + getBlockedUsers = this.asCallable<void, BlockedUserFromFirestore[]>('getBlockedUsers', true); |
60 | 48 |
|
61 | 49 | /** All blocked users current blocked by the blocking service. */ |
62 | | - readonly blockedUsers = collectionSnapshots( |
63 | | - collection(this.firestore, 'blockedUsers').withConverter(converter), |
64 | | - ).pipe( |
| 50 | + readonly blockedUsers = this.refreshBlockedUsers$.pipe( |
| 51 | + switchMap(() => from(this.getBlockedUsers())), |
65 | 52 | map((blockedUsers) => |
66 | 53 | blockedUsers |
67 | | - .map((snapshot) => snapshot.data()) |
| 54 | + .map((user) => ({ |
| 55 | + ...user, |
| 56 | + blockUntil: user.blockUntil === false ? false : new Date(user.blockUntil), |
| 57 | + blockedOn: new Date(user.blockedOn), |
| 58 | + })) |
68 | 59 | .sort((a, b) => (a.username.toLowerCase() > b.username.toLowerCase() ? 1 : -1)), |
69 | 60 | ), |
70 | 61 | shareReplay(1), |
71 | 62 | ); |
72 | 63 |
|
| 64 | + /** Request a user to be blocked. */ |
| 65 | + block = this.asCallable<BlockUserParams, void>('blockUser'); |
| 66 | + |
| 67 | + /** Request a user to be unblocked. */ |
| 68 | + unblock = this.asCallable<UnblockUserParams, void>('unblockUser'); |
| 69 | + |
| 70 | + /** Request a sync of all blocked users with the current Github blockings. */ |
| 71 | + syncUsersFromGithub = this.asCallable<void, void>('syncUsersFromGithub'); |
| 72 | + |
73 | 73 | /** Update the metadata for a blocked user. */ |
74 | | - async update(username: string, data: Partial<BlockedUser>) { |
75 | | - const userDoc = await getDoc( |
76 | | - doc(collection(this.firestore, 'blockedUsers').withConverter(converter), username), |
77 | | - ); |
78 | | - if (userDoc.exists()) { |
79 | | - return await updateDoc(userDoc.ref, data); |
80 | | - } |
81 | | - throw Error(`The entry for ${username} does not exist`); |
82 | | - } |
83 | | -} |
| 74 | + update = this.asCallable<{username: string; data: Partial<BlockedUser>}, void>('updateUser'); |
84 | 75 |
|
85 | | -export const converter: FirestoreDataConverter<BlockedUser> = { |
86 | | - toFirestore: (user: BlockedUser) => { |
87 | | - return user; |
88 | | - }, |
89 | | - fromFirestore: (data: QueryDocumentSnapshot<BlockedUser>) => { |
90 | | - return { |
91 | | - username: data.get('username'), |
92 | | - context: data.get('context'), |
93 | | - comments: data.get('comments'), |
94 | | - blockedBy: data.get('blockedBy'), |
95 | | - blockUntil: |
96 | | - data.get('blockUntil') === false ? false : new Date(data.get('blockUntil').seconds * 1000), |
97 | | - blockedOn: new Date(data.get('blockedOn').seconds * 1000), |
| 76 | + /** |
| 77 | + * Helper function to create a callable function that automatically refreshes the blocked users list. |
| 78 | + * @param callableName The name of the callable function to create. |
| 79 | + * @returns A function that can be called to invoke the callable function. |
| 80 | + */ |
| 81 | + private asCallable<T, R>( |
| 82 | + callableName: string, |
| 83 | + skipRefresh = false, |
| 84 | + ): (callableArg: T) => Promise<R> { |
| 85 | + return async (callableArg: T) => { |
| 86 | + try { |
| 87 | + const result = await httpsCallable<T, R>(this.functions, callableName)(callableArg); |
| 88 | + if (!skipRefresh) { |
| 89 | + this.refreshBlockedUsers$.next(); |
| 90 | + } |
| 91 | + return result.data; |
| 92 | + } catch (error) { |
| 93 | + const message = error instanceof Error ? error.message : 'Unknown error'; |
| 94 | + this.snackBar.open(`Failed to execute ${callableName}: ${message}`, 'Dismiss', { |
| 95 | + duration: 5000, |
| 96 | + }); |
| 97 | + throw error; |
| 98 | + } |
98 | 99 | }; |
99 | | - }, |
100 | | -}; |
| 100 | + } |
| 101 | +} |
0 commit comments