[backend/federation] Fixup hashtags in incoming notes (ISH-421)

This commit is contained in:
Laura Hausmann 2024-07-14 21:18:05 +02:00
parent f45b020699
commit 5182d1bbbb
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -82,6 +82,8 @@ public class NoteService(
throw GracefulException.UnprocessableEntity("Cannot reply to a pure renote"); throw GracefulException.UnprocessableEntity("Cannot reply to a pure renote");
if (user.IsSuspended) if (user.IsSuspended)
throw GracefulException.Forbidden("User is suspended"); throw GracefulException.Forbidden("User is suspended");
if (attachments != null && attachments.Any(p => p.UserId != user.Id))
throw GracefulException.UnprocessableEntity("Refusing to create note with files belonging to someone else");
poll?.Choices.RemoveAll(string.IsNullOrWhiteSpace); poll?.Choices.RemoveAll(string.IsNullOrWhiteSpace);
if (poll is { Choices.Count: < 2 }) if (poll is { Choices.Count: < 2 })
@ -135,9 +137,6 @@ public class NoteService(
text = MfmSerializer.Serialize(nodes); text = MfmSerializer.Serialize(nodes);
} }
if (attachments != null && attachments.Any(p => p.UserId != user.Id))
throw GracefulException.UnprocessableEntity("Refusing to create note with files belonging to someone else");
if (cw != null && string.IsNullOrWhiteSpace(cw)) if (cw != null && string.IsNullOrWhiteSpace(cw))
cw = null; cw = null;
@ -153,6 +152,14 @@ public class NoteService(
} }
var tags = ResolveHashtags(text, asNote); var tags = ResolveHashtags(text, asNote);
if (tags.Count > 0 && text != null && asNote != null)
{
// @formatter:off
var match = asNote.Tags?.OfType<ASHashtag>().Where(p => p.Name != null && p.Href != null) ?? [];
//TODO: refactor this to use the nodes object instead of matching on text
text = match.Aggregate(text, (current, tag) => current.Replace($"[#{tag.Name!.TrimStart('#')}]({tag.Href})", $"#{tag.Name!.TrimStart('#')}"));
// @formatter:on
}
var mastoReplyUserId = reply?.UserId != user.Id var mastoReplyUserId = reply?.UserId != user.Id
? reply?.UserId ? reply?.UserId