forked from rallarey/FakeTwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
425 lines (362 loc) · 11.9 KB
/
script.js
File metadata and controls
425 lines (362 loc) · 11.9 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBgQyRNg9LuUQJzg6zl7AB6GQfsQqtwgxc",
authDomain: "twitter-4f92d.firebaseapp.com",
databaseURL: "https://twitter-4f92d-default-rtdb.firebaseio.com",
projectId: "twitter-4f92d",
storageBucket: "twitter-4f92d.appspot.com",
messagingSenderId: "444552325596",
appId: "1:444552325596:web:d87dda56d190cd445c4719",
measurementId: "G-VTRJQREXSD"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
let toggleLike = (tweetRef, uid)=>{
tweetRef.transaction((tObj) => {
if (!tObj) {
tObj = {likes: 0};
}
if (tObj.likes && tObj.likes_by_user[uid]) {
tObj.likes--;
tObj.likes_by_user[uid] = null;
} else {
tObj.likes++;
if (!tObj.likes_by_user) {
tObj.likes_by_user = {};
}
tObj.likes_by_user[uid] = true;
}
return tObj;
});
}
let renderedTweetLikeLookup = {};
let renderTweet = (tObj, tweetID, userObj)=>{
$("#alltweets").prepend(`
<div class="card mb-3 tweet" data-uuid="${tweetID}" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img src="${userObj.profile_picture}" class="img-fluid rounded-start" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">${userObj.username}</h5>
<p class="card-text">${tObj.content}</p>
<p class="card-text like-button" id = "like" data-tweetid="${tweetID}"></p>
<p class="card-text"><small class="text-muted">Tweeted at ${new Date(tObj.timestamp).toLocaleString()}</small></p>
</div>
</div>
</div>
</div>
`);
firebase.database().ref("/likes").child(tweetID).child("likes").on("value", ss=>{
$(`.like-button[data-tweetid=${tweetID}]`).html(`${ss.val() || 0} Likes`);
});
}
let renderLogIn = ()=>{
$("body").html(`
<div class = "log-in">
<div class = "container">
<div class="row align-items-center">
<div class = "col">
<h3>Login</h3>
<input type = "text" placeholder = "Email" id = "user-email" class = form-control mb-3">
<input type = "password" placeholder = "Password" id = "user-password" class = form-control mb-3">
<br>
<i>
<small id = "signinpage">Go back </small>
</i>
<small>
<input type="checkbox" id = "checkbox2"> Show Password
</small>
</br>
<button type = "text" id = "loginbutton" class = "btn btn-outline-primary mb-3">Login</button>
<button type = "text" id = "login2" class = "btn btn-outline-primary mb-3">Google</button>
</div>
</div>
</div>
</div>
`);
$("#checkbox2").on("click", ()=>{
var x = document.getElementById("user-password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
})
$("#login2").on("click", ()=>{
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider)
.then((result)=>{
var credential = result.credential;
// This gives you a Google Access Token. You can use it to access the Google API.
var token = credential.accessToken;
// The signed-in user info.
var user = result.user;
var userName = user["displayName"];
var avatar = user["photoURL"];
var email = user["email"];
var uid = user["uid"];
writeUserData(uid, userName, email, avatar);
}).catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
});
});
$("#signinpage").on("click", ()=>{
renderSignIn();
});
$("#loginbutton").on("click", ()=>{
var userEmail = $("#user-email").val();
var userPassword = $("#user-password").val();
auth.signInWithEmailAndPassword(userEmail, userPassword)
.then((userCredential)=> {
var user = userCredential.user;
})
.catch((error)=>{
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
});
});
}
let renderSignIn = ()=>{
var profile_picture = '';
$("body").html(`
<div class = "sign-in">
<div class = "container">
<div class="row align-items-center">
<div class="col">
<h3>Sign Up</h3>
<input type = "text" placeholder = "Fullname" id = "new-user-name" class = form-control mb-3">
<input type = "text" placeholder = "Email" id = "new-user-email" class = form-control mb-3">
<!--
<input type = "text" placeholder = "Username" id = "userInp" class = form-control mb-3">
-->
<input type = "password" placeholder = "Password" id = "new-user-password" class = form-control mb-3">
<input type = "password" placeholder = "Confirm Password" id = "confirm-password" class = form-control mb-3">
<div id="fileuploadwrap">
<span id="status"></span>
<input type="file" id="fileupload" accept="image/png, image/jpeg"/>
<button id="imageupload">Upload Image</button>
<div id="output">
</div>
</div>
<br>
<small id = "image">*u must hit upload image for your profile picture to upload</small>
</br>
<br>
<i>
<small id = "loginpage">Already have an account?</small>
</i>
<small>
<input type="checkbox" id = "checkbox"> Show Password
</small>
</br>
<button type = "text" id = "signup" class = "btn btn-outline-primary mb-3">Sign Up</button>
<button type = "text" id = "login" class = "btn btn-outline-primary mb-3">Google</button>
</div>
</div>
</div>
</div>
`);
$("#loginpage").on("click", ()=>{
renderLogIn();
})
$("#checkbox").on("click", ()=>{
var x = document.getElementById("new-user-password");
var y = document.getElementById("confirm-password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
if (y.type === "password") {
y.type = "text";
} else {
y.type = "password";
}
})
$("#imageupload").on("click", (evt)=>{
const fileReader = new FileReader();
$("#status").html("Starting upload...");
var myFile = $('#fileupload').prop('files')[0];
fileReader.readAsDataURL(myFile);
fileReader.addEventListener("load", async (evt)=>{
console.log(evt);
let theFileData = fileReader.result;
$("#output").html(`File uploaded!`);
$("#status").html("");
console.log(theFileData);
profile_picture = theFileData;
});
});
$("#signup").on("click", ()=>{
var userEmail = $("#new-user-email").val();
var userPassword = $("#new-user-password").val();
var confirmPassword = $("#confirm-password").val();
var userName = $("#new-user-name").val();
if (userPassword === confirmPassword) {
auth.createUserWithEmailAndPassword(userEmail, userPassword)
.then((userCredential)=> {
var user = userCredential.user;
let myuid = user.uid;
writeUserData(myuid, userName, userEmail, profile_picture);
})
.catch((error)=>{
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
});
} else {
window.alert("Passwords must match!");
}
});
$("#login").on("click", ()=>{
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider)
.then((result)=>{
var credential = result.credential;
// This gives you a Google Access Token. You can use it to access the Google API.
var token = credential.accessToken;
// The signed-in user info.
var user = result.user;
var userName = user["displayName"];
var avatar = user["photoURL"];
var email = user["email"];
var uid = user["uid"];
writeUserData(uid, userName, email, avatar);
}).catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
});
});
}
let writeUserData = (userId, name, email, picURL) => {
let usersRef = firebase.database().ref('/users/' + userId);
usersRef.update({
username: name,
email: email,
profile_picture : picURL
})
}
let renderPage = (loggedIn)=>{
var username = '';
var pic = '';
let myuid = loggedIn.uid;
$("body").html(`
<div class="row align-items-center">
<div class="col">
<h3>Tweet</h3>
<input type = "text" id = "addme">
<button id = "sendit">Tweet</button>
<br>
</br>
<div id = "alltweets"></div>
<button id = "logout">LOG OUT</button>
</div>
</div>
`);
$("#logout").on("click", ()=>{
firebase.auth().signOut();
});
var userEmail;
var message;
var date;
// let tweetRefUsers = firebase.database().ref("/tweets/" + tweetID); // NEED TWEET ID
let tweetRef = firebase.database().ref("/tweets/");
$("#sendit").on("click", ()=>{
message = $("#addme").val();
date = Date.now();
/*
usersRef.child(myuid).get().then((ss) => {
if (ss.exists()) {
ss.forEach((child) => {
if (child.key === "username"){
username = child.val();
}
})
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
*/
let newTweetRef = tweetRef.push({
author: myuid,
content: message,
retweets: 0,
timestamp: date,
})
var postID = newTweetRef.key;
var pic = "https://i.guim.co.uk/img/media/327e46c3ab049358fad80575146be9e0e65686e7/0_56_1023_614/master/1023.jpg?width=1200&quality=85&auto=format&fit=max&s=4592a7be8bdbebd0e0b97e5e10a6c433"
})
tweetRef.on("child_added", (ss)=>{
let usersRef = firebase.database().ref("/users");
let tObj = ss.val();
usersRef.child(tObj.author).get().then((snapshot) =>{
let userObj = snapshot.val();
console.log(userObj);
renderTweet(tObj, ss.key, userObj);
$(".like-button").off("click");
$(".like-button").on("click", (evt)=>{
let clickedTweet = $(evt.currentTarget).attr("data-tweetid");
let likesRef = firebase.database().ref("/likes").child(clickedTweet);
toggleLike(likesRef, myuid);
});
});
/*
renderTweet(tObj, ss.key, userObj);
$(".like-button").off("click");
$(".like-button").on("click", (evt)=>{
let clickedTweet = $(evt.currentTarget).attr("data-tweetid");
let likesRef = firebase.database().ref("/likes").child(clickedTweet);
toggleLike(likesRef, myuid);
});
*/
});
};
let getUserData = (tObj) => {
let usersRef = firebase.database().ref("/users");
console.log(tObj.author);
usersRef.child(tObj.author).get().then((ss) => {
console.log(ss.val());
let userObj = ss.val();
/*
if (ss.exists()) {
ss.forEach((child) => {
if (child.key === "username"){
var username = child.val();
}
if (child.key === "profile_picture"){
var pic = child.val();
}
})
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
*/
});
}
firebase.auth().onAuthStateChanged(user=>{
if (!user){
renderSignIn();
} else {
renderPage(user);
}
})