@@ -34,14 +34,15 @@ var myTopPostsMenuButton = document.getElementById('menu-my-top-posts');
34
34
* Saves a new post to the Firebase DB.
35
35
*/
36
36
// [START write_fan_out]
37
- function writeNewPost ( uid , username , title , body ) {
37
+ function writeNewPost ( uid , username , picture , title , body ) {
38
38
// A post entry.
39
39
var postData = {
40
40
author : username ,
41
41
uid : uid ,
42
42
body : body ,
43
43
title : title ,
44
- starCount : 0
44
+ starCount : 0 ,
45
+ authorPic : picture
45
46
} ;
46
47
47
48
// Get a key for a new Post.
@@ -82,7 +83,7 @@ function toggleStar(postRef, uid) {
82
83
/**
83
84
* Creates a post element.
84
85
*/
85
- function createPostElement ( postId , title , text , author , authorId ) {
86
+ function createPostElement ( postId , title , text , author , authorId , authorPic ) {
86
87
var uid = firebase . auth ( ) . currentUser . uid ;
87
88
88
89
var html =
@@ -128,7 +129,8 @@ function createPostElement(postId, title, text, author, authorId) {
128
129
// Set values.
129
130
postElement . getElementsByClassName ( 'text' ) [ 0 ] . innerText = text ;
130
131
postElement . getElementsByClassName ( 'mdl-card__title-text' ) [ 0 ] . innerText = title ;
131
- postElement . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author ;
132
+ postElement . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author || 'Anonymous' ;
133
+ postElement . getElementsByClassName ( 'avatar' ) [ 0 ] . style . backgroundImage = `url("${ authorPic || './silhouette.jpg' } ")` ;
132
134
133
135
// Listen for comments.
134
136
// [START child_event_listener_recycler]
@@ -218,7 +220,7 @@ function addCommentElement(postElement, id, text, author) {
218
220
comment . classList . add ( 'comment-' + id ) ;
219
221
comment . innerHTML = '<span class="username"></span><span class="comment"></span>' ;
220
222
comment . getElementsByClassName ( 'comment' ) [ 0 ] . innerText = text ;
221
- comment . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author ;
223
+ comment . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author || 'Anonymous' ;
222
224
223
225
var commentsContainer = postElement . getElementsByClassName ( 'comments-container' ) [ 0 ] ;
224
226
commentsContainer . appendChild ( comment ) ;
@@ -256,9 +258,10 @@ function startDatabaseQueries() {
256
258
257
259
var fetchPosts = function ( postsRef , sectionElement ) {
258
260
postsRef . on ( 'child_added' , function ( data ) {
261
+ var author = data . val ( ) . author || 'Anonymous' ;
259
262
var containerElement = sectionElement . getElementsByClassName ( 'posts-container' ) [ 0 ] ;
260
263
containerElement . insertBefore (
261
- createPostElement ( data . key , data . val ( ) . title , data . val ( ) . body , data . val ( ) . author , data . val ( ) . uid ) ,
264
+ createPostElement ( data . key , data . val ( ) . title , data . val ( ) . body , author , data . val ( ) . uid , data . val ( ) . authorPic ) ,
262
265
containerElement . firstChild ) ;
263
266
} ) ;
264
267
} ;
@@ -272,10 +275,11 @@ function startDatabaseQueries() {
272
275
* Writes the user's data to the database.
273
276
*/
274
277
// [START basic_write]
275
- function writeUserData ( userId , name , email ) {
278
+ function writeUserData ( userId , name , email , imageUrl ) {
276
279
firebase . database ( ) . ref ( 'users/' + userId ) . set ( {
277
280
username : name ,
278
- email : email
281
+ email : email ,
282
+ profile_picture : imageUrl
279
283
} ) ;
280
284
}
281
285
// [END basic_write]
@@ -292,7 +296,7 @@ window.addEventListener('load', function() {
292
296
firebase . auth ( ) . onAuthStateChanged ( function ( user ) {
293
297
if ( user ) {
294
298
splashPage . style . display = 'none' ;
295
- writeUserData ( user . uid , user . displayName , user . email ) ;
299
+ writeUserData ( user . uid , user . displayName , user . email , user . photoURL ) ;
296
300
startDatabaseQueries ( ) ;
297
301
} else {
298
302
splashPage . style . display = '' ;
@@ -311,6 +315,7 @@ window.addEventListener('load', function() {
311
315
var username = snapshot . val ( ) . username ;
312
316
// [START_EXCLUDE]
313
317
writeNewPost ( firebase . auth ( ) . currentUser . uid , firebase . auth ( ) . currentUser . displayName ,
318
+ firebase . auth ( ) . currentUser . photoURL ,
314
319
titleInput . value , postText ) . then ( function ( ) {
315
320
myPostsMenuButton . click ( ) ;
316
321
} ) ;
0 commit comments