diff --git a/common/cassandra/config.go b/common/cassandra/config.go index 4c3da56..6091ce5 100644 --- a/common/cassandra/config.go +++ b/common/cassandra/config.go @@ -5,9 +5,9 @@ Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved package cassandra type Config struct { - Host string `json:"host" required:"true"` - User string `json:"user" required:"true"` - Pwd string `json:"pwd" required:"true"` - Name string `json:"name" required:"true"` - Port int `json:"port" required:"true"` + Host string `json:"host" required:"true"` + User string `json:"user" required:"true"` + Password string `json:"pwd" required:"true"` + Name string `json:"name" required:"true"` + Port int `json:"port" required:"true"` } diff --git a/common/cassandra/db.go b/common/cassandra/db.go index cc8889e..7955e91 100644 --- a/common/cassandra/db.go +++ b/common/cassandra/db.go @@ -20,7 +20,7 @@ func Init(cfg *Config) error { cluster.Port = cfg.Port cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.User, - Password: cfg.Pwd, + Password: cfg.Password, } sessionInstance, err := cluster.CreateSession() if err != nil { diff --git a/common/user/user.go b/common/user/user.go index a415237..29ca81e 100644 --- a/common/user/user.go +++ b/common/user/user.go @@ -14,6 +14,7 @@ import ( "regexp" "github.com/gin-gonic/gin" + "github.com/opensourceways/message-manager/common/postgresql" "github.com/sirupsen/logrus" "golang.org/x/xerrors" ) @@ -92,7 +93,7 @@ func getManagerToken(appId string, appSecret string) (string, error) { return data.ManagerToken, nil } -func GetEulerUserName(ctx *gin.Context) (string, error) { +func GetSystemUserName(ctx *gin.Context) (string, error) { token := ctx.Request.Header.Get("token") YGCookie, err := extractYGCookie(ctx.Request.Header.Get("Cookie")) if err != nil { @@ -151,3 +152,12 @@ func fetchUserName(managerToken, userToken, YGCookie string) (string, error) { } return data.UserName, nil } + +func GetThirdUserName(userName string) (string, error) { + var thirdUsername string + query := `select gitee_user_name from recipient_config where user_id = ?` + if result := postgresql.DB().Raw(query, userName).Scan(&thirdUsername); result.Error != nil { + return "", result.Error + } + return thirdUsername, nil +} diff --git a/go.mod b/go.mod index 7af79fa..5f83746 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 gorm.io/datatypes v1.2.0 gorm.io/driver/postgres v1.5.7 - gorm.io/gorm v1.25.10 + gorm.io/gorm v1.25.12 k8s.io/apimachinery v0.29.3 sigs.k8s.io/yaml v1.4.0 ) @@ -28,6 +28,7 @@ require ( github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dmitryburov/gorm-paginator v0.9.6 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect @@ -67,9 +68,9 @@ require ( golang.org/x/arch v0.8.0 // indirect golang.org/x/crypto v0.24.0 // indirect golang.org/x/net v0.26.0 // indirect - golang.org/x/sync v0.7.0 // indirect + golang.org/x/sync v0.10.0 // indirect golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/text v0.21.0 // indirect golang.org/x/tools v0.22.0 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect diff --git a/go.sum b/go.sum index daf0fb5..7208a0f 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dmitryburov/gorm-paginator v0.9.6 h1:ymzpXzsFH1I6CzwzKhSW/E3TJToOWIuBOU0GU5JxIgE= +github.com/dmitryburov/gorm-paginator v0.9.6/go.mod h1:6FEjK8jxnb8OLBjDZSfsAr6RhlKKyaIarsvHponvwAM= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= @@ -41,6 +43,7 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao= github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= @@ -72,6 +75,7 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= @@ -168,6 +172,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -187,6 +193,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -208,6 +216,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/datatypes v1.2.0 h1:5YT+eokWdIxhJgWHdrb2zYUimyk0+TaFth+7a0ybzco= gorm.io/datatypes v1.2.0/go.mod h1:o1dh0ZvjIjhH/bngTpypG6lVRJ5chTBxE09FH/71k04= +gorm.io/driver/mysql v1.0.3/go.mod h1:twGxftLBlFgNVNakL7F+P/x9oYqoymG3YYT8cAfI9oI= gorm.io/driver/mysql v1.4.7 h1:rY46lkCspzGHn7+IYsNpSfEv9tA+SU4SkkB+GFX125Y= gorm.io/driver/mysql v1.4.7/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc= gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM= @@ -216,9 +225,13 @@ gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0= gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHDnJLig= +gorm.io/gorm v1.20.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= +gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s= gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU= k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/message/app/message.go b/message/app/message.go index e0293e6..e786242 100644 --- a/message/app/message.go +++ b/message/app/message.go @@ -12,36 +12,41 @@ import ( type MessageListAppService interface { CountAllUnReadMessage(userName string) ([]CountDTO, error) - SetMessageIsRead(cmd *CmdToSetIsRead) error - RemoveMessage(cmd *CmdToSetIsRead) error + SetMessageIsRead(userName string, eventId string) error + RemoveMessage(userName string, eventId string) error - GetAllToDoMessage(userName string, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) + GetAllToDoMessage(userName string, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetAllAboutMessage(userName string, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetAllWatchMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) + CountAllMessage(userName string, giteeUsername string) (CountDataDTO, error) + GetForumSystemMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetForumAboutMessage(userName string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) - GetMeetingToDoMessage(userName string, filter int, pageNum, countPerPage int) ( - []MessageListDTO, int64, error) - GetCVEToDoMessage(userName string, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) + GetMeetingToDoMessage(userName string, filter int, pageNum, countPerPage int, + startTime string, isRead *bool) ([]MessageListDTO, int64, error) + GetCVEToDoMessage(userName string, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetCVEMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) - GetIssueToDoMessage(userName string, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) - GetPullRequestToDoMessage(userName string, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) + GetIssueToDoMessage(userName string, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) + GetPullRequestToDoMessage(userName string, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetGiteeAboutMessage(userName string, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetGiteeMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) GetEurMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) + + GetAllMessage(userName string, pageNum, countPerPage int, isRead *bool) ([]MessageListDTO, + int64, error) } func NewMessageListAppService( @@ -63,28 +68,32 @@ func (s *messageListAppService) CountAllUnReadMessage(userName string) ([]CountD } return count, nil } -func (s *messageListAppService) SetMessageIsRead(cmd *CmdToSetIsRead) error { - if err := s.messageListAdapter.SetMessageIsRead(cmd.Source, cmd.EventId); err != nil { + +func (s *messageListAppService) SetMessageIsRead(userName string, eventId string) error { + if err := s.messageListAdapter.SetMessageIsRead(userName, eventId); err != nil { return xerrors.Errorf("set message is_read failed, err:%v", err.Error()) } return nil } -func (s *messageListAppService) RemoveMessage(cmd *CmdToSetIsRead) error { - if err := s.messageListAdapter.RemoveMessage(cmd.Source, cmd.EventId); err != nil { + +func (s *messageListAppService) RemoveMessage(userName string, eventId string) error { + if err := s.messageListAdapter.RemoveMessage(userName, eventId); err != nil { return xerrors.Errorf("set message is_read failed, err:%v", err.Error()) } return nil } + func (s *messageListAppService) GetAllToDoMessage(userName string, giteeUsername string, - isDone bool, pageNum, countPerPage int, startTime string) ( + isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ( []MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetAllToDoMessage(userName, giteeUsername, - isDone, pageNum, countPerPage, startTime) + isDone, pageNum, countPerPage, startTime, isRead) if err != nil { return []MessageListDTO{}, 0, err } return response, count, nil } + func (s *messageListAppService) GetAllAboutMessage(userName string, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetAllAboutMessage(userName, giteeUsername, @@ -94,6 +103,7 @@ func (s *messageListAppService) GetAllAboutMessage(userName string, giteeUsernam } return response, count, nil } + func (s *messageListAppService) GetAllWatchMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetAllWatchMessage(userName, giteeUsername, @@ -103,6 +113,7 @@ func (s *messageListAppService) GetAllWatchMessage(userName string, giteeUsernam } return response, count, nil } + func (s *messageListAppService) GetForumSystemMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetForumSystemMessage(userName, pageNum, @@ -112,6 +123,7 @@ func (s *messageListAppService) GetForumSystemMessage(userName string, pageNum, } return response, count, nil } + func (s *messageListAppService) GetForumAboutMessage(userName string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetForumAboutMessage(userName, isBot, pageNum, @@ -121,24 +133,27 @@ func (s *messageListAppService) GetForumAboutMessage(userName string, isBot *boo } return response, count, nil } + func (s *messageListAppService) GetMeetingToDoMessage(userName string, filter int, pageNum, - countPerPage int) ([]MessageListDTO, int64, error) { + countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetMeetingToDoMessage(userName, filter, - pageNum, countPerPage) + pageNum, countPerPage, startTime, isRead) if err != nil { return []MessageListDTO{}, 0, err } return response, count, nil } + func (s *messageListAppService) GetCVEToDoMessage(userName string, giteeUsername string, - isDone bool, pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) { + isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetCVEToDoMessage(userName, giteeUsername, - isDone, pageNum, countPerPage, startTime) + isDone, pageNum, countPerPage, startTime, isRead) if err != nil { return []MessageListDTO{}, 0, err } return response, count, nil } + func (s *messageListAppService) GetCVEMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetCVEMessage(userName, giteeUsername, pageNum, @@ -148,24 +163,28 @@ func (s *messageListAppService) GetCVEMessage(userName string, giteeUsername str } return response, count, nil } + func (s *messageListAppService) GetIssueToDoMessage(userName string, giteeUsername string, - isDone bool, pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) { + isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetIssueToDoMessage(userName, giteeUsername, - isDone, pageNum, countPerPage, startTime) + isDone, pageNum, countPerPage, startTime, isRead) if err != nil { return []MessageListDTO{}, 0, err } return response, count, nil } + func (s *messageListAppService) GetPullRequestToDoMessage(userName string, giteeUsername string, - isDone bool, pageNum, countPerPage int, startTime string) ([]MessageListDTO, int64, error) { + isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, + int64, error) { response, count, err := s.messageListAdapter.GetPullRequestToDoMessage(userName, - giteeUsername, isDone, pageNum, countPerPage, startTime) + giteeUsername, isDone, pageNum, countPerPage, startTime, isRead) if err != nil { return []MessageListDTO{}, 0, err } return response, count, nil } + func (s *messageListAppService) GetGiteeAboutMessage(userName string, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetGiteeAboutMessage(userName, giteeUsername, @@ -175,6 +194,7 @@ func (s *messageListAppService) GetGiteeAboutMessage(userName string, giteeUsern } return response, count, nil } + func (s *messageListAppService) GetGiteeMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetGiteeMessage(userName, giteeUsername, @@ -184,6 +204,7 @@ func (s *messageListAppService) GetGiteeMessage(userName string, giteeUsername s } return response, count, nil } + func (s *messageListAppService) GetEurMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDTO, int64, error) { response, count, err := s.messageListAdapter.GetEurMessage(userName, pageNum, countPerPage, @@ -193,6 +214,7 @@ func (s *messageListAppService) GetEurMessage(userName string, pageNum, countPer } return response, count, nil } + func (s *messageListAppService) CountAllMessage(userName string, giteeUsername string) (CountDataDTO, error) { data, err := s.messageListAdapter.CountAllMessage(userName, giteeUsername) if err != nil { @@ -200,3 +222,12 @@ func (s *messageListAppService) CountAllMessage(userName string, giteeUsername s } return data, nil } + +func (s *messageListAppService) GetAllMessage(userName string, pageNum, countPerPage int, + isRead *bool) ([]MessageListDTO, int64, error) { + response, count, err := s.messageListAdapter.GetAllMessage(userName, pageNum, countPerPage, isRead) + if err != nil { + return []MessageListDTO{}, 0, err + } + return response, count, nil +} diff --git a/message/app/message_test.go b/message/app/message_test.go index a5f4c8d..ab041e2 100644 --- a/message/app/message_test.go +++ b/message/app/message_test.go @@ -14,59 +14,78 @@ type MockMessageListAdapter struct { mock.Mock } -func (m *MockMessageListAdapter) GetAllToDoMessage(userName, giteeUsername string, isDone bool, pageNum, countPerPage int, startTime string) ([]domain.MessageListDO, int64, error) { +func (m *MockMessageListAdapter) GetAllToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetAllAboutMessage(userName, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetAllWatchMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetForumSystemMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetForumAboutMessage(userName string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } -func (m *MockMessageListAdapter) GetMeetingToDoMessage(userName string, filter int, pageNum, countPerPage int) ([]domain.MessageListDO, int64, error) { +func (m *MockMessageListAdapter) GetMeetingToDoMessage(userName string, filter int, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } -func (m *MockMessageListAdapter) GetCVEToDoMessage(userName, giteeUsername string, isDone bool, pageNum, countPerPage int, startTime string) ([]domain.MessageListDO, int64, error) { +func (m *MockMessageListAdapter) GetCVEToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetCVEMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } -func (m *MockMessageListAdapter) GetIssueToDoMessage(userName, giteeUsername string, isDone bool, pageNum, countPerPage int, startTime string) ([]domain.MessageListDO, int64, error) { +func (m *MockMessageListAdapter) GetIssueToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } -func (m *MockMessageListAdapter) GetPullRequestToDoMessage(userName, giteeUsername string, isDone bool, pageNum, countPerPage int, startTime string) ([]domain.MessageListDO, int64, error) { +func (m *MockMessageListAdapter) GetPullRequestToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetGiteeAboutMessage(userName, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetGiteeMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) GetEurMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } func (m *MockMessageListAdapter) CountAllMessage(username, giteeUsername string) (domain.CountDataDO, error) { + //TODO implement me + panic("implement me") +} + +func (m *MockMessageListAdapter) GetAllMessage(username string, pageNum, countPerPage int, isRead *bool) ([]domain.MessageListDO, int64, error) { + //TODO implement me panic("implement me") } @@ -116,11 +135,11 @@ func TestCountAllUnReadMessage(t *testing.T) { func TestSetMessageIsRead(t *testing.T) { mockAdapter := new(MockMessageListAdapter) service := NewMessageListAppService(mockAdapter) + userName := "testUser" + eventId := "event1" + mockAdapter.On("SetMessageIsRead", userName, eventId).Return(nil) - cmd := CmdToSetIsRead{Source: "test_source", EventId: "event1"} - mockAdapter.On("SetMessageIsRead", cmd.Source, cmd.EventId).Return(nil) - - err := service.SetMessageIsRead(&cmd) + err := service.SetMessageIsRead(userName, eventId) assert.NoError(t, err) mockAdapter.AssertExpectations(t) @@ -130,10 +149,11 @@ func TestSetMessageIsRead_Error(t *testing.T) { mockAdapter := new(MockMessageListAdapter) service := NewMessageListAppService(mockAdapter) - cmd := CmdToSetIsRead{Source: "test_source", EventId: "event1"} - mockAdapter.On("SetMessageIsRead", cmd.Source, cmd.EventId).Return(xerrors.New("error")) + userName := "testUser" + eventId := "event1" + mockAdapter.On("SetMessageIsRead", userName, eventId).Return(xerrors.New("error")) - err := service.SetMessageIsRead(&cmd) + err := service.SetMessageIsRead(userName, eventId) assert.Error(t, err) assert.Contains(t, err.Error(), "set message is_read failed") @@ -143,10 +163,11 @@ func TestRemoveMessage(t *testing.T) { mockAdapter := new(MockMessageListAdapter) service := NewMessageListAppService(mockAdapter) - cmd := CmdToSetIsRead{Source: "test_source", EventId: "event1"} - mockAdapter.On("RemoveMessage", cmd.Source, cmd.EventId).Return(nil) + userName := "testUser" + eventId := "event1" + mockAdapter.On("RemoveMessage", userName, eventId).Return(nil) - err := service.RemoveMessage(&cmd) + err := service.RemoveMessage(userName, eventId) assert.NoError(t, err) mockAdapter.AssertExpectations(t) @@ -156,10 +177,11 @@ func TestRemoveMessage_Error(t *testing.T) { mockAdapter := new(MockMessageListAdapter) service := NewMessageListAppService(mockAdapter) - cmd := CmdToSetIsRead{Source: "test_source", EventId: "event1"} - mockAdapter.On("RemoveMessage", cmd.Source, cmd.EventId).Return(xerrors.New("error")) + userName := "testUser" + eventId := "event1" + mockAdapter.On("RemoveMessage", userName, eventId).Return(xerrors.New("error")) - err := service.RemoveMessage(&cmd) + err := service.RemoveMessage(userName, eventId) assert.Error(t, err) assert.Contains(t, err.Error(), "set message is_read failed") diff --git a/message/controller/message.go b/message/controller/message.go index 9f14a63..17a814f 100644 --- a/message/controller/message.go +++ b/message/controller/message.go @@ -24,6 +24,7 @@ func AddRouterForMessageListController( } v1 := r.Group("/message_center") + // basic v1.GET("/inner/count", ctl.CountAllUnReadMessage) v1.PUT("/inner", ctl.SetMessageIsRead) v1.DELETE("/inner", ctl.RemoveMessage) @@ -44,6 +45,9 @@ func AddRouterForMessageListController( v1.GET("/inner/gitee/about", ctl.GetGiteeAboutMessage) v1.GET("/inner/gitee", ctl.GetGiteeMessage) v1.GET("/inner/eur", ctl.GetEurMessage) + + // ubmc + v1.GET("/all", ctl.GetAllMessage) } type messageListController struct { @@ -61,7 +65,7 @@ type messageListController struct { // @Router /message_center/inner/count [get] // @Id countAllUnReadMessage func (ctl *messageListController) CountAllUnReadMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -78,7 +82,7 @@ func (ctl *messageListController) CountAllUnReadMessage(ctx *gin.Context) { // @Summary SetMessageIsRead // @Description set message read // @Tags message_center -// @Param body body messageStatus true "messageStatus" +// @Param eventId body []string true "eventId" // @Accept json // @Success 202 string accepted 设置已读成功 // @Failure 400 string bad_request 无法解析请求正文 @@ -86,19 +90,18 @@ func (ctl *messageListController) CountAllUnReadMessage(ctx *gin.Context) { // @Router /message_center/inner [put] // @Id setMessageIsRead func (ctl *messageListController) SetMessageIsRead(ctx *gin.Context) { - var messages []messageStatus + var messages []string if err := ctx.BindJSON(&messages); err != nil { ctx.JSON(http.StatusBadRequest, "无法解析请求正文") return } - for _, msg := range messages { - cmd, err := msg.toCmd() - if err != nil { - commonctl.SendBadRequestParam(ctx, xerrors.Errorf("failed to convert req to cmd, %v", - err)) - return - } - if err := ctl.appService.SetMessageIsRead(&cmd); err != nil { + userName, err := user.GetSystemUserName(ctx) + if err != nil { + commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) + return + } + for _, eventId := range messages { + if err := ctl.appService.SetMessageIsRead(userName, eventId); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf( "设置已读失败,err:%v", err)}) return @@ -111,7 +114,7 @@ func (ctl *messageListController) SetMessageIsRead(ctx *gin.Context) { // @Summary RemoveMessage // @Description remove message // @Tags message_center -// @Param body body messageStatus true "messageStatus" +// @Param eventId body []string true "eventId" // @Accept json // @Success 202 string accepted 消息删除成功 // @Failure 400 string bad_request 无法解析请求正文 @@ -119,19 +122,19 @@ func (ctl *messageListController) SetMessageIsRead(ctx *gin.Context) { // @Router /message_center/inner [delete] // @Id removeMessage func (ctl *messageListController) RemoveMessage(ctx *gin.Context) { - var messages []messageStatus + var messages []string + if err := ctx.BindJSON(&messages); err != nil { commonctl.SendBadRequestParam(ctx, xerrors.Errorf("无法解析请求正文")) return } - for _, msg := range messages { - cmd, err := msg.toCmd() - if err != nil { - commonctl.SendBadRequestParam(ctx, xerrors.Errorf("failed to convert req to cmd, %v", - err)) - return - } - if err := ctl.appService.RemoveMessage(&cmd); err != nil { + userName, err := user.GetSystemUserName(ctx) + if err != nil { + commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) + return + } + for _, eventId := range messages { + if err := ctl.appService.RemoveMessage(userName, eventId); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("消息删除失败,"+ "err:%v", err)}) return @@ -139,8 +142,20 @@ func (ctl *messageListController) RemoveMessage(ctx *gin.Context) { } ctx.JSON(http.StatusAccepted, gin.H{"message": "消息删除成功"}) } + +// GetForumSystemMessage get form system message +// @Summary GetForumSystemMessage +// @Description get forum system message 获取论坛系统通知消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/forum/system [get] +// @Id getForumSystemMessage func (ctl *messageListController) GetForumSystemMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -157,8 +172,20 @@ func (ctl *messageListController) GetForumSystemMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetForumAboutMessage get form about message +// @Summary GetForumAboutMessage +// @Description get forum about message 获取论坛提到我的消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/forum/about [get] +// @Id getForumAboutMessage func (ctl *messageListController) GetForumAboutMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -175,8 +202,20 @@ func (ctl *messageListController) GetForumAboutMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetMeetingToDoMessage get meeting to do message +// @Summary GetMeetingToDoMessage +// @Description get meeting to do message 获取待参加的会议消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/meeting/todo [get] +// @Id getMeetingToDoMessage func (ctl *messageListController) GetMeetingToDoMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -187,14 +226,26 @@ func (ctl *messageListController) GetMeetingToDoMessage(ctx *gin.Context) { return } if data, count, err := ctl.appService.GetMeetingToDoMessage(userName, params.Filter, - params.PageNum, params.CountPerPage); err != nil { + params.PageNum, params.CountPerPage, params.StartTime, params.IsRead); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) } else { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetCVEToDoMessage get cve to do message +// @Summary GetCVEToDoMessage +// @Description get cve to do message 获取待我处理的漏洞消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/cve/todo [get] +// @Id getCVEToDoMessage func (ctl *messageListController) GetCVEToDoMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -204,15 +255,28 @@ func (ctl *messageListController) GetCVEToDoMessage(ctx *gin.Context) { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + if data, count, err := ctl.appService.GetCVEToDoMessage(userName, params.GiteeUserName, - params.IsDone, params.PageNum, params.CountPerPage, params.StartTime); err != nil { + params.IsDone, params.PageNum, params.CountPerPage, params.StartTime, params.IsRead); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) } else { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetCVEMessage get cve message +// @Summary GetCVEMessage +// @Description get cve message 获取漏洞关注消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/cve [get] +// @Id getCVEMessage func (ctl *messageListController) GetCVEMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -222,6 +286,7 @@ func (ctl *messageListController) GetCVEMessage(ctx *gin.Context) { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + if data, count, err := ctl.appService.GetCVEMessage(userName, params.GiteeUserName, params.PageNum, params.CountPerPage, params.StartTime, params.IsRead); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) @@ -229,8 +294,20 @@ func (ctl *messageListController) GetCVEMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetIssueToDoMessage get issue to do message +// @Summary GetIssueToDoMessage +// @Description get issue to do message 获取待我处理的issue +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/issue/todo [get] +// @Id getIssueToDoMessage func (ctl *messageListController) GetIssueToDoMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -240,15 +317,28 @@ func (ctl *messageListController) GetIssueToDoMessage(ctx *gin.Context) { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + if data, count, err := ctl.appService.GetIssueToDoMessage(userName, params.GiteeUserName, - params.IsDone, params.PageNum, params.CountPerPage, params.StartTime); err != nil { + params.IsDone, params.PageNum, params.CountPerPage, params.StartTime, params.IsRead); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) } else { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetPullRequestToDoMessage get pull request to do message +// @Summary GetPullRequestToDoMessage +// @Description get pull request to do message 获取待我处理的pr +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/pull_request/todo [get] +// @Id getPullRequestToDoMessage func (ctl *messageListController) GetPullRequestToDoMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -258,16 +348,29 @@ func (ctl *messageListController) GetPullRequestToDoMessage(ctx *gin.Context) { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + if data, count, err := ctl.appService.GetPullRequestToDoMessage(userName, params.GiteeUserName, params.IsDone, params.PageNum, params.CountPerPage, - params.StartTime); err != nil { + params.StartTime, params.IsRead); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) } else { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetGiteeAboutMessage get gitee about message +// @Summary GetGiteeAboutMessage +// @Description get gitee about message 获取gitee提到我的 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/gitee/about [get] +// @Id getGiteeAboutMessage func (ctl *messageListController) GetGiteeAboutMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -284,8 +387,20 @@ func (ctl *messageListController) GetGiteeAboutMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetGiteeMessage get gitee message +// @Summary GetGiteeMessage +// @Description get gitee message 获取gitee 动态消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/gitee [get] +// @Id getGiteeMessage func (ctl *messageListController) GetGiteeMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -303,8 +418,20 @@ func (ctl *messageListController) GetGiteeMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetEurMessage get eur message +// @Summary GetEurMessage +// @Description get eur message 获取eur关注消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/eur [get] +// @Id getEurMessage func (ctl *messageListController) GetEurMessage(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -321,31 +448,56 @@ func (ctl *messageListController) GetEurMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetAllTodoMessage get alltodo message +// @Summary GetAllTodoMessage +// @Description get all todo message 获取所有待办消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/todo [get] +// @Id getAllTodoMessage func (ctl *messageListController) GetAllTodoMessage(ctx *gin.Context) { var params QueryParams if err := ctx.ShouldBindQuery(¶ms); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return } if data, count, err := ctl.appService.GetAllToDoMessage(userName, params.GiteeUserName, - params.IsDone, params.PageNum, params.CountPerPage, params.StartTime); err != nil { + params.IsDone, params.PageNum, params.CountPerPage, params.StartTime, + params.IsRead); err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) } else { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetAllAboutMessage get all about message +// @Summary GetAllAboutMessage +// @Description get all about message 获取所有提到我的消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/about [get] +// @Id getAllAboutMessage func (ctl *messageListController) GetAllAboutMessage(ctx *gin.Context) { var params QueryParams if err := ctx.ShouldBindQuery(¶ms); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -357,13 +509,25 @@ func (ctl *messageListController) GetAllAboutMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// GetAllWatchMessage get all watch message +// @Summary GetAllWatchMessage +// @Description get all watch message 获取所有关注消息 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/watch [get] +// @Id getAllWatchMessage func (ctl *messageListController) GetAllWatchMessage(ctx *gin.Context) { var params QueryParams if err := ctx.ShouldBindQuery(¶ms); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -375,13 +539,25 @@ func (ctl *messageListController) GetAllWatchMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) } } + +// CountAllMessage count all message +// @Summary CountAllMessage +// @Description count all message 获取所有消息分类数量 +// @Tags message_center_openeuler_summit +// @Param body body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/inner/count_new [get] +// @Id countAllMessage func (ctl *messageListController) CountAllMessage(ctx *gin.Context) { var params QueryParams if err := ctx.ShouldBindQuery(¶ms); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -392,3 +568,32 @@ func (ctl *messageListController) CountAllMessage(ctx *gin.Context) { ctx.JSON(http.StatusAccepted, gin.H{"count": data}) } } + +// GetAllMessage get all message +// @Summary GetAllMessage +// @Description get all message 获取所有消息 +// @Tags message_center_ubmc +// @Param params body QueryParams true "QueryParams" +// @Accept json +// @Success 202 string accepted 查询成功 +// @Failure 400 string bad_request 无法解析请求正文 +// @Failure 500 string system_error 查询失败 +// @Router /message_center/all [get] +// @Id getAllMessage +func (ctl *messageListController) GetAllMessage(ctx *gin.Context) { + var params QueryParams + if err := ctx.ShouldBindQuery(¶ms); err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + userName, err := user.GetSystemUserName(ctx) + if err != nil { + commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) + return + } + if data, count, err := ctl.appService.GetAllMessage(userName, params.PageNum, params.CountPerPage, params.IsRead); err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": xerrors.Errorf("查询失败,err:%v", err)}) + } else { + ctx.JSON(http.StatusAccepted, gin.H{"query_info": data, "count": count}) + } +} diff --git a/message/controller/message_request.go b/message/controller/message_request.go index 6e1ce39..a6b8e29 100644 --- a/message/controller/message_request.go +++ b/message/controller/message_request.go @@ -94,13 +94,11 @@ func (req *queryInnerParamsQuick) toCmd() (cmd app.CmdToGetInnerMessageQuick, er } type messageStatus struct { - Source string `json:"source"` EventId string `json:"event_id"` } func (req *messageStatus) toCmd() (cmd app.CmdToSetIsRead, err error) { cmd.EventId = req.EventId - cmd.Source = req.Source return cmd, nil } @@ -108,7 +106,7 @@ type QueryParams struct { GiteeUserName string `form:"gitee_user_name"` IsBot *bool `form:"is_bot"` Filter int `form:"filter"` - IsDone bool `form:"is_done"` + IsDone *bool `form:"is_done"` PageNum int `form:"page_num"` CountPerPage int `form:"count_per_page"` StartTime string `form:"start_time"` diff --git a/message/controller/message_request_test.go b/message/controller/message_request_test.go index a077d95..5362833 100644 --- a/message/controller/message_request_test.go +++ b/message/controller/message_request_test.go @@ -69,12 +69,9 @@ func TestQueryInnerParamsQuickToCmd(t *testing.T) { func TestMessageStatusToCmd(t *testing.T) { req := &messageStatus{ - Source: "status_source", EventId: "event_123", } - cmd, err := req.toCmd() assert.NoError(t, err) - assert.Equal(t, "status_source", cmd.Source) assert.Equal(t, "event_123", cmd.EventId) } diff --git a/message/controller/push.go b/message/controller/push.go index 0da106a..a3b8ecb 100644 --- a/message/controller/push.go +++ b/message/controller/push.go @@ -48,7 +48,7 @@ type messagePushController struct { func (ctl *messagePushController) GetPushConfig(ctx *gin.Context) { subsIdsStr := ctx.DefaultQuery("subscribe_id", "") subsIds := strings.Split(subsIdsStr, ",") - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return diff --git a/message/controller/recipient.go b/message/controller/recipient.go index c3e2911..9f6be9e 100644 --- a/message/controller/recipient.go +++ b/message/controller/recipient.go @@ -46,7 +46,7 @@ type messageRecipientController struct { // @Router /message_center/config/recipient [get] // @Id getRecipientConfig func (ctl *messageRecipientController) GetRecipientConfig(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -89,7 +89,7 @@ func (ctl *messageRecipientController) AddRecipientConfig(ctx *gin.Context) { commonctl.SendBadRequestParam(ctx, xerrors.Errorf("failed to convert req to cmd, %w", err)) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -125,7 +125,7 @@ func (ctl *messageRecipientController) UpdateRecipientConfig(ctx *gin.Context) { commonctl.SendBadRequestParam(ctx, xerrors.Errorf("failed to convert req to cmd, %w", err)) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -161,7 +161,7 @@ func (ctl *messageRecipientController) RemoveRecipientConfig(ctx *gin.Context) { commonctl.SendBadRequestParam(ctx, xerrors.Errorf("failed to convert req to cmd, %w", err)) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return diff --git a/message/controller/subscribe.go b/message/controller/subscribe.go index ccedaa6..c14b58e 100644 --- a/message/controller/subscribe.go +++ b/message/controller/subscribe.go @@ -45,7 +45,7 @@ type messageSubscribeController struct { // @Router /message_center/config/subs/all [get] // @Id getAllSubsConfig func (ctl *messageSubscribeController) GetAllSubsConfig(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -70,7 +70,7 @@ func (ctl *messageSubscribeController) GetAllSubsConfig(ctx *gin.Context) { // @Router /message_center/config/subs [get] // @Id getSubsConfig func (ctl *messageSubscribeController) GetSubsConfig(ctx *gin.Context) { - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -108,7 +108,7 @@ func (ctl *messageSubscribeController) AddSubsConfig(ctx *gin.Context) { xerrors.Errorf("failed to convert req to cmd, %w", err)) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -146,7 +146,7 @@ func (ctl *messageSubscribeController) UpdateSubsConfig(ctx *gin.Context) { xerrors.Errorf("failed to convert req to cmd, %w", err)) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return @@ -183,7 +183,7 @@ func (ctl *messageSubscribeController) RemoveSubsConfig(ctx *gin.Context) { xerrors.Errorf("failed to convert req to cmd, %w", err)) return } - userName, err := user.GetEulerUserName(ctx) + userName, err := user.GetSystemUserName(ctx) if err != nil { commonctl.SendUnauthorized(ctx, xerrors.Errorf("get username failed, err:%v", err)) return diff --git a/message/domain/message.go b/message/domain/message.go index aee6a21..dbef9d7 100644 --- a/message/domain/message.go +++ b/message/domain/message.go @@ -6,28 +6,30 @@ package domain type MessageListAdapter interface { CountAllUnReadMessage(userName string) ([]CountDO, error) - SetMessageIsRead(source, eventId string) error - RemoveMessage(source, eventId string) error - GetAllToDoMessage(userName, giteeUsername string, isDone bool, pageNum, - countPerPage int, startTime string) ([]MessageListDO, int64, error) + SetMessageIsRead(userName string, eventId string) error + RemoveMessage(userName string, eventId string) error + + GetAllToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, + countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetAllAboutMessage(userName, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetAllWatchMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) + GetForumSystemMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetForumAboutMessage(userName string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetMeetingToDoMessage(userName string, filter int, pageNum, - countPerPage int) ([]MessageListDO, int64, error) - GetCVEToDoMessage(userName, giteeUsername string, isDone bool, pageNum, - countPerPage int, startTime string) ([]MessageListDO, int64, error) + countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) + GetCVEToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, + countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetCVEMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) - GetIssueToDoMessage(userName, giteeUsername string, isDone bool, pageNum, - countPerPage int, startTime string) ([]MessageListDO, int64, error) - GetPullRequestToDoMessage(userName, giteeUsername string, isDone bool, pageNum, - countPerPage int, startTime string) ([]MessageListDO, int64, error) + GetIssueToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, + countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) + GetPullRequestToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, + countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetGiteeAboutMessage(userName, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) GetGiteeMessage(userName, giteeUsername string, pageNum, countPerPage int, @@ -35,4 +37,6 @@ type MessageListAdapter interface { GetEurMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDO, int64, error) CountAllMessage(username, giteeUsername string) (CountDataDO, error) + GetAllMessage(username string, pageNum, countPerPage int, isRead *bool) ([]MessageListDO, + int64, error) } diff --git a/message/infrastructure/dao.go b/message/infrastructure/dao.go index 531f459..15ecc1b 100644 --- a/message/infrastructure/dao.go +++ b/message/infrastructure/dao.go @@ -26,6 +26,7 @@ type MessageListDAO struct { UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" swaggerignore:"true"` IsRead bool `gorm:"column:is_read" json:"is_read"` SourceGroup string `gorm:"column:source_group" json:"source_group"` + TotalCount int64 `json:"total_count"` } type MessagePushDAO struct { @@ -130,7 +131,6 @@ type CmdToGetInnerMessage struct { } type CmdToSetIsRead struct { - Source string `json:"source"` EventId string `json:"event_id"` } diff --git a/message/infrastructure/message.go b/message/infrastructure/message.go index 0f5213e..e65b5c1 100644 --- a/message/infrastructure/message.go +++ b/message/infrastructure/message.go @@ -7,10 +7,12 @@ package infrastructure import ( "fmt" - "github.com/opensourceways/message-manager/common/postgresql" - "github.com/opensourceways/message-manager/utils" "github.com/sirupsen/logrus" "golang.org/x/xerrors" + + "github.com/opensourceways/message-manager/common/postgresql" + "github.com/opensourceways/message-manager/common/user" + "github.com/opensourceways/message-manager/utils" ) func MessageListAdapter() *messageAdapter { @@ -21,366 +23,525 @@ type messageAdapter struct{} func (s *messageAdapter) CountAllUnReadMessage(userName string) ([]CountDAO, error) { var CountData []CountDAO - sqlCount := `SELECT inner_message.source, COUNT(*) FROM message_center.inner_message - JOIN message_center.cloud_event_message ON inner_message.event_id = cloud_event_message.event_id - AND inner_message.source = cloud_event_message.source - JOIN message_center.recipient_config ON - cast(inner_message.recipient_id AS BIGINT) = recipient_config.id - WHERE is_read = ? AND recipient_config.user_id = ? - AND inner_message.is_deleted = ? - AND recipient_config.is_deleted = ? - GROUP BY inner_message.source` - if result := postgresql.DB().Raw(sqlCount, false, userName, false, false). + query := `SELECT source, SUM(count) AS count +FROM ( + SELECT cem.source, COUNT(*) AS count + FROM message_center.cloud_event_message cem + JOIN message_center.follow_message fm ON cem.event_id = fm.event_id + JOIN message_center.recipient_config rc ON fm.recipient_id = rc.id + WHERE fm.is_read = false + AND fm.is_deleted = false + AND rc.user_id = ? -- 替换为实际的用户 ID + GROUP BY cem.source + + UNION ALL + + SELECT cem.source, COUNT(*) AS count + FROM message_center.cloud_event_message cem + JOIN message_center.related_message rm ON cem.event_id = rm.event_id + JOIN message_center.recipient_config rc ON rm.recipient_id = rc.id + WHERE rm.is_read = false + AND rm.is_deleted = false + AND rc.user_id = ? -- 替换为实际的用户 ID + GROUP BY cem.source + + UNION ALL + + SELECT cem.source, COUNT(*) AS count + FROM message_center.cloud_event_message cem + JOIN message_center.todo_message tm ON tm.latest_event_id = cem.event_id + JOIN message_center.recipient_config rc ON tm.recipient_id = rc.id + WHERE tm.is_read = false + AND tm.is_deleted = false + AND rc.user_id = ? -- 替换为实际的用户 ID + GROUP BY cem.source +) AS unread_counts +GROUP BY source` + if result := postgresql.DB().Raw(query, userName, userName, userName). Scan(&CountData); result.Error != nil { return []CountDAO{}, xerrors.Errorf("get count failed, err:%v", result.Error) } return CountData, nil } -func (s *messageAdapter) SetMessageIsRead(source, eventId string) error { - if result := postgresql.DB().Table("message_center.inner_message"). - Where("inner_message.source = ? AND inner_message.event_id = ?", source, - eventId).Where("inner_message.is_deleted = ?", false). - Update("is_read", true); result.Error != nil { + +func (s *messageAdapter) SetMessageIsRead(userName string, eventId string) error { + query := ` + update message_center.follow_message + set is_read = true + where event_id = ? and is_read = false and recipient_id in ( + select id from recipient_config where user_id = ? + ); + + update message_center.related_message + set is_read = true + where event_id = ? and is_read = false and recipient_id in ( + select id from recipient_config where user_id = ? + ); + + update message_center.todo_message + set is_read = true + where latest_event_id = ? and is_read = false and recipient_id in ( + select id from recipient_config where user_id = ? + ); + ` + + if result := postgresql.DB().Exec(query, eventId, userName, eventId, userName, eventId, + userName); result.Error != nil { return xerrors.Errorf("set message is_read failed, err:%v", result.Error.Error()) } return nil } -func (s *messageAdapter) RemoveMessage(source, eventId string) error { - if result := postgresql.DB().Table("message_center.inner_message"). - Where("inner_message.source = ? AND inner_message."+ - "event_id = ?", source, eventId). - Update("is_deleted", true); result.Error != nil { + +func (s *messageAdapter) RemoveMessage(userName string, eventId string) error { + query := ` + UPDATE message_center.follow_message + SET is_deleted = true + WHERE event_id = ? AND is_deleted = false and recipient_id in ( + select id from recipient_config where user_id = ? + ); + + UPDATE message_center.related_message + SET is_deleted = true + WHERE event_id = ? AND is_deleted = false and recipient_id in ( + select id from recipient_config where user_id = ? + ); + + UPDATE message_center.todo_message + SET is_deleted = true + WHERE latest_event_id = ? AND is_deleted = false and recipient_id in ( + select id from recipient_config where user_id = ? + ); + ` + if result := postgresql.DB().Exec(query, eventId, userName, eventId, userName, + eventId, userName); result.Error != nil { return xerrors.Errorf("remove inner message failed, err:%v", result.Error.Error()) } + return nil } -func pagination(messages []MessageListDAO, pageNum, countPerPage int) []MessageListDAO { - if countPerPage == 0 { - return messages +func filterTodoSql(query *string, isDone *bool, isRead *bool, startTime string) { + if isDone != nil { + *query += fmt.Sprintf(` and is_done=%t`, *isDone) } - start := (pageNum - 1) * countPerPage - end := start + countPerPage - if start > len(messages) { - return []MessageListDAO{} + if isRead != nil { + *query += fmt.Sprintf(` and is_read = %t`, *isRead) } - if end > len(messages) { - return messages[start:] + if startTime != "" { + *query += fmt.Sprintf(` and time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) } - return messages[start:end] } -func (s *messageAdapter) GetAllToDoMessage(userName string, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDAO, int64, error) { - var response []MessageListDAO +func filterMeetingTodoSql(query *string, isDone *bool, isRead *bool, startTime string) { + if isDone != nil { + *query += fmt.Sprintf(` and is_done=%t`, *isDone) + } + if isRead != nil { + *query += fmt.Sprintf(` and is_read = %t`, *isRead) + } + if startTime != "" { + *query += fmt.Sprintf(` and time <= '%s' and time >= NOW()`, + *utils.ParseUnixTimestampNew(startTime)) + } +} - issueTodo, issueCount, err := s.GetIssueToDoMessage(userName, giteeUsername, isDone, - 0, 0, startTime) - if err != nil { - return []MessageListDAO{}, 0, err +func filterAboutSql(query *string, isRead *bool, startTime string) { + if startTime != "" { + *query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) } - prTodo, prCount, err := s.GetPullRequestToDoMessage(userName, giteeUsername, isDone, - 0, 0, startTime) - if err != nil { - return []MessageListDAO{}, 0, err - } - cveTodo, cveCount, err := s.GetCVEToDoMessage(userName, giteeUsername, isDone, - 0, 0, startTime) - response = append(response, issueTodo...) - response = append(response, prTodo...) - response = append(response, cveTodo...) - return pagination(response, pageNum, countPerPage), issueCount + prCount + cveCount, nil + if isRead != nil { + *query += fmt.Sprintf(` and rm.is_read = %t`, *isRead) + } +} + +func filterFollowSql(query *string, isRead *bool, startTime string) { + if startTime != "" { + *query += fmt.Sprintf(` and time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) + } + if isRead != nil { + *query += fmt.Sprintf(` and is_read = %t`, *isRead) + } +} + +func (s *messageAdapter) GetAllToDoMessage(userName string, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { + query := `with latest_messages as ( + select + cem.*, + tm.is_read, + tm.is_done, + ROW_NUMBER() OVER (PARTITION BY tm.business_id, tm.recipient_id, cem.type ORDER BY cem.updated_at DESC) AS rn + from + todo_message tm + join + cloud_event_message cem ON cem.event_id = tm.latest_event_id + join + recipient_config rc ON rc.id = tm.recipient_id + where + tm.is_deleted = false + and rc.is_deleted = false + and ((rc.gitee_user_name != '' and rc.gitee_user_name = ?) OR rc.user_id = ?) + and cem.type <> 'meeting' + ) + select *, count(*) over () as total_count + from latest_messages + where rn = 1` + filterTodoSql(&query, isDone, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Debug().Raw(query, giteeUsername, userName, + countPerPage, offset).Scan(&response); result.Error != nil { + return []MessageListDAO{}, 0, xerrors.Errorf("get todo message failed, err:%v", + result.Error) + } + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } func (s *messageAdapter) GetAllAboutMessage(userName string, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { + query := `select cem.*, rm.is_read, count(*) over () as total_count from cloud_event_message cem + join message_center.related_message rm on cem.event_id = rm.event_id + join message_center.recipient_config rc on rm.recipient_id = rc.id + where rm.is_deleted = false + and rc.is_deleted = false + and ( + (cem.type = 'note' and ((rc.gitee_user_name != '' and rc.gitee_user_name = ?) or rc.user_id = ?)` + if isBot != nil { + if *isBot { + query += ` and cem."user" IN ('openeuler-ci-bot','ci-robot','openeuler-sync-bot')` + } else { + query += ` and cem."user" NOT IN ('openeuler-ci-bot','ci-robot','openeuler-sync-bot')` + } + } + query += `)` + query += `or (cem.source = 'forum' and rc.user_id = ?` + if isBot != nil { + if *isBot { + query += ` and cem.data_json #>> '{Data, OriginalUsername}' = 'system'` + } else { + query += ` and cem.data_json #>> '{Data, OriginalUsername}' <> 'system'` + } + } + query += `))` + filterAboutSql(&query, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` + offset := (pageNum - 1) * countPerPage var response []MessageListDAO - giteeAbout, giteeCount, err := s.GetGiteeAboutMessage(userName, giteeUsername, isBot, - 0, 0, startTime, isRead) - if err != nil { - return []MessageListDAO{}, 0, err + if result := postgresql.DB().Raw(query, giteeUsername, userName, userName, countPerPage, + offset).Scan(&response); result.Error != nil { + return []MessageListDAO{}, 0, xerrors.Errorf("get about message failed, err:%v", + result.Error) } - forumAbout, forumCount, err := s.GetForumAboutMessage(userName, isBot, - 0, 0, startTime, isRead) - if err != nil { - return []MessageListDAO{}, 0, err + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount } - response = append(response, giteeAbout...) - response = append(response, forumAbout...) - return pagination(response, pageNum, countPerPage), giteeCount + forumCount, nil + return response, totalCount, nil } func (s *messageAdapter) GetAllWatchMessage(userName string, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { + query := ` + with filtered_recipient as ( + select * + from recipient_config + where not is_deleted and (user_id = ? or gitee_user_name = ?) + ), + filtered_messages as ( + select fm.is_read, cem.* + from follow_message fm + join cloud_event_message cem on cem.event_id = fm.event_id + join filtered_recipient rc on rc.id = fm.recipient_id + where not fm.is_deleted + ) + select *, count(*) over () as total_count + from filtered_messages + where true` + filterFollowSql(&query, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage var response []MessageListDAO - forumMsg, forumCount, err := s.GetForumSystemMessage(userName, - 0, 0, startTime, isRead) - if err != nil { - return []MessageListDAO{}, 0, err - } - cveMsg, cveCount, err := s.GetCVEMessage(userName, giteeUsername, - 0, 0, startTime, isRead) - if err != nil { - return []MessageListDAO{}, 0, err + if result := postgresql.DB().Debug().Raw(query, userName, giteeUsername, countPerPage, + offset).Scan(&response); result.Error != nil { + logrus.Errorf("get watch message failed, err:%v", result.Error) + return []MessageListDAO{}, 0, xerrors.Errorf("get watch message failed, err:%v", result.Error) } - giteeMsg, giteeCount, err := s.GetGiteeMessage(userName, giteeUsername, - 0, 0, startTime, isRead) - if err != nil { - return []MessageListDAO{}, 0, err + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount } - eurMsg, eurCount, err := s.GetEurMessage(userName, 0, 0, startTime, isRead) - if err != nil { - return []MessageListDAO{}, 0, err - } - response = append(response, forumMsg...) - response = append(response, cveMsg...) - response = append(response, giteeMsg...) - response = append(response, eurMsg...) - return pagination(response, pageNum, countPerPage), forumCount + cveCount + giteeCount + eurCount, nil + return response, totalCount, nil } func (s *messageAdapter) GetForumSystemMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { - var response []MessageListDAO - query := `select cem.*, im.is_read from message_center.cloud_event_message cem - join message_center.inner_message im on im.event_id = cem.event_id - join message_center.recipient_config rc on rc.id = im.recipient_id - where im.is_deleted = false and rc.is_deleted = false and cem.source = 'forum' - and rc.user_id = ? and cem.type IN ('12','24','37')` - if startTime != "" { - query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - if isRead != nil && *isRead == false { - query += ` and im.is_read = false` - } - query += ` order by time desc` - if result := postgresql.DB().Raw(query, userName).Scan(&response); result.Error != nil { + query := `with filtered_recipient as ( + select * + from recipient_config + where not is_deleted and user_id = ? + ), + filtered_messages as ( + select fm.is_read, cem.* + from follow_message fm + join cloud_event_message cem on cem.event_id = fm.event_id + join filtered_recipient rc on rc.id = fm.recipient_id + where not fm.is_deleted + ) + select *, count(*) over () as total_count + from filtered_messages + where source = 'forum'` + filterFollowSql(&query, isRead, startTime) + + query += ` order by updated_at desc limit ? offset ?` + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, userName, countPerPage, offset). + Scan(&response); result.Error != nil { return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } func (s *messageAdapter) GetForumAboutMessage(userName string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { - var response []MessageListDAO - query := `select cem.*, im.is_read from message_center.cloud_event_message cem - join message_center.inner_message im on im.event_id = cem.event_id - join message_center.recipient_config rc on rc.id = im.recipient_id - where im.is_deleted = false and rc.is_deleted = false and cem.source = 'forum' - and rc.user_id = ? and cem.type NOT IN ('12','24','37') ` + query := `select cem.*, rm.is_read, count(*) over () as total_count + from related_message rm + join cloud_event_message cem on cem.event_id = rm.event_id + join recipient_config rc on rc.id = rm.recipient_id + where rm.is_deleted = false and rc.is_deleted = false + and cem.source = 'forum' and rc.user_id = ?` if isBot != nil { if *isBot { - query += `and jsonb_extract_path_text(cem.data_json::jsonb, - 'Data', 'OriginalUsername') = 'system'` + query += ` and cem.data_json #>> '{Data, OriginalUsername}' = 'system'` } else { - query += `and jsonb_extract_path_text(cem.data_json::jsonb, - 'Data', 'OriginalUsername') <> 'system'` + query += ` and cem.data_json #>> '{Data, OriginalUsername}' <> 'system'` } } - if startTime != "" { - query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - if isRead != nil && *isRead == false { - query += ` and im.is_read = false` - } - query += ` order by time desc` - if result := postgresql.DB().Raw(query, userName).Scan(&response); result.Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + filterAboutSql(&query, isRead, startTime) + query += ` order by time desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } -func (s *messageAdapter) GetMeetingToDoMessage(userName string, filter int, - pageNum, countPerPage int) ([]MessageListDAO, int64, error) { - var response []MessageListDAO - query := `select * - from (select distinct on (cem.source_url) cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - where cem.type = 'meeting' - and im.is_deleted = false and rc.is_deleted = false - and cem.source = 'https://www.openEuler.org/meeting' - and (rc.user_id = ?) - and (cem.data_json #>> '{Action}') <> 'delete_meeting' - order by cem.source_url, updated_at desc) a` +func (s *messageAdapter) GetMeetingToDoMessage(username string, filter int, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { + query := `select a.*, count(*) over () as total_count + from ( + select distinct on (tm.business_id, tm.recipient_id) tm.is_read, cem.* + from todo_message tm + join cloud_event_message cem ON cem.event_id = tm.latest_event_id + join recipient_config rc ON rc.id = tm.recipient_id + where rc.is_deleted = false + and tm.is_deleted = false + and cem.type = 'meeting' + and (rc.gitee_user_name != '' and rc.gitee_user_name = ?) + order by tm.business_id, tm.recipient_id, cem.updated_at desc + ) as a where true` if filter == 1 { - query += ` where NOW() <= to_timestamp(a.data_json ->> 'MeetingEndTime', -'YYYY-MM-DDHH24:MI')` + query += ` and NOW() <= time` } else if filter == 2 { - query += ` where NOW() > to_timestamp(a.data_json ->> 'MeetingEndTime', -'YYYY-MM-DDHH24:MI')` - } else { + query += ` and NOW() > time` } - query += ` order by updated_at` - if result := postgresql.DB().Debug().Raw(query, userName).Scan(&response); result.Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + filterMeetingTodoSql(&query, nil, isRead, startTime) + query += ` order by time limit ? offset ?` + giteeUsername, err := user.GetThirdUserName(username) + if err != nil { + return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", + xerrors.Errorf("get gitee username failed, err:%v", err)) + } + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, giteeUsername, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } -func (s *messageAdapter) GetCVEToDoMessage(userName, giteeUsername string, isDone bool, pageNum, - countPerPage int, startTime string) ([]MessageListDAO, int64, error) { - var response []MessageListDAO +func (s *messageAdapter) GetCVEToDoMessage(userName, giteeUsername string, isDone *bool, pageNum, + countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { if giteeUsername == "" { return []MessageListDAO{}, 0, nil } - query := `select * - from (select distinct on (cem.source_url) cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - where cem.type = 'issue' - and cem.source = 'cve' - and im.is_deleted = false and rc.is_deleted = false - and (rc.gitee_user_name = ? or rc.user_id = ?) - and (cem.data_json #>> '{IssueEvent,Issue,Assignee,UserName}') = ? - order by cem.source_url, cem.updated_at desc) a` - if isDone { - query += ` where (a.data_json #>> '{IssueEvent,Issue,State}') IN ('rejected','closed')` - } else { - query += ` where (a.data_json #>> '{IssueEvent,Issue,State}') NOT IN ('rejected','closed')` - } - if startTime != "" { - query += fmt.Sprintf(` and a.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - query += ` order by updated_at desc` + query := `select *, count(*) over () as total_count from ( + select distinct on (tm.business_id, tm.recipient_id) cem.*, + tm.is_read, tm.is_done from todo_message tm + join cloud_event_message cem on cem.event_id = tm.latest_event_id + join recipient_config rc on rc.id = tm.recipient_id + where rc.is_deleted = false and tm.is_deleted = false + and cem.source = 'cve' + and ((rc.gitee_user_name != '' and rc.gitee_user_name = ?) or rc.user_id = ?) + order by tm.business_id, tm.recipient_id, cem.updated_at desc) a where true` + filterTodoSql(&query, isDone, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` - if result := postgresql.DB().Raw(query, giteeUsername, userName, - giteeUsername).Scan(&response); result.Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, giteeUsername, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } func (s *messageAdapter) GetCVEMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { - var response []MessageListDAO if giteeUsername == "" { return []MessageListDAO{}, 0, nil } - query := `select cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - where cem.type = 'issue' - and cem.source = 'cve' - and im.is_deleted = false and rc.is_deleted = false - and (rc.gitee_user_name = ? or rc.user_id = ?)` - if startTime != "" { - query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - if isRead != nil && *isRead == false { - query += ` and im.is_read = false` - } - query += ` order by cem.updated_at desc` - if result := postgresql.DB().Raw(query, giteeUsername, userName).Scan(&response); result. - Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + query := `with filtered_recipient as ( + select * + from recipient_config + where not is_deleted and ((gitee_user_name != '' and gitee_user_name = ?) or user_id = ?) + ), + filtered_messages as ( + select fm.is_read, cem.* + from follow_message fm + join cloud_event_message cem on cem.event_id = fm.event_id + join filtered_recipient rc on rc.id = fm.recipient_id + where not fm.is_deleted + ) + select *, count(*) over () as total_count + from filtered_messages + where source = 'cve'` + filterFollowSql(&query, isRead, startTime) + + query += ` order by updated_at desc limit ? offset ?` + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, giteeUsername, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } -func (s *messageAdapter) GetIssueToDoMessage(userName, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDAO, int64, error) { - var response []MessageListDAO +func (s *messageAdapter) GetIssueToDoMessage(userName, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { if giteeUsername == "" { return []MessageListDAO{}, 0, nil } - query := `select * - from (select distinct on (cem.source_url) cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - where cem.type = 'issue' - and cem.source = 'https://gitee.com' - and im.is_deleted = false and rc.is_deleted = false - and (rc.gitee_user_name = ? or rc.user_id = ?) - and (cem.data_json #>> '{IssueEvent,Issue,Assignee,UserName}') = ? - order by cem.source_url, cem.updated_at desc) a` - if isDone { - query += ` where (a.data_json #>> '{IssueEvent,Issue,State}') IN ('rejected','closed')` - } else { - query += ` where (a.data_json #>> '{IssueEvent,Issue,State}') NOT IN ('rejected', -'closed')` - } - if startTime != "" { - query += fmt.Sprintf(` and a.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - query += ` order by updated_at desc` - if result := postgresql.DB().Raw(query, giteeUsername, userName, giteeUsername). + query := `select *, count(*) over () as total_count from + ( + select DISTINCT ON (tm.business_id, tm.recipient_id) cem.*, + tm.is_read, tm.is_done from todo_message tm + join cloud_event_message cem on cem.event_id = latest_event_id + join recipient_config rc on rc.id = tm.recipient_id + where tm.is_deleted = false and rc.is_deleted = false + and cem.type = 'issue' and cem.source = 'https://gitee.com' + and ((rc.gitee_user_name != '' and rc.gitee_user_name = ?) or rc.user_id = ?) + order by tm.business_id, tm.recipient_id, cem.updated_at desc) a where true` + + filterTodoSql(&query, isDone, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, giteeUsername, userName, countPerPage, offset). Scan(&response); result.Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } -func (s *messageAdapter) GetPullRequestToDoMessage(userName, giteeUsername string, isDone bool, - pageNum, countPerPage int, startTime string) ([]MessageListDAO, int64, error) { - var response []MessageListDAO +func (s *messageAdapter) GetPullRequestToDoMessage(userName, giteeUsername string, isDone *bool, + pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { if giteeUsername == "" { return []MessageListDAO{}, 0, nil } - query := `select * - from (select distinct on (cem.source_url) cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - and cem.type = 'pr' - and cem.source = 'https://gitee.com' - and im.is_deleted = false and rc.is_deleted = false - and (rc.gitee_user_name = ? or rc.user_id = ?) - and (cem.data_json ->> 'Assignees') :: text like ? - order by cem.source_url, cem.updated_at desc) a` - if isDone { - query += ` where (a.data_json #>> '{PullRequestEvent,State}') IN ('closed', 'merged')` - } else { - query += ` where (a.data_json #>> '{PullRequestEvent,State}') NOT IN ('closed', 'merged')` - } - if startTime != "" { - query += fmt.Sprintf(` and a.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } + query := `select *, count(*) over () as total_count from + ( + select DISTINCT ON (tm.business_id, tm.recipient_id) cem.*, + tm.is_read, tm.is_done from todo_message tm + join cloud_event_message cem on cem.event_id = latest_event_id + join recipient_config rc on rc.id = tm.recipient_id + where tm.is_deleted = false and rc.is_deleted = false + and cem.type = 'pr' and ((rc.gitee_user_name != '' and rc.gitee_user_name = ?) or rc.user_id = ?) + order by tm.business_id, tm.recipient_id, cem.updated_at desc) a where true` + + filterTodoSql(&query, isDone, isRead, startTime) - query += ` order by updated_at desc` - if result := postgresql.DB().Raw(query, giteeUsername, userName, - "%"+giteeUsername+"%").Scan(&response); result.Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, giteeUsername, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } func (s *messageAdapter) GetGiteeAboutMessage(userName, giteeUsername string, isBot *bool, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { - var response []MessageListDAO if giteeUsername == "" { return []MessageListDAO{}, 0, nil } - query := `select cem.*, im.is_read + query := `select cem.*, rm.is_read, count(*) over () as total_count from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - and cem.type = 'note' - and cem.source = 'https://gitee.com' - and im.is_deleted = false and rc.is_deleted = false - and (rc.gitee_user_name = ? or rc.user_id = ?) - and (cem.data_json #>> '{NoteEvent,Issue,User,UserName}' = ? - or cem.data_json #>> '{NoteEvent,PullRequest,User,UserName}' = ? - or cem.data_json #>> '{NoteEvent,Comment,Body}' like ?)` + join message_center.related_message rm on cem.event_id = rm.event_id + join message_center.recipient_config rc on rm.recipient_id = rc.id + where cem.type = 'note' + and cem.source = 'https://gitee.com' + and rm.is_deleted = false and rc.is_deleted = false + and ((rc.gitee_user_name != '' and rc.gitee_user_name = ?) or rc.user_id = ?)` if isBot != nil { if *isBot { query += ` and cem."user" IN ('openeuler-ci-bot','ci-robot','openeuler-sync-bot') ` @@ -388,90 +549,192 @@ func (s *messageAdapter) GetGiteeAboutMessage(userName, giteeUsername string, is query += ` and cem."user" NOT IN ('openeuler-ci-bot','ci-robot','openeuler-sync-bot') ` } } - if startTime != "" { - query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - if isRead != nil && *isRead == false { - query += ` and im.is_read = false` - } - query += ` order by cem.updated_at desc` - if result := postgresql.DB().Raw(query, giteeUsername, userName, - giteeUsername, giteeUsername, "%"+giteeUsername+"%").Scan(&response); result.Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + filterAboutSql(&query, isRead, startTime) + query += ` order by cem.updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, giteeUsername, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } func (s *messageAdapter) GetGiteeMessage(userName, giteeUsername string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { + query := `with filtered_recipient as ( + select * + from recipient_config + where not is_deleted and ((gitee_user_name != '' and gitee_user_name = ?) or user_id = ?) + ), + filtered_messages as ( + select fm.is_read, cem.* + from follow_message fm + join cloud_event_message cem on cem.event_id = fm.event_id + join filtered_recipient rc on rc.id = fm.recipient_id + where not fm.is_deleted + ) + select *, count(*) over () as total_count + from filtered_messages + where source = 'https://gitee.com'` + filterFollowSql(&query, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage var response []MessageListDAO - if giteeUsername == "" { - return []MessageListDAO{}, 0, nil - } - query := `select cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - and cem.source = 'https://gitee.com' - and im.is_deleted = false and rc.is_deleted = false - and (rc.gitee_user_name = ? or rc.user_id = ?) - and cem."user" NOT IN ('openeuler-ci-bot','ci-robot','openeuler-sync-bot')` - if startTime != "" { - query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) - } - if isRead != nil && *isRead == false { - query += ` and im.is_read = false` - } - query += ` order by cem.updated_at desc` - if result := postgresql.DB().Raw(query, giteeUsername, userName).Scan(&response); result. - Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + if result := postgresql.DB().Raw(query, giteeUsername, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } func (s *messageAdapter) GetEurMessage(userName string, pageNum, countPerPage int, startTime string, isRead *bool) ([]MessageListDAO, int64, error) { + query := `with filtered_recipient as ( + select * + from recipient_config + where not is_deleted and user_id = ? + ), + filtered_messages as ( + select fm.is_read, cem.* + from follow_message fm + join cloud_event_message cem on cem.event_id = fm.event_id + join filtered_recipient rc on rc.id = fm.recipient_id + where not fm.is_deleted + ) + select *, count(*) over () as total_count + from filtered_messages + where source = 'https://eur.openeuler.openatom.cn'` + filterFollowSql(&query, isRead, startTime) + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage var response []MessageListDAO - query := `select cem.*, im.is_read - from cloud_event_message cem - join message_center.inner_message im on cem.event_id = im.event_id - join message_center.recipient_config rc on im.recipient_id = rc.id - and cem.source = 'https://eur.openeuler.openatom.cn' - and im.is_deleted = false and rc.is_deleted = false - and rc.user_id = ? - and (cem.data_json #>> '{Body,User}' = ? - or cem.data_json #>> '{Body,Owner}' = ?)` - if startTime != "" { - query += fmt.Sprintf(` and cem.time >= '%s'`, *utils.ParseUnixTimestampNew(startTime)) + if result := postgresql.DB().Raw(query, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + return []MessageListDAO{}, 0, xerrors.Errorf("get message failed, err:%v", + result.Error) } - if isRead != nil && *isRead == false { - query += ` and im.is_read = false` + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount } - query += ` order by cem.updated_at desc` - if result := postgresql.DB().Raw(query, userName, userName, userName).Scan(&response); result. - Error != nil { - logrus.Errorf("get inner message failed, err:%v", result.Error.Error()) + return response, totalCount, nil +} + +func (s *messageAdapter) CountAllMessage(userName string, giteeUserName string) (CountDataDAO, error) { + + response := CountDataDAO{} + query := ` +WITH params AS (SELECT ? AS user_id, ? AS gitee_user_name) +SELECT (SELECT count(*) + FROM message_center.follow_message fm + JOIN recipient_config rc ON fm.recipient_id = rc.id + WHERE (rc.user_id = params.user_id + OR (rc.gitee_user_name != '' and rc.gitee_user_name = params.gitee_user_name)) + AND rc.is_deleted IS false + AND fm.is_deleted IS false + AND fm.is_read IS false + AND fm.source in ('forum', 'https://eur.openeuler.openatom.cn', 'cve', 'https://gitee.com')) + AS watch_count, + + (SELECT count(*) + FROM message_center.related_message rm + JOIN recipient_config rc ON rm.recipient_id = rc.id + WHERE (rc.user_id = params.user_id + OR (rc.gitee_user_name != '' and rc.gitee_user_name = params.gitee_user_name)) + AND rc.is_deleted IS false + AND rm.is_deleted IS false + AND rm.is_read IS false + AND rm.source in ('forum', 'https://gitee.com')) AS about_count, + + (SELECT count(*) + FROM message_center.todo_message tm + JOIN recipient_config rc ON tm.recipient_id = rc.id + JOIN cloud_event_message cem ON tm.latest_event_id = cem.event_id + WHERE (rc.gitee_user_name != '' and rc.gitee_user_name = params.gitee_user_name) + AND rc.is_deleted IS false + AND tm.is_deleted IS false + AND tm.is_done IS false + AND tm.source = 'https://www.openEuler.org/meeting' + AND cem.time >= current_timestamp) AS meeting_count, + + (SELECT count(*) + FROM message_center.todo_message tm + JOIN recipient_config rc ON tm.recipient_id = rc.id + WHERE (rc.user_id = params.user_id + OR (rc.gitee_user_name != '' and rc.gitee_user_name = params.gitee_user_name)) + AND rc.is_deleted IS false + AND tm.is_deleted IS false + AND tm.is_done IS false + AND tm.source in ('forum', 'cve', 'https://gitee.com')) AS todo_count +FROM params; +` + if result := postgresql.DB().Raw(query, userName, giteeUserName).Scan(&response); result.Error != nil { + logrus.Errorf("get count failed, err:%v", result.Error.Error()) + return CountDataDAO{}, xerrors.Errorf("查询失败, err:%v", result.Error) + } + return response, nil +} + +func (s *messageAdapter) GetAllMessage(userName string, pageNum, countPerPage int, + isRead *bool) ([]MessageListDAO, int64, error) { + query := `with filtered_recipient as ( + select * + from recipient_config + where not is_deleted and user_id = ? + ), + all_messages as ( + select fm.is_read, cem.* + from follow_message fm + join cloud_event_message cem on cem.event_id = fm.event_id + join filtered_recipient rc on rc.id = fm.recipient_id + where fm.is_deleted = false + union all + select tm.is_read, cem.* + from todo_message tm + join cloud_event_message cem on cem.event_id = tm.latest_event_id + join filtered_recipient rc on rc.id = tm.recipient_id + where tm.is_deleted = false + union all + select rm.is_read, cem.* + from related_message rm + join cloud_event_message cem on cem.event_id = rm.event_id + join filtered_recipient rc on rc.id = rm.recipient_id + where rm.is_deleted = false + ) + select *, count(*) over () as total_count + from all_messages` + if isRead != nil { + query += fmt.Sprintf(" where is_read = %t", *isRead) + } + query += ` order by updated_at desc limit ? offset ?` + + offset := (pageNum - 1) * countPerPage + var response []MessageListDAO + if result := postgresql.DB().Raw(query, userName, countPerPage, offset). + Scan(&response); result.Error != nil { + logrus.Errorf("get message failed, err:%v", result.Error.Error()) return []MessageListDAO{}, 0, xerrors.Errorf("查询失败, err:%v", result.Error) } - return pagination(response, pageNum, countPerPage), int64(len(response)), nil -} - -func (s *messageAdapter) CountAllMessage(userName, giteeUserName string) (CountDataDAO, error) { - isRead := false - _, todoCountNotDone, _ := s.GetAllToDoMessage(userName, giteeUserName, false, 1, 0, "") - _, aboutCount, _ := s.GetAllAboutMessage(userName, giteeUserName, nil, 1, 0, "", &isRead) - _, watchCount, _ := s.GetAllWatchMessage(userName, giteeUserName, 1, 0, "", &isRead) - _, meetingCount, _ := s.GetMeetingToDoMessage(userName, 1, 1, 0) - return CountDataDAO{ - TodoCount: todoCountNotDone, - AboutCount: aboutCount, - WatchCount: watchCount, - MeetingCount: meetingCount, - }, nil + var totalCount int64 + if len(response) != 0 { + totalCount = response[0].TotalCount + } + return response, totalCount, nil } diff --git a/message/infrastructure/recipient.go b/message/infrastructure/recipient.go index 8843f79..ff2f60d 100644 --- a/message/infrastructure/recipient.go +++ b/message/infrastructure/recipient.go @@ -104,7 +104,12 @@ func (ctl *messageRecipientAdapter) RemoveRecipientConfig(cmd CmdToDeleteRecipie func (ctl *messageRecipientAdapter) SyncUserInfo(cmd CmdToSyncUserInfo) (uint, error) { var oldInfo RecipientController - + if cmd.GiteeUserName != "" { + getTable().Where("gitee_user_name = ?", cmd.GiteeUserName). + Updates(map[string]interface{}{ + "gitee_user_name": "", + }) + } if result := getTable(). Where("user_id = ?", cmd.UserName). Scan(&oldInfo); result.RowsAffected != 0 {