mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-21 11:37:26 +08:00
Merge remote-tracking branch 'origin/main' into codex/integrate-browser-notifications-for-website
This commit is contained in:
@@ -11,17 +11,16 @@ import { useToast } from 'vue-toastification'
|
|||||||
import { checkToken, clearToken, isLogin } from './utils/auth'
|
import { checkToken, clearToken, isLogin } from './utils/auth'
|
||||||
import { initTheme } from './utils/theme'
|
import { initTheme } from './utils/theme'
|
||||||
import { loginWithGoogle } from './utils/google'
|
import { loginWithGoogle } from './utils/google'
|
||||||
import { registerPush } from './utils/push'
|
|
||||||
|
|
||||||
// Configurable API domain and port
|
// Configurable API domain and port
|
||||||
// export const API_DOMAIN = 'http://127.0.0.1'
|
export const API_DOMAIN = 'http://127.0.0.1'
|
||||||
// export const API_PORT = 8081
|
export const API_PORT = 8081
|
||||||
|
|
||||||
export const API_DOMAIN = 'http://47.82.99.208'
|
// export const API_DOMAIN = 'http://47.82.99.208'
|
||||||
export const API_PORT = 8080
|
// export const API_PORT = 8080
|
||||||
|
|
||||||
// export const API_BASE_URL = API_PORT ? `${API_DOMAIN}:${API_PORT}` : API_DOMAIN
|
export const API_BASE_URL = API_PORT ? `${API_DOMAIN}:${API_PORT}` : API_DOMAIN
|
||||||
export const API_BASE_URL = "";
|
// export const API_BASE_URL = "";
|
||||||
export const GOOGLE_CLIENT_ID = '777830451304-nt8afkkap18gui4f9entcha99unal744.apps.googleusercontent.com'
|
export const GOOGLE_CLIENT_ID = '777830451304-nt8afkkap18gui4f9entcha99unal744.apps.googleusercontent.com'
|
||||||
export const GITHUB_CLIENT_ID = 'Ov23liVkO1NPAX5JyWxJ'
|
export const GITHUB_CLIENT_ID = 'Ov23liVkO1NPAX5JyWxJ'
|
||||||
export const DISCORD_CLIENT_ID = '1394985417044000779'
|
export const DISCORD_CLIENT_ID = '1394985417044000779'
|
||||||
@@ -44,7 +43,6 @@ app.use(
|
|||||||
)
|
)
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
registerPush()
|
|
||||||
|
|
||||||
checkToken().then(valid => {
|
checkToken().then(valid => {
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ public class AdminUserController {
|
|||||||
User user = userRepository.findById(id).orElseThrow();
|
User user = userRepository.findById(id).orElseThrow();
|
||||||
user.setApproved(true);
|
user.setApproved(true);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
emailSender.sendEmail(user.getEmail(), "Registration Approved",
|
emailSender.sendEmail(user.getEmail(), "【OpenIsle】您的注册已审核通过",
|
||||||
"Your account has been approved. Visit: " + websiteUrl);
|
"🎉您的注册已经审核通过, 点击以访问网站: " + websiteUrl);
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,8 +32,8 @@ public class AdminUserController {
|
|||||||
User user = userRepository.findById(id).orElseThrow();
|
User user = userRepository.findById(id).orElseThrow();
|
||||||
user.setApproved(false);
|
user.setApproved(false);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
emailSender.sendEmail(user.getEmail(), "Registration Rejected",
|
emailSender.sendEmail(user.getEmail(), "【OpenIsle】您的注册已被管理员拒绝",
|
||||||
"Your account request was rejected. Visit: " + websiteUrl);
|
"您的注册被管理员拒绝, 点击链接可以重新填写理由申请: " + websiteUrl);
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok().build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class AuthController {
|
|||||||
}
|
}
|
||||||
User user = userService.register(
|
User user = userService.register(
|
||||||
req.getUsername(), req.getEmail(), req.getPassword(), "", registerModeService.getRegisterMode());
|
req.getUsername(), req.getEmail(), req.getPassword(), "", registerModeService.getRegisterMode());
|
||||||
emailService.sendEmail(user.getEmail(), "Verification Code", "Your verification code is " + user.getVerificationCode());
|
emailService.sendEmail(user.getEmail(), "【OpenIsle】在网站填写验证码以验证", "您的验证码是 " + user.getVerificationCode());
|
||||||
if (!user.isApproved()) {
|
if (!user.isApproved()) {
|
||||||
notificationService.createRegisterRequestNotifications(user, user.getRegisterReason());
|
notificationService.createRegisterRequestNotifications(user, user.getRegisterReason());
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ public class AuthController {
|
|||||||
User user = userOpt.get();
|
User user = userOpt.get();
|
||||||
if (!user.isVerified()) {
|
if (!user.isVerified()) {
|
||||||
user = userService.register(user.getUsername(), user.getEmail(), user.getPassword(), user.getRegisterReason(), registerModeService.getRegisterMode());
|
user = userService.register(user.getUsername(), user.getEmail(), user.getPassword(), user.getRegisterReason(), registerModeService.getRegisterMode());
|
||||||
emailService.sendEmail(user.getEmail(), "Verification Code", "Your verification code is " + user.getVerificationCode());
|
emailService.sendEmail(user.getEmail(), "【OpenIsle】在网站填写验证码以验证", "您的验证码是 " + user.getVerificationCode());
|
||||||
return ResponseEntity.badRequest().body(Map.of(
|
return ResponseEntity.badRequest().body(Map.of(
|
||||||
"error", "User not verified",
|
"error", "User not verified",
|
||||||
"reason_code", "NOT_VERIFIED",
|
"reason_code", "NOT_VERIFIED",
|
||||||
@@ -279,7 +279,7 @@ public class AuthController {
|
|||||||
return ResponseEntity.badRequest().body(Map.of("error", "User not found"));
|
return ResponseEntity.badRequest().body(Map.of("error", "User not found"));
|
||||||
}
|
}
|
||||||
String code = userService.generatePasswordResetCode(req.getEmail());
|
String code = userService.generatePasswordResetCode(req.getEmail());
|
||||||
emailService.sendEmail(req.getEmail(), "Password Reset Code", "Your verification code is " + code);
|
emailService.sendEmail(req.getEmail(), "【OpenIsle】请填写验证码以重置密码", "您的验证码是" + code);
|
||||||
return ResponseEntity.ok(Map.of("message", "Verification code sent"));
|
return ResponseEntity.ok(Map.of("message", "Verification code sent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,4 +32,7 @@ public interface ReactionRepository extends JpaRepository<Reaction, Long> {
|
|||||||
|
|
||||||
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.type = com.openisle.model.ReactionType.LIKE AND ((r.post IS NOT NULL AND r.post.author.username = :username) OR (r.comment IS NOT NULL AND r.comment.author.username = :username))")
|
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.type = com.openisle.model.ReactionType.LIKE AND ((r.post IS NOT NULL AND r.post.author.username = :username) OR (r.comment IS NOT NULL AND r.comment.author.username = :username))")
|
||||||
long countLikesReceived(@Param("username") String username);
|
long countLikesReceived(@Param("username") String username);
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(r) FROM Reaction r WHERE (r.post IS NOT NULL AND r.post.author.username = :username) OR (r.comment IS NOT NULL AND r.comment.author.username = :username)")
|
||||||
|
long countReceived(@Param("username") String username);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.openisle.model.*;
|
|||||||
import com.openisle.repository.NotificationRepository;
|
import com.openisle.repository.NotificationRepository;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import com.openisle.service.EmailSender;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -14,8 +16,12 @@ import java.util.List;
|
|||||||
public class NotificationService {
|
public class NotificationService {
|
||||||
private final NotificationRepository notificationRepository;
|
private final NotificationRepository notificationRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final EmailSender emailSender;
|
||||||
private final PushNotificationService pushNotificationService;
|
private final PushNotificationService pushNotificationService;
|
||||||
|
|
||||||
|
@Value("${app.website-url}")
|
||||||
|
private String websiteUrl;
|
||||||
|
|
||||||
public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved) {
|
public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved) {
|
||||||
return createNotification(user, type, post, comment, approved, null, null, null);
|
return createNotification(user, type, post, comment, approved, null, null, null);
|
||||||
}
|
}
|
||||||
@@ -31,9 +37,15 @@ public class NotificationService {
|
|||||||
n.setFromUser(fromUser);
|
n.setFromUser(fromUser);
|
||||||
n.setReactionType(reactionType);
|
n.setReactionType(reactionType);
|
||||||
n.setContent(content);
|
n.setContent(content);
|
||||||
Notification saved = notificationRepository.save(n);
|
n = notificationRepository.save(n);
|
||||||
pushNotificationService.sendNotification(user, "You have a new notification");
|
pushNotificationService.sendNotification(user, "You have a new notification");
|
||||||
return saved;
|
|
||||||
|
if (type == NotificationType.COMMENT_REPLY && user.getEmail() != null && post != null && comment != null) {
|
||||||
|
String url = String.format("%s/posts/%d#comment-%d", websiteUrl, post.getId(), comment.getId());
|
||||||
|
emailSender.sendEmail(user.getEmail(), "【OpenIsle】有人回复了你", url);
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ import com.openisle.repository.PostRepository;
|
|||||||
import com.openisle.repository.ReactionRepository;
|
import com.openisle.repository.ReactionRepository;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
import com.openisle.service.NotificationService;
|
import com.openisle.service.NotificationService;
|
||||||
|
import com.openisle.service.EmailSender;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -22,6 +24,10 @@ public class ReactionService {
|
|||||||
private final PostRepository postRepository;
|
private final PostRepository postRepository;
|
||||||
private final CommentRepository commentRepository;
|
private final CommentRepository commentRepository;
|
||||||
private final NotificationService notificationService;
|
private final NotificationService notificationService;
|
||||||
|
private final EmailSender emailSender;
|
||||||
|
|
||||||
|
@Value("${app.website-url}")
|
||||||
|
private String websiteUrl;
|
||||||
|
|
||||||
public Reaction reactToPost(String username, Long postId, ReactionType type) {
|
public Reaction reactToPost(String username, Long postId, ReactionType type) {
|
||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
@@ -41,6 +47,11 @@ public class ReactionService {
|
|||||||
reaction = reactionRepository.save(reaction);
|
reaction = reactionRepository.save(reaction);
|
||||||
if (!user.getId().equals(post.getAuthor().getId())) {
|
if (!user.getId().equals(post.getAuthor().getId())) {
|
||||||
notificationService.createNotification(post.getAuthor(), NotificationType.REACTION, post, null, null, user, type, null);
|
notificationService.createNotification(post.getAuthor(), NotificationType.REACTION, post, null, null, user, type, null);
|
||||||
|
long count = reactionRepository.countReceived(post.getAuthor().getUsername());
|
||||||
|
if (count % 5 == 0 && post.getAuthor().getEmail() != null) {
|
||||||
|
String url = websiteUrl + "/messages";
|
||||||
|
emailSender.sendEmail(post.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return reaction;
|
return reaction;
|
||||||
}
|
}
|
||||||
@@ -64,6 +75,11 @@ public class ReactionService {
|
|||||||
reaction = reactionRepository.save(reaction);
|
reaction = reactionRepository.save(reaction);
|
||||||
if (!user.getId().equals(comment.getAuthor().getId())) {
|
if (!user.getId().equals(comment.getAuthor().getId())) {
|
||||||
notificationService.createNotification(comment.getAuthor(), NotificationType.REACTION, comment.getPost(), comment, null, user, type, null);
|
notificationService.createNotification(comment.getAuthor(), NotificationType.REACTION, comment.getPost(), comment, null, user, type, null);
|
||||||
|
long count = reactionRepository.countReceived(comment.getAuthor().getUsername());
|
||||||
|
if (count % 5 == 0 && comment.getAuthor().getEmail() != null) {
|
||||||
|
String url = websiteUrl + "/messages";
|
||||||
|
emailSender.sendEmail(comment.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return reaction;
|
return reaction;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ class NotificationServiceTest {
|
|||||||
void markReadUpdatesOnlyOwnedNotifications() {
|
void markReadUpdatesOnlyOwnedNotifications() {
|
||||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
UserRepository uRepo = mock(UserRepository.class);
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(1L);
|
user.setId(1L);
|
||||||
@@ -44,7 +46,9 @@ class NotificationServiceTest {
|
|||||||
void listNotificationsWithoutFilter() {
|
void listNotificationsWithoutFilter() {
|
||||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
UserRepository uRepo = mock(UserRepository.class);
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(2L);
|
user.setId(2L);
|
||||||
@@ -64,7 +68,9 @@ class NotificationServiceTest {
|
|||||||
void countUnreadReturnsRepositoryValue() {
|
void countUnreadReturnsRepositoryValue() {
|
||||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
UserRepository uRepo = mock(UserRepository.class);
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(3L);
|
user.setId(3L);
|
||||||
@@ -82,7 +88,9 @@ class NotificationServiceTest {
|
|||||||
void createRegisterRequestNotificationsDeletesOldOnes() {
|
void createRegisterRequestNotificationsDeletesOldOnes() {
|
||||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
UserRepository uRepo = mock(UserRepository.class);
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
User admin = new User();
|
User admin = new User();
|
||||||
admin.setId(10L);
|
admin.setId(10L);
|
||||||
@@ -101,7 +109,9 @@ class NotificationServiceTest {
|
|||||||
void createActivityRedeemNotificationsDeletesOldOnes() {
|
void createActivityRedeemNotificationsDeletesOldOnes() {
|
||||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
UserRepository uRepo = mock(UserRepository.class);
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
User admin = new User();
|
User admin = new User();
|
||||||
admin.setId(10L);
|
admin.setId(10L);
|
||||||
@@ -115,4 +125,25 @@ class NotificationServiceTest {
|
|||||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.ACTIVITY_REDEEM, user);
|
verify(nRepo).deleteByTypeAndFromUser(NotificationType.ACTIVITY_REDEEM, user);
|
||||||
verify(nRepo).save(any(Notification.class));
|
verify(nRepo).save(any(Notification.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createNotificationSendsEmailForCommentReply() {
|
||||||
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setEmail("a@a.com");
|
||||||
|
Post post = new Post();
|
||||||
|
post.setId(1L);
|
||||||
|
Comment comment = new Comment();
|
||||||
|
comment.setId(2L);
|
||||||
|
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
|
||||||
|
|
||||||
|
service.createNotification(user, NotificationType.COMMENT_REPLY, post, comment, null, null, null, null);
|
||||||
|
|
||||||
|
verify(email).sendEmail("a@a.com", "【OpenIsle】有人回复了你", "https://ex.com/posts/1#comment-2");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
43
src/test/java/com/openisle/service/ReactionServiceTest.java
Normal file
43
src/test/java/com/openisle/service/ReactionServiceTest.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package com.openisle.service;
|
||||||
|
|
||||||
|
import com.openisle.model.*;
|
||||||
|
import com.openisle.repository.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
class ReactionServiceTest {
|
||||||
|
@Test
|
||||||
|
void reactToPostSendsEmailEveryFive() {
|
||||||
|
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||||
|
UserRepository userRepo = mock(UserRepository.class);
|
||||||
|
PostRepository postRepo = mock(PostRepository.class);
|
||||||
|
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||||
|
NotificationService notif = mock(NotificationService.class);
|
||||||
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
ReactionService service = new ReactionService(reactionRepo, userRepo, postRepo, commentRepo, notif, email);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setId(1L);
|
||||||
|
user.setUsername("bob");
|
||||||
|
User author = new User();
|
||||||
|
author.setId(2L);
|
||||||
|
author.setEmail("a@a.com");
|
||||||
|
Post post = new Post();
|
||||||
|
post.setId(3L);
|
||||||
|
post.setAuthor(author);
|
||||||
|
|
||||||
|
when(userRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||||
|
when(postRepo.findById(3L)).thenReturn(Optional.of(post));
|
||||||
|
when(reactionRepo.findByUserAndPostAndType(user, post, ReactionType.LIKE)).thenReturn(Optional.empty());
|
||||||
|
when(reactionRepo.save(any(Reaction.class))).thenAnswer(i -> i.getArgument(0));
|
||||||
|
when(reactionRepo.countReceived(author.getUsername())).thenReturn(5L);
|
||||||
|
|
||||||
|
service.reactToPost("bob", 3L, ReactionType.LIKE);
|
||||||
|
|
||||||
|
verify(email).sendEmail("a@a.com", "【OpenIsle】你有新的互动", "https://ex.com/messages");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user