forked from Podcastindex-org/helipad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.js
214 lines (188 loc) · 8.02 KB
/
home.js
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
$(document).ready(function () {
let messages = $('div.mesgs');
let inbox = messages.find('div.msg_history');
let appIconUrlBase = 'https://podcastindex.org/api/images/';
let pewAudioFile = '/pew.mp3';
let pewAudio = new Audio(pewAudioFile);
const urlParams = new URLSearchParams(window.location.search);
const chat_id = urlParams.get('cid');
var intvlChatPolling = null;
var connection = null;
var messageIds = [];
getBoosts();
function getBoosts(startIndex, max, scrollToTop, old) {
var noIndex = false;
//Find newest index
let lastIndex = $('div.outgoing_msg:first').data('msgid');
if (typeof lastIndex === "undefined") {
lastIndex = "";
}
//console.log("Last index: ["+lastIndex+"]");
let firstIndex = $('div.outgoing_msg:last').data('msgid');
if (typeof firstIndex === "undefined") {
firstIndex = "";
}
// console.log("First index: ["+firstIndex+"]");
//Get current id set
messageIds = [];
$('div.outgoing_msg').map(function () {
messageIds.push($(this).data('msgid'));
});
// console.log(messageIds);
//Params
if (typeof startIndex === "number") {
boostIndex = startIndex;
} else {
boostIndex = lastIndex;
}
if(typeof boostIndex !== "number") {
noIndex = true;
}
if(startIndex === null) {
boostIndex = "";
}
if (typeof max !== "number") {
max = 0;
}
if (typeof scrollToTop !== "boolean") {
scrollToTop = true;
}
//Build the endpoint url
var url = '/boosts?index=' + boostIndex;
if(max > 0) {
url += '&count=' + max;
}
if(old) {
url += '&old=true';
}
$.ajax({
url: url,
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
data.forEach((element, index) => {
let displayedMessageCount = $('div.outgoing_msg').length;
//console.log(element);
let boostMessage = element.message || "";
let boostSats = Math.trunc(element.value_msat_total / 1000) || Math.trunc(element.value_msat / 1000);
let boostIndex = element.index;
let boostAction = element.action;
let boostSender = element.sender;
let boostApp = element.app;
let boostPodcast = element.podcast;
let boostEpisode = element.episode;
//Icon
var appIconUrl = "";
switch (boostApp.toLowerCase()) {
case 'fountain':
appIconUrl = appIconUrlBase + 'fountain.png';
break;
case 'podfriend':
appIconUrl = appIconUrlBase + 'podfriend.jpg';
break;
case 'castamatic':
appIconUrl = appIconUrlBase + 'castamatic.png';
break;
case 'curiocaster':
appIconUrl = appIconUrlBase + 'curiocaster.png';
break;
case 'breez':
appIconUrl = appIconUrlBase + 'breez.png';
break;
case 'podstation':
appIconUrl = appIconUrlBase + 'podstation.jpg';
break;
case 'sphinx':
appIconUrl = appIconUrlBase + 'sphinxchat.png';
break;
case 'podverse':
appIconUrl = appIconUrlBase + 'podverse.jpg';
break;
}
if (!messageIds.includes(boostIndex) && element.action == 2) {
let dateTime = new Date(element.time * 1000).toISOString();
$('div.nodata').remove();
//Build the message element
elMessage = '' +
'<div class="outgoing_msg message" data-msgid="' + boostIndex + '">' +
' <div class="sent_msg">' +
' <div class="sent_withd_msg">' +
' <span class="app"><img src="' + appIconUrl + '"></span>' +
' <h5>' + boostSats + ' sats <small>from ' + boostSender + '</small></h5>' +
' <span class="time_date" data-timestamp="' + dateTime + '">' + prettyDate(dateTime) + '</span>' +
' <small class="podcast_episode">' + boostPodcast + ' - ' + boostEpisode + '</small>' +
' <br>' +
' <hr>' +
' <p>' + boostMessage + '</p>' +
' </div>' +
' </div>' +
'</div>';
//Insert the message in the right spot
if(displayedMessageCount == 0) {
inbox.prepend(elMessage);
//Scroll the list back up if necessary
if (scrollToTop) {
inbox.scrollTop();
}
} else {
//Get the closest matching id
var prepend = false;
let closestId = closest(messageIds, boostIndex);
if(boostIndex < closestId) {
prepend = true;
}
if(prepend) {
$('div.outgoing_msg[data-msgid='+closestId+']').after(elMessage);
} else {
$('div.outgoing_msg[data-msgid='+closestId+']').before(elMessage);
}
}
//Update the tracking array
messageIds.push(boostIndex);
messageIds = messageIds.sort((a,b) => a-b);
//Pew pew pew!
pewAudio.play();
}
});
//Show a message if still building
if ($('div.outgoing_msg').length == 0 && $('div.nodata').length == 0) {
inbox.prepend('<div class="nodata">No data to show yet. Building the initial database may take some time...</div>');
}
//Load more link
if ($('div.outgoing_msg').length > 0 && $('div.loadmore').length == 0 && ( boostIndex > 1 || noIndex)) {
inbox.append('<div class="loadmore"><a href="#">Show older boosts...</a></div>');
}
}
});
}
//Load more messages handler
$(document).on('click', 'div.loadmore a', function () {
var old = true;
let boostIndex = $('div.outgoing_msg:last').data('msgid');
if (typeof boostIndex === "undefined") {
return false;
}
boostIndex = boostIndex;
if(boostIndex < 1) {
boostIndex = 1;
max = boostIndex
old = false;
}
getBoosts(boostIndex, 200, false, old);
return false;
});
//Set a periodic checker for new boosts
setInterval(function () {
getBoosts(null, 20, true);
}, 7000);
});
const closest = (arr, num) => {
return arr.reduce((acc, val) => {
if(Math.abs(val - num) < Math.abs(acc)){
return val - num;
}else{
return acc;
}
}, Infinity) + num;
}