98 lines
2.5 KiB
Go
Raw Normal View History

2019-06-03 09:32:43 +00:00
package arn
import "sort"
// ActivityConsumeAnime is a user activity that consumes anime.
type ActivityConsumeAnime struct {
2019-06-07 01:50:38 +00:00
AnimeID AnimeID `json:"animeId"`
FromEpisode int `json:"fromEpisode"`
ToEpisode int `json:"toEpisode"`
2019-06-03 09:32:43 +00:00
hasID
hasCreator
hasLikes
}
// NewActivityConsumeAnime creates a new activity.
2019-06-07 01:50:38 +00:00
func NewActivityConsumeAnime(animeID AnimeID, fromEpisode int, toEpisode int, userID UserID) *ActivityConsumeAnime {
2019-06-03 09:32:43 +00:00
return &ActivityConsumeAnime{
hasID: hasID{
ID: GenerateID("ActivityConsumeAnime"),
},
hasCreator: hasCreator{
Created: DateTimeUTC(),
CreatedBy: userID,
},
AnimeID: animeID,
FromEpisode: fromEpisode,
ToEpisode: toEpisode,
}
}
// Anime returns the anime.
func (activity *ActivityConsumeAnime) Anime() *Anime {
anime, _ := GetAnime(activity.AnimeID)
return anime
}
// TypeName returns the type name.
func (activity *ActivityConsumeAnime) TypeName() string {
return "ActivityConsumeAnime"
}
// Self returns the object itself.
func (activity *ActivityConsumeAnime) Self() Loggable {
return activity
}
// LastActivityConsumeAnime returns the last activity for the given anime.
2019-06-07 01:50:38 +00:00
func (user *User) LastActivityConsumeAnime(animeID AnimeID) *ActivityConsumeAnime {
2019-06-03 09:32:43 +00:00
activities := FilterActivitiesConsumeAnime(func(activity *ActivityConsumeAnime) bool {
return activity.AnimeID == animeID && activity.CreatedBy == user.ID
})
if len(activities) == 0 {
return nil
}
sort.Slice(activities, func(i, j int) bool {
return activities[i].Created > activities[j].Created
})
return activities[0]
}
// FilterActivitiesConsumeAnime filters all anime consumption activities by a custom function.
func FilterActivitiesConsumeAnime(filter func(*ActivityConsumeAnime) bool) []*ActivityConsumeAnime {
var filtered []*ActivityConsumeAnime
for obj := range DB.All("ActivityConsumeAnime") {
realObject := obj.(*ActivityConsumeAnime)
if filter(realObject) {
filtered = append(filtered, realObject)
}
}
return filtered
}
// // OnLike is called when the activity receives a like.
// func (activity *Activity) OnLike(likedBy *User) {
// if likedBy.ID == activity.CreatedBy {
// return
// }
// go func() {
// notifyUser := activity.Creator()
// notifyUser.SendNotification(&PushNotification{
// Title: likedBy.Nick + " liked your activity",
// Message: activity.TextByUser(notifyUser),
// Icon: "https:" + likedBy.AvatarLink("large"),
// Link: "https://notify.moe" + activity.Link(),
// Type: NotificationTypeLike,
// })
// }()
// }