-
Notifications
You must be signed in to change notification settings - Fork 4
/
route.go
120 lines (101 loc) · 2.93 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
package get_guild_task
import (
"errors"
"net/http"
"strings"
"github.com/anti-raid/splashtail/jobs/tasks"
"github.com/anti-raid/splashtail/splashcore/structparser/db"
types "github.com/anti-raid/splashtail/splashcore/types"
"github.com/anti-raid/splashtail/webserver/api"
"github.com/anti-raid/splashtail/webserver/state"
"github.com/go-chi/chi/v5"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/uapi"
"github.com/jackc/pgx/v5"
"go.uber.org/zap"
)
var (
taskColsArr = db.GetCols(types.Task{})
taskColsStr = strings.Join(taskColsArr, ", ")
)
func Docs() *docs.Doc {
return &docs.Doc{
Summary: "Get Guild Task",
Description: "Gets a task created on a guild",
Params: []docs.Parameter{
{
Name: "id",
Description: "User ID",
Required: true,
In: "path",
Schema: docs.IdSchema,
},
{
Name: "guild_id",
Description: "Guild ID",
Required: true,
In: "path",
Schema: docs.IdSchema,
},
{
Name: "tid",
Description: "The task ID",
Required: true,
In: "path",
Schema: docs.IdSchema,
},
},
Resp: types.Task{},
}
}
func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
taskId := chi.URLParam(r, "tid")
guildId := chi.URLParam(r, "guild_id")
if taskId == "" || guildId == "" {
return uapi.HttpResponse{
Status: http.StatusBadRequest,
Json: types.ApiError{Message: "tid/guild_id is required"},
}
}
// Delete expired tasks first
_, err := state.Pool.Exec(d.Context, "DELETE FROM tasks WHERE created_at + expiry < NOW()")
if err != nil {
state.Logger.Error("Failed to delete expired tasks [db delete]", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
var row pgx.Rows
row, err = state.Pool.Query(d.Context, "SELECT "+taskColsStr+" FROM tasks WHERE task_id = $1 AND guild_id = $2", taskId, guildId)
if err != nil {
state.Logger.Error("Failed to fetch task [db fetch]", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
task, err := pgx.CollectOneRow(row, pgx.RowToStructByName[types.Task])
if errors.Is(err, pgx.ErrNoRows) {
return uapi.HttpResponse{
Status: http.StatusNotFound,
Json: types.ApiError{Message: "Task not found"},
}
}
if err != nil {
state.Logger.Error("Failed to fetch task [db fetch]", zap.Error(err))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
baseTaskDef, ok := tasks.TaskDefinitionRegistry[task.TaskName]
if !ok {
return uapi.HttpResponse{
Json: types.ApiError{
Message: "Internal Error: Unknown task name",
},
Status: http.StatusInternalServerError,
}
}
// Check permissions
resp, ok := api.HandlePermissionCheck(d.Auth.ID, guildId, baseTaskDef.CorrespondingBotCommand_View(), api.PermLimits(d.Auth))
if !ok {
return resp
}
return uapi.HttpResponse{
Status: http.StatusOK,
Json: task,
}
}