diff --git a/biz/application/service/system.go b/biz/application/service/system.go index 720a145..f686026 100644 --- a/biz/application/service/system.go +++ b/biz/application/service/system.go @@ -593,12 +593,16 @@ func (s *SystemServiceImpl) DeleteCommunity(ctx context.Context, req *system.Del } func (s *SystemServiceImpl) ListNotification(ctx context.Context, req *system.ListNotificationReq) (resp *system.ListNotificationResp, err error) { + resp = new(system.ListNotificationResp) - notification, total, err := s.NotificationModel.ListNotification(ctx, req, mongop.IdCursorType) + var notifications []*db.Notification + filter := util.ParseNotificationFilter(req) + p := util.ParsePagination(req.PaginationOptions) + notifications, resp.Total, err = s.NotificationModel.ListNotification(ctx, filter, p, mongop.IdCursorType) if err != nil { return nil, err } - notRead, err := s.NotificationModel.CountNotification(ctx, &system.CountNotificationReq{ + resp.NotRead, err = s.NotificationModel.CountNotification(ctx, &system.CountNotificationReq{ UserId: req.GetUserId(), Type: req.Type, TargetType: req.TargetType, @@ -607,11 +611,11 @@ func (s *SystemServiceImpl) ListNotification(ctx context.Context, req *system.Li return nil, err } - return &system.ListNotificationResp{ - Notifications: util.ConvertNotifications(notification), - NotRead: notRead, - Total: total, - }, nil + if p.LastToken != nil { + resp.Token = *p.LastToken + } + resp.Notifications = util.ConvertNotifications(notifications) + return resp, nil } func (s *SystemServiceImpl) CountNotification(ctx context.Context, req *system.CountNotificationReq) (resp *system.CountNotificationResp, err error) { diff --git a/biz/infrastructure/mapper/notification/notification_model.go b/biz/infrastructure/mapper/notification/notification_model.go index 2176951..152ee91 100644 --- a/biz/infrastructure/mapper/notification/notification_model.go +++ b/biz/infrastructure/mapper/notification/notification_model.go @@ -17,7 +17,6 @@ import ( "github.com/xh-polaris/meowchat-system/biz/infrastructure/consts" "github.com/xh-polaris/meowchat-system/biz/infrastructure/data/db" "github.com/xh-polaris/meowchat-system/biz/infrastructure/mapper" - "github.com/xh-polaris/meowchat-system/biz/infrastructure/util" ) const ( @@ -31,7 +30,7 @@ type ( // and implement the added methods in customNotificationModel. NotificationModel interface { notificationModel - ListNotification(ctx context.Context, req *system.ListNotificationReq, sorter mongop.MongoCursor) ([]*db.Notification, int64, error) + ListNotification(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*db.Notification, int64, error) CleanNotification(ctx context.Context, userId string) error ReadNotification(ctx context.Context, notificationId string) error CountNotification(ctx context.Context, req *system.CountNotificationReq) (int64, error) @@ -43,16 +42,11 @@ type ( } ) -func (m customNotificationModel) ListNotification(ctx context.Context, req *system.ListNotificationReq, sorter mongop.MongoCursor) ([]*db.Notification, int64, error) { +func (m customNotificationModel) ListNotification(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*db.Notification, int64, error) { var data []*db.Notification - popts := util.ParsePagination(req.GetPaginationOptions()) p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) - f := &FilterOptions{ - OnlyUserId: req.UserId, - OnlyType: req.Type, - OnlyTargetType: req.TargetType, - } - filter := makeMongoFilter(f) + + filter := makeMongoFilter(fopts) sort, err := p.MakeSortOptions(ctx, filter) if err != nil { return nil, 0, err @@ -60,8 +54,8 @@ func (m customNotificationModel) ListNotification(ctx context.Context, req *syst if err = m.conn.Find(ctx, &data, filter, &options.FindOptions{ Sort: sort, - Limit: req.PaginationOptions.Limit, - Skip: req.PaginationOptions.Offset, + Limit: popts.Limit, + Skip: popts.Offset, }); err != nil { return nil, 0, err } diff --git a/biz/infrastructure/util/util.go b/biz/infrastructure/util/util.go index 8c0636c..ac4abf6 100644 --- a/biz/infrastructure/util/util.go +++ b/biz/infrastructure/util/util.go @@ -10,6 +10,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" "github.com/xh-polaris/meowchat-system/biz/infrastructure/data/db" + "github.com/xh-polaris/meowchat-system/biz/infrastructure/mapper/notification" ) func ConvertAdmin(in *db.Admin) *system.Admin { @@ -114,3 +115,16 @@ func ParallelRun(fns ...func()) { } wg.Wait() } + +func ParseNotificationFilter(req *system.ListNotificationReq) (filter *notification.FilterOptions) { + if req == nil { + filter = ¬ification.FilterOptions{} + } else { + filter = ¬ification.FilterOptions{ + OnlyUserId: req.UserId, + OnlyType: req.Type, + OnlyTargetType: req.TargetType, + } + } + return +}