app.component.ts
8.04 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { MatSnackBar } from '@angular/material';
import * as firebase from 'firebase';
const LOADING_IMAGE_URL = 'https://www.google.com/images/spin-32.gif';
const PROFILE_PLACEHOLDER_IMAGE_URL = '/assets/images/profile_placeholder.png';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
user: Observable<firebase.User>;
currentUser: firebase.User;
messages: Observable<any[]>;
profilePicStyles: {};
topics = '';
value = '';
constructor(public db: AngularFireDatabase, public afAuth: AngularFireAuth, public snackBar: MatSnackBar) {
this.user = afAuth.authState;
this.user.subscribe((user: firebase.User) => {
console.log(user);
this.currentUser = user;
if (user) { // User is signed in!
this.profilePicStyles = {
'background-image': `url(${this.currentUser.photoURL})`
};
// We load currently existing chat messages.
this.messages = this.db.list<any>('/messages', ref => ref.limitToLast(12)).valueChanges();
this.messages.subscribe((messages) => {
// Calculate list of recently discussed topics
const topicsMap = {};
const topics = [];
let hasEntities = false;
messages.forEach((message) => {
if (message.entities) {
for (let entity of message.entities) {
if (!topicsMap.hasOwnProperty(entity.name)) {
topicsMap[entity.name] = 0
}
topicsMap[entity.name] += entity.salience;
hasEntities = true;
}
}
});
if (hasEntities) {
for (let name in topicsMap) {
topics.push({ name, score: topicsMap[name] });
}
topics.sort((a, b) => b.score - a.score);
this.topics = topics.map((topic) => topic.name).join(', ');
}
// Make sure new message scroll into view
setTimeout(() => {
const messageList = document.getElementById('messages');
messageList.scrollTop = messageList.scrollHeight;
document.getElementById('message').focus();
}, 500);
});
// We save the Firebase Messaging Device token and enable notifications.
this.saveMessagingDeviceToken();
} else { // User is signed out!
this.profilePicStyles = {
'background-image': PROFILE_PLACEHOLDER_IMAGE_URL
};
this.topics = '';
}
});
}
login() {
this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
}
logout() {
this.afAuth.auth.signOut();
}
// TODO: Refactor into text message form component
update(value: string) {
this.value = value;
}
// Returns true if user is signed-in. Otherwise false and displays a message.
checkSignedInWithMessage() {
// Return true if the user is signed in Firebase
if (this.currentUser) {
return true;
}
this.snackBar
.open('You must sign-in first', 'Sign in', {
duration: 5000
})
.onAction()
.subscribe(() => this.login());
return false;
};
// TODO: Refactor into text message form component
saveMessage(event: any, el: HTMLInputElement) {
event.preventDefault();
if (this.value && this.checkSignedInWithMessage()) {
// Add a new message entry to the Firebase Database.
const messages = this.db.list('/messages');
messages.push({
name: this.currentUser.displayName,
text: this.value,
photoUrl: this.currentUser.photoURL || PROFILE_PLACEHOLDER_IMAGE_URL
}).then(() => {
// Clear message text field and SEND button state.
el.value = '';
}, (err) => {
this.snackBar.open('Error writing new message to Firebase Database.', null, {
duration: 5000
});
console.error(err);
});
}
}
// TODO: Refactor into image message form component
saveImageMessage(event: any) {
event.preventDefault();
const file = event.target.files[0];
// Clear the selection in the file picker input.
const imageForm = <HTMLFormElement>document.getElementById('image-form');
imageForm.reset();
// Check if the file is an image.
if (!file.type.match('image.*')) {
this.snackBar.open('You can only share images', null, {
duration: 5000
});
return;
}
// Check if the user is signed-in
if (this.checkSignedInWithMessage()) {
// We add a message with a loading icon that will get updated with the shared image.
const messages = this.db.list('/messages');
messages.push({
name: this.currentUser.displayName,
imageUrl: LOADING_IMAGE_URL,
photoUrl: this.currentUser.photoURL || PROFILE_PLACEHOLDER_IMAGE_URL
}).then((data) => {
// Upload the image to Cloud Storage.
const filePath = `${this.currentUser.uid}/${data.key}/${file.name}`;
return firebase.storage().ref(filePath).put(file)
.then((snapshot) => {
// Get the file's Storage URI and update the chat message placeholder.
const fullPath = snapshot.metadata.fullPath;
const imageUrl = firebase.storage().ref(fullPath).toString();
return firebase.storage().refFromURL(imageUrl).getMetadata();
}).then((metadata) => {
// TODO: Instead of saving the download URL, save the GCS URI and
// dynamically load the download URL when displaying the image
// message.
return data.update({
imageUrl: metadata.downloadURLs[0]
});
});
}).then(console.log, (err) => {
this.snackBar.open('There was an error uploading a file to Cloud Storage.', null, {
duration: 5000
});
console.error(err);
});
}
}
// TODO: Refactor into image message form component
onImageClick(event: any) {
event.preventDefault();
document.getElementById('mediaCapture').click();
}
// Saves the messaging device token to the datastore.
saveMessagingDeviceToken() {
return firebase.messaging().getToken()
.then((currentToken) => {
if (currentToken) {
console.log('Got FCM device token:', currentToken);
// Save the Device Token to the datastore.
firebase.database()
.ref('/fcmTokens')
.child(currentToken)
.set(this.currentUser.uid);
} else {
// Need to request permissions to show notifications.
return this.requestNotificationsPermissions();
}
}).catch((err) => {
this.snackBar.open('Unable to get messaging token.', null, {
duration: 5000
});
console.error(err);
});
};
// Requests permissions to show notifications.
requestNotificationsPermissions() {
console.log('Requesting notifications permission...');
return firebase.messaging().requestPermission()
// Notification permission granted.
.then(() => this.saveMessagingDeviceToken())
.catch((err) => {
this.snackBar.open('Unable to get permission to notify.', null, {
duration: 5000
});
console.error(err);
});
};
}