feat: replace notifications with continue watching
This commit is contained in:
@@ -314,51 +314,6 @@ func (h *Handler) HandleAPIDiscoverUpcoming(w http.ResponseWriter, r *http.Reque
|
||||
templates.DiscoverItems(res.Animes, "upcoming", page+1, res.HasNextPage).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleNotifications(w http.ResponseWriter, r *http.Request) {
|
||||
userID := userIDFromRequest(r)
|
||||
|
||||
if userID == "" {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
tab := r.URL.Query().Get("tab")
|
||||
if tab != "sequels" {
|
||||
tab = "tracking"
|
||||
}
|
||||
|
||||
var watching []templates.WatchingAnimeWithDetails
|
||||
if tab == "tracking" {
|
||||
var err error
|
||||
watching, err = h.svc.GetWatchingAnime(r.Context(), userID)
|
||||
if err != nil {
|
||||
log.Printf("watching anime error: %v", err)
|
||||
http.Error(w, "Failed to fetch watching anime", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
templates.Notifications(watching, tab).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleNotificationsUpcoming(w http.ResponseWriter, r *http.Request) {
|
||||
userID := userIDFromRequest(r)
|
||||
|
||||
if userID == "" {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
upcomingSeasons, err := h.svc.GetUpcomingSeasons(r.Context(), userID)
|
||||
if err != nil {
|
||||
log.Printf("upcoming seasons error: %v", err)
|
||||
http.Error(w, "Failed to fetch upcoming seasons", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
templates.UpcomingSeasonsList(upcomingSeasons).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleStudioDetails(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := r.URL.Path[len("/studios/"):]
|
||||
id, err := strconv.Atoi(idStr)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"mal/internal/database"
|
||||
"mal/internal/jikan"
|
||||
"mal/internal/templates"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -98,52 +97,6 @@ func (s *Service) GetRecommendations(ctx context.Context, animeID int, limit int
|
||||
return s.jikanClient.GetRecommendations(ctx, animeID, limit)
|
||||
}
|
||||
|
||||
func (s *Service) GetWatchingAnime(ctx context.Context, userID string) ([]templates.WatchingAnimeWithDetails, error) {
|
||||
rows, err := s.db.GetWatchingAnime(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get watching anime: %w", err)
|
||||
}
|
||||
|
||||
var result []templates.WatchingAnimeWithDetails
|
||||
for _, row := range rows {
|
||||
anime, err := s.jikanClient.GetAnimeByID(ctx, int(row.AnimeID))
|
||||
if err != nil {
|
||||
if jikan.IsRetryableError(err) {
|
||||
s.jikanClient.EnqueueAnimeFetchRetry(ctx, int(row.AnimeID), err)
|
||||
}
|
||||
// Instead of skipping, we still append it, but without the extra Jikan details
|
||||
// This prevents anime from vanishing from the watchlist when Jikan rate limits us.
|
||||
anime = jikan.Anime{}
|
||||
}
|
||||
result = append(result, templates.WatchingAnimeWithDetails{
|
||||
Entry: row,
|
||||
Anime: anime,
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetUpcomingSeasons(ctx context.Context, userID string) ([]database.GetUpcomingSeasonsRow, error) {
|
||||
rows, err := s.db.GetUpcomingSeasons(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get upcoming seasons: %w", err)
|
||||
}
|
||||
|
||||
// Deduplicate by related anime ID
|
||||
// Because of the recursive query, multiple prequels can point to the same upcoming season
|
||||
seen := make(map[int64]bool)
|
||||
var deduped []database.GetUpcomingSeasonsRow
|
||||
for _, row := range rows {
|
||||
if !seen[row.ID] {
|
||||
seen[row.ID] = true
|
||||
deduped = append(deduped, row)
|
||||
}
|
||||
}
|
||||
|
||||
return deduped, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetAnimeByProducer(ctx context.Context, producerID int, page int) (jikan.StudioAnimeResult, error) {
|
||||
return s.jikanClient.GetAnimeByProducer(ctx, producerID, page)
|
||||
}
|
||||
|
||||
@@ -173,14 +173,7 @@ func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var filteredEntries []database.GetUserWatchListRow
|
||||
if statusFilter == "continuing" {
|
||||
// Show airing anime with watching or plan_to_watch status
|
||||
for _, entry := range entries {
|
||||
if entry.Airing.Bool && (entry.Status == "watching" || entry.Status == "plan_to_watch") {
|
||||
filteredEntries = append(filteredEntries, entry)
|
||||
}
|
||||
}
|
||||
} else if statusFilter != "" && statusFilter != "all" {
|
||||
if statusFilter != "" && statusFilter != "all" {
|
||||
for _, entry := range entries {
|
||||
if entry.Status == statusFilter {
|
||||
filteredEntries = append(filteredEntries, entry)
|
||||
@@ -197,6 +190,59 @@ func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||
templates.Watchlist(filteredEntries, layout, statusFilter, sortBy, sortOrder).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleContinueWatching(w http.ResponseWriter, r *http.Request) {
|
||||
if !requireMethod(w, r, http.MethodGet) {
|
||||
return
|
||||
}
|
||||
|
||||
user := middleware.GetUser(r.Context())
|
||||
if user == nil {
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
entries, err := h.svc.db.GetContinueWatchingEntries(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
log.Printf("continue watching fetch failed: user_id=%s err=%v", user.ID, err)
|
||||
http.Error(w, "failed to fetch continue watching", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
templates.ContinueWatching(entries).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleDeleteContinueWatching(w http.ResponseWriter, r *http.Request) {
|
||||
if !requireMethod(w, r, http.MethodDelete) {
|
||||
return
|
||||
}
|
||||
|
||||
user := middleware.GetUser(r.Context())
|
||||
if user == nil {
|
||||
w.Header().Set("HX-Redirect", "/login")
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
path := r.URL.Path[len("/api/continue-watching/"):]
|
||||
animeID, err := strconv.ParseInt(path, 10, 64)
|
||||
if err != nil || animeID <= 0 {
|
||||
http.Error(w, "invalid anime ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.svc.db.DeleteContinueWatchingEntry(r.Context(), database.DeleteContinueWatchingEntryParams{
|
||||
UserID: user.ID,
|
||||
AnimeID: animeID,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("continue watching delete failed: user_id=%s anime_id=%d err=%v", user.ID, animeID, err)
|
||||
http.Error(w, "failed to delete continue watching entry", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||
if !requireMethod(w, r, http.MethodGet) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user