-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.go
More file actions
392 lines (336 loc) · 16 KB
/
script.go
File metadata and controls
392 lines (336 loc) · 16 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package script
import (
"context"
"strings"
"github.com/cago-frame/cago/pkg/i18n"
"github.com/cago-frame/cago/pkg/utils/httputils"
"github.com/cago-frame/cago/server/mux"
"github.com/scriptscat/scriptlist/internal/model/entity/script_entity"
"github.com/scriptscat/scriptlist/internal/model/entity/user_entity"
"github.com/scriptscat/scriptlist/internal/pkg/code"
)
type Script struct {
Script *Code `json:"script"`
ID int64 `json:"id"`
user_entity.UserInfo `json:",inline"`
PostID int64 `json:"post_id"`
Name string `json:"name"`
Description string `json:"description"`
Category *CategoryListItem `json:"category"`
Tags []*CategoryListItem `json:"tags"`
Status int64 `json:"status"`
Score int64 `json:"score"`
ScoreNum int64 `json:"score_num"`
Type script_entity.Type `json:"type"`
Public int `json:"public"`
Unwell int `json:"unwell"`
Archive int `json:"archive"`
Danger int `json:"danger"`
EnablePreRelease script_entity.EnablePreRelease `json:"enable_pre_release"`
TodayInstall int64 `json:"today_install"`
TotalInstall int64 `json:"total_install"`
Createtime int64 `json:"createtime"`
Updatetime int64 `json:"updatetime"`
}
// CategoryListItem 拥有的分类列表
type CategoryListItem struct {
ID int64 `json:"id"`
Name string `json:"name"`
// 本分类下脚本数量
Num int64 `json:"num"`
Sort int64 `json:"sort"`
Type script_entity.ScriptCategoryType `json:"type"` // 1:脚本分类 2:脚本标签
Createtime int64 `json:"createtime"`
Updatetime int64 `json:"updatetime"`
}
type Code struct {
ID int64 `json:"id" form:"id"`
user_entity.UserInfo `json:",inline"`
Meta string `json:"meta,omitempty"`
MetaJson interface{} `json:"meta_json"`
ScriptID int64 `json:"script_id"`
Version string `json:"version"`
Changelog string `json:"changelog"`
IsPreRelease script_entity.EnablePreRelease `json:"is_pre_release"`
Status int64 `json:"status"`
Createtime int64 `json:"createtime"`
Code string `json:"code,omitempty"`
Definition string `json:"definition,omitempty"`
}
// ListRequest 获取脚本列表
type ListRequest struct {
mux.Meta `path:"/scripts" method:"GET"`
httputils.PageRequest `form:",inline"`
Keyword string `form:"keyword"`
Domain string `form:"domain"`
UserID int64 `form:"user_id"` // 用户ID
Category int64 `form:"category"` // 分类ID
ScriptType int `form:"script_type,default=0" binding:"oneof=0 1 2 3 4"` // 0:全部 1: 脚本 2: 库 3: 后台脚本 4: 定时脚本
Sort string `form:"sort,default=today_download" binding:"oneof=today_update today_download total_download score createtime updatetime"`
}
type ListResponse struct {
httputils.PageResponse[*Script] `json:",inline"`
}
// CreateRequest 创建脚本
type CreateRequest struct {
mux.Meta `path:"/scripts" method:"POST"`
Content string `form:"content" binding:"required,max=102400" label:"脚本详细描述"`
Code string `form:"code" binding:"required,max=10485760" label:"脚本代码"`
Name string `form:"name" binding:"max=50" label:"库的名字"`
Description string `form:"description" binding:"max=200" label:"库的描述"`
Definition string `form:"definition" binding:"max=10240" label:"库的定义文件"`
Version string `form:"version" binding:"max=32" label:"库的版本"`
Tags []string `form:"tags" binding:"omitempty,max=5" label:"标签"` // 标签,只有脚本类型为库时才有意义
CategoryID int64 `form:"category" binding:"omitempty,numeric" label:"分类ID"` // 分类ID
Type script_entity.Type `form:"type" binding:"required,oneof=1 2 3" label:"脚本类型"` // 脚本类型:1 用户脚本 2 订阅脚本(不支持) 3 脚本引用库
Public script_entity.Public `form:"public" binding:"required,oneof=1 2 3" label:"公开类型"` // 公开类型:1 公开 2 半公开 3 私有
Unwell script_entity.UnwellContent `form:"unwell" binding:"required,oneof=1 2" label:"不适内容"` // 不适内容: 1 不适 2 适用
Changelog string `form:"changelog" binding:"max=102400" label:"更新日志"`
}
func (s *CreateRequest) Validate(ctx context.Context) error {
if s.Type == script_entity.LibraryType {
s.Name, s.Description = strings.TrimSpace(s.Name), strings.TrimSpace(s.Description)
if s.Name == "" {
return i18n.NewError(ctx, code.ScriptNameIsEmpty)
}
if s.Description == "" {
return i18n.NewError(ctx, code.ScriptDescIsEmpty)
}
}
return nil
}
type CreateResponse struct {
ID int64 `json:"id"`
}
// UpdateCodeRequest 更新脚本/库代码
type UpdateCodeRequest struct {
mux.Meta `path:"/scripts/:id/code" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
//Name string `form:"name" binding:"max=128" label:"库的名字"`
//Description string `form:"description" binding:"max=102400" label:"库的描述"`
Version string `binding:"required,max=128" form:"version" label:"库的版本号"`
Tags []string `form:"tags" binding:"omitempty,max=5" label:"标签"` // 标签,只有脚本类型为库时才有意义
Content string `binding:"required,max=102400" form:"content" label:"脚本详细描述"`
Code string `binding:"required,max=10485760" form:"code" label:"脚本代码"`
Definition string `binding:"max=102400" form:"definition" label:"库的定义文件"`
Changelog string `binding:"max=102400" form:"changelog" label:"更新日志"`
IsPreRelease script_entity.EnablePreRelease `form:"is_pre_release" json:"is_pre_release" binding:"omitempty,oneof=0 1 2" label:"是否预发布"`
CategoryID int64 `json:"category_id" form:"category_id" binding:"omitempty,numeric" label:"分类ID"` // 分类ID
//Public script_entity.Public `form:"public" binding:"required,oneof=1 2" label:"公开类型"` // 公开类型:1 公开 2 半公开
//Unwell script_entity.UnwellContent `form:"unwell" binding:"required,oneof=1 2" label:"不适内容"`
}
type UpdateCodeResponse struct {
}
// DeleteCodeRequest 删除脚本/库代码
type DeleteCodeRequest struct {
mux.Meta `path:"/scripts/:id/code/:codeId" method:"DELETE"`
ID int64 `uri:"id" binding:"required"`
CodeID int64 `uri:"codeId" binding:"required"`
}
type DeleteCodeResponse struct {
}
// MigrateEsRequest 全量迁移数据到es
type MigrateEsRequest struct {
mux.Meta `path:"/scripts/migrate/es" method:"POST"`
}
type MigrateEsResponse struct {
}
// InfoRequest 获取脚本信息
type InfoRequest struct {
mux.Meta `path:"/scripts/:id" method:"GET"`
ID int64 `uri:"id" binding:"required"`
}
type InfoResponse struct {
*Script `json:",inline"`
Content string `json:"content"`
Role script_entity.AccessRole `json:"role"`
SRI string `json:"sri,omitempty"` // 如果是库的话,会返回sha512 sri
}
// CodeRequest 获取脚本代码信息
type CodeRequest struct {
mux.Meta `path:"/scripts/:id/code" method:"GET"`
ID int64 `uri:"id" binding:"required"`
}
type CodeResponse struct {
*Script `json:",inline"`
Content string `json:"content"`
}
// VersionListRequest 获取版本列表
type VersionListRequest struct {
mux.Meta `path:"/scripts/:id/versions" method:"GET"`
httputils.PageRequest `form:",inline"`
ID int64 `uri:"id" binding:"required"`
}
type VersionListResponse struct {
httputils.PageResponse[*Code] `json:",inline"`
}
// VersionStatRequest 获取脚本版本统计信息
type VersionStatRequest struct {
mux.Meta `path:"/scripts/:id/versions/stat" method:"GET"`
ID int64 `uri:"id" binding:"required"`
}
type VersionStatResponse struct {
// 正式版本数量
ReleaseNum int64 `json:"release_num"`
// 预发布版本数量
PreReleaseNum int64 `json:"pre_release_num"`
}
// VersionCodeRequest 获取指定版本代码
type VersionCodeRequest struct {
mux.Meta `path:"/scripts/:id/versions/:version/code" method:"GET"`
ID int64 `uri:"id" binding:"required"`
Version string `uri:"version" binding:"required"`
}
type VersionCodeResponse struct {
*Script `json:",inline"`
}
// StateRequest 获取脚本状态,脚本关注等
type StateRequest struct {
mux.Meta `path:"/scripts/:id/state" method:"GET"`
ID int64 `uri:"id" binding:"required"`
}
type StateResponse struct {
Watch script_entity.ScriptWatchLevel `json:"watch"`
// 收藏夹
FavoriteIds []int64 `json:"favorite_ids"`
// 关注人数
WatchCount int64 `json:"watch_count"`
// 收藏人数
FavoriteCount int64 `json:"favorite_count"`
// Issue数量
IssueCount int64 `json:"issue_count"`
// 未解决举报数量
ReportCount int64 `json:"report_count"`
}
// WatchRequest 关注脚本
type WatchRequest struct {
mux.Meta `path:"/scripts/:id/watch" method:"POST"`
ID int64 `uri:"id" binding:"required"`
Watch script_entity.ScriptWatchLevel `json:"watch" binding:"oneof=0 1 2 3"`
}
type WatchResponse struct {
}
// GetSettingRequest 获取脚本设置
type GetSettingRequest struct {
mux.Meta `path:"/scripts/:id/setting" method:"GET"`
ID int64 `uri:"id" binding:"required"`
}
type GetSettingResponse struct {
SyncUrl string `json:"sync_url"`
ContentUrl string `json:"content_url"`
DefinitionUrl string `json:"definition_url"`
SyncMode script_entity.SyncMode `json:"sync_mode"`
EnablePreRelease script_entity.EnablePreRelease `json:"enable_pre_release"`
GrayControls []*script_entity.GrayControl `json:"gray_controls"`
}
// UpdateSettingRequest 更新脚本设置
type UpdateSettingRequest struct {
mux.Meta `path:"/scripts/:id/setting" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
Name string `json:"name" binding:"max=50" label:"库的名字"`
Description string `json:"description" binding:"max=200" label:"库的描述"`
SyncUrl string `json:"sync_url" binding:"omitempty,url,max=1024" label:"代码同步url"`
ContentUrl string `json:"content_url" binding:"omitempty,url,max=1024" label:"详细描述同步url"`
DefinitionUrl string `json:"definition_url" binding:"omitempty,url,max=1024" label:"定义文件同步url"`
SyncMode script_entity.SyncMode `json:"sync_mode" binding:"number" label:"同步模式"`
}
type UpdateSettingResponse struct {
Sync bool
SyncError string
}
// UpdateLibInfoRequest 更新库信息
type UpdateLibInfoRequest struct {
mux.Meta `path:"/scripts/:id/lib-info" method:"PUT"`
Name string `json:"name" binding:"max=50" label:"库的名字"`
Description string `json:"description" binding:"max=200" label:"库的描述"`
}
type UpdateLibInfoResponse struct {
}
// UpdateSyncSettingRequest 更新同步配置
type UpdateSyncSettingRequest struct {
mux.Meta `path:"/scripts/:id/sync" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
SyncUrl string `json:"sync_url" binding:"omitempty,url,max=1024" label:"代码同步url"`
ContentUrl string `json:"content_url" binding:"omitempty,url,max=1024" label:"详细描述同步url"`
DefinitionUrl string `json:"definition_url" binding:"omitempty,url,max=1024" label:"定义文件同步url"`
SyncMode script_entity.SyncMode `json:"sync_mode" binding:"number" label:"同步模式"`
}
type UpdateSyncSettingResponse struct {
Sync bool
SyncError string
}
// ArchiveRequest 归档脚本
type ArchiveRequest struct {
mux.Meta `path:"/scripts/:id/archive" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
Archive bool `json:"archive" binding:"omitempty,required"`
}
type ArchiveResponse struct {
}
// DeleteRequest 删除脚本
type DeleteRequest struct {
mux.Meta `path:"/scripts/:id" method:"DELETE"`
ID int64 `uri:"id" binding:"required"`
Reason string `form:"reason" json:"reason" binding:"max=1024"` // 删除原因(可选)
}
type DeleteResponse struct {
}
// UpdateCodeSettingRequest 更新脚本设置
type UpdateCodeSettingRequest struct {
mux.Meta `path:"/scripts/:id/code/:codeId" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
CodeID int64 `uri:"codeId" binding:"required"`
Changelog string `json:"changelog" binding:"max=102400" label:"更新日志"`
IsPreRelease script_entity.EnablePreRelease `json:"is_pre_release" binding:"oneof=1 2" label:"是否预发布"`
}
type UpdateCodeSettingResponse struct {
}
// UpdateScriptPublicRequest 更新脚本公开类型
type UpdateScriptPublicRequest struct {
mux.Meta `path:"/scripts/:id/public" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
Public script_entity.Public `json:"public" binding:"required,oneof=1 2 3" label:"公开类型"`
}
type UpdateScriptPublicResponse struct {
}
// UpdateScriptUnwellRequest 更新脚本不适内容
type UpdateScriptUnwellRequest struct {
mux.Meta `path:"/scripts/:id/unwell" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
Unwell script_entity.UnwellContent `json:"unwell" binding:"required,oneof=1 2" label:"不适内容"`
}
type UpdateScriptUnwellResponse struct {
}
// UpdateScriptGrayRequest 更新脚本灰度策略
type UpdateScriptGrayRequest struct {
mux.Meta `path:"/scripts/:id/gray" method:"PUT"`
EnablePreRelease script_entity.EnablePreRelease `json:"enable_pre_release" binding:"oneof=1 2" label:"是否开启预发布"`
GrayControls []*script_entity.GrayControl `json:"gray_controls" binding:"required" label:"灰度策略"`
}
type UpdateScriptGrayResponse struct {
}
// WebhookRequest 处理webhook请求
type WebhookRequest struct {
mux.Meta `path:"/webhook/:user_id" method:"POST"`
UserID int64 `uri:"user_id" binding:"required"`
UA string `header:"User-Agent" binding:"required"`
XHubSignature256 string `header:"X-Hub-Signature-256" binding:"required"`
}
type WebhookResponse struct {
ErrorMessages map[string]string `json:"error_messages"`
}
// LastScoreRequest 最新评分脚本
type LastScoreRequest struct {
mux.Meta `path:"/scripts/last-score" method:"GET"`
httputils.PageRequest `form:",inline"`
}
type LastScoreResponse struct {
httputils.PageResponse[*Script] `json:",inline"`
}
// RecordVisitRequest 记录脚本访问统计
type RecordVisitRequest struct {
mux.Meta `path:"/scripts/:id/visit" method:"POST"`
ID int64 `uri:"id" binding:"required"`
}
type RecordVisitResponse struct {
}