Skip to content

Commit

Permalink
feat: smtp test feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 27, 2023
1 parent d278f24 commit d19ce5e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 22 deletions.
15 changes: 8 additions & 7 deletions app/src/dialogs/PackageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Separator } from "@/components/ui/separator.tsx";
import { Badge } from "@/components/ui/badge.tsx";
import { useEffectAsync } from "@/utils/hook.ts";
import { selectAuthenticated } from "@/store/auth.ts";
import { deeptrainEndpoint } from "@/utils/env.ts";
import { deeptrainEndpoint, useDeeptrain } from "@/utils/env.ts";

function PackageDialog() {
const { t } = useTranslation();
Expand All @@ -33,13 +33,14 @@ function PackageDialog() {
const teenager = useSelector(teenagerSelector);
const auth = useSelector(selectAuthenticated);

useEffectAsync(async () => {
if (!auth) return;
const task = setInterval(() => refreshPackage(dispatch), 20000);
await refreshPackage(dispatch);
useDeeptrain &&
useEffectAsync(async () => {
if (!auth) return;
const task = setInterval(() => refreshPackage(dispatch), 20000);
await refreshPackage(dispatch);

return () => clearInterval(task);
}, [auth]);
return () => clearInterval(task);
}, [auth]);

return (
<Dialog open={open} onOpenChange={(open) => dispatch(setDialog(open))}>
Expand Down
1 change: 1 addition & 0 deletions app/src/resources/i18n/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
"search": "联网搜索",
"mail": "SMTP 发件设置",
"save": "保存",
"test": "测试发件",
"backend": "后端域名",
"backendTip": "后端回调域名(docker 安装默认路径为 /api),接收回调参数。",
"mailHost": "发件域名",
Expand Down
72 changes: 64 additions & 8 deletions app/src/routes/admin/System.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Paragraph, {
import { Button } from "@/components/ui/button.tsx";
import { Label } from "@/components/ui/label.tsx";
import { Input } from "@/components/ui/input.tsx";
import { useReducer } from "react";
import { useReducer, useState } from "react";
import { formReducer } from "@/utils/form.ts";
import { NumberInput } from "@/components/ui/number-input.tsx";
import {
Expand All @@ -27,12 +27,22 @@ import {
} from "@/admin/api/system.ts";
import { useEffectAsync } from "@/utils/hook.ts";
import { toastState } from "@/admin/utils.ts";
import { useToast } from "@/components/ui/use-toast.ts";
import { toast, useToast } from "@/components/ui/use-toast.ts";
import { doVerify } from "@/api/auth.ts";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTrigger,
} from "@/components/ui/dialog.tsx";
import { DialogTitle } from "@radix-ui/react-dialog";

type CompProps<T> = {
data: T;
dispatch: (action: any) => void;
onChange: () => void;
onChange: (doToast?: boolean) => Promise<void>;
};

function General({ data, dispatch, onChange }: CompProps<GeneralState>) {
Expand Down Expand Up @@ -72,6 +82,18 @@ function General({ data, dispatch, onChange }: CompProps<GeneralState>) {

function Mail({ data, dispatch, onChange }: CompProps<MailState>) {
const { t } = useTranslation();
const [email, setEmail] = useState<string>("");

const [mailDialog, setMailDialog] = useState<boolean>(false);

const onTest = async () => {
if (!email.trim()) return;
await onChange(false);
const res = await doVerify(email);
toastState(toast, t, res, true);

if (res.status) setMailDialog(false);
};

return (
<Paragraph
Expand Down Expand Up @@ -145,6 +167,39 @@ function Mail({ data, dispatch, onChange }: CompProps<MailState>) {
</ParagraphItem>
<ParagraphFooter>
<div className={`grow`} />
<Dialog open={mailDialog} onOpenChange={setMailDialog}>
<DialogTrigger>
<Button variant={`outline`} size={`sm`} loading={true}>
{t("admin.system.test")}
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>{t("admin.system.test")}</DialogTitle>
<DialogDescription className={`pt-2`}>
<Input
placeholder={t("auth.email-placeholder")}
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
variant={`outline`}
onClick={() => {
setEmail("");
setMailDialog(false);
}}
>
{t("admin.cancel")}
</Button>
<Button variant={`default`} loading={true} onClick={onTest}>
{t("admin.confirm")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<Button size={`sm`} loading={true} onClick={onChange}>
{t("admin.system.save")}
</Button>
Expand Down Expand Up @@ -206,9 +261,10 @@ function System() {
initialSystemState,
);

const save = async () => {
const doSaving = async (doToast?: boolean) => {
const res = await setConfig(data);
toastState(toast, t, res, true);

if (doToast !== false) toastState(toast, t, res, true);
};

useEffectAsync(async () => {
Expand All @@ -226,9 +282,9 @@ function System() {
<CardTitle>{t("admin.settings")}</CardTitle>
</CardHeader>
<CardContent className={`flex flex-col gap-1`}>
<General data={data.general} dispatch={setData} onChange={save} />
<Mail data={data.mail} dispatch={setData} onChange={save} />
<Search data={data.search} dispatch={setData} onChange={save} />
<General data={data.general} dispatch={setData} onChange={doSaving} />
<Mail data={data.mail} dispatch={setData} onChange={doSaving} />
<Search data={data.search} dispatch={setData} onChange={doSaving} />
</CardContent>
</Card>
</div>
Expand Down
12 changes: 5 additions & 7 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,15 @@ func Verify(c *gin.Context, email string) error {

provider := channel.SystemInstance.GetMail()

template, err := provider.RenderTemplate("code.html", struct {
Code string
}{Code: code})
if err != nil {
return err
type Temp struct {
Code string `json:"code"`
}

return provider.SendMail(
return provider.RenderMail(
"code.html",
Temp{Code: code},
email,
"Chat Nio | OTP Verification",
template,
)
}

Expand Down
13 changes: 13 additions & 0 deletions utils/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func NewSmtpPoster(host string, port int, username string, password string, from
}

func (s *SmtpPoster) SendMail(to string, subject string, body string) error {
if s.Host == "" || s.Port <= 0 || s.Port > 65535 || s.Username == "" || s.Password == "" || s.From == "" {
return fmt.Errorf("smtp not configured properly")
}

addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
auth := smtp.PlainAuth("", s.From, s.Password, s.Host)

Expand All @@ -54,6 +58,15 @@ func (s *SmtpPoster) RenderTemplate(filename string, data interface{}) (string,
return buf.String(), nil
}

func (s *SmtpPoster) RenderMail(filename string, data interface{}, to string, subject string) error {
body, err := s.RenderTemplate(filename, data)
if err != nil {
return err
}

return s.SendMail(to, subject, body)
}

func dial(addr string) (*smtp.Client, error) {
conn, err := tls.Dial("tcp", addr, nil)
if err != nil {
Expand Down

0 comments on commit d19ce5e

Please sign in to comment.