2018-11-07 07:49:49 +00:00
|
|
|
import AnimeNotifier from "./AnimeNotifier"
|
2018-11-12 05:24:00 +00:00
|
|
|
import { plural } from "./Utils"
|
2018-11-07 07:49:49 +00:00
|
|
|
|
|
|
|
const reconnectDelay = 3000
|
|
|
|
|
2018-11-06 20:40:03 +00:00
|
|
|
class ServerEvent {
|
|
|
|
data: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ServerEvents {
|
|
|
|
supported: boolean
|
|
|
|
eventSource: EventSource
|
2018-11-07 07:49:49 +00:00
|
|
|
arn: AnimeNotifier
|
|
|
|
etags: Map<string, string>
|
2018-11-06 20:40:03 +00:00
|
|
|
|
2018-11-07 07:49:49 +00:00
|
|
|
constructor(arn: AnimeNotifier) {
|
2018-11-06 20:40:03 +00:00
|
|
|
this.supported = ("EventSource" in window)
|
|
|
|
|
|
|
|
if(!this.supported) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-07 07:49:49 +00:00
|
|
|
this.arn = arn
|
|
|
|
this.etags = new Map<string, string>()
|
|
|
|
this.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
if(this.eventSource) {
|
|
|
|
this.eventSource.close()
|
|
|
|
}
|
|
|
|
|
2018-11-06 20:40:03 +00:00
|
|
|
this.eventSource = new EventSource("/api/sse/events", {
|
|
|
|
withCredentials: true
|
|
|
|
})
|
|
|
|
|
|
|
|
this.eventSource.addEventListener("ping", (e: any) => this.ping(e))
|
2018-11-07 07:49:49 +00:00
|
|
|
this.eventSource.addEventListener("etag", (e: any) => this.etag(e))
|
2018-11-12 05:24:00 +00:00
|
|
|
this.eventSource.addEventListener("activity", (e: any) => this.activity(e))
|
2018-11-07 07:49:49 +00:00
|
|
|
this.eventSource.addEventListener("notificationCount", (e: any) => this.notificationCount(e))
|
|
|
|
|
|
|
|
this.eventSource.onerror = () => {
|
|
|
|
setTimeout(() => this.connect(), reconnectDelay)
|
|
|
|
}
|
2018-11-06 20:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ping(e: ServerEvent) {
|
2018-11-07 07:49:49 +00:00
|
|
|
console.log("ping")
|
|
|
|
}
|
|
|
|
|
|
|
|
etag(e: ServerEvent) {
|
|
|
|
let data = JSON.parse(e.data)
|
|
|
|
let oldETag = this.etags.get(data.url)
|
|
|
|
let newETag = data.etag
|
|
|
|
|
|
|
|
if(oldETag && newETag && oldETag != newETag) {
|
|
|
|
this.arn.statusMessage.showInfo("A new version of the website is available. Please refresh the page.", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.etags.set(data.url, newETag)
|
|
|
|
}
|
|
|
|
|
2018-11-12 05:24:00 +00:00
|
|
|
activity(e: ServerEvent) {
|
|
|
|
if(!location.pathname.startsWith("/activity")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let isFollowingUser = JSON.parse(e.data)
|
|
|
|
|
|
|
|
// If we're on the followed only feed and we receive an activity
|
|
|
|
// about a user we don't follow, ignore the message.
|
|
|
|
if(location.pathname.startsWith("/activity/followed") && !isFollowingUser) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let button = document.getElementById("load-new-activities")
|
2018-11-16 12:45:13 +00:00
|
|
|
|
2019-04-19 13:12:33 +00:00
|
|
|
if(!button || !button.dataset.count) {
|
2018-11-16 12:45:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-12 05:24:00 +00:00
|
|
|
let buttonText = document.getElementById("load-new-activities-text")
|
2019-04-19 13:12:33 +00:00
|
|
|
|
|
|
|
if(!buttonText) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-12 05:24:00 +00:00
|
|
|
let newCount = parseInt(button.dataset.count) + 1
|
|
|
|
button.dataset.count = newCount.toString()
|
|
|
|
buttonText.textContent = plural(newCount, "new activity")
|
|
|
|
}
|
|
|
|
|
2018-11-07 07:49:49 +00:00
|
|
|
notificationCount(e: ServerEvent) {
|
2019-04-19 13:50:52 +00:00
|
|
|
if(!this.arn.notificationManager) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-07 07:49:49 +00:00
|
|
|
this.arn.notificationManager.setCounter(parseInt(e.data))
|
2018-11-06 20:40:03 +00:00
|
|
|
}
|
|
|
|
}
|