-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
380 lines (317 loc) · 13.2 KB
/
main.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
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
import './style.css'
import { reactive, watch, html, } from '@arrow-js/core'
import * as jellyfinRewind from './src/rewind.js'
import JellyHelper from './src/jelly-helper.js'
import * as Onboarding from './src/onboarding.js'
import * as Features from './src/features.js'
document.querySelector('#app').innerHTML = `
`
// {/* <div>
// <a href="https://vitejs.dev" target="_blank">
// <img src="/vite.svg" class="logo" alt="Vite logo" />
// </a>
// <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" target="_blank">
// <img src="${javascriptLogo}" class="logo vanilla" alt="JavaScript logo" />
// </a>
// <h1>Hello Vite!</h1>
// <div class="card">
// <button id="counter" type="button"></button>
// </div>
// <p class="read-the-docs">
// Click on the Vite logo to learn more
// </p>
// </div> */}
let userInfo = null;
let helper = null;
const connectToServerButton = document.querySelector(`#connectToServer`)
const serverUrl = document.querySelector(`#serverUrl`)
const serverConfig = document.querySelector(`#server-config`)
const userSelect = document.querySelector(`#user-select`)
const userLogin = document.querySelector(`#user-login`)
const usernameInput = document.querySelector(`#username-input`)
const authTokenInput = document.querySelector(`#auth-token-input`)
const passwordInput = document.querySelector(`#password-input`)
const username = document.querySelector(`#username`)
const password = document.querySelector(`#password`)
const authToken = document.querySelector(`#auth-token`)
const authenticateUser = document.querySelector(`#authenticateUser`)
const showReport = document.querySelector(`#show-report`)
const generateReport = document.querySelector(`#generate-report`)
const loadingSpinner = document.querySelector(`#loading-spinner`)
const output = document.querySelector(`#output`)
const loggedInInfo = document.querySelector(`#logged-in-info`)
const logOutButton = document.querySelector(`#log-out`)
const loggedInUserInfo = document.querySelector(`#logged-in-user`)
const loggedInServerInfo = document.querySelector(`#logged-in-server`)
const infoOutput = document.querySelector(`#info`)
let selectedUsername = ``
let featuresInitialized = false
let staleReport = false
window.onload = async () => {
window.jellyfinRewind = jellyfinRewind
console.log(`target year:`, import.meta.env.VITE_TARGET_YEAR)
console.log(`commit hash:`, __COMMITHASH__)
if (jellyfinRewind.auth.restoreSession()) {
console.info(`Session restored!`)
enableLogout()
serverConfig.classList.add(`hidden`)
init()
}
helper = new JellyHelper(jellyfinRewind.auth)
window.helper = helper
await Onboarding.init(jellyfinRewind.auth)
Onboarding.render()
}
function enableLogout() {
loggedInInfo.classList.remove(`hidden`)
logOutButton.addEventListener(`click`, () => {
jellyfinRewind.auth.destroySession()
window.location.reload()
})
loggedInUserInfo.innerText = jellyfinRewind.auth.config.user.name
loggedInServerInfo.innerText = jellyfinRewind.auth.config.serverInfo.ServerName
}
connectToServerButton.addEventListener(`click`, connectToServer)
serverUrl.addEventListener(`keydown`, (event) => {
if (event.key === `Enter`) {
connectToServer()
}
})
async function connectToServer() {
try {
await jellyfinRewind.auth.connectToServer(serverUrl.value)
userInfo = await jellyfinRewind.auth.fetchUsers()
console.info(`Connected to server!`)
console.info(`Users:`, userInfo)
serverConfig.classList.add(`hidden`)
showUsers()
} catch (err) {
console.error(`Error while connecting to the server:`, err)
infoOutput.innerText = `Error while connecting to the server. Make sure your using the same protocol (https or http) as your server is using. Make sure you're not using a local IP address or mDNS hostname. For example, you could use your server's Tailscale IP address, if you use Tailscale as your VPN.`
}
}
function showUsers() {
userSelect.classList.remove(`hidden`)
html`
${() => userInfo.map(user => html`
<li>
<button
class="rounded-xl p-2 w-full flex flex-row items-center justify-start gap-4 bg-white dark:bg-[#00A4DC] cursor-pointer focus:bg-[#0085B2] hover:bg-[#0085B2]"
data-user-id=${user.Id}
@click="${() => {
selectedUsername = user.Name
console.info(`User selected:`, user)
password.removeEventListener(`keydown`, login(user.Id))
password.addEventListener(`keydown`, login(user.Id))
authenticateUser.removeEventListener(`click`, login(user.Id))
authenticateUser.addEventListener(`click`, login(user.Id))
userLogin.classList.remove(`hidden`)
passwordInput.classList.remove(`hidden`)
userSelect.classList.add(`hidden`)
}}"
>
<img class="w-12 h-12 rounded-md" src="${() => user.PrimaryImageTag ? `${jellyfinRewind.auth.config.baseUrl}/Users/${user.Id}/Images/Primary?tag=${user.PrimaryImageTag}` : `/media/ArtistPlaceholder.png`}" />
<span class="text-lg">${user.Name}</span>
</button>
</li>
`)}
<li>
<button
class="rounded-xl p-2 w-full flex flex-row items-center justify-center gap-4 bg-white dark:bg-[#00A4DC] cursor-pointer focus:bg-[#0085B2] hover:bg-[#0085B2]"
@click="${() => {
userLogin.classList.remove(`hidden`)
usernameInput.classList.remove(`hidden`)
passwordInput.classList.remove(`hidden`)
userSelect.classList.add(`hidden`)
username.addEventListener(`keyup`, () => {
if (username.value.length > 0) {
authenticateUser.classList.remove(`hidden`)
selectedUsername = username.value
} else {
authenticateUser.classList.add(`hidden`)
}
})
password.removeEventListener(`keydown`, login(``))
password.addEventListener(`keydown`, login(``))
authenticateUser.removeEventListener(`click`, login(``))
authenticateUser.addEventListener(`click`, login(``))
}}"
>
<span class="italic">Manually enter username</span>
</button>
</li>
<li>
<button
class="rounded-xl p-2 w-full flex flex-row items-center justify-center gap-4 bg-white dark:bg-[#00A4DC] cursor-pointer focus:bg-[#0085B2] hover:bg-[#0085B2]"
@click="${() => {
userLogin.classList.remove(`hidden`)
authTokenInput.classList.remove(`hidden`)
userSelect.classList.add(`hidden`)
authToken.removeEventListener(`keydown`, loginWithAuthToken())
authToken.addEventListener(`keydown`, loginWithAuthToken())
authenticateUser.removeEventListener(`click`, loginWithAuthToken())
authenticateUser.addEventListener(`click`, loginWithAuthToken())
}}"
>
<span class="italic">Log in via auth token</span>
</button>
</li>
`(userSelect)
// manualUser.addEventListener(`click`, () => {
// userLogin.classList.remove(`hidden`)
// usernameInput.classList.remove(`hidden`)
// userSelect.classList.add(`hidden`)
// username.addEventListener(`keyup`, () => {
// selectedUsername = username.value
// })
// authenticateUser.removeEventListener(`click`, login(``))
// authenticateUser.addEventListener(`click`, login(``))
// password.removeEventListener(`keydown`, login(``))
// password.addEventListener(`keydown`, login(``))
// })
// userInfo.forEach(user => {
// const li = document.createElement(`li`)
// const button = document.createElement(`button`)
// button.textContent = user.Name
// button.setAttribute(`data-user-id`, user.Id)
// button.classList.add(`rounded-md`, `p-2`, `cursor-pointer`, `focus:bg-blue-200`, `hover:bg-blue-200`)
// button.addEventListener(`click`, () => {
// userLogin.classList.remove(`hidden`)
// selectedUsername = user.Name
// console.info(`User selected:`, user)
// userSelect.classList.add(`hidden`)
// authenticateUser.removeEventListener(`click`, login(user.Id))
// authenticateUser.addEventListener(`click`, login(user.Id))
// password.removeEventListener(`keydown`, login(user.Id))
// password.addEventListener(`keydown`, login(user.Id))
// })
// li.appendChild(button)
// userSelect.appendChild(li)
// })
}
const login = (userId) => async (event) => {
if (event.type !== `click` && event.key !== `Enter`) {
return
}
try {
await jellyfinRewind.auth.authenticateUser(selectedUsername, password.value)
console.info(`Successfully logged in as ${selectedUsername}`)
jellyfinRewind.auth.saveSession()
enableLogout()
userLogin.classList.add(`hidden`)
init()
} catch (err) {
console.error(`Error while logging in:`, err)
}
}
const loginWithAuthToken = () => async (event) => {
if (event.type !== `click` && event.key !== `Enter`) {
return
}
try {
await jellyfinRewind.auth.authenticateUserViaToken(authToken.value)
console.info(`Successfully logged in as ${jellyfinRewind.auth.config.name}`)
jellyfinRewind.auth.saveSession()
enableLogout()
userLogin.classList.add(`hidden`)
init()
} catch (err) {
console.error(`Error while logging in:`, err)
}
}
function init() {
showReport.addEventListener(`click`, async () => {
let rewindReportData = null
try {
loadingSpinner.classList.remove(`hidden`)
rewindReportData = {
jellyfinRewindReport: jellyfinRewind.restoreRewindReport()
}
if (rewindReportData.jellyfinRewindReport.commit !== __COMMITHASH__) {
staleReport = true
}
// check if the report is for the previous year and it's after February
if (rewindReportData.jellyfinRewindReport.year < new Date().getFullYear() && new Date().getMonth() > 1) {
staleReport = true
}
loadingSpinner.classList.add(`hidden`)
} catch (err) {
console.warn(`Couldn't restore Rewind report:`, err)
console.info(`Generating new report...`)
rewindReportData = await generateRewindReport()
}
if (staleReport) {
infoOutput.innerText = `The stored rewind report is stale. Please re-generate it for the best experience.`
}
// showRewindReport(rewindReport)
initializeFeatureStory(rewindReportData)
})
generateReport.addEventListener(`click`, async () => {
console.info(`Generating new report...`)
featuresInitialized = false // reset features
let rewindReportData = await generateRewindReport()
// showRewindReport(rewindReport)
initializeFeatureStory(rewindReportData)
})
generateReport.classList.remove(`hidden`)
showReport.classList.remove(`hidden`)
}
async function generateRewindReport() {
let reportData
try {
loadingSpinner.classList.remove(`hidden`)
reportData = await jellyfinRewind.generateRewindReport({
year: Number(import.meta.env.VITE_TARGET_YEAR)
})
console.info(`Report generated successfully!`)
loadingSpinner.classList.add(`hidden`)
} catch (err) {
console.error(`Error while generating the report:`, err)
}
try {
jellyfinRewind.saveRewindReport()
console.info(`Report saved successfully!`)
} catch (err) {
console.error(`Couldn't save Rewind report:`, err)
}
return reportData
}
function testShowRewindReport(report) {
output.value = JSON.stringify(report, null, 2)
const topSongByDuration = report.jellyfinRewindReport.tracks?.[`topTracksByPlayCount`]?.[0]
if (topSongByDuration) {
document.querySelector('#app').innerHTML += `
<div class="m-4 text-gray-800">
<h3 class="text-xl">Your Top Song of the Year</h3>
<div class="flex mt-4 flex-col">
<img id="test-image" class="w-64 h-auto rounded-md" />
<span class="font-semibold mt-2">${topSongByDuration?.name}</span>
<span class="italic pl-3 mt-1">by ${topSongByDuration.artistsBaseInfo.reduce((acc, cur, index) => index > 0 ? `${acc} & ${cur.name}` : cur.name, ``)}</span>
</div>
</div>
`
const testImage = document.querySelector(`#test-image`)
helper.loadImage(testImage, topSongByDuration.image)
}
}
function initializeFeatureStory(report) {
Features.openFeatures()
Features.init(report, helper, jellyfinRewind.auth)
if (!featuresInitialized) {
Features.render()
featuresInitialized = true
}
}
function downloadRewindReportData(reportData, skipVerification) {
if (reportData.rawData || skipVerification || confirm(`The report you're about to download is incomplete and missing some data. Please re-generate and download the report without reloading the page in-between. Do you want to download the incomplete report anyway?`)) {
const reportDataString = JSON.stringify(reportData, null, 2)
const blob = new Blob([reportDataString], {type: `application/json`})
const url = URL.createObjectURL(blob)
const a = document.createElement(`a`)
console.log(`jellyfinRewind.auth.config:`, jellyfinRewind.auth.config)
a.href = url
a.download = `jellyfin-rewind-report-${reportData.jellyfinRewindReport.year}_for-${jellyfinRewind.auth.config.user.name}-at-${jellyfinRewind.auth.config.serverInfo.ServerName}_${new Date().toISOString().slice(0, 10)}.json`
a.click()
}
}
window.downloadRewindReportData = downloadRewindReportData