From 0f5f18db0ca02f17fe83ee5c39e33abfa9e5fd25 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 23 Nov 2021 15:47:25 +0900 Subject: [PATCH] Improved activity page --- arn/ActivityConsumeAnime.go | 13 +++---- arn/AnimeListItemAPI.go | 2 +- arn/PostAPI.go | 7 ++++ arn/Settings.go | 6 ++++ arn/ThreadAPI.go | 7 ++++ arn/UserFollows.go | 4 +++ pages/activity/activity.go | 38 +++++++++++++++----- pages/activity/activity.pixy | 36 +++++++++++++++---- pages/index/activityroutes/activityroutes.go | 8 ++--- scripts/NotificationManager.ts | 2 +- scripts/ServerEvent/receiveServerEvents.ts | 20 ++++++++--- scripts/Utils/uploadWithProgress.ts | 2 +- 12 files changed, 113 insertions(+), 32 deletions(-) diff --git a/arn/ActivityConsumeAnime.go b/arn/ActivityConsumeAnime.go index 8ec7d8ab..0e5dda20 100644 --- a/arn/ActivityConsumeAnime.go +++ b/arn/ActivityConsumeAnime.go @@ -47,8 +47,9 @@ func (activity *ActivityConsumeAnime) Self() Loggable { // LastActivityConsumeAnime returns the last activity for the given anime. func (user *User) LastActivityConsumeAnime(animeID AnimeID) *ActivityConsumeAnime { - activities := FilterActivitiesConsumeAnime(func(activity *ActivityConsumeAnime) bool { - return activity.AnimeID == animeID && activity.CreatedBy == user.ID + activities := FilterActivitiesConsumeAnime(func(activity Activity) bool { + consume := activity.(*ActivityConsumeAnime) + return consume.AnimeID == animeID && consume.CreatedBy == user.ID }) if len(activities) == 0 { @@ -56,15 +57,15 @@ func (user *User) LastActivityConsumeAnime(animeID AnimeID) *ActivityConsumeAnim } sort.Slice(activities, func(i, j int) bool { - return activities[i].Created > activities[j].Created + return activities[i].GetCreated() > activities[j].GetCreated() }) - return activities[0] + return activities[0].(*ActivityConsumeAnime) } // FilterActivitiesConsumeAnime filters all anime consumption activities by a custom function. -func FilterActivitiesConsumeAnime(filter func(*ActivityConsumeAnime) bool) []*ActivityConsumeAnime { - var filtered []*ActivityConsumeAnime +func FilterActivitiesConsumeAnime(filter func(Activity) bool) []Activity { + var filtered []Activity for obj := range DB.All("ActivityConsumeAnime") { realObject := obj.(*ActivityConsumeAnime) diff --git a/arn/AnimeListItemAPI.go b/arn/AnimeListItemAPI.go index 09a2cbe6..22b31f14 100644 --- a/arn/AnimeListItemAPI.go +++ b/arn/AnimeListItemAPI.go @@ -47,7 +47,7 @@ func (item *AnimeListItem) Edit(ctx aero.Context, key string, value reflect.Valu // Broadcast event to all users so they can reload the activity page if needed. for receiver := range StreamUsers() { - activityEvent := event.New("activity", receiver.IsFollowing(user.ID)) + activityEvent := event.New("watch activity", receiver.IsFollowing(user.ID)) receiver.BroadcastEvent(activityEvent) } } diff --git a/arn/PostAPI.go b/arn/PostAPI.go index 86c6c086..4da252f9 100644 --- a/arn/PostAPI.go +++ b/arn/PostAPI.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/aerogo/api" "github.com/aerogo/markdown" "github.com/animenotifier/notify.moe/arn/autocorrect" @@ -182,6 +183,12 @@ func (post *Post) Create(ctx aero.Context) error { activity := NewActivityCreate("Post", post.ID, user.ID) activity.Save() + // Broadcast event to all users so they can reload the activity page if needed + for receiver := range StreamUsers() { + activityEvent := event.New("post activity", receiver.IsFollowing(user.ID)) + receiver.BroadcastEvent(activityEvent) + } + return nil } diff --git a/arn/Settings.go b/arn/Settings.go index 0e0ac705..a90cab9e 100644 --- a/arn/Settings.go +++ b/arn/Settings.go @@ -37,10 +37,16 @@ type Settings struct { Notification NotificationSettings `json:"notification"` Editor EditorSettings `json:"editor"` Privacy PrivacySettings `json:"privacy"` + Activity ActivitySettings `json:"activity"` Calendar CalendarSettings `json:"calendar" editable:"true"` Theme string `json:"theme" editable:"true"` } +// ActivitySettings ... +type ActivitySettings struct { + ShowFollowedOnly bool `json:"showFollowedOnly" editable:"true"` +} + // PrivacySettings ... type PrivacySettings struct { ShowAge bool `json:"showAge" editable:"true"` diff --git a/arn/ThreadAPI.go b/arn/ThreadAPI.go index 7b29c787..a3b0dd38 100644 --- a/arn/ThreadAPI.go +++ b/arn/ThreadAPI.go @@ -6,6 +6,7 @@ import ( "reflect" "github.com/aerogo/aero" + "github.com/aerogo/aero/event" "github.com/aerogo/api" "github.com/aerogo/markdown" "github.com/animenotifier/notify.moe/arn/autocorrect" @@ -120,6 +121,12 @@ func (thread *Thread) Create(ctx aero.Context) error { activity := NewActivityCreate("Thread", thread.ID, user.ID) activity.Save() + // Broadcast event to all users so they can reload the activity page if needed + for receiver := range StreamUsers() { + activityEvent := event.New("post activity", receiver.IsFollowing(user.ID)) + receiver.BroadcastEvent(activityEvent) + } + return nil } diff --git a/arn/UserFollows.go b/arn/UserFollows.go index 76c97317..aa166993 100644 --- a/arn/UserFollows.go +++ b/arn/UserFollows.go @@ -55,6 +55,10 @@ func (user *User) Unfollow(userID UserID) bool { // IsFollowing checks if the object follows the user ID. func (user *User) IsFollowing(userID UserID) bool { + if userID == user.ID { + return true + } + for _, item := range user.FollowIDs { if item == userID { return true diff --git a/pages/activity/activity.go b/pages/activity/activity.go index 44a282b4..b58a3403 100644 --- a/pages/activity/activity.go +++ b/pages/activity/activity.go @@ -5,22 +5,24 @@ import ( "github.com/animenotifier/notify.moe/arn" ) -// Global activity page. -func Global(ctx aero.Context) error { +// Posts activity page. +func Posts(ctx aero.Context) error { user := arn.GetUserFromContext(ctx) - activities := fetchActivities(user, false) + activities := fetchCreateActivities(user) return render(ctx, activities) } -// Followed activity page. -func Followed(ctx aero.Context) error { +// Watch activity page. +func Watch(ctx aero.Context) error { user := arn.GetUserFromContext(ctx) - activities := fetchActivities(user, true) + activities := fetchConsumeActivities(user) return render(ctx, activities) } -// fetchActivities filters the activities by the given filters. -func fetchActivities(user *arn.User, followedOnly bool) []arn.Activity { +// fetchCreateActivities filters the activities by the given filters. +func fetchCreateActivities(user *arn.User) []arn.Activity { + followedOnly := user.Settings().Activity.ShowFollowedOnly + activities := arn.FilterActivityCreates(func(activity arn.Activity) bool { if followedOnly && user != nil && !user.IsFollowing(activity.GetCreatedBy()) { return false @@ -43,3 +45,23 @@ func fetchActivities(user *arn.User, followedOnly bool) []arn.Activity { arn.SortActivitiesLatestFirst(activities) return activities } + +// fetchConsumeActivities filters the consume activities by the given filters. +func fetchConsumeActivities(user *arn.User) []arn.Activity { + followedOnly := user.Settings().Activity.ShowFollowedOnly + + activities := arn.FilterActivitiesConsumeAnime(func(activity arn.Activity) bool { + if followedOnly && user != nil && !user.IsFollowing(activity.GetCreatedBy()) { + return false + } + + if !activity.Creator().HasNick() { + return false + } + + return true + }) + + arn.SortActivitiesLatestFirst(activities) + return activities +} diff --git a/pages/activity/activity.pixy b/pages/activity/activity.pixy index dbc5d830..c622cf76 100644 --- a/pages/activity/activity.pixy +++ b/pages/activity/activity.pixy @@ -2,8 +2,18 @@ component ActivityFeed(entries []arn.Activity, nextIndex int, user *arn.User) h1.page-title Activity .tabs - Tab("Global", "globe", "/activity") - Tab("Followed", "user-plus", "/activity/followed") + Tab("Posts", "comment", "/activity") + Tab("Watch", "eye", "/activity/watch") + + .corner-buttons(data-api="/api/settings/" + user.ID) + if user.Settings().Activity.ShowFollowedOnly + button.action(id="Activity.ShowFollowedOnly", data-action="disable", data-trigger="click", data-field="Activity.ShowFollowedOnly", title="Followed only") + Icon("toggle-on") + span Followed + else + button.action(id="Activity.ShowFollowedOnly", data-action="enable", data-trigger="click", data-field="Activity.ShowFollowedOnly", title="Followed only") + Icon("toggle-off") + span Followed if len(entries) == 0 p.no-data.mountable No activity here. @@ -62,16 +72,28 @@ component Activity(activity arn.Activity, user *arn.User) //- ActivityConsumeAnimeText(activity.(*arn.ActivityConsumeAnime), user) component ActivityConsumeAnime(activity *arn.ActivityConsumeAnime, user *arn.User) - h1 TODO + .activity.mountable.post-parent(id=fmt.Sprintf("activity-%s", activity.GetID()), data-type="consume-anime") + .post-author + Avatar(activity.Creator()) + + .post-box + .post-header + .post-header-info + ActivityConsumeAnimeTitle(activity, user) + + .post-date.utc-date(data-date=activity.GetCreated()) + + .post-content + ActivityConsumeAnimeText(activity, user) -component ActivityConsumeAnimeText(activity *arn.ActivityConsumeAnime, user *arn.User) - span watched +component ActivityConsumeAnimeTitle(activity *arn.ActivityConsumeAnime, user *arn.User) a(href=activity.Anime().Link())= activity.Anime().TitleByUser(user) +component ActivityConsumeAnimeText(activity *arn.ActivityConsumeAnime, user *arn.User) if activity.ToEpisode > activity.FromEpisode - span= fmt.Sprintf(" episodes %d - %d.", activity.FromEpisode, activity.ToEpisode) + span= fmt.Sprintf("%s watched episodes %d - %d.", activity.Creator().Nick, activity.FromEpisode, activity.ToEpisode) else - span= fmt.Sprintf(" episode %d.", activity.ToEpisode) + span= fmt.Sprintf("%s watched episode %d.", activity.Creator().Nick, activity.ToEpisode) component ActivityCreate(activity *arn.ActivityCreate, user *arn.User) Postable(activity.Postable(), user, false, true, "") diff --git a/pages/index/activityroutes/activityroutes.go b/pages/index/activityroutes/activityroutes.go index 5718f314..167d861b 100644 --- a/pages/index/activityroutes/activityroutes.go +++ b/pages/index/activityroutes/activityroutes.go @@ -8,8 +8,8 @@ import ( // Register registers the page routes. func Register(app *aero.Application) { - page.Get(app, "/activity", activity.Global) - page.Get(app, "/activity/from/:index", activity.Global) - page.Get(app, "/activity/followed", activity.Followed) - page.Get(app, "/activity/followed/from/:index", activity.Followed) + page.Get(app, "/activity", activity.Posts) + page.Get(app, "/activity/from/:index", activity.Posts) + page.Get(app, "/activity/watch", activity.Watch) + page.Get(app, "/activity/watch/from/:index", activity.Watch) } diff --git a/scripts/NotificationManager.ts b/scripts/NotificationManager.ts index 971fd452..c22fcbcc 100644 --- a/scripts/NotificationManager.ts +++ b/scripts/NotificationManager.ts @@ -16,7 +16,7 @@ export default class NotificationManager { }) const body = await response.text() - this.setCounter(parseInt(body)) + this.setCounter(parseInt(body, 10)) } setCounter(unseen: number) { diff --git a/scripts/ServerEvent/receiveServerEvents.ts b/scripts/ServerEvent/receiveServerEvents.ts index b9cef4fc..a6a96c56 100644 --- a/scripts/ServerEvent/receiveServerEvents.ts +++ b/scripts/ServerEvent/receiveServerEvents.ts @@ -32,7 +32,8 @@ function connect() { eventSource.addEventListener("ping", (e: any) => ping(e)) eventSource.addEventListener("etag", (e: any) => etag(e)) - eventSource.addEventListener("activity", (e: any) => activity(e)) + eventSource.addEventListener("post activity", (e: any) => activity(e, "post")) + eventSource.addEventListener("watch activity", (e: any) => activity(e, "watch")) eventSource.addEventListener("notificationCount", (e: any) => notificationCount(e)) eventSource.onerror = () => { @@ -56,16 +57,27 @@ function etag(e: ServerEvent) { etags.set(data.url, newETag) } -function activity(e: ServerEvent) { - if(!location.pathname.startsWith("/activity")) { +function activity(e: ServerEvent, activityType: string) { + if(activityType === "post" && !location.pathname.endsWith("/activity")) { return } + if(activityType === "watch" && !location.pathname.endsWith("/activity/watch")) { + return + } + + const showFollowedOnlyButton = document.getElementById("Activity.ShowFollowedOnly") + + if(!showFollowedOnlyButton) { + return + } + + const showFollowedOnly = (showFollowedOnlyButton.dataset.action === "disable") const 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) { + if(showFollowedOnly && !isFollowingUser) { return } diff --git a/scripts/Utils/uploadWithProgress.ts b/scripts/Utils/uploadWithProgress.ts index e0ba5e18..1b7bd868 100644 --- a/scripts/Utils/uploadWithProgress.ts +++ b/scripts/Utils/uploadWithProgress.ts @@ -21,7 +21,7 @@ export default function uploadWithProgress(url, options: RequestInit, onProgress xhr.open(options.method || "GET", url, true) if(options.headers) { - for(const key in options.headers) { + for(const key of Object.keys(options.headers)) { xhr.setRequestHeader(key, options.headers[key]) } }