-
Notifications
You must be signed in to change notification settings - Fork 61
/
get_subject.go
118 lines (96 loc) · 2.95 KB
/
get_subject.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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 ctrl
import (
"context"
"errors"
"time"
"go.uber.org/zap"
"github.com/bangumi/server/internal/auth"
"github.com/bangumi/server/internal/ctrl/internal/cachekey"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/null"
"github.com/bangumi/server/internal/subject"
)
func (ctl Ctrl) GetSubject(ctx context.Context, user domain.Auth, subjectID model.SubjectID) (model.Subject, error) {
s, err := ctl.getSubject(ctx, subjectID)
if err != nil {
return model.Subject{}, err
}
if !auth.AllowReadSubject(user, s) {
return model.Subject{}, domain.ErrSubjectNotFound
}
return s, nil
}
func (ctl Ctrl) GetSubjectNoRedirect(
ctx context.Context,
user domain.Auth,
subjectID model.SubjectID,
) (model.Subject, error) {
s, err := ctl.getSubject(ctx, subjectID)
if err != nil {
return model.Subject{}, err
}
if s.Redirect != 0 {
return model.Subject{}, domain.ErrSubjectNotFound
}
if !auth.AllowReadSubject(user, s) {
return model.Subject{}, domain.ErrSubjectNotFound
}
return s, nil
}
type SubjectFilter struct {
NSFW null.Bool
}
func (ctl Ctrl) GetSubjectByIDs(
ctx context.Context,
subjectIDs []model.SubjectID,
filter SubjectFilter,
) (map[model.SubjectID]model.Subject, error) {
if len(subjectIDs) == 0 {
return map[model.SubjectID]model.Subject{}, nil
}
notCachedSubjects, err := ctl.subject.GetByIDs(ctx, subjectIDs, subject.Filter{NSFW: filter.NSFW})
if err != nil {
return nil, errgo.Wrap(err, "failed to get subjects")
}
return notCachedSubjects, nil
}
func (ctl Ctrl) getSubject(ctx context.Context, id model.SubjectID) (model.Subject, error) {
ctl.metricSubjectQueryCount.Inc(1)
var key = cachekey.Subject(id)
// try to read from cache
var r model.Subject
ok, err := ctl.cache.Get(ctx, key, &r)
if err != nil {
return r, errgo.Wrap(err, "cache.Get")
}
if ok {
ctl.metricSubjectQueryCached.Inc(1)
return r, nil
}
r, err = ctl.subject.Get(ctx, id, subject.Filter{})
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
return r, domain.ErrSubjectNotFound
}
return r, errgo.Wrap(err, "SubjectRepo.Get")
}
if e := ctl.cache.Set(ctx, key, r, time.Minute); e != nil {
ctl.log.Error("can't set response to cache", zap.Error(e))
}
return r, nil
}