/
user_history.go
79 lines (76 loc) · 2.51 KB
/
user_history.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
/*
* @Author: SpenserCai
* @Date: 2023-10-04 20:26:32
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-10-18 22:08:17
* @Description: file content
*/
package business
import (
"encoding/json"
"strings"
"github.com/SpenserCai/sd-webui-discord/api/gen/models"
ServiceOperations "github.com/SpenserCai/sd-webui-discord/api/gen/restapi/operations/user"
"github.com/SpenserCai/sd-webui-discord/global"
DbotUser "github.com/SpenserCai/sd-webui-discord/user"
"github.com/SpenserCai/sd-webui-go/intersvc"
"github.com/go-openapi/runtime/middleware"
)
func (b BusinessBase) SetUserHistoryHandler() {
global.ApiService.UserUserHistoryHandler = ServiceOperations.UserHistoryHandlerFunc(func(params ServiceOperations.UserHistoryParams, principal interface{}) middleware.Responder {
history, total, err := global.UserCenterSvc.GetUserHistoryList(principal.(DbotUser.UserInfo).Id, params.Body.Query.Command, int(params.Body.PageInfo.Page), int(params.Body.PageInfo.PageSize), false)
if err != nil {
return ServiceOperations.NewUserHistoryOK().WithPayload(&models.HistoryList{
Code: -1,
Message: err.Error(),
})
}
userInfo, err := global.UserCenterSvc.GetUserInfo(principal.(DbotUser.UserInfo).Id)
if err != nil {
return ServiceOperations.NewUserHistoryOK().WithPayload(&models.HistoryList{
Code: -1,
Message: err.Error(),
})
}
historyRes := make([]*models.HistoryItem, 0)
for _, item := range history {
historyRes = append(historyRes, &models.HistoryItem{
ID: item.MessageID,
Command: item.CommandName,
Options: func() interface{} {
option := &intersvc.SdapiV1Txt2imgRequest{}
json.Unmarshal([]byte(item.OptionJson), &option)
return option
}(),
Created: item.Created,
UserID: item.UserID,
UserName: userInfo.Name,
UserAvatar: func() string {
if userInfo.Avatar == "" {
return "https://cdn.discordapp.com/embed/avatars/0.png"
}
return userInfo.Avatar
}(),
Images: strings.Split(item.Images, ","),
ImagesBlurhash: func() []string {
blurs := make([]string, 0)
json.Unmarshal([]byte(item.ImageBlurHashs), &blurs)
return blurs
}(),
})
}
return ServiceOperations.NewUserHistoryOK().WithPayload(&models.HistoryList{
Code: 0,
Message: "success",
Data: &models.HistoryListData{
History: historyRes,
PageInfo: &models.PageInfoResponse{
Page: params.Body.PageInfo.Page,
PageSize: params.Body.PageInfo.PageSize,
Total: int32(total),
},
},
})
})
}