Improved activity page
This commit is contained in:
parent
1cf4e6a1ae
commit
0f5f18db0c
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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"`
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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())
|
||||
|
||||
component ActivityConsumeAnimeText(activity *arn.ActivityConsumeAnime, user *arn.User)
|
||||
span watched
|
||||
.post-box
|
||||
.post-header
|
||||
.post-header-info
|
||||
ActivityConsumeAnimeTitle(activity, user)
|
||||
|
||||
.post-date.utc-date(data-date=activity.GetCreated())
|
||||
|
||||
.post-content
|
||||
ActivityConsumeAnimeText(activity, user)
|
||||
|
||||
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, "")
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user