🐛 Bug fixes

This commit is contained in:
LittleSheep 2025-06-28 01:28:20 +08:00
parent ac496777ed
commit 2e8d6a3667
6 changed files with 43 additions and 48 deletions

View File

@ -215,7 +215,7 @@ public class NotificationService(
var notifications = subDict.Select(value => var notifications = subDict.Select(value =>
{ {
int platformCode = value.Key switch var platformCode = value.Key switch
{ {
NotificationPushProvider.Apple => 1, NotificationPushProvider.Apple => 1,
NotificationPushProvider.Google => 2, NotificationPushProvider.Google => 2,

View File

@ -262,26 +262,19 @@ public class ChatRoomController(
chatRoom.RealmId = member.RealmId; chatRoom.RealmId = member.RealmId;
} }
var chatRoomResourceId = $"chatroom:{chatRoom.Id}";
if (request.PictureId is not null) if (request.PictureId is not null)
{ {
var picture = await db.Files.FindAsync(request.PictureId); var picture = await db.Files.FindAsync(request.PictureId);
if (picture is null) return BadRequest("Invalid picture id, unable to find the file on cloud."); if (picture is null) return BadRequest("Invalid picture id, unable to find the file on cloud.");
// Remove old references for pictures // Remove old references for pictures
var oldPictureRefs = await fileRefService.DeleteResourceReferencesAsync(chatRoom.ResourceIdentifier, "chat.room.picture");
await fileRefService.GetResourceReferencesAsync(chatRoomResourceId, "chat.room.picture");
foreach (var oldRef in oldPictureRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
// Add a new reference // Add a new reference
await fileRefService.CreateReferenceAsync( await fileRefService.CreateReferenceAsync(
picture.Id, picture.Id,
"chat.room.picture", "chat.room.picture",
chatRoomResourceId chatRoom.ResourceIdentifier
); );
chatRoom.Picture = picture.ToReferenceObject(); chatRoom.Picture = picture.ToReferenceObject();
@ -293,18 +286,13 @@ public class ChatRoomController(
if (background is null) return BadRequest("Invalid background id, unable to find the file on cloud."); if (background is null) return BadRequest("Invalid background id, unable to find the file on cloud.");
// Remove old references for backgrounds // Remove old references for backgrounds
var oldBackgroundRefs = await fileRefService.DeleteResourceReferencesAsync(chatRoom.ResourceIdentifier, "chat.room.background");
await fileRefService.GetResourceReferencesAsync(chatRoomResourceId, "chat.room.background");
foreach (var oldRef in oldBackgroundRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
// Add a new reference // Add a new reference
await fileRefService.CreateReferenceAsync( await fileRefService.CreateReferenceAsync(
background.Id, background.Id,
"chat.room.background", "chat.room.background",
chatRoomResourceId chatRoom.ResourceIdentifier
); );
chatRoom.Background = background.ToReferenceObject(); chatRoom.Background = background.ToReferenceObject();

View File

@ -355,12 +355,7 @@ public class PublisherController(
// Remove old references for the publisher picture // Remove old references for the publisher picture
if (publisher.Picture is not null) if (publisher.Picture is not null)
{ {
var oldPictureRefs = await fileRefService.GetResourceReferencesAsync( await fileRefService.DeleteResourceReferencesAsync(publisher.ResourceIdentifier, "publisher.picture");
publisher.ResourceIdentifier,
"publisher.picture"
);
foreach (var oldRef in oldPictureRefs)
await fileRefService.DeleteReferenceAsync(oldRef.Id);
} }
publisher.Picture = picture.ToReferenceObject(); publisher.Picture = picture.ToReferenceObject();
@ -378,17 +373,10 @@ public class PublisherController(
var background = await db.Files.Where(f => f.Id == request.BackgroundId).FirstOrDefaultAsync(); var background = await db.Files.Where(f => f.Id == request.BackgroundId).FirstOrDefaultAsync();
if (background is null) return BadRequest("Invalid background id."); if (background is null) return BadRequest("Invalid background id.");
var publisherResourceId = $"publisher:{publisher.Id}";
// Remove old references for the publisher background // Remove old references for the publisher background
if (publisher.Background is not null) if (publisher.Background is not null)
{ {
var oldBackgroundRefs = await fileRefService.DeleteResourceReferencesAsync(publisher.ResourceIdentifier, "publisher.background");
await fileRefService.GetResourceReferencesAsync(publisherResourceId, "publisher.background");
foreach (var oldRef in oldBackgroundRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
} }
publisher.Background = background.ToReferenceObject(); publisher.Background = background.ToReferenceObject();
@ -397,7 +385,7 @@ public class PublisherController(
await fileRefService.CreateReferenceAsync( await fileRefService.CreateReferenceAsync(
background.Id, background.Id,
"publisher.background", "publisher.background",
publisherResourceId publisher.ResourceIdentifier
); );
} }

View File

@ -69,7 +69,7 @@ public class PublisherSubscriptionService(
// Notify each subscriber // Notify each subscriber
var notifiedCount = 0; var notifiedCount = 0;
foreach (var subscription in subscribers) foreach (var subscription in subscribers.GroupBy(s => s.AccountId).Select(g => g.First()))
{ {
try try
{ {

View File

@ -409,8 +409,6 @@ public class RealmController(
if (request.IsPublic is not null) if (request.IsPublic is not null)
realm.IsPublic = request.IsPublic.Value; realm.IsPublic = request.IsPublic.Value;
var realmResourceId = $"realm:{realm.Id}";
if (request.PictureId is not null) if (request.PictureId is not null)
{ {
var picture = await db.Files.FindAsync(request.PictureId); var picture = await db.Files.FindAsync(request.PictureId);
@ -419,11 +417,7 @@ public class RealmController(
// Remove old references for the realm picture // Remove old references for the realm picture
if (realm.Picture is not null) if (realm.Picture is not null)
{ {
var oldPictureRefs = await fileRefService.GetResourceReferencesAsync(realmResourceId, "realm.picture"); await fileRefService.DeleteResourceReferencesAsync(realm.ResourceIdentifier, "realm.picture");
foreach (var oldRef in oldPictureRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
} }
realm.Picture = picture.ToReferenceObject(); realm.Picture = picture.ToReferenceObject();
@ -432,7 +426,7 @@ public class RealmController(
await fileRefService.CreateReferenceAsync( await fileRefService.CreateReferenceAsync(
picture.Id, picture.Id,
"realm.picture", "realm.picture",
realmResourceId realm.ResourceIdentifier
); );
} }
@ -444,12 +438,7 @@ public class RealmController(
// Remove old references for the realm background // Remove old references for the realm background
if (realm.Background is not null) if (realm.Background is not null)
{ {
var oldBackgroundRefs = await fileRefService.DeleteResourceReferencesAsync(realm.ResourceIdentifier, "realm.background");
await fileRefService.GetResourceReferencesAsync(realmResourceId, "realm.background");
foreach (var oldRef in oldBackgroundRefs)
{
await fileRefService.DeleteReferenceAsync(oldRef.Id);
}
} }
realm.Background = background.ToReferenceObject(); realm.Background = background.ToReferenceObject();
@ -458,7 +447,7 @@ public class RealmController(
await fileRefService.CreateReferenceAsync( await fileRefService.CreateReferenceAsync(
background.Id, background.Id,
"realm.background", "realm.background",
realmResourceId realm.ResourceIdentifier
); );
} }

View File

@ -156,6 +156,36 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
return deletedCount; return deletedCount;
} }
/// <summary>
/// Deletes references for a specific resource and usage
/// </summary>
/// <param name="resourceId">The ID of the resource</param>
/// <param name="usage">The usage context</param>
/// <returns>The number of deleted references</returns>
public async Task<int> DeleteResourceReferencesAsync(string resourceId, string usage)
{
var references = await db.FileReferences
.Where(r => r.ResourceId == resourceId && r.Usage == usage)
.ToListAsync();
if (!references.Any())
{
return 0;
}
var fileIds = references.Select(r => r.FileId).Distinct().ToList();
db.FileReferences.RemoveRange(references);
var deletedCount = await db.SaveChangesAsync();
// Purge caches
var tasks = fileIds.Select(fileService._PurgeCacheAsync).ToList();
tasks.Add(PurgeCacheForResourceAsync(resourceId));
await Task.WhenAll(tasks);
return deletedCount;
}
/// <summary> /// <summary>
/// Deletes a specific file reference /// Deletes a specific file reference
/// </summary> /// </summary>