Skip to content

Commit

Permalink
chore: sync feat in redeem table
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 9, 2024
1 parent 19c09ab commit 9f41572
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
6 changes: 3 additions & 3 deletions addition/web/duckduckgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
}
23 changes: 16 additions & 7 deletions addition/web/search.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package web

import (
"chat/globals"
"chat/utils"
"fmt"
"net/url"
)

Expand All @@ -22,17 +24,24 @@ 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)
}
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
}
11 changes: 7 additions & 4 deletions app/src/components/admin/RedeemTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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>
);
Expand Down
8 changes: 4 additions & 4 deletions channel/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion utils/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f41572

Please sign in to comment.