-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathchat.service.ts
121 lines (104 loc) · 2.71 KB
/
chat.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { inject, Injectable } from '@angular/core';
import {
Auth,
authState,
signInWithPopup,
GoogleAuthProvider,
signOut,
user,
getAuth,
User,
} from '@angular/fire/auth';
import { map, switchMap, firstValueFrom, filter, Observable, Subscription } from 'rxjs';
import {
doc,
docData,
DocumentReference,
Firestore,
getDoc,
setDoc,
updateDoc,
collection,
addDoc,
deleteDoc,
collectionData,
Timestamp,
serverTimestamp,
query,
orderBy,
limit,
onSnapshot,
DocumentData,
FieldValue,
} from '@angular/fire/firestore';
import {
Storage,
getDownloadURL,
ref,
uploadBytesResumable,
} from '@angular/fire/storage';
import { getToken, Messaging, onMessage } from '@angular/fire/messaging';
import { Router } from '@angular/router';
type ChatMessage = {
name: string | null,
profilePicUrl: string | null,
timestamp: FieldValue,
uid: string | null,
text?: string,
imageUrl?: string
};
@Injectable({
providedIn: 'root',
})
export class ChatService {
firestore: Firestore = inject(Firestore);
auth: Auth = inject(Auth);
storage: Storage = inject(Storage);
messaging: Messaging = inject(Messaging);
router: Router = inject(Router);
private provider = new GoogleAuthProvider();
LOADING_IMAGE_URL = 'https://www.google.com/images/spin-32.gif?a';
// observable that is updated when the auth state changes
user$ = user(this.auth);
currentUser: User | null = this.auth.currentUser;
userSubscription: Subscription;
constructor() {
this.userSubscription = this.user$.subscribe((aUser: User | null) => {
this.currentUser = aUser;
});
}
// Login Friendly Chat.
login() {}
// Logout of Friendly Chat.
logout() {}
// Adds a text or image message to Cloud Firestore.
addMessage = async (
textMessage: string | null,
imageUrl: string | null
): Promise<void | DocumentReference<DocumentData>> => {};
// Saves a new message to Cloud Firestore.
saveTextMessage = async (messageText: string) => {
return this.addMessage(messageText, null);
};
// Loads chat messages history and listens for upcoming ones.
loadMessages = () => {
return null as unknown;
};
// Saves a new message containing an image in Firebase.
// This first saves the image in Firebase storage.
saveImageMessage = async (file: any) => {};
async updateData(path: string, data: any) {}
async deleteData(path: string) {}
getDocData(path: string) {}
getCollectionData(path: string) {}
async uploadToStorage(
path: string,
input: HTMLInputElement,
contentType: any
) {
return null;
}
// Requests permissions to show notifications.
requestNotificationsPermissions = async () => {};
saveMessagingDeviceToken = async () => {};
}