Skip to content

Commit

Permalink
feat: Complete **SPAM** blocking of critical data apis.
Browse files Browse the repository at this point in the history
  • Loading branch information
haitaoo committed Jul 21, 2023
1 parent c9921b9 commit fa54cbc
Show file tree
Hide file tree
Showing 25 changed files with 968 additions and 767 deletions.
36 changes: 30 additions & 6 deletions ui/src/common/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export interface FormDataType {
[prop: string]: FormValue;
}

export interface FieldError {
error_field: string;
error_msg: string;
}

export interface Paging {
page: number;
page_size?: number;
Expand Down Expand Up @@ -52,7 +57,7 @@ export interface TagInfo extends TagBase {
main_tag_slug_name?: string;
excerpt?;
}
export interface QuestionParams {
export interface QuestionParams extends ImgCodeReq{
title: string;
url_title?: string;
content: string;
Expand All @@ -68,7 +73,7 @@ export interface ListResult<T = any> {
list: T[];
}

export interface AnswerParams {
export interface AnswerParams extends ImgCodeReq {
content: string;
html: string;
question_id: string;
Expand Down Expand Up @@ -169,10 +174,29 @@ export interface PasswordResetReq extends ImgCodeReq {
e_mail: string;
}

export interface CheckImgReq {
action: 'login' | 'e_mail' | 'find_pass' | 'modify_pass';
export interface PasswordReplaceReq extends ImgCodeReq {
code: string;
pass: string;
}

export interface CaptchaReq extends ImgCodeReq {
verify: ImgCodeRes['verify'];
}

export type CaptchaKey =
| 'email'
| 'password'
| 'edit_userinfo'
| 'question'
| 'answer'
| 'comment'
| 'edit'
| 'invitation_answer'
| 'search'
| 'report'
| 'delete'
| 'vote';

export interface SetNoticeReq {
notice_switch: boolean;
}
Expand Down Expand Up @@ -222,7 +246,7 @@ export interface AnswerItem {
[prop: string]: any;
}

export interface PostAnswerReq {
export interface PostAnswerReq extends ImgCodeReq {
content: string;
html?: string;
question_id: string;
Expand Down Expand Up @@ -425,7 +449,7 @@ export interface FollowParams {
/**
* @description search request params
*/
export interface SearchParams {
export interface SearchParams extends ImgCodeReq {
q: string;
order: string;
page: number;
Expand Down
59 changes: 37 additions & 22 deletions ui/src/components/Actions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import classNames from 'classnames';

import { Icon } from '@/components';
import { loggedUserInfoStore } from '@/stores';
import { useToast } from '@/hooks';
import { useToast, useCaptchaModal } from '@/hooks';
import { tryNormalLogged } from '@/utils/guard';
import { bookmark, postVote } from '@/services';
import * as Types from '@/common/interface';

interface Props {
className?: string;
Expand Down Expand Up @@ -36,6 +37,8 @@ const Index: FC<Props> = ({ className, data, source }) => {
const { username = '' } = loggedUserInfoStore((state) => state.user);
const toast = useToast();
const { t } = useTranslation();
const vCaptcha = useCaptchaModal('vote');

useEffect(() => {
if (data) {
setVotes(data.votesCount);
Expand All @@ -61,27 +64,39 @@ const Index: FC<Props> = ({ className, data, source }) => {
return;
}
const isCancel = (type === 'up' && like) || (type === 'down' && hate);
postVote(
{
object_id: data?.id,
is_cancel: isCancel,
},
type,
)
.then((res) => {
setVotes(res.votes);
setLike(res.vote_status === 'vote_up');
setHated(res.vote_status === 'vote_down');
})
.catch((err) => {
const errMsg = err?.value;
if (errMsg) {
toast.onShow({
msg: errMsg,
variant: 'danger',
});
}
});
vCaptcha.check(() => {
const imgCode: Types.ImgCodeReq = {
captcha_id: undefined,
captcha_code: undefined,
};
vCaptcha.resolveCaptchaReq(imgCode);
postVote(
{
object_id: data?.id,
is_cancel: isCancel,
...imgCode,
},
type,
)
.then(async (res) => {
await vCaptcha.close();
setVotes(res.votes);
setLike(res.vote_status === 'vote_up');
setHated(res.vote_status === 'vote_down');
})
.catch((err) => {
if (err?.isError) {
vCaptcha.handleCaptchaError(err.list);
}
const errMsg = err?.value;
if (errMsg) {
toast.onShow({
msg: errMsg,
variant: 'danger',
});
}
});
});
};

const handleBookmark = () => {
Expand Down
169 changes: 113 additions & 56 deletions ui/src/components/Comment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { unionBy } from 'lodash';

import * as Types from '@/common/interface';
import { Modal } from '@/components';
import { usePageUsers, useReportModal } from '@/hooks';
import { usePageUsers, useReportModal, useCaptchaModal } from '@/hooks';
import {
matchedUsers,
parseUserInfo,
Expand Down Expand Up @@ -43,6 +43,11 @@ const Comment = ({ objectId, mode, commentId }) => {

const reportModal = useReportModal();

const addCaptcha = useCaptchaModal('comment');
const editCaptcha = useCaptchaModal('edit');
const dCaptcha = useCaptchaModal('delete');
const vCaptcha = useCaptchaModal('vote');

const { t } = useTranslation('translation', { keyPrefix: 'comment' });
const scrollCallback = useCallback((el, co) => {
if (pageIndex === 0 && co.comment_id === commentId) {
Expand Down Expand Up @@ -120,43 +125,67 @@ const Comment = ({ objectId, mode, commentId }) => {
};

if (item.type === 'edit') {
return updateComment({
...params,
comment_id: item.comment_id,
}).then((res) => {
setComments(
comments.map((comment) => {
if (comment.comment_id === item.comment_id) {
comment.showEdit = false;
comment.parsed_text = res.parsed_text;
comment.original_text = res.original_text;
return editCaptcha.check(() => {
const up = {
...params,
comment_id: item.comment_id,
captcha_code: undefined,
captcha_id: undefined,
};
editCaptcha.resolveCaptchaReq(up);

return updateComment(up)
.then(async (res) => {
await editCaptcha.close();
setComments(
comments.map((comment) => {
if (comment.comment_id === item.comment_id) {
comment.showEdit = false;
comment.parsed_text = res.parsed_text;
comment.original_text = res.original_text;
}
return comment;
}),
);
})
.catch((err) => {
if (err.isError) {
editCaptcha.handleCaptchaError(err.list);
}
return comment;
}),
);
});
});
}
return addComment(params).then((res) => {
if (item.type === 'reply') {
const index = comments.findIndex(
(comment) => comment.comment_id === item.comment_id,
);
comments[index].showReply = false;
comments.splice(index + 1, 0, res);
setComments([...comments]);
} else {
setComments([
...comments.map((comment) => {
if (comment.comment_id === item.comment_id) {
comment.showReply = false;
}
return comment;
}),
res,
]);
}

setVisibleComment(false);
return addCaptcha.check(() => {
const req = {
...params,
captcha_code: undefined,
captcha_id: undefined,
};
addCaptcha.resolveCaptchaReq(req);

return addComment(req).then((res) => {
if (item.type === 'reply') {
const index = comments.findIndex(
(comment) => comment.comment_id === item.comment_id,
);
comments[index].showReply = false;
comments.splice(index + 1, 0, res);
setComments([...comments]);
} else {
setComments([
...comments.map((comment) => {
if (comment.comment_id === item.comment_id) {
comment.showReply = false;
}
return comment;
}),
res,
]);
}

setVisibleComment(false);
});
});
};

Expand All @@ -167,11 +196,23 @@ const Comment = ({ objectId, mode, commentId }) => {
confirmBtnVariant: 'danger',
confirmText: t('delete', { keyPrefix: 'btns' }),
onConfirm: () => {
deleteComment(id).then(() => {
if (pageIndex === 0) {
mutate();
}
setComments(comments.filter((item) => item.comment_id !== id));
dCaptcha.check(() => {
const imgCode = { captcha_id: undefined, captcha_code: undefined };
dCaptcha.resolveCaptchaReq(imgCode);

deleteComment(id, imgCode)
.then(async () => {
await dCaptcha.close();
if (pageIndex === 0) {
mutate();
}
setComments(comments.filter((item) => item.comment_id !== id));
})
.catch((ex) => {
if (ex.isError) {
dCaptcha.handleCaptchaError(ex.list);
}
});
});
},
});
Expand All @@ -182,24 +223,40 @@ const Comment = ({ objectId, mode, commentId }) => {
return;
}

postVote(
{
object_id: id,
is_cancel,
},
'up',
).then(() => {
setComments(
comments.map((item) => {
if (item.comment_id === id) {
item.vote_count = is_cancel
? item.vote_count - 1
: item.vote_count + 1;
item.is_vote = !is_cancel;
vCaptcha.check(() => {
const imgCode: Types.ImgCodeReq = {
captcha_id: undefined,
captcha_code: undefined,
};
vCaptcha.resolveCaptchaReq(imgCode);

postVote(
{
object_id: id,
is_cancel,
...imgCode,
},
'up',
)
.then(async () => {
await vCaptcha.close();
setComments(
comments.map((item) => {
if (item.comment_id === id) {
item.vote_count = is_cancel
? item.vote_count - 1
: item.vote_count + 1;
item.is_vote = !is_cancel;
}
return item;
}),
);
})
.catch((ex) => {
if (ex.isError) {
vCaptcha.handleCaptchaError(ex.list);
}
return item;
}),
);
});
});
};

Expand Down
Loading

0 comments on commit fa54cbc

Please sign in to comment.