Seperated global and followed activity feed

This commit is contained in:
2018-11-12 14:24:00 +09:00
parent 76990417e4
commit f711cdb011
12 changed files with 114 additions and 20 deletions

View File

@ -0,0 +1,6 @@
import AnimeNotifier from "scripts/AnimeNotifier"
// Reload content of current page
export function reloadContent(arn: AnimeNotifier) {
return arn.reloadContent()
}

View File

@ -1,3 +1,4 @@
export * from "./Activity"
export * from "./Audio"
export * from "./AnimeList"
export * from "./Diff"

View File

@ -1,4 +1,5 @@
import AnimeNotifier from "./AnimeNotifier"
import { plural } from "./Utils"
const reconnectDelay = 3000
@ -35,6 +36,7 @@ export default class ServerEvents {
this.eventSource.addEventListener("ping", (e: any) => this.ping(e))
this.eventSource.addEventListener("etag", (e: any) => this.etag(e))
this.eventSource.addEventListener("activity", (e: any) => this.activity(e))
this.eventSource.addEventListener("notificationCount", (e: any) => this.notificationCount(e))
this.eventSource.onerror = () => {
@ -58,6 +60,26 @@ export default class ServerEvents {
this.etags.set(data.url, newETag)
}
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")
let buttonText = document.getElementById("load-new-activities-text")
let newCount = parseInt(button.dataset.count) + 1
button.dataset.count = newCount.toString()
buttonText.textContent = plural(newCount, "new activity")
}
notificationCount(e: ServerEvent) {
this.arn.notificationManager.setCounter(parseInt(e.data))
}

View File

@ -1,3 +1,15 @@
const specialized = {
"new activity": "new activities"
}
export function plural(count: number, singular: string): string {
return (count === 1 || count === -1) ? (count + " " + singular) : (count + " " + singular + "s")
if(count === 1 || count === -1) {
return count + " " + singular
}
if(specialized[singular]) {
return count + " " + specialized[singular]
}
return count + " " + singular + "s"
}