mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
Compare commits
37 Commits
codex/add-
...
codex/fix-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d42d38ff7a | ||
|
|
2b4601bd4b | ||
|
|
5071d9c6d5 | ||
|
|
cfaa4cd094 | ||
|
|
fc414794ff | ||
|
|
d8264956c3 | ||
|
|
effa7f25ca | ||
|
|
9b19fae69a | ||
|
|
ec04f64ce1 | ||
|
|
50bea76c0e | ||
|
|
05522fcdc7 | ||
|
|
3820eaa774 | ||
|
|
7effaf920a | ||
|
|
e40a6a3ca9 | ||
|
|
7c9475cfe2 | ||
|
|
17929dd95d | ||
|
|
f478b55538 | ||
|
|
c58c14f9b7 | ||
|
|
990d7cfbf9 | ||
|
|
43fa408f46 | ||
|
|
eb860a74af | ||
|
|
b3d050b42e | ||
|
|
db678a95c6 | ||
|
|
6d66cb48dc | ||
|
|
1fe2994743 | ||
|
|
126b10ce45 | ||
|
|
3b1843b6dd | ||
|
|
6a5d00f086 | ||
|
|
06368a6cf1 | ||
|
|
c38e4bc44c | ||
|
|
e9f25d3b1a | ||
|
|
fe167aa0b9 | ||
|
|
f3421265d2 | ||
|
|
f4817cd6d1 | ||
|
|
5ae0f9311c | ||
|
|
567452f570 | ||
|
|
bb4e866bd0 |
9
.github/workflows/deploy-docs.yml
vendored
9
.github/workflows/deploy-docs.yml
vendored
@@ -1,7 +1,11 @@
|
|||||||
name: Deploy Documentation
|
name: Deploy Documentation
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
build-id:
|
||||||
|
required: false
|
||||||
|
type: string
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
@@ -16,6 +20,9 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 1
|
fetch-depth: 1
|
||||||
|
|
||||||
|
- name: Log build
|
||||||
|
run: echo "Running documentation deployment from build ${{ inputs.build-id }}"
|
||||||
|
|
||||||
- name: Setup Bun
|
- name: Setup Bun
|
||||||
uses: oven-sh/setup-bun@v1
|
uses: oven-sh/setup-bun@v1
|
||||||
with:
|
with:
|
||||||
|
|||||||
11
.github/workflows/deploy-staging.yml
vendored
11
.github/workflows/deploy-staging.yml
vendored
@@ -5,6 +5,9 @@ on:
|
|||||||
branches: [main]
|
branches: [main]
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-deploy:
|
build-and-deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -21,3 +24,11 @@ jobs:
|
|||||||
key: ${{ secrets.SSH_KEY }}
|
key: ${{ secrets.SSH_KEY }}
|
||||||
script: bash /opt/openisle/deploy-staging.sh
|
script: bash /opt/openisle/deploy-staging.sh
|
||||||
|
|
||||||
|
deploy-docs:
|
||||||
|
needs: build-and-deploy
|
||||||
|
if: ${{ success() }}
|
||||||
|
uses: ./.github/workflows/deploy-docs.yml
|
||||||
|
secrets: inherit
|
||||||
|
with:
|
||||||
|
build-id: ${{ github.run_id }}
|
||||||
|
|
||||||
|
|||||||
@@ -246,3 +246,9 @@ https://resend.com/emails 创建账号并登录
|
|||||||
`RESEND_FROM_EMAIL`: **noreply@域名**
|
`RESEND_FROM_EMAIL`: **noreply@域名**
|
||||||
`RESEND_API_KEY`:**刚刚复制的 Key**
|
`RESEND_API_KEY`:**刚刚复制的 Key**
|
||||||

|

