瀏覽代碼

更新 message-service 的 dto 和 po 数据约束, 以及 po 的数据库表结构

reghao 5 月之前
父節點
當前提交
a95591213c

+ 12 - 0
common/src/main/java/cn/reghao/tnb/common/util/StringRegexp.java

@@ -3,6 +3,8 @@ package cn.reghao.tnb.common.util;
 import java.util.regex.Pattern;
 
 /**
+ * 一些常用的正则表达式
+ *
  * @author reghao
  * @date 2023-02-20 13:53:17
  */
@@ -10,6 +12,8 @@ public class StringRegexp {
     private static final Pattern mobilePattern = Pattern.compile("^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$");
     private static final Pattern emailPattern = Pattern.compile("^s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
     private static final Pattern ipv4Pattern = Pattern.compile("^((2((5[0-5])|([0-4]\\d)))|([0-1]?\\d{1,2}))(\\.((2((5[0-5])|([0-4]\\d)))|([0-1]?\\d{1,2}))){3}$ ");
+    private static final Pattern domainPattern = Pattern.compile("^(?=^.{3,255}$)[a-z0-9][-a-z0-9]{0,62}(\\.[a-z0-9][-a-z0-9]{0,62})+$");
+    private static final Pattern urlPattern = Pattern.compile("(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
     private static final Pattern passwordPattern = Pattern.compile("^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{10,32}$");
 
     public static boolean matchMobile(String str) {
@@ -24,6 +28,14 @@ public class StringRegexp {
         return ipv4Pattern.matcher(str).matches();
     }
 
+    public static boolean matchDomain(String str) {
+        return domainPattern.matcher(str).matches();
+    }
+
+    public static boolean matchUrl(String str) {
+        return urlPattern.matcher(str).matches();
+    }
+
     /**
      * 密码正则表达式, 密码必须包含大写字母, 数字, 特殊字符(四种里至少三种,且至少 10 个字符)
      *

+ 16 - 9
message/message-service/src/main/java/cn/reghao/tnb/message/app/controller/AdminMessageController.java

@@ -1,11 +1,14 @@
 package cn.reghao.tnb.message.app.controller;
 
+import cn.reghao.jutil.jdk.result.Result;
 import cn.reghao.jutil.web.WebResult;
-import cn.reghao.tnb.message.app.model.dto.EmailDto;
+import cn.reghao.tnb.message.app.model.po.EmailAccount;
+import cn.reghao.tnb.message.app.model.po.Webhook;
 import cn.reghao.tnb.message.app.service.EmailAccountService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.springframework.http.MediaType;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -26,39 +29,43 @@ public class AdminMessageController {
 
     @Operation(summary = "添加邮件帐号", description = "N")
     @PostMapping(value = "/notify/email", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String addEmailAccount() {
-        return WebResult.success();
+    public String addEmailAccount(@RequestBody @Validated EmailAccount emailAccount) {
+        Result result = emailAccountService.addEmailAccount(emailAccount);
+        return WebResult.result(result);
     }
 
     @Operation(summary = "删除邮件帐号", description = "N")
     @DeleteMapping(value = "/notify/email", produces = MediaType.APPLICATION_JSON_VALUE)
     public String deleteEmailAccount() {
-        return WebResult.success();
+        Result result = emailAccountService.deleteEmailAccount("");
+        return WebResult.result(result);
     }
 
     @Operation(summary = "邮件帐号列表", description = "N")
     @GetMapping(value = "/notify/email", produces = MediaType.APPLICATION_JSON_VALUE)
     public String emailAccountList() {
-        List<EmailDto> list = emailAccountService.getEmailAccounts();
+        List<EmailAccount> list = emailAccountService.getEmailAccountList();
         return WebResult.success(list);
     }
 
     @Operation(summary = "添加 webhook", description = "N")
     @PostMapping(value = "/notify/webhook", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String addWebhook() {
-        return WebResult.success();
+    public String addWebhook(@RequestBody @Validated Webhook webhook) {
+        Result result = emailAccountService.addWebhook(webhook);
+        return WebResult.result(result);
     }
 
     @Operation(summary = "删除 webhook", description = "N")
     @DeleteMapping(value = "/notify/webhook", produces = MediaType.APPLICATION_JSON_VALUE)
     public String deleteWebhook() {
-        return WebResult.success();
+        Result result = emailAccountService.deleteWebhook("");
+        return WebResult.result(result);
     }
 
     @Operation(summary = "webhook 列表", description = "N")
     @GetMapping(value = "/notify/webhook", produces = MediaType.APPLICATION_JSON_VALUE)
     public String webhookList() {
-        List<EmailDto> list = emailAccountService.getEmailAccounts();
+        List<Webhook> list = emailAccountService.getWebhookList();
         return WebResult.success(list);
     }
 }

+ 0 - 5
message/message-service/src/main/java/cn/reghao/tnb/message/app/db/mapper/EmailAccountMapper.java

@@ -3,7 +3,6 @@ package cn.reghao.tnb.message.app.db.mapper;
 import cn.reghao.jutil.jdk.db.BaseMapper;
 import cn.reghao.tnb.message.app.model.po.EmailAccount;
 import org.apache.ibatis.annotations.Mapper;
-import org.apache.ibatis.annotations.Param;
 
 /**
  * @author reghao
@@ -11,10 +10,6 @@ import org.apache.ibatis.annotations.Param;
  */
 @Mapper
 public interface EmailAccountMapper extends BaseMapper<EmailAccount> {
-    void updatePassword(@Param("id") int id, @Param("password") String password);
-    void updateDefaultSender(@Param("id") int id, @Param("defaultSender") boolean defaultSender);
     void deleteById(int id);
-    EmailAccount findById(int id);
     EmailAccount findByUsername(String username);
-    EmailAccount findDefaultSender();
 }

+ 1 - 0
message/message-service/src/main/java/cn/reghao/tnb/message/app/db/mapper/WebhookMapper.java

@@ -10,5 +10,6 @@ import org.apache.ibatis.annotations.Mapper;
  */
 @Mapper
 public interface WebhookMapper extends BaseMapper<Webhook> {
+    void deleteByName(String name);
     Webhook findByName(String name);
 }

+ 0 - 28
message/message-service/src/main/java/cn/reghao/tnb/message/app/model/dto/EmailDto.java

@@ -1,28 +0,0 @@
-package cn.reghao.tnb.message.app.model.dto;
-
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-
-import javax.validation.constraints.NotBlank;
-import java.io.Serializable;
-
-/**
- * @author reghao
- * @date 2024-09-25 14:02:40
- */
-@Getter
-@AllArgsConstructor
-public class EmailDto implements Serializable {
-    private static final long serialVersionUID = 1L;
-
-    private Integer id;
-    @NotBlank
-    private String smtp;
-    @NotBlank
-    private String username;
-    @NotBlank
-    private String password;
-    @NotBlank
-    private String personal;
-    private String defaultSender;
-}

+ 6 - 12
message/message-service/src/main/java/cn/reghao/tnb/message/app/model/po/EmailAccount.java

@@ -1,13 +1,13 @@
 package cn.reghao.tnb.message.app.model.po;
 
 import cn.reghao.jutil.jdk.db.BaseObject;
-import cn.reghao.tnb.message.app.model.dto.EmailDto;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
 
 import javax.validation.constraints.NotBlank;
-import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
 
 /**
  * @author reghao
@@ -18,21 +18,15 @@ import javax.validation.constraints.NotNull;
 @Getter
 public class EmailAccount extends BaseObject<Integer> {
     @NotBlank
+    @Pattern(regexp = "^(?=^.{3,255}$)[a-z0-9][-a-z0-9]{0,62}(\\.[a-z0-9][-a-z0-9]{0,62})+$", message = "SMTP 域名格式不正确")
     private String smtp;
     @NotBlank
+    @Pattern(regexp = "^s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$", message = "邮箱格式不正确")
     private String username;
     @NotBlank
+    @Size(min = 8, max = 20, message = "password 长度在 8 ~ 20 个字符之间")
     private String password;
     @NotBlank
+    @Size(min = 3, max = 10, message = "personal 长度在 3 ~ 10 个字符之间")
     private String personal;
-    @NotNull
-    private Boolean defaultSender;
-
-    public EmailAccount(EmailDto emailDto) {
-        this.smtp = emailDto.getSmtp();
-        this.username = emailDto.getUsername();
-        this.password = emailDto.getPassword();
-        this.personal = emailDto.getPersonal();
-        this.defaultSender = false;
-    }
 }

+ 12 - 0
message/message-service/src/main/java/cn/reghao/tnb/message/app/model/po/UserMessage.java

@@ -7,6 +7,9 @@ import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
 
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
 import java.time.LocalDateTime;
 
 /**
@@ -18,12 +21,21 @@ import java.time.LocalDateTime;
 @Setter
 @Getter
 public class UserMessage extends BaseObject<Integer> {
+    @NotNull
     private Integer msgType;
+    @NotNull
     private Long messageId;
+    @NotBlank
+    @Size(min = 1, max = 64)
     private String title;
+    @NotBlank
+    @Size(min = 1, max = 255)
     private String content;
+    @NotNull
     private Boolean unread;
+    @NotNull
     private Long userId;
+    @NotNull
     private LocalDateTime createAt;
 
     public UserMessage(Long messageId, UserMessageDto userMessageDto) {

+ 7 - 0
message/message-service/src/main/java/cn/reghao/tnb/message/app/model/po/Webhook.java

@@ -5,6 +5,8 @@ import lombok.Getter;
 import lombok.Setter;
 
 import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
 
 /**
  * @author reghao
@@ -13,8 +15,13 @@ import javax.validation.constraints.NotBlank;
 @Setter
 @Getter
 public class Webhook extends BaseObject<Integer> {
+    @NotBlank
+    @Size(min = 3, max = 10, message = "name 长度在 3 ~ 10 个字符之间")
     private String name;
     @NotBlank
+    @Pattern(regexp = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]", message = "url 格式不正确")
     private String url;
+    @NotBlank
+    @Size(min = 1, max = 255, message = "sign 长度应小于 255 个字符")
     private String sign;
 }

+ 43 - 66
message/message-service/src/main/java/cn/reghao/tnb/message/app/service/EmailAccountService.java

@@ -1,15 +1,14 @@
 package cn.reghao.tnb.message.app.service;
 
-import cn.reghao.jutil.jdk.db.PageList;
 import cn.reghao.jutil.jdk.result.Result;
-import cn.reghao.jutil.jdk.string.StringRegexp;
-import cn.reghao.tnb.message.app.model.dto.EmailDto;
+import cn.reghao.tnb.common.util.StringRegexp;
 import cn.reghao.tnb.message.app.db.mapper.EmailAccountMapper;
+import cn.reghao.tnb.message.app.db.mapper.WebhookMapper;
 import cn.reghao.tnb.message.app.model.po.EmailAccount;
+import cn.reghao.tnb.message.app.model.po.Webhook;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
-import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -17,106 +16,84 @@ import java.util.stream.Collectors;
  */
 @Service
 public class EmailAccountService {
+    private final int maxSize = 10;
     private final EmailAccountMapper emailAccountMapper;
+    private final WebhookMapper webhookMapper;
 
-    public EmailAccountService(EmailAccountMapper emailAccountMapper) {
+    public EmailAccountService(EmailAccountMapper emailAccountMapper, WebhookMapper webhookMapper) {
         this.emailAccountMapper = emailAccountMapper;
+        this.webhookMapper = webhookMapper;
     }
 
-    public Result addEmailAccount(EmailDto emailDto) {
-        String email = emailDto.getUsername();
+    public Result addEmailAccount(EmailAccount emailAccount) {
+        if (emailAccountMapper.findAll().size() > maxSize) {
+            String errMsg = String.format("可使用的邮箱数量不能超过 %s 个, 请删除一些邮箱后再尝试添加", maxSize);
+            return Result.fail(errMsg);
+        }
+
+        String email = emailAccount.getUsername();
         boolean matched = StringRegexp.matchEmail(email);
         if (!matched) {
-            return Result.fail("邮箱地址格式不正确, 请检查");
+            return Result.fail("邮箱格式不正确");
         }
 
-        EmailAccount emailAccount = emailAccountMapper.findByUsername(email);
-        if (emailAccount != null) {
+        EmailAccount emailAccount1 = emailAccountMapper.findByUsername(email);
+        if (emailAccount1 != null) {
             String errMsg = String.format("邮箱 %s 已存在", email);
             return Result.fail(errMsg);
         }
-
-        emailAccount = new EmailAccount(emailDto);
-        int total = emailAccountMapper.count();
-        if (total == 0) {
-            emailAccount.setDefaultSender(true);
-        }
-
         emailAccountMapper.save(emailAccount);
         return Result.success();
     }
 
-    public Result updatePassword(int emailId, String password) {
-        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
+    public Result deleteEmailAccount(String username) {
+        EmailAccount emailAccount = emailAccountMapper.findByUsername(username);
         if (emailAccount == null) {
             String errMsg = String.format("邮箱不存在");
             return Result.fail(errMsg);
         }
 
-        emailAccountMapper.updatePassword(emailId, password);
+        emailAccountMapper.deleteById(emailAccount.getId());
         return Result.success();
     }
 
-    public Result updateDefaultSender(int emailId) {
-        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
-        if (emailAccount == null) {
-            String errMsg = String.format("邮箱不存在");
-            return Result.fail(errMsg);
-        }
-
-        if (!emailAccount.getDefaultSender()) {
-            EmailAccount emailAccount1 = emailAccountMapper.findDefaultSender();
-            int emailId1 = emailAccount1.getId();
-
-            emailAccountMapper.updateDefaultSender(emailId, true);
-            emailAccountMapper.updateDefaultSender(emailId1, false);
-        }
+    public EmailAccount getEmailSender() {
+        String username = "";
+        EmailAccount emailAccount = emailAccountMapper.findByUsername(username);
+        return emailAccount;
+    }
 
-        return Result.success();
+    public List<EmailAccount> getEmailAccountList() {
+        return emailAccountMapper.findAll();
     }
 
-    public Result deleteEmailAccount(int emailId) {
-        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
-        if (emailAccount == null) {
-            String errMsg = String.format("邮箱不存在");
+    public Result addWebhook(Webhook webhook) {
+        if (webhookMapper.findAll().size() > maxSize) {
+            String errMsg = String.format("可使用的 webhook 数量不能超过 %s 个, 请删除一些 webhook 后再尝试添加", maxSize);
             return Result.fail(errMsg);
         }
 
-        if (emailAccount.getDefaultSender()) {
-            String errMsg = String.format("不能删除默认发送邮箱");
+        String name = webhook.getName();
+        Webhook webhook1 = webhookMapper.findByName(name);
+        if (webhook1 != null) {
+            String errMsg = String.format("webhook %s 已存在", name);
             return Result.fail(errMsg);
         }
-
-        emailAccountMapper.deleteById(emailId);
+        webhookMapper.save(webhook);
         return Result.success();
     }
 
-    public EmailDto getEmailAccount(int emailId) {
-        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
-        return getEmailDto(emailAccount);
-    }
-
-    public EmailDto getEmailSender() {
-        EmailAccount emailAccount = emailAccountMapper.findDefaultSender();
-        if (emailAccount == null) {
-            return null;
-        }
-
-        return getEmailDto(emailAccount);
+    public Result deleteWebhook(String name) {
+        webhookMapper.deleteByName(name);
+        return Result.success();
     }
 
-    private EmailDto getEmailDto(EmailAccount emailAccount) {
-        int id = emailAccount.getId();
-        String smtp = emailAccount.getSmtp();
-        String username = emailAccount.getUsername();
-        String password = emailAccount.getPassword();
-        String personal = emailAccount.getPersonal();
-        String defaultSender = emailAccount.getDefaultSender() ? "YES" : "NO";
-        return new EmailDto(id, smtp, username, password, personal, defaultSender);
+    public Webhook getWebhookReceiver() {
+        String name = "";
+        return webhookMapper.findByName(name);
     }
 
-    public List<EmailDto> getEmailAccounts() {
-        List<EmailDto> list = emailAccountMapper.findAll().stream().map(this::getEmailDto).collect(Collectors.toList());
-        return list;
+    public List<Webhook> getWebhookList() {
+        return webhookMapper.findAll();
     }
 }

+ 7 - 7
message/message-service/src/main/java/cn/reghao/tnb/message/app/service/notifier/email/EmailNotify.java

@@ -1,6 +1,6 @@
 package cn.reghao.tnb.message.app.service.notifier.email;
 
-import cn.reghao.tnb.message.app.model.dto.EmailDto;
+import cn.reghao.tnb.message.app.model.po.EmailAccount;
 import cn.reghao.tnb.message.app.service.EmailAccountService;
 import cn.reghao.tnb.message.app.service.notifier.Notify;
 import lombok.extern.slf4j.Slf4j;
@@ -33,15 +33,15 @@ public class EmailNotify implements Notify<EmailMsg> {
 
     @Override
     public void send(String receiver, EmailMsg msg) throws Exception {
-        EmailDto emailDto = emailAccountService.getEmailSender();
-        if (emailDto == null) {
+        EmailAccount emailAccount = emailAccountService.getEmailSender();
+        if (emailAccount == null) {
             throw new Exception("没有可用于发送通知的邮箱配置");
         }
 
-        String smtp = emailDto.getSmtp();
-        String myEmail = emailDto.getUsername();
-        String password = emailDto.getPassword();
-        String personal = emailDto.getPersonal();
+        String smtp = emailAccount.getSmtp();
+        String myEmail = emailAccount.getUsername();
+        String password = emailAccount.getPassword();
+        String personal = emailAccount.getPersonal();
 
         Properties props = new Properties();
         props.setProperty("mail.transport.protocol", "smtp");

+ 2 - 27
message/message-service/src/main/resources/mapper/EmailAccountMapper.xml

@@ -4,48 +4,23 @@
 <mapper namespace="cn.reghao.tnb.message.app.db.mapper.EmailAccountMapper">
     <insert id="save">
         insert into message_email_account
-        (`smtp`,`username`,`password`,`personal`,`default_sender`)
+        (`smtp`,`username`,`password`,`personal`)
         values
-        (#{smtp},#{username},#{password},#{personal},#{defaultSender})
+        (#{smtp},#{username},#{password},#{personal})
     </insert>
 
-    <update id="updatePassword">
-        update message_email_account
-        set update_time=now(),`password`=#{password}
-        where id=#{id}
-    </update>
-    <update id="updateDefaultSender">
-        update message_email_account
-        set update_time=now(),default_sender=#{defaultSender}
-        where id=#{id}
-    </update>
-
     <delete id="deleteById">
         delete from message_email_account
         where id=#{id}
     </delete>
 
-    <select id="count" resultType="java.lang.Integer">
-        select count(*)
-        from message_email_account
-    </select>
     <select id="findAll" resultType="cn.reghao.tnb.message.app.model.po.EmailAccount">
         select *
         from message_email_account
     </select>
-    <select id="findById" resultType="cn.reghao.tnb.message.app.model.po.EmailAccount">
-        select *
-        from message_email_account
-        where id=#{id}
-    </select>
     <select id="findByUsername" resultType="cn.reghao.tnb.message.app.model.po.EmailAccount">
         select *
         from message_email_account
         where username=#{username}
     </select>
-    <select id="findDefaultSender" resultType="cn.reghao.tnb.message.app.model.po.EmailAccount">
-        select *
-        from message_email_account
-        where default_sender=1
-    </select>
 </mapper>

+ 9 - 0
message/message-service/src/main/resources/mapper/WebhookMapper.xml

@@ -9,6 +9,15 @@
             (#{name},#{url},#{sign})
     </insert>
 
+    <delete id="deleteByName">
+        delete from message_webhook
+        where `name`=#{name}
+    </delete>
+
+    <select id="findAll" resultType="cn.reghao.tnb.message.app.model.po.Webhook">
+        select *
+        from message_webhook
+    </select>
     <select id="findByName" resultType="cn.reghao.tnb.message.app.model.po.Webhook">
         select *
         from message_webhook