-
Notifications
You must be signed in to change notification settings - Fork 62
/
create.go
73 lines (66 loc) · 2.48 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package pm
import (
"errors"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v2"
"github.com/bangumi/server/internal/ctrl"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/generic/slice"
"github.com/bangumi/server/internal/pkg/null"
"github.com/bangumi/server/internal/web/req"
"github.com/bangumi/server/internal/web/res"
)
func (h PrivateMessage) Create(c *fiber.Ctx) error {
accessor := h.Common.GetHTTPAccessor(c)
var r req.PrivateMessageCreate
if err := sonic.Unmarshal(c.Body(), &r); err != nil {
return res.JSONError(c, err)
}
if err := h.Common.V.Struct(r); err != nil {
return h.ValidationError(c, err)
}
receiverIDs := slice.Map(r.ReceiverIDs, func(v uint32) model.UserID { return model.UserID(v) })
msgs, err := h.ctrl.CreatePrivateMessage(
c.UserContext(),
accessor.ID,
receiverIDs,
domain.PrivateMessageIDFilter{Type: null.NewFromPtr((*model.PrivateMessageID)(r.RelatedID))},
r.Title,
r.Content)
if err != nil {
switch {
case errors.Is(err, ctrl.ErrPmBlocked):
case errors.Is(err, ctrl.ErrPmNotAFriend):
case errors.Is(err, ctrl.ErrPmNotAllReceiversExist):
case errors.Is(err, ctrl.ErrPmReceiverReject):
case errors.Is(err, domain.ErrPmRelatedNotExists):
case errors.Is(err, domain.ErrPmInvalidOperation):
return res.BadRequest(err.Error())
}
return res.InternalError(c, err, "failed to create private message(s)")
}
userIDs := make([]model.UserID, len(r.ReceiverIDs)+1)
copy(userIDs, receiverIDs)
userIDs[len(userIDs)-1] = accessor.ID
users, err := h.ctrl.GetUsersByIDs(c.Context(), slice.Unique(userIDs))
if err != nil {
return res.InternalError(c, err, "failed to get users")
}
return c.JSON(slice.Map(msgs, func(v model.PrivateMessage) res.PrivateMessage {
return res.ConvertModelPrivateMessage(v, users)
}))
}