chore: sync feat in redeem table

This commit is contained in:
Zhang Minghan 2024-03-09 17:29:48 +08:00
parent 19c09aba62
commit 9f41572b40
5 changed files with 36 additions and 19 deletions

View File

@ -29,7 +29,7 @@ func formatResponse(data *DDGResponse) string {
return strings.Join(res, "\n")
}
func CallDuckDuckGoAPI(query string) *DDGResponse {
func CallDuckDuckGoAPI(query string) (*DDGResponse, error) {
data, err := utils.Get(fmt.Sprintf(
"%s/search?q=%s&max_results=%d",
channel.SystemInstance.GetSearchEndpoint(),
@ -38,8 +38,8 @@ func CallDuckDuckGoAPI(query string) *DDGResponse {
), nil)
if err != nil {
return nil
return nil, err
}
return utils.MapToStruct[DDGResponse](data)
return utils.MapToRawStruct[DDGResponse](data)
}

View File

@ -1,7 +1,9 @@
package web
import (
"chat/globals"
"chat/utils"
"fmt"
"net/url"
)
@ -22,13 +24,8 @@ func RequestWithUA(url string) string {
return data
}
func SearchWebResult(q string) string {
if res := CallDuckDuckGoAPI(q); res != nil {
if resp := formatResponse(res); resp != "" {
return resp
}
}
func SearchReverse(q string) string {
// deprecated
uri := GetBingUrl(q)
if res := CallPilotAPI(uri); res != nil {
return utils.Marshal(res.Results)
@ -36,3 +33,15 @@ func SearchWebResult(q string) string {
data := RequestWithUA(uri)
return ParseBing(data)
}
func SearchWebResult(q string) string {
res, err := CallDuckDuckGoAPI(q)
if err != nil {
globals.Warn(fmt.Sprintf("[web] failed to get search result: %s (query: %s)", err.Error(), q))
return ""
}
content := formatResponse(res)
globals.Debug(fmt.Sprintf("[web] search result: %s (query: %s)", utils.Extract(content, 50, "..."), q))
return content
}

View File

@ -27,7 +27,7 @@ import { Textarea } from "@/components/ui/textarea.tsx";
import { saveAsFile } from "@/utils/dom.ts";
import { useEffectAsync } from "@/utils/hook.ts";
function GenerateDialog() {
function GenerateDialog({ sync }: { sync: () => void }) {
const { t } = useTranslation();
const { toast } = useToast();
const [open, setOpen] = useState<boolean>(false);
@ -41,12 +41,15 @@ function GenerateDialog() {
async function generateCode() {
const data = await generateRedeem(Number(quota), Number(number));
if (data.status) setData(data.data.join("\n"));
else
if (data.status) {
setData(data.data.join("\n"));
sync();
} else {
toast({
title: t("admin.error"),
description: data.message,
});
}
}
function close() {
@ -174,7 +177,7 @@ function RedeemTable() {
<Button variant={`outline`} size={`icon`} onClick={sync}>
<RotateCw className={`h-4 w-4`} />
</Button>
<GenerateDialog />
<GenerateDialog sync={sync} />
</div>
</div>
);

View File

@ -142,7 +142,7 @@ func (c *SystemConfig) GetInitialQuota() float64 {
}
func (c *SystemConfig) GetBackend() string {
return c.General.Backend
return strings.TrimSuffix(c.General.Backend, "/")
}
func (c *SystemConfig) GetMail() *utils.SmtpPoster {
@ -209,10 +209,10 @@ func (c *SystemConfig) GetSearchEndpoint() string {
}
endpoint := c.Search.Endpoint
if strings.HasSuffix(endpoint, "/") {
return endpoint[:len(endpoint)-1]
} else if !strings.HasSuffix(endpoint, "/search") {
if strings.HasSuffix(endpoint, "/search") {
return endpoint[:len(endpoint)-7]
} else if strings.HasSuffix(endpoint, "/") {
return endpoint[:len(endpoint)-1]
}
return endpoint

View File

@ -169,7 +169,12 @@ func SplitLangItems(data string) []string {
return SplitItems(data, []string{",", "", " ", "\n"})
}
func Extract(data string, length int, flow string) string {
func Extract(data string, length int, flow_ ...string) string {
flow := ""
if len(flow_) > 0 {
flow = flow_[0]
}
value := []rune(data)
if len(value) > length {
return string(value[:length]) + flow