2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2023-08-10 05:02:44 +08:00
|
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
2022-02-10 04:25:34 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2018-08-06 23:06:55 +08:00
|
|
|
"github.com/mainflux/mainflux/readers"
|
2023-06-14 18:40:37 +08:00
|
|
|
tpolicies "github.com/mainflux/mainflux/things/policies"
|
|
|
|
upolicies "github.com/mainflux/mainflux/users/policies"
|
2018-08-06 23:06:55 +08:00
|
|
|
)
|
|
|
|
|
2023-07-28 20:39:13 +08:00
|
|
|
func listMessagesEndpoint(svc readers.MessageRepository, tc tpolicies.AuthServiceClient, ac upolicies.AuthServiceClient) endpoint.Endpoint {
|
2022-02-10 04:25:34 +08:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2018-08-06 23:06:55 +08:00
|
|
|
req := request.(listMessagesReq)
|
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
2023-08-10 05:02:44 +08:00
|
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
2022-02-10 04:25:34 +08:00
|
|
|
if err := authorize(ctx, req, tc, ac); err != nil {
|
2023-08-10 05:02:44 +08:00
|
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrAuthorization, err))
|
2022-02-10 04:25:34 +08:00
|
|
|
}
|
2021-01-26 19:23:15 +08:00
|
|
|
page, err := svc.ReadAll(req.chanID, req.pageMeta)
|
2019-05-07 21:59:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-06 23:06:55 +08:00
|
|
|
|
2019-04-25 06:18:43 +08:00
|
|
|
return pageRes{
|
2021-01-26 19:23:15 +08:00
|
|
|
PageMetadata: page.PageMetadata,
|
|
|
|
Total: page.Total,
|
|
|
|
Messages: page.Messages,
|
2019-04-25 06:18:43 +08:00
|
|
|
}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
}
|