feat(wxwork): refactor recipient handling to use slices instead of structs
This commit is contained in:
@@ -21,10 +21,6 @@ type wxWorkMessageSender interface {
|
|||||||
SendText(request wxmessage.SendTextRequest) (*wxmessage.SendResponse, error)
|
SendText(request wxmessage.SendTextRequest) (*wxmessage.SendResponse, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type wxWorkNotifyRecipients struct {
|
|
||||||
ToUsers []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type wxWorkNotifyService struct {
|
type wxWorkNotifyService struct {
|
||||||
senderFactory func() (wxWorkMessageSender, error)
|
senderFactory func() (wxWorkMessageSender, error)
|
||||||
}
|
}
|
||||||
@@ -51,17 +47,17 @@ func (s *wxWorkNotifyService) SendTextToAssigneeOrDefault(assigneeID int64, titl
|
|||||||
if !s.Enabled() {
|
if !s.Enabled() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
recipients := s.resolveRecipientsByUserIDs([]int64{assigneeID})
|
toUsers := s.resolveToUsersByUserIDs([]int64{assigneeID})
|
||||||
if recipients.empty() {
|
if len(toUsers) == 0 {
|
||||||
recipients = s.defaultRecipients()
|
toUsers = s.defaultToUsers()
|
||||||
}
|
}
|
||||||
if recipients.empty() {
|
if len(toUsers) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return s.sendText(title, body, recipients)
|
return s.sendText(title, body, toUsers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *wxWorkNotifyService) sendText(title, body string, recipients wxWorkNotifyRecipients) error {
|
func (s *wxWorkNotifyService) sendText(title, body string, toUsers []string) error {
|
||||||
if !s.Enabled() {
|
if !s.Enabled() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -76,7 +72,7 @@ func (s *wxWorkNotifyService) sendText(title, body string, recipients wxWorkNoti
|
|||||||
cfg := config.Current().WxWork
|
cfg := config.Current().WxWork
|
||||||
req := wxmessage.SendTextRequest{
|
req := wxmessage.SendTextRequest{
|
||||||
SendRequestCommon: &wxmessage.SendRequestCommon{
|
SendRequestCommon: &wxmessage.SendRequestCommon{
|
||||||
ToUser: strings.Join(recipients.ToUsers, "|"),
|
ToUser: strings.Join(toUsers, "|"),
|
||||||
AgentID: strings.TrimSpace(cfg.AgentID),
|
AgentID: strings.TrimSpace(cfg.AgentID),
|
||||||
Safe: cast.ToInt(cfg.Notify.Safe),
|
Safe: cast.ToInt(cfg.Notify.Safe),
|
||||||
EnableDuplicateCheck: cast.ToInt(cfg.Notify.EnableDuplicateCheck),
|
EnableDuplicateCheck: cast.ToInt(cfg.Notify.EnableDuplicateCheck),
|
||||||
@@ -88,10 +84,10 @@ func (s *wxWorkNotifyService) sendText(title, body string, recipients wxWorkNoti
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *wxWorkNotifyService) resolveRecipientsByUserIDs(userIDs []int64) wxWorkNotifyRecipients {
|
func (s *wxWorkNotifyService) resolveToUsersByUserIDs(userIDs []int64) []string {
|
||||||
userIDs = arrs.Distinct(userIDs)
|
userIDs = arrs.Distinct(userIDs)
|
||||||
if len(userIDs) == 0 {
|
if len(userIDs) == 0 {
|
||||||
return wxWorkNotifyRecipients{}
|
return nil
|
||||||
}
|
}
|
||||||
cfg := config.Current().WxWork
|
cfg := config.Current().WxWork
|
||||||
identities := repositories.UserIdentityRepository.Find(sqls.DB(), sqls.NewCnd().
|
identities := repositories.UserIdentityRepository.Find(sqls.DB(), sqls.NewCnd().
|
||||||
@@ -100,21 +96,18 @@ func (s *wxWorkNotifyService) resolveRecipientsByUserIDs(userIDs []int64) wxWork
|
|||||||
Eq("status", enums.StatusOk).
|
Eq("status", enums.StatusOk).
|
||||||
In("user_id", userIDs).
|
In("user_id", userIDs).
|
||||||
Asc("id"))
|
Asc("id"))
|
||||||
recipients := wxWorkNotifyRecipients{}
|
toUsers := make([]string, 0, len(identities))
|
||||||
for i := range identities {
|
for i := range identities {
|
||||||
if receiver := strings.TrimSpace(identities[i].ProviderUserID); receiver != "" {
|
if receiver := strings.TrimSpace(identities[i].ProviderUserID); receiver != "" {
|
||||||
recipients.ToUsers = append(recipients.ToUsers, receiver)
|
toUsers = append(toUsers, receiver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
recipients.ToUsers = arrs.Distinct(recipients.ToUsers)
|
return arrs.Distinct(toUsers)
|
||||||
return recipients
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *wxWorkNotifyService) defaultRecipients() wxWorkNotifyRecipients {
|
func (s *wxWorkNotifyService) defaultToUsers() []string {
|
||||||
cfg := config.Current().WxWork.Notify
|
cfg := config.Current().WxWork.Notify
|
||||||
return wxWorkNotifyRecipients{
|
return arrs.Distinct(cfg.ToUsers)
|
||||||
ToUsers: arrs.Distinct(cfg.ToUsers),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *wxWorkNotifyService) buildTextContent(title, body string) string {
|
func (s *wxWorkNotifyService) buildTextContent(title, body string) string {
|
||||||
@@ -142,10 +135,6 @@ func (s *wxWorkNotifyService) normalizeDuplicateCheckInterval(value int) int {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r wxWorkNotifyRecipients) empty() bool {
|
|
||||||
return len(r.ToUsers) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func truncateRunes(value string, max int) string {
|
func truncateRunes(value string, max int) string {
|
||||||
if max <= 0 {
|
if max <= 0 {
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ func TestWxWorkNotifyDefaultRecipients(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
svc := newWxWorkNotifyService()
|
svc := newWxWorkNotifyService()
|
||||||
recipients := svc.defaultRecipients()
|
toUsers := svc.defaultToUsers()
|
||||||
if len(recipients.ToUsers) != 2 || recipients.ToUsers[0] != "user_a" || recipients.ToUsers[1] != "user_b" {
|
if len(toUsers) != 2 || toUsers[0] != "user_a" || toUsers[1] != "user_b" {
|
||||||
t.Fatalf("unexpected users: %#v", recipients.ToUsers)
|
t.Fatalf("unexpected users: %#v", toUsers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user