[backend/core] Add reaction to reaction notification

This commit is contained in:
pancakes 2024-11-24 00:25:47 +10:00 committed by Laura Hausmann
parent e056d7ec9f
commit 54a899df0b
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 43 additions and 3 deletions

View file

@ -1,11 +1,12 @@
using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Services;
using Iceshrimp.Shared.Schemas.Web;
using static Iceshrimp.Shared.Schemas.Web.NotificationResponse;
namespace Iceshrimp.Backend.Controllers.Web.Renderers;
public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRenderer) : IScopedService
public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRenderer, EmojiService emojiSvc) : IScopedService
{
private static NotificationResponse Render(Notification notification, NotificationRendererDto data)
{
@ -24,6 +25,11 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
throw new Exception("DTO didn't contain the bite")
: null;
var reaction = notification.Reaction != null
? data.Reactions?.First(p => p.Name == notification.Reaction) ??
throw new Exception("DTO didn't contain the reaction")
: null;
return new NotificationResponse
{
Id = notification.Id,
@ -32,6 +38,7 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
User = user,
Note = note,
Bite = bite,
Reaction = reaction,
Type = RenderType(notification.Type)
};
}
@ -44,7 +51,8 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
{
Users = await GetUsersAsync([notification]),
Notes = await GetNotesAsync([notification], localUser),
Bites = GetBites([notification])
Bites = GetBites([notification]),
Reactions = await GetReactionsAsync([notification])
};
return Render(notification, data);
@ -89,6 +97,26 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
return bites.Select(p => new BiteResponse { Id = p.Id, BiteBack = p.TargetBiteId != null }).ToList();
}
private async Task<List<ReactionResponse>> GetReactionsAsync(IEnumerable<Notification> notifications)
{
var reactions = notifications.Select(p => p.Reaction).NotNull().ToList();
var emojis = reactions.Where(p => !p.StartsWith(':')).Select(p => new ReactionResponse { Name = p, Url = null, Sensitive = false }).ToList();
var custom = reactions.Where(p => p.StartsWith(':')).ToAsyncEnumerable();
await foreach (var s in custom)
{
var emoji = await emojiSvc.ResolveEmojiAsync(s);
var reaction = emoji != null
? new ReactionResponse { Name = s, Url = emoji.PublicUrl, Sensitive = emoji.Sensitive }
: new ReactionResponse { Name = s, Url = null, Sensitive = false };
emojis.Add(reaction);
}
return emojis;
}
public async Task<IEnumerable<NotificationResponse>> RenderManyAsync(
IEnumerable<Notification> notifications, User user
)
@ -98,7 +126,8 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
{
Users = await GetUsersAsync(notificationsList),
Notes = await GetNotesAsync(notificationsList, user),
Bites = GetBites(notificationsList)
Bites = GetBites(notificationsList),
Reactions = await GetReactionsAsync(notificationsList)
};
return notificationsList.Select(p => Render(p, data));
@ -109,5 +138,6 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
public List<NoteResponse>? Notes;
public List<UserResponse>? Users;
public List<BiteResponse>? Bites;
public List<ReactionResponse>? Reactions;
}
}

View file

@ -14,10 +14,20 @@ public class NotificationResponse
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
public BiteResponse? Bite { get; set; }
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ReactionResponse? Reaction { get; set; }
public class BiteResponse
{
public required string Id { get; set; }
public required bool BiteBack { get; set; }
}
public class ReactionResponse
{
public required string Name { get; set; }
public required string? Url { get; set; }
public required bool Sensitive { get; set; }
}
}