-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
121 lines (100 loc) · 2.98 KB
/
route.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
package get_app_list
import (
"errors"
"net/http"
"popplio/db"
"popplio/routes/staff/assets"
"popplio/state"
"popplio/types"
"popplio/validators/kittycat/ext"
"popplio/validators/kittycat/perms"
"strings"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/dovewing"
"github.com/infinitybotlist/eureka/uapi"
"go.uber.org/zap"
"github.com/jackc/pgx/v5"
)
var (
appColsArr = db.GetCols(types.AppResponse{})
appCols = strings.Join(appColsArr, ",")
)
func Docs() *docs.Doc {
return &docs.Doc{
Summary: "Staff: Get Application List",
Description: "Gets all applications returning a list of apps.",
Params: []docs.Parameter{
{
Name: "user_id",
Description: "The ID of the user to get the applications for. If not specified, all applications will be returned.",
In: "query",
Required: false,
Schema: docs.IdSchema,
},
},
Resp: types.AppListResponse{},
}
}
func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
var err error
d.Auth.ID, err = assets.EnsurePanelAuth(d.Context, r)
if err != nil {
return uapi.HttpResponse{
Status: http.StatusFailedDependency,
Json: types.ApiError{Message: err.Error()},
}
}
permList, err := ext.GetUserStaffPerms(d.Context, d.Auth.ID)
if err != nil {
return uapi.HttpResponse{
Status: http.StatusFailedDependency,
Json: types.ApiError{Message: err.Error()},
}
}
resolvedPerms := permList.Resolve()
// Check if the user has the permission to view apps
if !perms.HasPerm(resolvedPerms, "apps.view") {
return uapi.HttpResponse{
Status: http.StatusForbidden,
Json: types.ApiError{
Message: "You do not have permission to view apps.",
},
}
}
userId := r.URL.Query().Get("user_id")
var row pgx.Rows
if userId != "" {
row, err = state.Pool.Query(d.Context, "SELECT "+appCols+" FROM apps WHERE user_id = $1 ORDER BY created_at DESC", userId)
} else {
row, err = state.Pool.Query(d.Context, "SELECT "+appCols+" FROM apps ORDER BY created_at DESC")
}
if err != nil {
state.Logger.Error("Failed to fetch application list [db fetch]", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
app, err := pgx.CollectRows(row, pgx.RowToStructByName[types.AppResponse])
if errors.Is(err, pgx.ErrNoRows) {
return uapi.HttpResponse{
Json: types.AppListResponse{
Apps: []types.AppResponse{},
},
}
}
if err != nil {
state.Logger.Error("Failed to fetch application list [collection]", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
for i := range app {
user, err := dovewing.GetUser(d.Context, app[i].UserID, state.DovewingPlatformDiscord)
if err != nil {
state.Logger.Error("Failed to fetch application list [user fetch]", zap.String("userId", app[i].UserID), zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
app[i].User = user
}
return uapi.HttpResponse{
Json: types.AppListResponse{
Apps: app,
},
}
}