mirror of
https://github.com/R0m1k3/FlowReader.git
synced 2026-10-20 13:48:10 +02:00
fix: add immediate feed fetch, refresh api and frontend update button
This commit is contained in:
1 parent
1263a12ca2
commit
007aec2094
4 files changed
+75
-9
No files matched your search
+2
-1
@@ -55,7 +55,7 @@ func main() {
|
|||||||
|
|
||||||
// Initialize handlers
|
// Initialize handlers
|
||||||
authHandler := handler.NewAuthHandler(authService)
|
authHandler := handler.NewAuthHandler(authService)
|
||||||
feedHandler := handler.NewFeedHandler(feedService, authService)
|
feedHandler := handler.NewFeedHandler(feedService, fetchService, authService)
|
||||||
articleHandler := handler.NewArticleHandler(articleRepo, feedService, authService, hub)
|
articleHandler := handler.NewArticleHandler(articleRepo, feedService, authService, hub)
|
||||||
wsHandler := handler.NewWSHandler(hub, authService)
|
wsHandler := handler.NewWSHandler(hub, authService)
|
||||||
adminHandler := handler.NewAdminHandler(userRepo, authService)
|
adminHandler := handler.NewAdminHandler(userRepo, authService)
|
||||||
@@ -109,6 +109,7 @@ func main() {
|
|||||||
r.Route("/feeds", func(r chi.Router) {
|
r.Route("/feeds", func(r chi.Router) {
|
||||||
r.Get("/", feedHandler.List)
|
r.Get("/", feedHandler.List)
|
||||||
r.Post("/", feedHandler.Add)
|
r.Post("/", feedHandler.Add)
|
||||||
|
r.Post("/refresh", feedHandler.Refresh)
|
||||||
r.Post("/import/opml", feedHandler.ImportOPML)
|
r.Post("/import/opml", feedHandler.ImportOPML)
|
||||||
r.Get("/export/opml", feedHandler.ExportOPML)
|
r.Get("/export/opml", feedHandler.ExportOPML)
|
||||||
r.Get("/{id}", feedHandler.Get)
|
r.Get("/{id}", feedHandler.Get)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -13,15 +15,17 @@ import (
|
|||||||
|
|
||||||
// FeedHandler handles feed-related HTTP requests.
|
// FeedHandler handles feed-related HTTP requests.
|
||||||
type FeedHandler struct {
|
type FeedHandler struct {
|
||||||
feedService *service.FeedService
|
feedService *service.FeedService
|
||||||
authService *service.AuthService
|
fetchService *service.FetchService
|
||||||
|
authService *service.AuthService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFeedHandler creates a new feed handler.
|
// NewFeedHandler creates a new feed handler.
|
||||||
func NewFeedHandler(feedService *service.FeedService, authService *service.AuthService) *FeedHandler {
|
func NewFeedHandler(feedService *service.FeedService, fetchService *service.FetchService, authService *service.AuthService) *FeedHandler {
|
||||||
return &FeedHandler{
|
return &FeedHandler{
|
||||||
feedService: feedService,
|
feedService: feedService,
|
||||||
authService: authService,
|
fetchService: fetchService,
|
||||||
|
authService: authService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,9 +89,39 @@ func (h *FeedHandler) Add(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trigger immediate fetch in background
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
_ = h.fetchService.FetchFeed(ctx, resp.ID)
|
||||||
|
}()
|
||||||
|
|
||||||
respondJSON(w, http.StatusCreated, resp)
|
respondJSON(w, http.StatusCreated, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Refresh handles POST /api/v1/feeds/refresh
|
||||||
|
func (h *FeedHandler) Refresh(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, err := h.getUserFromRequest(r)
|
||||||
|
if err != nil {
|
||||||
|
respondError(w, http.StatusUnauthorized, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For now, we refresh all feeds for the user synchronously or in background
|
||||||
|
// Let's do background and return 202 Accepted
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
feeds, _ := h.feedService.GetUserFeeds(userID)
|
||||||
|
for _, f := range feeds {
|
||||||
|
_ = h.fetchService.FetchFeed(ctx, f.ID)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
respondJSON(w, http.StatusAccepted, map[string]string{"message": "Refresh started"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get handles GET /api/v1/feeds/{id}
|
// Get handles GET /api/v1/feeds/{id}
|
||||||
func (h *FeedHandler) Get(w http.ResponseWriter, r *http.Request) {
|
func (h *FeedHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := h.getUserFromRequest(r)
|
userID, err := h.getUserFromRequest(r)
|
||||||
|
|||||||
@@ -52,6 +52,14 @@ export const feedsApi = {
|
|||||||
await handleResponse(response);
|
await handleResponse(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async refresh(): Promise<{ message: string }> {
|
||||||
|
const response = await fetch(`${API_BASE}/feeds/refresh`, {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
});
|
||||||
|
return handleResponse(response);
|
||||||
|
},
|
||||||
|
|
||||||
async importOPML(file: File): Promise<{ imported: number; skipped: number; errors?: string[] }> {
|
async importOPML(file: File): Promise<{ imported: number; skipped: number; errors?: string[] }> {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', file);
|
formData.append('file', file);
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
import { articlesApi } from '../api/articles';
|
import { articlesApi } from '../api/articles';
|
||||||
|
import { feedsApi } from '../api/feeds';
|
||||||
import { ArticleCard } from '../components/ArticleCard';
|
import { ArticleCard } from '../components/ArticleCard';
|
||||||
import { ReaderView } from '../components/ReaderView';
|
import { ReaderView } from '../components/ReaderView';
|
||||||
|
import { useWebsocket } from '../hooks/useWebsocket';
|
||||||
import type { Article } from '../api/articles';
|
import type { Article } from '../api/articles';
|
||||||
|
|
||||||
interface DashboardPageProps {
|
interface DashboardPageProps {
|
||||||
@@ -13,11 +15,23 @@ export function DashboardPage({ selectedFeedId }: DashboardPageProps) {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [selectedArticle, setSelectedArticle] = useState<Article | null>(null);
|
const [selectedArticle, setSelectedArticle] = useState<Article | null>(null);
|
||||||
|
|
||||||
|
// Real-time sync
|
||||||
|
useWebsocket();
|
||||||
|
|
||||||
const { data: articles, isLoading } = useQuery({
|
const { data: articles, isLoading } = useQuery({
|
||||||
queryKey: ['articles', selectedFeedId],
|
queryKey: ['articles', selectedFeedId],
|
||||||
queryFn: () => articlesApi.list({ feed_id: selectedFeedId || undefined }),
|
queryFn: () => articlesApi.list({ feed_id: selectedFeedId || undefined }),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const refreshMutation = useMutation({
|
||||||
|
mutationFn: () => feedsApi.refresh(),
|
||||||
|
onSuccess: () => {
|
||||||
|
// Optimistic feedback or just wait for WS?
|
||||||
|
// In case WS fails, we invalid articles
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['articles'] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const toggleReadMutation = useMutation({
|
const toggleReadMutation = useMutation({
|
||||||
mutationFn: ({ id, is_read }: { id: string; is_read: boolean }) =>
|
mutationFn: ({ id, is_read }: { id: string; is_read: boolean }) =>
|
||||||
is_read ? articlesApi.markRead(id) : articlesApi.markUnread(id),
|
is_read ? articlesApi.markRead(id) : articlesApi.markUnread(id),
|
||||||
@@ -50,9 +64,18 @@ export function DashboardPage({ selectedFeedId }: DashboardPageProps) {
|
|||||||
{selectedFeedId ? 'Archives du Flux' : 'La Une'}
|
{selectedFeedId ? 'Archives du Flux' : 'La Une'}
|
||||||
</h1>
|
</h1>
|
||||||
</div>
|
</div>
|
||||||
<div className="text-right">
|
<div className="flex flex-col items-end gap-3 text-right">
|
||||||
<p className="text-paper-muted text-xs font-serif italic">Mercredi 4 Février 2026</p>
|
<div>
|
||||||
<p className="text-[10px] text-paper-muted/30 uppercase tracking-widest font-black mt-1">Volume IX • No. 42</p>
|
<p className="text-paper-muted text-xs font-serif italic">Mercredi 4 Février 2026</p>
|
||||||
|
<p className="text-[10px] text-paper-muted/30 uppercase tracking-widest font-black mt-1">Volume IX • No. 42</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={() => refreshMutation.mutate()}
|
||||||
|
disabled={refreshMutation.isPending}
|
||||||
|
className={`text-[10px] uppercase tracking-[0.2em] font-bold px-4 py-2 rounded-full border border-gold/20 text-gold hover:bg-gold/10 transition-all ${refreshMutation.isPending ? 'animate-pulse' : ''}`}
|
||||||
|
>
|
||||||
|
{refreshMutation.isPending ? 'Mise à jour...' : 'Mettre à jour'}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
Reference in new issue
Block a user