✨ Post category tags subscriptions
This commit is contained in:
@@ -35,6 +35,7 @@ public class AppDatabase(
|
||||
public DbSet<PostCategory> PostCategories { get; set; } = null!;
|
||||
public DbSet<PostCollection> PostCollections { get; set; } = null!;
|
||||
public DbSet<PostFeaturedRecord> PostFeaturedRecords { get; set; } = null!;
|
||||
public DbSet<PostCategorySubscription> PostCategorySubscriptions { get; set; } = null!;
|
||||
|
||||
public DbSet<Poll.Poll> Polls { get; set; } = null!;
|
||||
public DbSet<Poll.PollQuestion> PollQuestions { get; set; } = null!;
|
||||
|
2066
DysonNetwork.Sphere/Migrations/20250825054901_AddPostCategoryTagSubscription.Designer.cs
generated
Normal file
2066
DysonNetwork.Sphere/Migrations/20250825054901_AddPostCategoryTagSubscription.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using NodaTime;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DysonNetwork.Sphere.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddPostCategoryTagSubscription : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "post_category_subscriptions",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
account_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
category_id = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
tag_id = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
created_at = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
updated_at = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
deleted_at = table.Column<Instant>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_post_category_subscriptions", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_post_category_subscriptions_post_categories_category_id",
|
||||
column: x => x.category_id,
|
||||
principalTable: "post_categories",
|
||||
principalColumn: "id");
|
||||
table.ForeignKey(
|
||||
name: "fk_post_category_subscriptions_post_tags_tag_id",
|
||||
column: x => x.tag_id,
|
||||
principalTable: "post_tags",
|
||||
principalColumn: "id");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_post_category_subscriptions_category_id",
|
||||
table: "post_category_subscriptions",
|
||||
column: "category_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_post_category_subscriptions_tag_id",
|
||||
table: "post_category_subscriptions",
|
||||
column: "tag_id");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "post_category_subscriptions");
|
||||
}
|
||||
}
|
||||
}
|
@@ -695,6 +695,49 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
b.ToTable("post_categories", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Sphere.Post.PostCategorySubscription", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<Guid>("AccountId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("account_id");
|
||||
|
||||
b.Property<Guid?>("CategoryId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("category_id");
|
||||
|
||||
b.Property<Instant>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<Instant?>("DeletedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("deleted_at");
|
||||
|
||||
b.Property<Guid?>("TagId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("tag_id");
|
||||
|
||||
b.Property<Instant>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_post_category_subscriptions");
|
||||
|
||||
b.HasIndex("CategoryId")
|
||||
.HasDatabaseName("ix_post_category_subscriptions_category_id");
|
||||
|
||||
b.HasIndex("TagId")
|
||||
.HasDatabaseName("ix_post_category_subscriptions_tag_id");
|
||||
|
||||
b.ToTable("post_category_subscriptions", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Sphere.Post.PostCollection", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
@@ -1727,6 +1770,23 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
b.Navigation("RepliedPost");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Sphere.Post.PostCategorySubscription", b =>
|
||||
{
|
||||
b.HasOne("DysonNetwork.Sphere.Post.PostCategory", "Category")
|
||||
.WithMany()
|
||||
.HasForeignKey("CategoryId")
|
||||
.HasConstraintName("fk_post_category_subscriptions_post_categories_category_id");
|
||||
|
||||
b.HasOne("DysonNetwork.Sphere.Post.PostTag", "Tag")
|
||||
.WithMany()
|
||||
.HasForeignKey("TagId")
|
||||
.HasConstraintName("fk_post_category_subscriptions_post_tags_tag_id");
|
||||
|
||||
b.Navigation("Category");
|
||||
|
||||
b.Navigation("Tag");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Sphere.Post.PostCollection", b =>
|
||||
{
|
||||
b.HasOne("DysonNetwork.Sphere.Publisher.Publisher", "Publisher")
|
||||
|
@@ -119,6 +119,17 @@ public class PostCategory : ModelBase
|
||||
[NotMapped] public int? Usage { get; set; }
|
||||
}
|
||||
|
||||
public class PostCategorySubscription : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid AccountId { get; set; }
|
||||
|
||||
public Guid? CategoryId { get; set; }
|
||||
public PostCategory? Category { get; set; }
|
||||
public Guid? TagId { get; set; }
|
||||
public PostTag? Tag { get; set; }
|
||||
}
|
||||
|
||||
public class PostCollection : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
@@ -1,3 +1,6 @@
|
||||
using DysonNetwork.Shared.Data;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -120,4 +123,162 @@ public class PostCategoryController(AppDatabase db) : ControllerBase
|
||||
return NotFound();
|
||||
return Ok(tag);
|
||||
}
|
||||
|
||||
[HttpPost("categories/{slug}/subscribe")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<PostCategorySubscription>> SubscribeCategory(string slug)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var category = await db.PostCategories.FirstOrDefaultAsync(c => c.Slug == slug);
|
||||
if (category == null)
|
||||
{
|
||||
return NotFound("Category not found.");
|
||||
}
|
||||
|
||||
var existingSubscription = await db.PostCategorySubscriptions
|
||||
.FirstOrDefaultAsync(s => s.CategoryId == category.Id && s.AccountId == accountId);
|
||||
|
||||
if (existingSubscription != null)
|
||||
return Ok(existingSubscription);
|
||||
|
||||
var subscription = new PostCategorySubscription
|
||||
{
|
||||
AccountId = accountId,
|
||||
CategoryId = category.Id
|
||||
};
|
||||
|
||||
db.PostCategorySubscriptions.Add(subscription);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetCategorySubscription), new { slug }, subscription);
|
||||
}
|
||||
|
||||
[HttpPost("categories/{slug}/unsubscribe")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> UnsubscribeCategory(string slug)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var category = await db.PostCategories.FirstOrDefaultAsync(c => c.Slug == slug);
|
||||
if (category == null)
|
||||
return NotFound("Category not found.");
|
||||
|
||||
var subscription = await db.PostCategorySubscriptions
|
||||
.FirstOrDefaultAsync(s => s.CategoryId == category.Id && s.AccountId == accountId);
|
||||
|
||||
if (subscription == null)
|
||||
return NoContent();
|
||||
|
||||
db.PostCategorySubscriptions.Remove(subscription);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("categories/{slug}/subscription")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<PostCategorySubscription>> GetCategorySubscription(string slug)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var category = await db.PostCategories.FirstOrDefaultAsync(c => c.Slug == slug);
|
||||
if (category == null)
|
||||
return NotFound("Category not found.");
|
||||
|
||||
var subscription = await db.PostCategorySubscriptions
|
||||
.FirstOrDefaultAsync(s => s.CategoryId == category.Id && s.AccountId == accountId);
|
||||
|
||||
if (subscription == null)
|
||||
return NotFound("Subscription not found.");
|
||||
|
||||
return Ok(subscription);
|
||||
}
|
||||
|
||||
[HttpPost("tags/{slug}/subscribe")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<PostCategorySubscription>> SubscribeTag(string slug)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var tag = await db.PostTags.FirstOrDefaultAsync(t => t.Slug == slug);
|
||||
if (tag == null)
|
||||
{
|
||||
return NotFound("Tag not found.");
|
||||
}
|
||||
|
||||
var existingSubscription = await db.PostCategorySubscriptions
|
||||
.FirstOrDefaultAsync(s => s.TagId == tag.Id && s.AccountId == accountId);
|
||||
|
||||
if (existingSubscription != null)
|
||||
{
|
||||
return Ok(existingSubscription);
|
||||
}
|
||||
|
||||
var subscription = new PostCategorySubscription
|
||||
{
|
||||
AccountId = accountId,
|
||||
TagId = tag.Id
|
||||
};
|
||||
|
||||
db.PostCategorySubscriptions.Add(subscription);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetTagSubscription), new { slug }, subscription);
|
||||
}
|
||||
|
||||
[HttpPost("tags/{slug}/unsubscribe")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> UnsubscribeTag(string slug)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var tag = await db.PostTags.FirstOrDefaultAsync(t => t.Slug == slug);
|
||||
if (tag == null)
|
||||
{
|
||||
return NotFound("Tag not found.");
|
||||
}
|
||||
|
||||
var subscription = await db.PostCategorySubscriptions
|
||||
.FirstOrDefaultAsync(s => s.TagId == tag.Id && s.AccountId == accountId);
|
||||
|
||||
if (subscription == null)
|
||||
{
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
db.PostCategorySubscriptions.Remove(subscription);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("tags/{slug}/subscription")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<PostCategorySubscription>> GetTagSubscription(string slug)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var tag = await db.PostTags.FirstOrDefaultAsync(t => t.Slug == slug);
|
||||
if (tag == null)
|
||||
{
|
||||
return NotFound("Tag not found.");
|
||||
}
|
||||
|
||||
var subscription = await db.PostCategorySubscriptions
|
||||
.FirstOrDefaultAsync(s => s.TagId == tag.Id && s.AccountId == accountId);
|
||||
|
||||
if (subscription == null)
|
||||
{
|
||||
return NotFound("Subscription not found.");
|
||||
}
|
||||
|
||||
return Ok(subscription);
|
||||
}
|
||||
}
|
||||
|
@@ -56,13 +56,6 @@ public class PublisherSubscriptionService(
|
||||
if (post.Visibility != PostVisibility.Public)
|
||||
return 0;
|
||||
|
||||
var subscribers = await db.PublisherSubscriptions
|
||||
.Where(p => p.PublisherId == post.PublisherId &&
|
||||
p.Status == PublisherSubscriptionStatus.Active)
|
||||
.ToListAsync();
|
||||
if (subscribers.Count == 0)
|
||||
return 0;
|
||||
|
||||
// Create notification data
|
||||
var (title, message) = ps.ChopPostForNotification(post);
|
||||
|
||||
@@ -73,9 +66,38 @@ public class PublisherSubscriptionService(
|
||||
{ "publisher_id", post.Publisher.Id.ToString() }
|
||||
};
|
||||
|
||||
// Gather subscribers
|
||||
var subscribers = await db.PublisherSubscriptions
|
||||
.Where(p => p.PublisherId == post.PublisherId &&
|
||||
p.Status == PublisherSubscriptionStatus.Active)
|
||||
.ToListAsync();
|
||||
if (subscribers.Count == 0)
|
||||
return 0;
|
||||
|
||||
List<PostCategorySubscription> categorySubscribers = [];
|
||||
if (post.Categories.Count > 0)
|
||||
{
|
||||
var categoryIds = post.Categories.Select(x => x.Id).ToList();
|
||||
var subs = await db.PostCategorySubscriptions
|
||||
.Where(s => s.CategoryId != null && categoryIds.Contains(s.CategoryId.Value))
|
||||
.ToListAsync();
|
||||
categorySubscribers.AddRange(subs);
|
||||
}
|
||||
if (post.Tags.Count > 0)
|
||||
{
|
||||
var tagIds = post.Tags.Select(x => x.Id).ToList();
|
||||
var subs = await db.PostCategorySubscriptions
|
||||
.Where(s => s.TagId != null && tagIds.Contains(s.TagId.Value))
|
||||
.ToListAsync();
|
||||
categorySubscribers.AddRange(subs);
|
||||
}
|
||||
|
||||
List<string> requestAccountIds = [];
|
||||
requestAccountIds.AddRange(subscribers.Select(x => x.AccountId.ToString()));
|
||||
requestAccountIds.AddRange(categorySubscribers.Select(x => x.AccountId.ToString()));
|
||||
|
||||
var queryRequest = new GetAccountBatchRequest();
|
||||
queryRequest.Id.AddRange(subscribers.DistinctBy(s => s.AccountId).Select(m => m.AccountId.ToString()));
|
||||
queryRequest.Id.AddRange(requestAccountIds.Distinct());
|
||||
var queryResponse = await accounts.GetAccountBatchAsync(queryRequest);
|
||||
|
||||
// Notify each subscriber
|
||||
|
Reference in New Issue
Block a user