-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotifications.js
More file actions
118 lines (98 loc) · 4.45 KB
/
notifications.js
File metadata and controls
118 lines (98 loc) · 4.45 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
function formatDate(date) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + " " + monthNames[monthIndex] + " " + year + " at " + date.toLocaleTimeString("en-GB", {hour: "2-digit", minute: "2-digit"});
}
function moveToArchive(key) {
$(".notificationArchive").children("p").remove();
$(".notificationArchive").prepend($("[data-key=" + key + "]").detach().removeClass("coloured"));
$(".notificationArchive").find(".options:first").remove();
if ($(".notificationUnread").html() == "") {
$(".notificationUnreadSection").hide();
}
firebase.database().ref("users/" + currentUid + "/notifications/unread/" + key).once("value", function(snapshot) {
firebase.database().ref("users/" + currentUid + "/notifications/archive").push().set(snapshot.val());
firebase.database().ref("users/" + currentUid + "/notifications/unread/" + key).set(null);
});
}
function newNotification(key, value, unread = true) {
if (!unread) {
$(".notificationArchive").children("p").remove();
}
$("<div class='notification card'>")
.addClass(unread ? "coloured" : "")
.attr("data-key", key)
.prependTo(unread ? ".notificationUnread" : ".notificationArchive")
;
if (unread) {
$("<div class='options top'>").appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first");
$("<button class='secondary'>")
.css({
"padding": "5px",
"padding-bottom": "0"
})
.html("<i class='material-icons'>check</i>")
.find("i")
.css("font-size", "inherit")
.parent()
.attr("onclick", "moveToArchive('" + key + "');")
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".options:first")
;
}
$("<span class='commentDate'>")
.text(formatDate(new Date(value.date)) + ": ")
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first")
;
$("<strong><a class='hidden'></strong>")
.find("a")
.text(value.username)
.attr("href", "/profile?user=" + value.uid)
.css("color", isStaff(value.uid) ? "#27ef70" : (isGameProxyPro(value.uid) ? "#b3c20f" : "inherit"))
.parent()
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first")
;
if (value.type == "comment") {
$("<span>")
.text(" commented on ")
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first")
;
$("<strong><a class='hidden'></strong>")
.find("a")
.text(value.targetName)
.attr("href", "/game?play=" + value.target)
.parent()
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first")
;
$("<span>")
.text(": ")
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first")
;
$("<blockquote>")
.text(value.content)
.appendTo((unread ? ".notificationUnread " : ".notificationArchive ") + ".notification:first")
;
}
}
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
firebase.database().ref("users/" + currentUid + "/notifications/unread").once("value", function(snapshot) {
if (snapshot.val() != null && Object.keys(snapshot.val()).length != 0) {
for (var i = 0; i < Object.keys(snapshot.val()).length; i++) {
var key = Object.keys(snapshot.val())[i];
newNotification(key, snapshot.val()[key]);
}
$(".notificationUnreadSection").show();
}
});
firebase.database().ref("users/" + currentUid + "/notifications/archive").once("value", function(snapshot) {
if (snapshot.val() != null && Object.keys(snapshot.val()).length != 0) {
for (var i = 0; i < Object.keys(snapshot.val()).length; i++) {
var key = Object.keys(snapshot.val())[i];
newNotification(key, snapshot.val()[key], false);
}
}
});
}
});