[backend/core] Add reaction to reaction notification
This commit is contained in:
parent
e056d7ec9f
commit
54a899df0b
2 changed files with 43 additions and 3 deletions
|
@ -1,11 +1,12 @@
|
||||||
using Iceshrimp.Backend.Core.Database.Tables;
|
using Iceshrimp.Backend.Core.Database.Tables;
|
||||||
using Iceshrimp.Backend.Core.Extensions;
|
using Iceshrimp.Backend.Core.Extensions;
|
||||||
|
using Iceshrimp.Backend.Core.Services;
|
||||||
using Iceshrimp.Shared.Schemas.Web;
|
using Iceshrimp.Shared.Schemas.Web;
|
||||||
using static Iceshrimp.Shared.Schemas.Web.NotificationResponse;
|
using static Iceshrimp.Shared.Schemas.Web.NotificationResponse;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Controllers.Web.Renderers;
|
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)
|
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")
|
throw new Exception("DTO didn't contain the bite")
|
||||||
: null;
|
: 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
|
return new NotificationResponse
|
||||||
{
|
{
|
||||||
Id = notification.Id,
|
Id = notification.Id,
|
||||||
|
@ -32,6 +38,7 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
|
||||||
User = user,
|
User = user,
|
||||||
Note = note,
|
Note = note,
|
||||||
Bite = bite,
|
Bite = bite,
|
||||||
|
Reaction = reaction,
|
||||||
Type = RenderType(notification.Type)
|
Type = RenderType(notification.Type)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -44,7 +51,8 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
|
||||||
{
|
{
|
||||||
Users = await GetUsersAsync([notification]),
|
Users = await GetUsersAsync([notification]),
|
||||||
Notes = await GetNotesAsync([notification], localUser),
|
Notes = await GetNotesAsync([notification], localUser),
|
||||||
Bites = GetBites([notification])
|
Bites = GetBites([notification]),
|
||||||
|
Reactions = await GetReactionsAsync([notification])
|
||||||
};
|
};
|
||||||
|
|
||||||
return Render(notification, data);
|
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();
|
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(
|
public async Task<IEnumerable<NotificationResponse>> RenderManyAsync(
|
||||||
IEnumerable<Notification> notifications, User user
|
IEnumerable<Notification> notifications, User user
|
||||||
)
|
)
|
||||||
|
@ -98,7 +126,8 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
|
||||||
{
|
{
|
||||||
Users = await GetUsersAsync(notificationsList),
|
Users = await GetUsersAsync(notificationsList),
|
||||||
Notes = await GetNotesAsync(notificationsList, user),
|
Notes = await GetNotesAsync(notificationsList, user),
|
||||||
Bites = GetBites(notificationsList)
|
Bites = GetBites(notificationsList),
|
||||||
|
Reactions = await GetReactionsAsync(notificationsList)
|
||||||
};
|
};
|
||||||
|
|
||||||
return notificationsList.Select(p => Render(p, data));
|
return notificationsList.Select(p => Render(p, data));
|
||||||
|
@ -109,5 +138,6 @@ public class NotificationRenderer(UserRenderer userRenderer, NoteRenderer noteRe
|
||||||
public List<NoteResponse>? Notes;
|
public List<NoteResponse>? Notes;
|
||||||
public List<UserResponse>? Users;
|
public List<UserResponse>? Users;
|
||||||
public List<BiteResponse>? Bites;
|
public List<BiteResponse>? Bites;
|
||||||
|
public List<ReactionResponse>? Reactions;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -14,10 +14,20 @@ public class NotificationResponse
|
||||||
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public BiteResponse? Bite { get; set; }
|
public BiteResponse? Bite { get; set; }
|
||||||
|
|
||||||
|
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
|
public ReactionResponse? Reaction { get; set; }
|
||||||
|
|
||||||
public class BiteResponse
|
public class BiteResponse
|
||||||
{
|
{
|
||||||
public required string Id { get; set; }
|
public required string Id { get; set; }
|
||||||
public required bool BiteBack { 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; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue