mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-05 21:36:07 +00:00
Add mention suggestions and admin list
This commit is contained in:
@@ -11,6 +11,11 @@ import org.springframework.stereotype.Service;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.Map;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -28,6 +33,8 @@ public class NotificationService {
|
||||
@Value("${app.website-url}")
|
||||
private String websiteUrl;
|
||||
|
||||
private static final Pattern MENTION_PATTERN = Pattern.compile("@([A-Za-z0-9_]+)");
|
||||
|
||||
private String buildPayload(String body, String url) {
|
||||
// try {
|
||||
// return new ObjectMapper().writeValueAsString(Map.of(
|
||||
@@ -144,4 +151,22 @@ public class NotificationService {
|
||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
||||
return notificationRepository.countByUserAndRead(user, false);
|
||||
}
|
||||
|
||||
public void notifyMentions(String content, User fromUser, Post post, Comment comment) {
|
||||
if (content == null || fromUser == null) {
|
||||
return;
|
||||
}
|
||||
Matcher matcher = MENTION_PATTERN.matcher(content);
|
||||
Set<String> names = new HashSet<>();
|
||||
while (matcher.find()) {
|
||||
names.add(matcher.group(1));
|
||||
}
|
||||
for (String name : names) {
|
||||
userRepository.findByUsername(name).ifPresent(target -> {
|
||||
if (!target.getId().equals(fromUser.getId())) {
|
||||
createNotification(target, NotificationType.MENTION, post, comment, null, fromUser, null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user