Seperated global and followed activity feed

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

View File

@ -9,11 +9,33 @@ import (
const maxActivitiesPerPage = 40
// Get activity page.
func Get(ctx *aero.Context) string {
// Global activity page.
func Global(ctx *aero.Context) string {
user := utils.GetUser(ctx)
activities := fetchActivities(user, false)
return ctx.HTML(components.ActivityFeed(activities, user))
}
// Followed activity page.
func Followed(ctx *aero.Context) string {
user := utils.GetUser(ctx)
activities := fetchActivities(user, true)
return ctx.HTML(components.ActivityFeed(activities, user))
}
// fetchActivities filters the activities by the given filters.
func fetchActivities(user *arn.User, followedOnly bool) []arn.Activity {
var followedUserIDs []string
if followedOnly && user != nil {
followedUserIDs = user.Follows().Items
}
activities := arn.FilterActivities(func(activity arn.Activity) bool {
if followedOnly && !arn.Contains(followedUserIDs, activity.GetCreatedBy()) {
return false
}
if activity.Type() == "ActivityCreate" {
obj := activity.(*arn.ActivityCreate).Object()
@ -21,8 +43,8 @@ func Get(ctx *aero.Context) string {
return false
}
draft, isDraftable := obj.(arn.HasDraft)
return !isDraftable || !draft.IsDraft
draft, isDraftable := obj.(arn.Draftable)
return !isDraftable || !draft.GetIsDraft()
}
if activity.Type() == "ActivityConsumeAnime" {
@ -38,5 +60,5 @@ func Get(ctx *aero.Context) string {
activities = activities[:maxActivitiesPerPage]
}
return ctx.HTML(components.ActivityFeed(activities, user))
return activities
}

View File

@ -1,12 +1,25 @@
component ActivityFeed(entries []arn.Activity, user *arn.User)
h1 Activity
h1.page-title Activity
.tabs
Tab("Global", "globe", "/activity")
Tab("Followed", "user-plus", "/activity/followed")
.activities
each entry in entries
Activity(entry, user)
if len(entries) == 0
p.no-data.mountable No activity here.
else
each entry in entries
Activity(entry, user)
#load-new-activities(data-count="0")
.buttons
button.page-main-action.action(data-action="reloadContent", data-trigger="click")
Icon("refresh")
span#load-new-activities-text 0 new activities
component Activity(activity arn.Activity, user *arn.User)
.activity.post-parent(id=fmt.Sprintf("activity-%s", activity.GetID()))
.activity.post-parent.mountable(id=fmt.Sprintf("activity-%s", activity.GetID()))
.post-author
Avatar(activity.Creator())
.post-content
@ -29,19 +42,23 @@ component ActivityConsumeAnimeTitle(activity *arn.ActivityConsumeAnime, user *ar
component ActivityConsumeAnimeText(activity *arn.ActivityConsumeAnime, user *arn.User)
if activity.ToEpisode > activity.FromEpisode
em= fmt.Sprintf("watched episodes %d - %d", activity.FromEpisode, activity.ToEpisode)
em.actvity-text-consume-anime= fmt.Sprintf("watched episodes %d - %d", activity.FromEpisode, activity.ToEpisode)
else
em= fmt.Sprintf("watched episode %d", activity.ToEpisode)
em.actvity-text-consume-anime= fmt.Sprintf("watched episode %d", activity.ToEpisode)
component ActivityCreateTitle(activity *arn.ActivityCreate, user *arn.User)
if activity.ObjectType == "Post"
a(href=activity.Postable().Parent().Link())= activity.Postable().Parent().TitleByUser(user)
else if activity.ObjectType == "Thread"
a(href=activity.Postable().Link())= activity.Postable().TitleByUser(user)
else if activity.ObjectType == "AMV" || activity.ObjectType == "SoundTrack" || activity.ObjectType == "Quote"
a(href=activity.Object().(arn.PostParent).Link())= activity.Object().(arn.PostParent).TitleByUser(user)
component ActivityCreateText(activity *arn.ActivityCreate, user *arn.User)
if activity.ObjectType == "Post" || activity.ObjectType == "Thread"
div!= activity.Postable().HTML()
else
em.actvity-text-create= "new " + strings.ToLower(activity.ObjectType)
//- component ActivityFeed(entries []*arn.EditLogEntry, user *arn.User)
//- h1 Activity

View File

@ -15,3 +15,10 @@
.activity-date
color hsla(text-color-h, text-color-s, text-color-l, 0.5)
.actvity-text-create
opacity 0.8
#load-new-activities
[data-count="0"]
display none

View File

@ -7,7 +7,7 @@ component Forum(tag string, threads []*arn.Thread, threadsPerPage int)
ThreadList(threads)
.buttons
button#new-thread.action(data-action="load", data-trigger="click", data-url="/new/thread")
button#new-thread.page-main-action.action(data-action="load", data-trigger="click", data-url="/new/thread")
Icon("plus")
span New thread
//- if len(threads) == threadsPerPage

View File

@ -7,7 +7,7 @@
max-width forum-width
> 1250px
#new-thread
.page-main-action
position fixed
right content-padding
bottom content-padding

View File

@ -20,7 +20,8 @@ func Register(l *layout.Layout) {
l.Page("/login", login.Get)
// Activity
l.Page("/activity", activity.Get)
l.Page("/activity", activity.Global)
l.Page("/activity/followed", activity.Followed)
// Calendar
l.Page("/calendar", calendar.Get)

View File

@ -16,9 +16,15 @@ func main() {
continue
}
draft, isDraftable := obj.(arn.HasDraft)
draft, isDraftable := obj.(arn.Draftable)
if isDraftable && draft.IsDraft {
if isDraftable && draft.GetIsDraft() {
continue
}
// We don't create activity entries for anything
// other than posts and threads.
if entry.ObjectType != "Post" && entry.ObjectType != "Thread" {
continue
}

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 @@
export function plural(count: number, singular: string): string {
return (count === 1 || count === -1) ? (count + " " + singular) : (count + " " + singular + "s")
const specialized = {
"new activity": "new activities"
}
export function plural(count: number, singular: string): string {
if(count === 1 || count === -1) {
return count + " " + singular
}
if(specialized[singular]) {
return count + " " + specialized[singular]
}
return count + " " + singular + "s"
}

View File

@ -67,7 +67,7 @@ var routeTests = map[string][]string{
},
"/user/:nick/animelist/watching/from/:index": []string{
"/+Akyoto/animelist/watching/from/3",
"/+Akyoto/animelist/watching/from/1",
},
"/user/:nick/animelist/completed": []string{