/
user.resolvers.go
157 lines (131 loc) · 4.71 KB
/
user.resolvers.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package resolver
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"fmt"
"sort"
"github.com/nedrocks/delphisbe/graph/generated"
"github.com/nedrocks/delphisbe/graph/model"
"github.com/nedrocks/delphisbe/internal/auth"
"github.com/sirupsen/logrus"
)
func (r *userResolver) Participants(ctx context.Context, obj *model.User) ([]*model.Participant, error) {
participants := make([]*model.Participant, 0)
for _, p := range obj.Participants {
discussion := p.Discussion
if discussion == nil && p.DiscussionID != nil {
var err error
discussion, err = r.DAOManager.GetDiscussionByID(ctx, *p.DiscussionID)
if err != nil {
logrus.Debugf("Caught an error getting discussion for participant")
// Silently ignore for now.
}
p.Discussion = discussion
}
if discussion != nil && discussion.DeletedAt == nil {
participants = append(participants, p)
}
}
sort.Slice(participants, func(i, j int) bool {
return participants[i].CreatedAt.Before(participants[j].CreatedAt)
})
return participants, nil
}
func (r *userResolver) Viewers(ctx context.Context, obj *model.User) ([]*model.Viewer, error) {
viewers := make([]*model.Viewer, 0)
for _, v := range obj.Viewers {
discussion := v.Discussion
if v != nil && discussion == nil && v.DiscussionID != nil {
var err error
discussion, err = r.DAOManager.GetDiscussionByID(ctx, *v.DiscussionID)
if err != nil {
logrus.Debugf("Caught an error getting discussion for participant")
// Silently ignore for now.
}
v.Discussion = discussion
}
if v != nil && v.Discussion != nil && v.Discussion.DeletedAt == nil {
viewers = append(viewers, v)
}
}
sort.Slice(viewers, func(i, j int) bool {
return viewers[i].CreatedAt.Before(viewers[j].CreatedAt)
})
return viewers, nil
}
func (r *userResolver) Bookmarks(ctx context.Context, obj *model.User) ([]*model.PostBookmark, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *userResolver) Profile(ctx context.Context, obj *model.User) (*model.UserProfile, error) {
if obj.UserProfile == nil {
userProfile, err := r.DAOManager.GetUserProfileByUserID(ctx, obj.ID)
if err != nil {
return nil, err
}
obj.UserProfile = userProfile
}
return obj.UserProfile, nil
}
func (r *userResolver) Flairs(ctx context.Context, obj *model.User) ([]*model.Flair, error) {
return r.DAOManager.GetFlairsByUserID(ctx, obj.ID)
}
func (r *userResolver) Devices(ctx context.Context, obj *model.User) ([]*model.UserDevice, error) {
devices, err := r.DAOManager.GetUserDevicesByUserID(ctx, obj.ID)
if err != nil {
return nil, err
}
resp := make([]*model.UserDevice, len(devices))
for i := range devices {
resp[i] = &devices[i]
}
return resp, nil
}
func (r *userResolver) Discussions(ctx context.Context, obj *model.User) ([]*model.Discussion, error) {
authedUser := auth.GetAuthedUser(ctx)
if authedUser == nil {
return nil, fmt.Errorf("Need auth")
}
// Do we want a mod override?
if authedUser.UserID != obj.ID {
return nil, fmt.Errorf("unauthorized")
}
return r.DAOManager.GetDiscussionAccessByUserID(ctx, authedUser.UserID)
}
func (r *userResolver) DiscussionInvites(ctx context.Context, obj *model.User, status model.InviteRequestStatus) ([]*model.DiscussionInvite, error) {
authedUser := auth.GetAuthedUser(ctx)
if authedUser == nil {
return nil, fmt.Errorf("Need auth")
}
// Do we want a mod override?
if authedUser.UserID != obj.ID {
return nil, fmt.Errorf("unauthorized")
}
// Do we want a different status? Made it generic for easy change
return r.DAOManager.GetDiscussionInvitesByUserIDAndStatus(ctx, authedUser.UserID, status)
}
func (r *userResolver) SentDiscussionInvites(ctx context.Context, obj *model.User) ([]*model.DiscussionInvite, error) {
authedUser := auth.GetAuthedUser(ctx)
if authedUser == nil {
return nil, fmt.Errorf("Need auth")
}
// Do we want a mod override?
if authedUser.UserID != obj.ID {
return nil, fmt.Errorf("unauthorized")
}
return r.DAOManager.GetSentDiscussionInvitesByUserID(ctx, authedUser.UserID)
}
func (r *userResolver) SentDiscussionAccessRequests(ctx context.Context, obj *model.User) ([]*model.DiscussionAccessRequest, error) {
authedUser := auth.GetAuthedUser(ctx)
if authedUser == nil {
return nil, fmt.Errorf("Need auth")
}
// Do we want a mod override?
if authedUser.UserID != obj.ID {
return nil, fmt.Errorf("unauthorized")
}
return r.DAOManager.GetSentDiscussionAccessRequestsByUserID(ctx, authedUser.UserID)
}
// User returns generated.UserResolver implementation.
func (r *Resolver) User() generated.UserResolver { return &userResolver{r} }
type userResolver struct{ *Resolver }