-
Notifications
You must be signed in to change notification settings - Fork 4
/
route.go
66 lines (54 loc) · 1.66 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
package get_user_sessions
import (
"errors"
"net/http"
"strings"
"github.com/anti-raid/splashtail/splashcore/structparser/db"
"github.com/anti-raid/splashtail/splashcore/types"
"github.com/anti-raid/splashtail/webserver/state"
"github.com/jackc/pgx/v5"
"go.uber.org/zap"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/uapi"
)
var (
webApiTokensCols = strings.Join(db.GetCols(types.UserSession{}), ", ")
)
func Docs() *docs.Doc {
return &docs.Doc{
Summary: "Get User Sessions",
Description: "Gets all session tokens of a user",
Resp: types.UserSessionList{},
Params: []docs.Parameter{
{
Name: "user_id",
Description: "The ID of the user to get sessions for",
In: "path",
Required: true,
Schema: docs.IdSchema,
},
},
}
}
func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
rows, err := state.Pool.Query(d.Context, "SELECT "+webApiTokensCols+" FROM web_api_tokens WHERE user_id = $1", d.Auth.ID)
if err != nil {
state.Logger.Error("Error while getting user tokens", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
defer rows.Close()
tokens, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[types.UserSession])
if errors.Is(err, pgx.ErrNoRows) {
return uapi.HttpResponse{
Status: http.StatusNotFound,
Json: types.ApiError{Message: "No sessions found"},
}
}
if err != nil {
state.Logger.Error("Error while getting user sessions", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
return uapi.HttpResponse{
Json: types.UserSessionList{Sessions: tokens},
}
}