|
||||||
|
|
||||||
|
## 开源共建和API文档
|
||||||
|
|
||||||
|
- API文档: https://openisle-docs.netlify.app/docs/openapi
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ public class CachingConfig {
|
|||||||
public static final String CATEGORY_CACHE_NAME="openisle_categories";
|
public static final String CATEGORY_CACHE_NAME="openisle_categories";
|
||||||
// 在线人数缓存名
|
// 在线人数缓存名
|
||||||
public static final String ONLINE_CACHE_NAME="openisle_online";
|
public static final String ONLINE_CACHE_NAME="openisle_online";
|
||||||
|
// 注册验证码
|
||||||
|
public static final String VERIFY_CACHE_NAME="openisle_verify";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义Redis的序列化器
|
* 自定义Redis的序列化器
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import io.swagger.v3.oas.models.OpenAPI;
|
|||||||
import io.swagger.v3.oas.models.info.Info;
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||||
|
import io.swagger.v3.oas.models.servers.Server;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -27,6 +30,9 @@ public class OpenApiConfig {
|
|||||||
@Value("${springdoc.info.header}")
|
@Value("${springdoc.info.header}")
|
||||||
private String header;
|
private String header;
|
||||||
|
|
||||||
|
@Value("${springdoc.api-docs.server-url}")
|
||||||
|
private String serverUrl;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public OpenAPI openAPI() {
|
public OpenAPI openAPI() {
|
||||||
SecurityScheme securityScheme = new SecurityScheme()
|
SecurityScheme securityScheme = new SecurityScheme()
|
||||||
@@ -37,12 +43,12 @@ public class OpenApiConfig {
|
|||||||
.name(header);
|
.name(header);
|
||||||
|
|
||||||
return new OpenAPI()
|
return new OpenAPI()
|
||||||
|
.servers(List.of(new Server().url(serverUrl)))
|
||||||
.info(new Info()
|
.info(new Info()
|
||||||
.title(title)
|
.title(title)
|
||||||
.description(description)
|
.description(description)
|
||||||
.version(version))
|
.version(version))
|
||||||
.components(new Components()
|
.components(new Components().addSecuritySchemes("JWT", securityScheme))
|
||||||
.addSecuritySchemes("JWT", securityScheme))
|
|
||||||
.addSecurityItem(new SecurityRequirement().addList("JWT"));
|
.addSecurityItem(new SecurityRequirement().addList("JWT"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.openisle.config;
|
||||||
|
|
||||||
|
import com.openisle.model.Role;
|
||||||
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.repository.UserRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure a dedicated "system" user exists for internal operations.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SystemUserInitializer implements CommandLineRunner {
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) {
|
||||||
|
userRepository.findByUsername("system").orElseGet(() -> {
|
||||||
|
User system = new User();
|
||||||
|
system.setUsername("system");
|
||||||
|
system.setEmail("system@openisle.local");
|
||||||
|
// todo(tim): raw password 采用环境变量
|
||||||
|
system.setPassword(passwordEncoder.encode("system"));
|
||||||
|
system.setRole(Role.USER);
|
||||||
|
system.setVerified(true);
|
||||||
|
system.setApproved(true);
|
||||||
|
system.setAvatar("https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/image.png");
|
||||||
|
return userRepository.save(system);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,18 +1,22 @@
|
|||||||
package com.openisle.controller;
|
package com.openisle.controller;
|
||||||
|
|
||||||
|
import com.openisle.config.CachingConfig;
|
||||||
import com.openisle.dto.*;
|
import com.openisle.dto.*;
|
||||||
import com.openisle.exception.FieldException;
|
import com.openisle.exception.FieldException;
|
||||||
import com.openisle.model.RegisterMode;
|
import com.openisle.model.RegisterMode;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
import com.openisle.service.*;
|
import com.openisle.service.*;
|
||||||
|
import com.openisle.util.VerifyType;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/auth")
|
@RequestMapping("/api/auth")
|
||||||
@@ -56,7 +60,8 @@ public class AuthController {
|
|||||||
User user = userService.registerWithInvite(
|
User user = userService.registerWithInvite(
|
||||||
req.getUsername(), req.getEmail(), req.getPassword());
|
req.getUsername(), req.getEmail(), req.getPassword());
|
||||||
inviteService.consume(req.getInviteToken(), user.getUsername());
|
inviteService.consume(req.getInviteToken(), user.getUsername());
|
||||||
emailService.sendEmail(user.getEmail(), "在网站填写验证码以验证", "您的验证码是 " + user.getVerificationCode());
|
// 发送确认邮件
|
||||||
|
userService.sendVerifyMail(user, VerifyType.REGISTER);
|
||||||
return ResponseEntity.ok(Map.of(
|
return ResponseEntity.ok(Map.of(
|
||||||
"token", jwtService.generateToken(user.getUsername()),
|
"token", jwtService.generateToken(user.getUsername()),
|
||||||
"reason_code", "INVITE_APPROVED"
|
"reason_code", "INVITE_APPROVED"
|
||||||
@@ -70,7 +75,8 @@ 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(), "在网站填写验证码以验证", "您的验证码是 " + user.getVerificationCode());
|
// 发送确认邮件
|
||||||
|
userService.sendVerifyMail(user, VerifyType.REGISTER);
|
||||||
if (!user.isApproved()) {
|
if (!user.isApproved()) {
|
||||||
notificationService.createRegisterRequestNotifications(user, user.getRegisterReason());
|
notificationService.createRegisterRequestNotifications(user, user.getRegisterReason());
|
||||||
}
|
}
|
||||||
@@ -79,13 +85,12 @@ public class AuthController {
|
|||||||
|
|
||||||
@PostMapping("/verify")
|
@PostMapping("/verify")
|
||||||
public ResponseEntity<?> verify(@RequestBody VerifyRequest req) {
|
public ResponseEntity<?> verify(@RequestBody VerifyRequest req) {
|
||||||
boolean ok = userService.verifyCode(req.getUsername(), req.getCode());
|
Optional<User> userOpt = userService.findByUsername(req.getUsername());
|
||||||
|
if (userOpt.isEmpty()) {
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("error", "Invalid credentials"));
|
||||||
|
}
|
||||||
|
boolean ok = userService.verifyCode(userOpt.get(), req.getCode(), VerifyType.REGISTER);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
Optional<User> userOpt = userService.findByUsername(req.getUsername());
|
|
||||||
if (userOpt.isEmpty()) {
|
|
||||||
return ResponseEntity.badRequest().body(Map.of("error", "Invalid credentials"));
|
|
||||||
}
|
|
||||||
|
|
||||||
User user = userOpt.get();
|
User user = userOpt.get();
|
||||||
|
|
||||||
if (user.isApproved()) {
|
if (user.isApproved()) {
|
||||||
@@ -122,7 +127,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(), "在网站填写验证码以验证", "您的验证码是 " + user.getVerificationCode());
|
userService.sendVerifyMail(user, VerifyType.REGISTER);
|
||||||
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",
|
||||||
@@ -417,14 +422,17 @@ public class AuthController {
|
|||||||
if (userOpt.isEmpty()) {
|
if (userOpt.isEmpty()) {
|
||||||
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());
|
userService.sendVerifyMail(userOpt.get(), VerifyType.RESET_PASSWORD);
|
||||||
emailService.sendEmail(req.getEmail(), "请填写验证码以重置密码", "您的验证码是" + code);
|
|
||||||
return ResponseEntity.ok(Map.of("message", "Verification code sent"));
|
return ResponseEntity.ok(Map.of("message", "Verification code sent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/forgot/verify")
|
@PostMapping("/forgot/verify")
|
||||||
public ResponseEntity<?> verifyReset(@RequestBody VerifyForgotRequest req) {
|
public ResponseEntity<?> verifyReset(@RequestBody VerifyForgotRequest req) {
|
||||||
boolean ok = userService.verifyPasswordResetCode(req.getEmail(), req.getCode());
|
Optional<User> userOpt = userService.findByEmail(req.getEmail());
|
||||||
|
if (userOpt.isEmpty()) {
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("error", "User not found"));
|
||||||
|
}
|
||||||
|
boolean ok = userService.verifyCode(userOpt.get(), req.getCode(), VerifyType.RESET_PASSWORD);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
String username = userService.findByEmail(req.getEmail()).get().getUsername();
|
String username = userService.findByEmail(req.getEmail()).get().getUsername();
|
||||||
return ResponseEntity.ok(Map.of("token", jwtService.generateResetToken(username)));
|
return ResponseEntity.ok(Map.of("token", jwtService.generateResetToken(username)));
|
||||||
|
|||||||
@@ -5,22 +5,24 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class PostChangeLogDto {
|
public class PostChangeLogDto {
|
||||||
private Long id;
|
private Long id;
|
||||||
private String username;
|
private String username;
|
||||||
|
private String userAvatar;
|
||||||
private PostChangeType type;
|
private PostChangeType type;
|
||||||
private LocalDateTime time;
|
private LocalDateTime time;
|
||||||
private String oldTitle;
|
private String oldTitle;
|
||||||
private String newTitle;
|
private String newTitle;
|
||||||
private String oldContent;
|
private String oldContent;
|
||||||
private String newContent;
|
private String newContent;
|
||||||
private String oldCategory;
|
private CategoryDto oldCategory;
|
||||||
private String newCategory;
|
private CategoryDto newCategory;
|
||||||
private String oldTags;
|
private List<TagDto> oldTags;
|
||||||
private String newTags;
|
private List<TagDto> newTags;
|
||||||
private Boolean oldClosed;
|
private Boolean oldClosed;
|
||||||
private Boolean newClosed;
|
private Boolean newClosed;
|
||||||
private LocalDateTime oldPinnedAt;
|
private LocalDateTime oldPinnedAt;
|
||||||
|
|||||||
@@ -1,15 +1,35 @@
|
|||||||
package com.openisle.mapper;
|
package com.openisle.mapper;
|
||||||
|
|
||||||
|
import com.openisle.dto.CategoryDto;
|
||||||
import com.openisle.dto.PostChangeLogDto;
|
import com.openisle.dto.PostChangeLogDto;
|
||||||
|
import com.openisle.dto.TagDto;
|
||||||
import com.openisle.model.*;
|
import com.openisle.model.*;
|
||||||
|
import com.openisle.repository.CategoryRepository;
|
||||||
|
import com.openisle.repository.TagRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class PostChangeLogMapper {
|
public class PostChangeLogMapper {
|
||||||
|
|
||||||
|
private final CategoryRepository categoryRepository;
|
||||||
|
private final TagRepository tagRepository;
|
||||||
|
private final CategoryMapper categoryMapper;
|
||||||
|
private final TagMapper tagMapper;
|
||||||
|
|
||||||
public PostChangeLogDto toDto(PostChangeLog log) {
|
public PostChangeLogDto toDto(PostChangeLog log) {
|
||||||
PostChangeLogDto dto = new PostChangeLogDto();
|
PostChangeLogDto dto = new PostChangeLogDto();
|
||||||
dto.setId(log.getId());
|
dto.setId(log.getId());
|
||||||
dto.setUsername(log.getUser().getUsername());
|
if (log.getUser() != null) {
|
||||||
|
dto.setUsername(log.getUser().getUsername());
|
||||||
|
dto.setUserAvatar(log.getUser().getAvatar());
|
||||||
|
}
|
||||||
dto.setType(log.getType());
|
dto.setType(log.getType());
|
||||||
dto.setTime(log.getCreatedAt());
|
dto.setTime(log.getCreatedAt());
|
||||||
if (log instanceof PostTitleChangeLog t) {
|
if (log instanceof PostTitleChangeLog t) {
|
||||||
@@ -19,11 +39,11 @@ public class PostChangeLogMapper {
|
|||||||
dto.setOldContent(c.getOldContent());
|
dto.setOldContent(c.getOldContent());
|
||||||
dto.setNewContent(c.getNewContent());
|
dto.setNewContent(c.getNewContent());
|
||||||
} else if (log instanceof PostCategoryChangeLog cat) {
|
} else if (log instanceof PostCategoryChangeLog cat) {
|
||||||
dto.setOldCategory(cat.getOldCategory());
|
dto.setOldCategory(mapCategory(cat.getOldCategory()));
|
||||||
dto.setNewCategory(cat.getNewCategory());
|
dto.setNewCategory(mapCategory(cat.getNewCategory()));
|
||||||
} else if (log instanceof PostTagChangeLog tag) {
|
} else if (log instanceof PostTagChangeLog tag) {
|
||||||
dto.setOldTags(tag.getOldTags());
|
dto.setOldTags(mapTags(tag.getOldTags()));
|
||||||
dto.setNewTags(tag.getNewTags());
|
dto.setNewTags(mapTags(tag.getNewTags()));
|
||||||
} else if (log instanceof PostClosedChangeLog cl) {
|
} else if (log instanceof PostClosedChangeLog cl) {
|
||||||
dto.setOldClosed(cl.isOldClosed());
|
dto.setOldClosed(cl.isOldClosed());
|
||||||
dto.setNewClosed(cl.isNewClosed());
|
dto.setNewClosed(cl.isNewClosed());
|
||||||
@@ -36,4 +56,37 @@ public class PostChangeLogMapper {
|
|||||||
}
|
}
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CategoryDto mapCategory(String name) {
|
||||||
|
if (name == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return categoryRepository.findByName(name)
|
||||||
|
.map(categoryMapper::toDto)
|
||||||
|
.orElseGet(() -> {
|
||||||
|
CategoryDto dto = new CategoryDto();
|
||||||
|
dto.setName(name);
|
||||||
|
return dto;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<TagDto> mapTags(String tags) {
|
||||||
|
if (tags == null || tags.isBlank()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return Arrays.stream(tags.split(","))
|
||||||
|
.map(String::trim)
|
||||||
|
.map(this::mapTag)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private TagDto mapTag(String name) {
|
||||||
|
return tagRepository.findByName(name)
|
||||||
|
.map(tagMapper::toDto)
|
||||||
|
.orElseGet(() -> {
|
||||||
|
TagDto dto = new TagDto();
|
||||||
|
dto.setName(name);
|
||||||
|
return dto;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public abstract class PostChangeLog {
|
|||||||
@JoinColumn(name = "post_id")
|
@JoinColumn(name = "post_id")
|
||||||
private Post post;
|
private Post post;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
@ManyToOne(fetch = FetchType.LAZY, optional = true)
|
||||||
@JoinColumn(name = "user_id")
|
@JoinColumn(name = "user_id")
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
|
|||||||
@@ -7,5 +7,7 @@ public enum PostChangeType {
|
|||||||
TAG,
|
TAG,
|
||||||
CLOSED,
|
CLOSED,
|
||||||
PINNED,
|
PINNED,
|
||||||
FEATURED
|
FEATURED,
|
||||||
|
VOTE_RESULT,
|
||||||
|
LOTTERY_RESULT
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.openisle.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Entity
|
||||||
|
@Table(name = "post_lottery_result_change_logs")
|
||||||
|
public class PostLotteryResultChangeLog extends PostChangeLog {
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.openisle.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Entity
|
||||||
|
@Table(name = "post_vote_result_change_logs")
|
||||||
|
public class PostVoteResultChangeLog extends PostChangeLog {
|
||||||
|
}
|
||||||
|
|
||||||
@@ -4,7 +4,10 @@ import com.openisle.model.Category;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface CategoryRepository extends JpaRepository<Category, Long> {
|
public interface CategoryRepository extends JpaRepository<Category, Long> {
|
||||||
List<Category> findByNameContainingIgnoreCase(String keyword);
|
List<Category> findByNameContainingIgnoreCase(String keyword);
|
||||||
|
|
||||||
|
Optional<Category> findByName(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface TagRepository extends JpaRepository<Tag, Long> {
|
public interface TagRepository extends JpaRepository<Tag, Long> {
|
||||||
List<Tag> findByNameContainingIgnoreCase(String keyword);
|
List<Tag> findByNameContainingIgnoreCase(String keyword);
|
||||||
@@ -15,4 +16,6 @@ public interface TagRepository extends JpaRepository<Tag, Long> {
|
|||||||
|
|
||||||
List<Tag> findByCreatorOrderByCreatedAtDesc(User creator, Pageable pageable);
|
List<Tag> findByCreatorOrderByCreatedAtDesc(User creator, Pageable pageable);
|
||||||
List<Tag> findByCreator(User creator);
|
List<Tag> findByCreator(User creator);
|
||||||
|
|
||||||
|
Optional<Tag> findByName(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.openisle.service;
|
|||||||
import com.openisle.model.*;
|
import com.openisle.model.*;
|
||||||
import com.openisle.repository.PostChangeLogRepository;
|
import com.openisle.repository.PostChangeLogRepository;
|
||||||
import com.openisle.repository.PostRepository;
|
import com.openisle.repository.PostRepository;
|
||||||
|
import com.openisle.repository.UserRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -15,6 +16,12 @@ import java.util.stream.Collectors;
|
|||||||
public class PostChangeLogService {
|
public class PostChangeLogService {
|
||||||
private final PostChangeLogRepository logRepository;
|
private final PostChangeLogRepository logRepository;
|
||||||
private final PostRepository postRepository;
|
private final PostRepository postRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
private User getSystemUser() {
|
||||||
|
return userRepository.findByUsername("system")
|
||||||
|
.orElseThrow(() -> new IllegalStateException("System user not found"));
|
||||||
|
}
|
||||||
|
|
||||||
public void recordContentChange(Post post, User user, String oldContent, String newContent) {
|
public void recordContentChange(Post post, User user, String oldContent, String newContent) {
|
||||||
PostContentChangeLog log = new PostContentChangeLog();
|
PostContentChangeLog log = new PostContentChangeLog();
|
||||||
@@ -86,6 +93,22 @@ public class PostChangeLogService {
|
|||||||
logRepository.save(log);
|
logRepository.save(log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void recordVoteResult(Post post) {
|
||||||
|
PostVoteResultChangeLog log = new PostVoteResultChangeLog();
|
||||||
|
log.setPost(post);
|
||||||
|
log.setUser(getSystemUser());
|
||||||
|
log.setType(PostChangeType.VOTE_RESULT);
|
||||||
|
logRepository.save(log);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void recordLotteryResult(Post post) {
|
||||||
|
PostLotteryResultChangeLog log = new PostLotteryResultChangeLog();
|
||||||
|
log.setPost(post);
|
||||||
|
log.setUser(getSystemUser());
|
||||||
|
log.setType(PostChangeType.LOTTERY_RESULT);
|
||||||
|
logRepository.save(log);
|
||||||
|
}
|
||||||
|
|
||||||
public List<PostChangeLog> listLogs(Long postId) {
|
public List<PostChangeLog> listLogs(Long postId) {
|
||||||
Post post = postRepository.findById(postId)
|
Post post = postRepository.findById(postId)
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
||||||
|
|||||||
@@ -368,6 +368,7 @@ public class PostService {
|
|||||||
for (User participant : pp.getParticipants()) {
|
for (User participant : pp.getParticipants()) {
|
||||||
notificationService.createNotification(participant, NotificationType.POLL_RESULT_PARTICIPANT, pp, null, null, null, null, null);
|
notificationService.createNotification(participant, NotificationType.POLL_RESULT_PARTICIPANT, pp, null, null, null, null, null);
|
||||||
}
|
}
|
||||||
|
postChangeLogService.recordVoteResult(pp);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,6 +403,7 @@ public class PostService {
|
|||||||
notificationService.createNotification(lp.getAuthor(), NotificationType.LOTTERY_DRAW, lp, null, null, null, null, null);
|
notificationService.createNotification(lp.getAuthor(), NotificationType.LOTTERY_DRAW, lp, null, null, null, null, null);
|
||||||
notificationService.sendCustomPush(lp.getAuthor(), "抽奖已开奖", String.format("%s/posts/%d", websiteUrl, lp.getId()));
|
notificationService.sendCustomPush(lp.getAuthor(), "抽奖已开奖", String.format("%s/posts/%d", websiteUrl, lp.getId()));
|
||||||
}
|
}
|
||||||
|
postChangeLogService.recordLotteryResult(lp);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.openisle.service;
|
package com.openisle.service;
|
||||||
|
|
||||||
|
import com.openisle.config.CachingConfig;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import com.openisle.model.Role;
|
import com.openisle.model.Role;
|
||||||
import com.openisle.service.PasswordValidator;
|
import com.openisle.service.PasswordValidator;
|
||||||
@@ -7,13 +8,18 @@ import com.openisle.service.UsernameValidator;
|
|||||||
import com.openisle.service.AvatarGenerator;
|
import com.openisle.service.AvatarGenerator;
|
||||||
import com.openisle.exception.FieldException;
|
import com.openisle.exception.FieldException;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
|
import com.openisle.util.VerifyType;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -25,6 +31,10 @@ public class UserService {
|
|||||||
private final ImageUploader imageUploader;
|
private final ImageUploader imageUploader;
|
||||||
private final AvatarGenerator avatarGenerator;
|
private final AvatarGenerator avatarGenerator;
|
||||||
|
|
||||||
|
private final RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
private final EmailSender emailService;
|
||||||
|
|
||||||
public User register(String username, String email, String password, String reason, com.openisle.model.RegisterMode mode) {
|
public User register(String username, String email, String password, String reason, com.openisle.model.RegisterMode mode) {
|
||||||
usernameValidator.validate(username);
|
usernameValidator.validate(username);
|
||||||
passwordValidator.validate(password);
|
passwordValidator.validate(password);
|
||||||
@@ -38,7 +48,7 @@ public class UserService {
|
|||||||
// 未验证 → 允许“重注册”:覆盖必要字段并重新发验证码
|
// 未验证 → 允许“重注册”:覆盖必要字段并重新发验证码
|
||||||
u.setEmail(email); // 若不允许改邮箱可去掉
|
u.setEmail(email); // 若不允许改邮箱可去掉
|
||||||
u.setPassword(passwordEncoder.encode(password));
|
u.setPassword(passwordEncoder.encode(password));
|
||||||
u.setVerificationCode(genCode());
|
// u.setVerificationCode(genCode());
|
||||||
u.setRegisterReason(reason);
|
u.setRegisterReason(reason);
|
||||||
u.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
u.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
||||||
return userRepository.save(u);
|
return userRepository.save(u);
|
||||||
@@ -54,7 +64,7 @@ public class UserService {
|
|||||||
// 未验证 → 允许“重注册”
|
// 未验证 → 允许“重注册”
|
||||||
u.setUsername(username); // 若不允许改用户名可去掉
|
u.setUsername(username); // 若不允许改用户名可去掉
|
||||||
u.setPassword(passwordEncoder.encode(password));
|
u.setPassword(passwordEncoder.encode(password));
|
||||||
u.setVerificationCode(genCode());
|
// u.setVerificationCode(genCode());
|
||||||
u.setRegisterReason(reason);
|
u.setRegisterReason(reason);
|
||||||
u.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
u.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
||||||
return userRepository.save(u);
|
return userRepository.save(u);
|
||||||
@@ -67,7 +77,7 @@ public class UserService {
|
|||||||
user.setPassword(passwordEncoder.encode(password));
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
user.setRole(Role.USER);
|
user.setRole(Role.USER);
|
||||||
user.setVerified(false);
|
user.setVerified(false);
|
||||||
user.setVerificationCode(genCode());
|
// user.setVerificationCode(genCode());
|
||||||
user.setAvatar(avatarGenerator.generate(username));
|
user.setAvatar(avatarGenerator.generate(username));
|
||||||
user.setRegisterReason(reason);
|
user.setRegisterReason(reason);
|
||||||
user.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
user.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
||||||
@@ -77,7 +87,7 @@ public class UserService {
|
|||||||
public User registerWithInvite(String username, String email, String password) {
|
public User registerWithInvite(String username, String email, String password) {
|
||||||
User user = register(username, email, password, "", com.openisle.model.RegisterMode.DIRECT);
|
User user = register(username, email, password, "", com.openisle.model.RegisterMode.DIRECT);
|
||||||
user.setVerified(true);
|
user.setVerified(true);
|
||||||
user.setVerificationCode(genCode());
|
// user.setVerificationCode(genCode());
|
||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,16 +95,58 @@ public class UserService {
|
|||||||
return String.format("%06d", new Random().nextInt(1000000));
|
return String.format("%06d", new Random().nextInt(1000000));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean verifyCode(String username, String code) {
|
/**
|
||||||
Optional<User> userOpt = userRepository.findByUsername(username);
|
* 将验证码存入缓存,并发送邮件
|
||||||
if (userOpt.isPresent() && code.equals(userOpt.get().getVerificationCode())) {
|
* @param user
|
||||||
User user = userOpt.get();
|
*/
|
||||||
user.setVerified(true);
|
public void sendVerifyMail(User user, VerifyType verifyType){
|
||||||
user.setVerificationCode(null);
|
//缓存验证码
|
||||||
userRepository.save(user);
|
String code = genCode();
|
||||||
return true;
|
String key;
|
||||||
|
String subject;
|
||||||
|
String content = "您的验证码是:" + code;
|
||||||
|
// 注册类型
|
||||||
|
if(verifyType.equals(VerifyType.REGISTER)){
|
||||||
|
key = CachingConfig.VERIFY_CACHE_NAME + ":register:code:" + user.getUsername();
|
||||||
|
subject = "在网站填写验证码以验证(有效期为5分钟)";
|
||||||
|
}else {
|
||||||
|
// 重置密码
|
||||||
|
key = CachingConfig.VERIFY_CACHE_NAME + ":reset_password:code:" + user.getUsername();
|
||||||
|
subject = "请填写验证码以重置密码(有效期为5分钟)";
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);// 五分钟后验证码过期
|
||||||
|
emailService.sendEmail(user.getEmail(), subject, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证code是否正确
|
||||||
|
* @param user
|
||||||
|
* @param code
|
||||||
|
* @param verifyType
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean verifyCode(User user, String code, VerifyType verifyType) {
|
||||||
|
// 生成key
|
||||||
|
String key1 = VerifyType.REGISTER.equals(verifyType)?":register:code:":":reset_password:code:";
|
||||||
|
String key = CachingConfig.VERIFY_CACHE_NAME + key1 + user.getUsername();
|
||||||
|
// 这里不能使用getAndDelete,需要6.x版本
|
||||||
|
String cachedCode = (String)redisTemplate.opsForValue().get(key);
|
||||||
|
// 如果校验code过期或者不存在
|
||||||
|
// 或者校验code不一致
|
||||||
|
if(Objects.isNull(cachedCode)
|
||||||
|
|| !cachedCode.equals(code)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 注册模式需要设置已经确认
|
||||||
|
if(VerifyType.REGISTER.equals(verifyType)){
|
||||||
|
user.setVerified(true);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
// 走到这里说明验证成功删除验证码
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<User> authenticate(String username, String password) {
|
public Optional<User> authenticate(String username, String password) {
|
||||||
@@ -165,26 +217,6 @@ public class UserService {
|
|||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generatePasswordResetCode(String email) {
|
|
||||||
User user = userRepository.findByEmail(email)
|
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
|
||||||
String code = genCode();
|
|
||||||
user.setPasswordResetCode(code);
|
|
||||||
userRepository.save(user);
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean verifyPasswordResetCode(String email, String code) {
|
|
||||||
Optional<User> userOpt = userRepository.findByEmail(email);
|
|
||||||
if (userOpt.isPresent() && code.equals(userOpt.get().getPasswordResetCode())) {
|
|
||||||
User user = userOpt.get();
|
|
||||||
user.setPasswordResetCode(null);
|
|
||||||
userRepository.save(user);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User updatePassword(String username, String newPassword) {
|
public User updatePassword(String username, String newPassword) {
|
||||||
passwordValidator.validate(newPassword);
|
passwordValidator.validate(newPassword);
|
||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
|
|||||||
20
backend/src/main/java/com/openisle/util/VerifyType.java
Normal file
20
backend/src/main/java/com/openisle/util/VerifyType.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.openisle.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码类型
|
||||||
|
* @author smallclover
|
||||||
|
* @since 2025-09-08
|
||||||
|
*/
|
||||||
|
public enum VerifyType {
|
||||||
|
REGISTER(1),
|
||||||
|
RESET_PASSWORD(2);
|
||||||
|
private final int code;
|
||||||
|
|
||||||
|
VerifyType(int code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -108,6 +108,7 @@ rabbitmq.sharding.enabled=true
|
|||||||
# see https://springdoc.org/#springdoc-openapi-core-properties
|
# see https://springdoc.org/#springdoc-openapi-core-properties
|
||||||
springdoc.api-docs.path=/api/v3/api-docs
|
springdoc.api-docs.path=/api/v3/api-docs
|
||||||
springdoc.api-docs.enabled=true
|
springdoc.api-docs.enabled=true
|
||||||
|
springdoc.api-docs.server-url=${WEBSITE_URL:https://www.open-isle.com}
|
||||||
springdoc.info.title=OpenIsle
|
springdoc.info.title=OpenIsle
|
||||||
springdoc.info.description=OpenIsle Open API Documentation
|
springdoc.info.description=OpenIsle Open API Documentation
|
||||||
springdoc.info.version=0.0.1
|
springdoc.info.version=0.0.1
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.openisle.model.User;
|
|||||||
import com.openisle.service.*;
|
import com.openisle.service.*;
|
||||||
import com.openisle.model.RegisterMode;
|
import com.openisle.model.RegisterMode;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
|
import com.openisle.util.VerifyType;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -71,7 +72,9 @@ class AuthControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void verifyCodeEndpoint() throws Exception {
|
void verifyCodeEndpoint() throws Exception {
|
||||||
Mockito.when(userService.verifyCode("u", "123")).thenReturn(true);
|
User user = new User();
|
||||||
|
user.setUsername("u");
|
||||||
|
Mockito.when(userService.verifyCode(user, "123", VerifyType.REGISTER)).thenReturn(true);
|
||||||
Mockito.when(jwtService.generateReasonToken("u")).thenReturn("reason_token");
|
Mockito.when(jwtService.generateReasonToken("u")).thenReturn("reason_token");
|
||||||
|
|
||||||
mockMvc.perform(post("/api/auth/verify")
|
mockMvc.perform(post("/api/auth/verify")
|
||||||
|
|||||||
@@ -37,11 +37,12 @@ class PostServiceTest {
|
|||||||
EmailSender emailSender = mock(EmailSender.class);
|
EmailSender emailSender = mock(EmailSender.class);
|
||||||
ApplicationContext context = mock(ApplicationContext.class);
|
ApplicationContext context = mock(ApplicationContext.class);
|
||||||
PointService pointService = mock(PointService.class);
|
PointService pointService = mock(PointService.class);
|
||||||
|
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||||
|
|
||||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||||
imageUploader, taskScheduler, emailSender, context, pointService, PublishMode.DIRECT);
|
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService, PublishMode.DIRECT);
|
||||||
when(context.getBean(PostService.class)).thenReturn(service);
|
when(context.getBean(PostService.class)).thenReturn(service);
|
||||||
|
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
@@ -86,11 +87,12 @@ class PostServiceTest {
|
|||||||
EmailSender emailSender = mock(EmailSender.class);
|
EmailSender emailSender = mock(EmailSender.class);
|
||||||
ApplicationContext context = mock(ApplicationContext.class);
|
ApplicationContext context = mock(ApplicationContext.class);
|
||||||
PointService pointService = mock(PointService.class);
|
PointService pointService = mock(PointService.class);
|
||||||
|
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||||
|
|
||||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||||
imageUploader, taskScheduler, emailSender, context, pointService, PublishMode.DIRECT);
|
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService, PublishMode.DIRECT);
|
||||||
when(context.getBean(PostService.class)).thenReturn(service);
|
when(context.getBean(PostService.class)).thenReturn(service);
|
||||||
|
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
@@ -141,11 +143,12 @@ class PostServiceTest {
|
|||||||
EmailSender emailSender = mock(EmailSender.class);
|
EmailSender emailSender = mock(EmailSender.class);
|
||||||
ApplicationContext context = mock(ApplicationContext.class);
|
ApplicationContext context = mock(ApplicationContext.class);
|
||||||
PointService pointService = mock(PointService.class);
|
PointService pointService = mock(PointService.class);
|
||||||
|
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||||
|
|
||||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||||
imageUploader, taskScheduler, emailSender, context, pointService, PublishMode.DIRECT);
|
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService, PublishMode.DIRECT);
|
||||||
when(context.getBean(PostService.class)).thenReturn(service);
|
when(context.getBean(PostService.class)).thenReturn(service);
|
||||||
|
|
||||||
when(postRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(1L);
|
when(postRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(1L);
|
||||||
@@ -177,11 +180,12 @@ class PostServiceTest {
|
|||||||
EmailSender emailSender = mock(EmailSender.class);
|
EmailSender emailSender = mock(EmailSender.class);
|
||||||
ApplicationContext context = mock(ApplicationContext.class);
|
ApplicationContext context = mock(ApplicationContext.class);
|
||||||
PointService pointService = mock(PointService.class);
|
PointService pointService = mock(PointService.class);
|
||||||
|
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||||
|
|
||||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||||
imageUploader, taskScheduler, emailSender, context, pointService, PublishMode.DIRECT);
|
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService, PublishMode.DIRECT);
|
||||||
when(context.getBean(PostService.class)).thenReturn(service);
|
when(context.getBean(PostService.class)).thenReturn(service);
|
||||||
|
|
||||||
User author = new User();
|
User author = new User();
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
--background-color: white;
|
--background-color: white;
|
||||||
--background-color-blur: rgba(255, 255, 255, 0.57);
|
--background-color-blur: rgba(255, 255, 255, 0.57);
|
||||||
--menu-border-color: lightgray;
|
--menu-border-color: lightgray;
|
||||||
--normal-border-color: lightgray;
|
--normal-border-color: rgba(211, 211, 211, 0.63);
|
||||||
--menu-selected-background-color: rgba(88, 241, 255, 0.166);
|
--menu-selected-background-color: rgba(88, 241, 255, 0.166);
|
||||||
--normal-light-background-color: rgba(242, 242, 242, 0.884);
|
--normal-light-background-color: rgba(242, 242, 242, 0.884);
|
||||||
--menu-selected-background-color-hover: rgba(242, 242, 242, 0.884);
|
--menu-selected-background-color-hover: rgba(242, 242, 242, 0.884);
|
||||||
@@ -348,6 +348,22 @@ body {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Adjust diff2html layout on mobile */
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
.content-diff .d2h-wrapper,
|
||||||
|
.content-diff .d2h-code-line,
|
||||||
|
.content-diff .d2h-code-side-line,
|
||||||
|
.content-diff .d2h-code-line-ctn,
|
||||||
|
.content-diff .d2h-code-side-line-ctn,
|
||||||
|
.content-diff .d2h-file-header {
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content-diff .d2h-wrapper {
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Transition API */
|
/* Transition API */
|
||||||
::view-transition-old(root),
|
::view-transition-old(root),
|
||||||
::view-transition-new(root) {
|
::view-transition-new(root) {
|
||||||
|
|||||||
@@ -1,28 +1,53 @@
|
|||||||
<template>
|
<template>
|
||||||
<div :id="`change-log-${log.id}`" class="change-log-container">
|
<div :id="`change-log-${log.id}`" class="change-log-container">
|
||||||
<div class="change-log-text">
|
<div class="change-log-text">
|
||||||
<span class="change-log-user">{{ log.username }}</span>
|
<BaseImage
|
||||||
<span v-if="log.type === 'CONTENT'">
|
v-if="log.userAvatar"
|
||||||
变更了文章内容
|
class="change-log-avatar"
|
||||||
<div class="content-diff" v-html="diffHtml"></div>
|
:src="log.userAvatar"
|
||||||
</span>
|
alt="avatar"
|
||||||
<span v-else-if="log.type === 'TITLE'">变更了文章标题</span>
|
@click="() => navigateTo(`/users/${log.username}`)"
|
||||||
<span v-else-if="log.type === 'CATEGORY'">变更了文章分类</span>
|
/>
|
||||||
<span v-else-if="log.type === 'TAG'">变更了文章标签</span>
|
<span v-if="log.username" class="change-log-user">{{ log.username }}</span>
|
||||||
<span v-else-if="log.type === 'CLOSED'">
|
<span v-if="log.type === 'CONTENT'" class="change-log-content">变更了文章内容</span>
|
||||||
|
<span v-else-if="log.type === 'TITLE'" class="change-log-content">变更了文章标题</span>
|
||||||
|
<template v-else-if="log.type === 'CATEGORY'">
|
||||||
|
<div class="change-log-category-text">变更了文章分类, 从</div>
|
||||||
|
<ArticleCategory :category="log.oldCategory" />
|
||||||
|
<div class="change-log-category-text">修改为</div>
|
||||||
|
<ArticleCategory :category="log.newCategory" />
|
||||||
|
</template>
|
||||||
|
<template v-else-if="log.type === 'TAG'">
|
||||||
|
<div class="change-log-category-text">变更了文章标签, 从</div>
|
||||||
|
<ArticleTags :tags="log.oldTags" />
|
||||||
|
<div class="change-log-category-text">修改为</div>
|
||||||
|
<ArticleTags :tags="log.newTags" />
|
||||||
|
</template>
|
||||||
|
<span v-else-if="log.type === 'CLOSED'" class="change-log-content">
|
||||||
<template v-if="log.newClosed">关闭了文章</template>
|
<template v-if="log.newClosed">关闭了文章</template>
|
||||||
<template v-else>重新打开了文章</template>
|
<template v-else>重新打开了文章</template>
|
||||||
</span>
|
</span>
|
||||||
<span v-else-if="log.type === 'PINNED'">
|
<span v-else-if="log.type === 'PINNED'" class="change-log-content">
|
||||||
<template v-if="log.newPinnedAt">置顶了文章</template>
|
<template v-if="log.newPinnedAt">置顶了文章</template>
|
||||||
<template v-else>取消置顶了文章</template>
|
<template v-else>取消置顶文章</template>
|
||||||
</span>
|
</span>
|
||||||
<span v-else-if="log.type === 'FEATURED'">
|
<span v-else-if="log.type === 'FEATURED'" class="change-log-content">
|
||||||
<template v-if="log.newFeatured">将文章设为精选</template>
|
<template v-if="log.newFeatured">将文章设为精选</template>
|
||||||
<template v-else>取消精选文章</template>
|
<template v-else>取消精选文章</template>
|
||||||
</span>
|
</span>
|
||||||
|
<span v-else-if="log.type === 'VOTE_RESULT'" class="change-log-content"
|
||||||
|
>系统已计算投票结果</span
|
||||||
|
>
|
||||||
|
<span v-else-if="log.type === 'LOTTERY_RESULT'" class="change-log-content"
|
||||||
|
>系统已「精密计算」抽奖结果 (=゚ω゚)ノ</span
|
||||||
|
>
|
||||||
</div>
|
</div>
|
||||||
<div class="change-log-time">{{ log.time }}</div>
|
<div class="change-log-time">{{ log.time }}</div>
|
||||||
|
<div
|
||||||
|
v-if="log.type === 'CONTENT' || log.type === 'TITLE'"
|
||||||
|
class="content-diff"
|
||||||
|
v-html="diffHtml"
|
||||||
|
></div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -30,14 +55,46 @@
|
|||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { html } from 'diff2html'
|
import { html } from 'diff2html'
|
||||||
import { createTwoFilesPatch } from 'diff'
|
import { createTwoFilesPatch } from 'diff'
|
||||||
|
import { useIsMobile } from '~/utils/screen'
|
||||||
import 'diff2html/bundles/css/diff2html.min.css'
|
import 'diff2html/bundles/css/diff2html.min.css'
|
||||||
const props = defineProps({ log: Object })
|
import BaseImage from '~/components/BaseImage.vue'
|
||||||
|
import { navigateTo } from 'nuxt/app'
|
||||||
|
import { themeState } from '~/utils/theme'
|
||||||
|
import ArticleCategory from '~/components/ArticleCategory.vue'
|
||||||
|
import ArticleTags from '~/components/ArticleTags.vue'
|
||||||
|
const props = defineProps({
|
||||||
|
log: Object,
|
||||||
|
title: String,
|
||||||
|
})
|
||||||
|
|
||||||
const diffHtml = computed(() => {
|
const diffHtml = computed(() => {
|
||||||
|
// Track theme changes
|
||||||
|
const isDark = import.meta.client && document.documentElement.dataset.theme === 'dark'
|
||||||
|
themeState.mode
|
||||||
|
const colorScheme = isDark ? 'dark' : 'light'
|
||||||
|
|
||||||
if (props.log.type === 'CONTENT') {
|
if (props.log.type === 'CONTENT') {
|
||||||
const oldContent = props.log.oldContent ?? ''
|
const oldContent = props.log.oldContent ?? ''
|
||||||
const newContent = props.log.newContent ?? ''
|
const newContent = props.log.newContent ?? ''
|
||||||
const diff = createTwoFilesPatch('old', 'new', oldContent, newContent)
|
const diff = createTwoFilesPatch(props.title, props.title, oldContent, newContent)
|
||||||
return html(diff, { inputFormat: 'diff', showFiles: false, matching: 'lines' })
|
return html(diff, {
|
||||||
|
inputFormat: 'diff',
|
||||||
|
showFiles: false,
|
||||||
|
matching: 'lines',
|
||||||
|
drawFileList: false,
|
||||||
|
colorScheme,
|
||||||
|
})
|
||||||
|
} else if (props.log.type === 'TITLE') {
|
||||||
|
const oldTitle = props.log.oldTitle ?? ''
|
||||||
|
const newTitle = props.log.newTitle ?? ''
|
||||||
|
const diff = createTwoFilesPatch(oldTitle, newTitle, '', '')
|
||||||
|
return html(diff, {
|
||||||
|
inputFormat: 'diff',
|
||||||
|
showFiles: false,
|
||||||
|
matching: 'lines',
|
||||||
|
drawFileList: false,
|
||||||
|
colorScheme,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
})
|
})
|
||||||
@@ -47,13 +104,34 @@ const diffHtml = computed(() => {
|
|||||||
.change-log-container {
|
.change-log-container {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
padding-bottom: 30px;
|
/* padding-top: 5px; */
|
||||||
opacity: 0.7;
|
/* padding-bottom: 30px; */
|
||||||
|
font-size: 14px;
|
||||||
|
border-bottom: 1px solid var(--normal-border-color);
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}
|
||||||
|
.change-log-text {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
}
|
}
|
||||||
.change-log-user {
|
.change-log-user {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
margin-right: 4px;
|
margin-right: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.change-log-user,
|
||||||
|
.change-log-content {
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.change-log-avatar {
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
border-radius: 50%;
|
||||||
|
margin-right: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
.change-log-time {
|
.change-log-time {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
opacity: 0.6;
|
opacity: 0.6;
|
||||||
@@ -62,4 +140,12 @@ const diffHtml = computed(() => {
|
|||||||
.content-diff {
|
.content-diff {
|
||||||
margin-top: 8px;
|
margin-top: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.change-log-category {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
gap: 4px;
|
||||||
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -134,7 +134,7 @@
|
|||||||
:post-closed="closed"
|
:post-closed="closed"
|
||||||
@deleted="onCommentDeleted"
|
@deleted="onCommentDeleted"
|
||||||
/>
|
/>
|
||||||
<PostChangeLogItem v-else :log="item" />
|
<PostChangeLogItem v-else :log="item" :title="title" />
|
||||||
</template>
|
</template>
|
||||||
</BaseTimeline>
|
</BaseTimeline>
|
||||||
</div>
|
</div>
|
||||||
@@ -363,6 +363,10 @@ const changeLogIcon = (l) => {
|
|||||||
} else {
|
} else {
|
||||||
return 'dislike'
|
return 'dislike'
|
||||||
}
|
}
|
||||||
|
} else if (l.type === 'VOTE_RESULT') {
|
||||||
|
return 'check-one'
|
||||||
|
} else if (l.type === 'LOTTERY_RESULT') {
|
||||||
|
return 'gift'
|
||||||
} else {
|
} else {
|
||||||
return 'info'
|
return 'info'
|
||||||
}
|
}
|
||||||
@@ -371,12 +375,21 @@ const changeLogIcon = (l) => {
|
|||||||
const mapChangeLog = (l) => ({
|
const mapChangeLog = (l) => ({
|
||||||
id: l.id,
|
id: l.id,
|
||||||
username: l.username,
|
username: l.username,
|
||||||
|
userAvatar: l.userAvatar,
|
||||||
type: l.type,
|
type: l.type,
|
||||||
createdAt: l.time,
|
createdAt: l.time,
|
||||||
time: TimeManager.format(l.time),
|
time: TimeManager.format(l.time),
|
||||||
newClosed: l.newClosed,
|
newClosed: l.newClosed,
|
||||||
newPinnedAt: l.newPinnedAt,
|
newPinnedAt: l.newPinnedAt,
|
||||||
newFeatured: l.newFeatured,
|
newFeatured: l.newFeatured,
|
||||||
|
oldContent: l.oldContent,
|
||||||
|
newContent: l.newContent,
|
||||||
|
oldTitle: l.oldTitle,
|
||||||
|
newTitle: l.newTitle,
|
||||||
|
oldCategory: l.oldCategory,
|
||||||
|
newCategory: l.newCategory,
|
||||||
|
oldTags: l.oldTags,
|
||||||
|
newTags: l.newTags,
|
||||||
icon: changeLogIcon(l),
|
icon: changeLogIcon(l),
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1150,6 +1163,7 @@ onMounted(async () => {
|
|||||||
margin-top: 10px;
|
margin-top: 10px;
|
||||||
gap: 10px;
|
gap: 10px;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
.info-content-container {
|
.info-content-container {
|
||||||
@@ -1205,7 +1219,7 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.post-time {
|
.post-time {
|
||||||
font-size: 14px;
|
font-size: 12px;
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1271,10 +1285,6 @@ onMounted(async () => {
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-time {
|
|
||||||
font-size: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.info-content-text {
|
.info-content-text {
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ import {
|
|||||||
DoubleDown,
|
DoubleDown,
|
||||||
Open,
|
Open,
|
||||||
Dislike,
|
Dislike,
|
||||||
|
CheckOne,
|
||||||
} from '@icon-park/vue-next'
|
} from '@icon-park/vue-next'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin((nuxtApp) => {
|
||||||
@@ -155,4 +156,5 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
nuxtApp.vueApp.component('DoubleDown', DoubleDown)
|
nuxtApp.vueApp.component('DoubleDown', DoubleDown)
|
||||||
nuxtApp.vueApp.component('OpenIcon', Open)
|
nuxtApp.vueApp.component('OpenIcon', Open)
|
||||||
nuxtApp.vueApp.component('Dislike', Dislike)
|
nuxtApp.vueApp.component('Dislike', Dislike)
|
||||||
|
nuxtApp.vueApp.component('CheckOne', CheckOne)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user