✨ Web feed subscription APIs
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using DysonNetwork.Shared.Proto;
|
using DysonNetwork.Shared.Proto;
|
||||||
using DysonNetwork.Sphere.Publisher;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
@@ -8,7 +8,8 @@ namespace DysonNetwork.Sphere.WebReader;
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("/api/feeds")]
|
[Route("/api/feeds")]
|
||||||
public class WebFeedPublicController(
|
public class WebFeedPublicController(
|
||||||
AppDatabase db
|
AppDatabase db,
|
||||||
|
WebFeedService webFeed
|
||||||
) : ControllerBase
|
) : ControllerBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -102,9 +103,9 @@ public class WebFeedPublicController(
|
|||||||
/// List all feeds the current user is subscribed to
|
/// List all feeds the current user is subscribed to
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>List of subscribed feeds</returns>
|
/// <returns>List of subscribed feeds</returns>
|
||||||
[HttpGet("me")]
|
[HttpGet("subscribed")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<IActionResult> GetMySubscriptions(
|
public async Task<ActionResult<WebFeed>> GetSubscribedFeeds(
|
||||||
[FromQuery] int offset = 0,
|
[FromQuery] int offset = 0,
|
||||||
[FromQuery] int take = 20
|
[FromQuery] int take = 20
|
||||||
)
|
)
|
||||||
@@ -129,4 +130,141 @@ public class WebFeedPublicController(
|
|||||||
Response.Headers["X-Total"] = totalCount.ToString();
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
return Ok(subscriptions);
|
return Ok(subscriptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get articles from subscribed feeds
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<WebFeed>> GetWebFeedArticles(
|
||||||
|
[FromQuery] int offset = 0,
|
||||||
|
[FromQuery] int take = 20
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (HttpContext.Items["CurrentUser"] is not Account currentUser)
|
||||||
|
return Unauthorized();
|
||||||
|
|
||||||
|
var accountId = Guid.Parse(currentUser.Id);
|
||||||
|
|
||||||
|
var subscribedFeedIds = await db.WebFeedSubscriptions
|
||||||
|
.Where(s => s.AccountId == accountId)
|
||||||
|
.Select(s => s.FeedId)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var query = db.WebFeeds
|
||||||
|
.Where(f => subscribedFeedIds.Contains(f.Id))
|
||||||
|
.Include(f => f.Publisher)
|
||||||
|
.OrderByDescending(f => f.CreatedAt);
|
||||||
|
|
||||||
|
var totalCount = await query.CountAsync();
|
||||||
|
var feeds = await query
|
||||||
|
.Skip(offset)
|
||||||
|
.Take(take)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
return Ok(feeds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get feed metadata by ID (public endpoint)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="feedId">The ID of the feed</param>
|
||||||
|
/// <returns>Feed metadata</returns>
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpGet("{feedId:guid}")]
|
||||||
|
public async Task<ActionResult<WebFeed>> GetFeedById(Guid feedId)
|
||||||
|
{
|
||||||
|
var feed = await webFeed.GetFeedAsync(feedId);
|
||||||
|
if (feed == null)
|
||||||
|
return NotFound();
|
||||||
|
|
||||||
|
return Ok(feed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get articles from a specific feed (public endpoint)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="feedId">The ID of the feed</param>
|
||||||
|
/// <param name="offset">Number of articles to skip</param>
|
||||||
|
/// <param name="take">Maximum number of articles to return</param>
|
||||||
|
/// <returns>List of articles from the feed</returns>
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpGet("{feedId:guid}/articles")]
|
||||||
|
public async Task<ActionResult<WebArticle>> GetFeedArticles(
|
||||||
|
[FromRoute] Guid feedId,
|
||||||
|
[FromQuery] int offset = 0,
|
||||||
|
[FromQuery] int take = 20
|
||||||
|
)
|
||||||
|
{
|
||||||
|
// Check if feed exists
|
||||||
|
var feedExists = await db.WebFeeds.AnyAsync(f => f.Id == feedId);
|
||||||
|
if (!feedExists)
|
||||||
|
return NotFound("Feed not found");
|
||||||
|
|
||||||
|
var query = db.WebArticles
|
||||||
|
.Where(a => a.FeedId == feedId)
|
||||||
|
.OrderByDescending(a => a.CreatedAt)
|
||||||
|
.Include(a => a.Feed)
|
||||||
|
.ThenInclude(f => f.Publisher);
|
||||||
|
|
||||||
|
var totalCount = await query.CountAsync();
|
||||||
|
var articles = await query
|
||||||
|
.Skip(offset)
|
||||||
|
.Take(take)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
return Ok(articles);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Explore available web feeds
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("explore")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<WebFeed>> ExploreFeeds(
|
||||||
|
[FromQuery] int offset = 0,
|
||||||
|
[FromQuery] int take = 20,
|
||||||
|
[FromQuery] string? query = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (HttpContext.Items["CurrentUser"] is not Account currentUser)
|
||||||
|
return Unauthorized();
|
||||||
|
|
||||||
|
var accountId = Guid.Parse(currentUser.Id);
|
||||||
|
|
||||||
|
// Get IDs of already subscribed feeds
|
||||||
|
var subscribedFeedIds = await db.WebFeedSubscriptions
|
||||||
|
.Where(s => s.AccountId == accountId)
|
||||||
|
.Select(s => s.FeedId)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var feedsQuery = db.WebFeeds
|
||||||
|
.Include(f => f.Publisher)
|
||||||
|
.Where(f => !subscribedFeedIds.Contains(f.Id))
|
||||||
|
.AsQueryable();
|
||||||
|
|
||||||
|
// Apply search filter if query is provided
|
||||||
|
if (!string.IsNullOrWhiteSpace(query))
|
||||||
|
{
|
||||||
|
var searchTerm = $"%{query}%";
|
||||||
|
feedsQuery = feedsQuery.Where(f =>
|
||||||
|
EF.Functions.ILike(f.Title, searchTerm) ||
|
||||||
|
(f.Description != null && EF.Functions.ILike(f.Description, searchTerm))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Order by most recently created first
|
||||||
|
feedsQuery = feedsQuery.OrderByDescending(f => f.CreatedAt);
|
||||||
|
|
||||||
|
var totalCount = await feedsQuery.CountAsync();
|
||||||
|
var feeds = await feedsQuery
|
||||||
|
.Skip(offset)
|
||||||
|
.Take(take)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
return Ok(feeds);
|
||||||
|
}
|
||||||
}
|
}
|
@@ -31,7 +31,10 @@ public class WebFeedService(
|
|||||||
|
|
||||||
public async Task<WebFeed?> GetFeedAsync(Guid id, Guid? publisherId = null)
|
public async Task<WebFeed?> GetFeedAsync(Guid id, Guid? publisherId = null)
|
||||||
{
|
{
|
||||||
var query = database.WebFeeds.Where(a => a.Id == id).AsQueryable();
|
var query = database.WebFeeds
|
||||||
|
.Include(a => a.Publisher)
|
||||||
|
.Where(a => a.Id == id)
|
||||||
|
.AsQueryable();
|
||||||
if (publisherId.HasValue)
|
if (publisherId.HasValue)
|
||||||
query = query.Where(a => a.PublisherId == publisherId.Value);
|
query = query.Where(a => a.PublisherId == publisherId.Value);
|
||||||
return await query.FirstOrDefaultAsync();
|
return await query.FirstOrDefaultAsync();
|
||||||
|
Reference in New Issue
Block a user