|
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'; |
14 | 5 |
|
15 | 6 | export interface BlockUserParams { |
16 | 7 | /** The username of the user being blocked. */ |
@@ -46,39 +37,55 @@ export type BlockedUserFromFirestore = BlockedUser & { |
46 | 37 | export class BlockService { |
47 | 38 | /** Firebase functions instance, provided from the root. */ |
48 | 39 | private functions = inject(Functions); |
49 | | - /** Firebase firestore instance, provided from the root. */ |
50 | | - private firestore = inject(Firestore); |
| 40 | + /** Subject to trigger refreshing the blocked users list. */ |
| 41 | + private refreshBlockedUsers$ = new BehaviorSubject<void>(undefined); |
51 | 42 |
|
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'); |
| 43 | + /** Request all blocked users. */ |
| 44 | + getBlockedUsers = this.asCallable<void, BlockedUserFromFirestore[]>('getBlockedUsers', true); |
60 | 45 |
|
61 | 46 | /** All blocked users current blocked by the blocking service. */ |
62 | | - readonly blockedUsers = collectionSnapshots( |
63 | | - collection(this.firestore, 'blockedUsers').withConverter(converter), |
64 | | - ).pipe( |
| 47 | + readonly blockedUsers = this.refreshBlockedUsers$.pipe( |
| 48 | + switchMap(() => from(this.getBlockedUsers())), |
65 | 49 | map((blockedUsers) => |
66 | 50 | blockedUsers |
67 | | - .map((snapshot) => snapshot.data()) |
| 51 | + .map((user) => ({ |
| 52 | + ...user, |
| 53 | + blockUntil: user.blockUntil === false ? false : new Date(user.blockUntil), |
| 54 | + blockedOn: new Date(user.blockedOn), |
| 55 | + })) |
68 | 56 | .sort((a, b) => (a.username.toLowerCase() > b.username.toLowerCase() ? 1 : -1)), |
69 | 57 | ), |
70 | 58 | shareReplay(1), |
71 | 59 | ); |
72 | 60 |
|
| 61 | + /** Request a user to be blocked. */ |
| 62 | + block = this.asCallable<BlockUserParams, void>('blockUser'); |
| 63 | + |
| 64 | + /** Request a user to be unblocked. */ |
| 65 | + unblock = this.asCallable<UnblockUserParams, void>('unblockUser'); |
| 66 | + |
| 67 | + /** Request a sync of all blocked users with the current Github blockings. */ |
| 68 | + syncUsersFromGithub = this.asCallable<void, void>('syncUsersFromGithub'); |
| 69 | + |
73 | 70 | /** 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`); |
| 71 | + update = this.asCallable<{username: string; data: Partial<BlockedUser>}, void>('updateUser'); |
| 72 | + |
| 73 | + /** |
| 74 | + * Helper function to create a callable function that automatically refreshes the blocked users list. |
| 75 | + * @param callableName The name of the callable function to create. |
| 76 | + * @returns A function that can be called to invoke the callable function. |
| 77 | + */ |
| 78 | + private asCallable<T, R>( |
| 79 | + callableName: string, |
| 80 | + skipRefresh = false, |
| 81 | + ): (callableArg: T) => Promise<R> { |
| 82 | + return async (callableArg: T) => { |
| 83 | + const result = await httpsCallable<T, R>(this.functions, callableName)(callableArg); |
| 84 | + if (!skipRefresh) { |
| 85 | + this.refreshBlockedUsers$.next(); |
| 86 | + } |
| 87 | + return result.data; |
| 88 | + }; |
82 | 89 | } |
83 | 90 | } |
84 | 91 |
|
|
0 commit comments