feat: merge alex version
This commit is contained in:
@@ -102,8 +102,13 @@ const baseQueryWithTokenCheck = async (args: any, api: any, extraOptions: any) =
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 403:
|
||||
toast.error("Request Not Allowed");
|
||||
case 403: {
|
||||
const whiteList403 = ["sendMsg"];
|
||||
if (!whiteList403.includes(api.endpoint)) {
|
||||
toast.error("Request Not Allowed");
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case 404:
|
||||
{
|
||||
|
||||
@@ -117,6 +117,25 @@ export const channelApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
createPrivateInviteLink: builder.query<string, number | void>({
|
||||
query: (gid) => ({
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
accept: "text/plain"
|
||||
},
|
||||
// 七天过期
|
||||
url: `/group/create_invite_private_magic_link?expired_in=604800&max_times=1&gid=${gid}`,
|
||||
responseHandler: "text"
|
||||
}),
|
||||
transformResponse: (link: string) => {
|
||||
// 确保http开头
|
||||
const _link = link.startsWith("http") ? link : `http://${link}`;
|
||||
// return _link;
|
||||
// 替换掉域名
|
||||
const invite = new URL(_link);
|
||||
return `${location.origin}${invite.pathname}${invite.hash}${invite.search}`;
|
||||
}
|
||||
}),
|
||||
removeChannel: builder.query<void, number>({
|
||||
query: (id) => ({
|
||||
url: `/group/${id}`,
|
||||
@@ -186,6 +205,13 @@ export const channelApi = createApi({
|
||||
body: members
|
||||
})
|
||||
}),
|
||||
joinPrivateChannel: builder.mutation<Channel, { magic_token: string }>({
|
||||
query: (body) => ({
|
||||
url: `/user/join_private`,
|
||||
method: "POST",
|
||||
body
|
||||
})
|
||||
}),
|
||||
updateIcon: builder.mutation<void, { gid: number; image: File }>({
|
||||
query: ({ gid, image }) => ({
|
||||
headers: {
|
||||
@@ -216,6 +242,8 @@ export const {
|
||||
useChangeChannelTypeMutation,
|
||||
useLazyLeaveChannelQuery,
|
||||
useLazyCreateInviteLinkQuery,
|
||||
useJoinPrivateChannelMutation,
|
||||
useLazyCreatePrivateInviteLinkQuery,
|
||||
useCreateInviteLinkQuery,
|
||||
useGetChannelQuery,
|
||||
useLazyGetChannelQuery,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { batch } from "react-redux";
|
||||
import { ContentTypes } from "../config";
|
||||
import { addChannelMsg, removeChannelMsg } from "../slices/message.channel";
|
||||
import { addUserMsg, removeUserMsg } from "../slices/message.user";
|
||||
import { addMessage, removeMessage } from "../slices/message";
|
||||
import { addMessage, removeMessage, updateMessage } from "../slices/message";
|
||||
// import { ContentType } from "../../types/message";
|
||||
|
||||
export const onMessageSendStarted = async (
|
||||
@@ -57,10 +57,16 @@ export const onMessageSendStarted = async (
|
||||
dispatch(removeMessage(ts));
|
||||
}, 300);
|
||||
// dispatch(removePendingMessage({ id, mid:ts, type: from }));
|
||||
} catch {
|
||||
toast.error("Send Message Failed");
|
||||
dispatch(removeContextMessage({ id, mid: ts }));
|
||||
dispatch(removeMessage(ts));
|
||||
} catch (error) {
|
||||
if (error?.error?.status == 403) {
|
||||
// 403 means blocked
|
||||
// toast.error(`Failed to send, blocked maybe.`,);
|
||||
dispatch(updateMessage({ mid: ts, failed: true }));
|
||||
} else {
|
||||
toast.error(`Send Message Failed ${JSON.stringify(error)}`,);
|
||||
dispatch(removeContextMessage({ id, mid: ts }));
|
||||
dispatch(removeMessage(ts));
|
||||
}
|
||||
// patchResult.undo();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,18 +2,19 @@ import { createApi } from "@reduxjs/toolkit/query/react";
|
||||
// import toast from "react-hot-toast";
|
||||
import baseQuery from "./base.query";
|
||||
import { updateAutoDeleteSetting, updateMute } from "../slices/footprint";
|
||||
import { fillUsers } from "../slices/users";
|
||||
import { fillUsers, updateContactStatus as updateStatus } from "../slices/users";
|
||||
import BASE_URL, { ContentTypes } from "../config";
|
||||
import { onMessageSendStarted } from "./handlers";
|
||||
import { AutoDeleteMsgDTO, BotAPIKey, User, UserCreateDTO, UserDTO, UserForAdmin, UserForAdminDTO } from "../../types/user";
|
||||
import { AutoDeleteMsgDTO, BotAPIKey, ContactAction, ContactResponse, ContactStatus, User, UserCreateDTO, UserDTO, UserForAdmin, UserForAdminDTO } from "../../types/user";
|
||||
import { ContentTypeKey, MuteDTO } from "../../types/message";
|
||||
import { RootState } from "../store";
|
||||
|
||||
export const userApi = createApi({
|
||||
reducerPath: "userApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
getUsers: builder.query<User[], void>({
|
||||
query: () => ({ url: `user` }),
|
||||
query: () => ({ url: `/user` }),
|
||||
transformResponse: (data: User[]) => {
|
||||
return data.map((user) => {
|
||||
return {
|
||||
@@ -25,12 +26,38 @@ export const userApi = createApi({
|
||||
};
|
||||
});
|
||||
},
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled, getState }) {
|
||||
try {
|
||||
const { data: users } = await queryFulfilled;
|
||||
const { authData: { user: loginUser } } = getState() as RootState;
|
||||
dispatch(fillUsers(users.map((u) => {
|
||||
const status = loginUser?.uid == u.uid ? "added" : "";
|
||||
return {
|
||||
...u,
|
||||
status
|
||||
};
|
||||
})));
|
||||
} catch {
|
||||
console.log("get user list error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
getContacts: builder.query<ContactResponse[], void>({
|
||||
query: () => ({ url: `/user/contacts` }),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data: users } = await queryFulfilled;
|
||||
dispatch(fillUsers(users));
|
||||
const payloads = users.map((c) => {
|
||||
const uid = c.target_uid;
|
||||
const status = c.contact_info.status;
|
||||
return {
|
||||
uid,
|
||||
status
|
||||
};
|
||||
});
|
||||
dispatch(updateStatus(payloads));
|
||||
} catch {
|
||||
console.log("get user list error");
|
||||
console.log("get contact list error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
@@ -44,6 +71,27 @@ export const userApi = createApi({
|
||||
method: "POST"
|
||||
})
|
||||
}),
|
||||
searchUser: builder.mutation<User, { search_type: "id" | "email", keyword: string }>({
|
||||
query: (input) => ({
|
||||
url: `/user/search`,
|
||||
body: input,
|
||||
method: "POST"
|
||||
})
|
||||
}),
|
||||
pinChat: builder.mutation<void, { uid: number } | { gid: number }>({
|
||||
query: (data) => ({
|
||||
url: `/user/pin_chat`,
|
||||
method: "POST",
|
||||
body: { target: data }
|
||||
})
|
||||
}),
|
||||
unpinChat: builder.mutation<void, { uid: number } | { gid: number }>({
|
||||
query: (data) => ({
|
||||
url: `/user/unpin_chat`,
|
||||
method: "POST",
|
||||
body: { target: data }
|
||||
})
|
||||
}),
|
||||
updateUser: builder.mutation<UserForAdmin, UserForAdminDTO>({
|
||||
query: ({ id, ...rest }) => ({
|
||||
url: `/admin/user/${id}`,
|
||||
@@ -75,6 +123,28 @@ export const userApi = createApi({
|
||||
}
|
||||
}),
|
||||
|
||||
updateContactStatus: builder.mutation<void, { action: ContactAction, target_uid: number }>({
|
||||
query: (payload) => ({
|
||||
url: `/user/update_contact_status`,
|
||||
method: "POST",
|
||||
body: payload
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
const map = {
|
||||
"add": "added",
|
||||
"block": "blocked",
|
||||
"remove": "",
|
||||
"unblock": "",
|
||||
};
|
||||
try {
|
||||
await queryFulfilled;
|
||||
const status = map[data.action] as ContactStatus;
|
||||
dispatch(updateStatus({ uid: data.target_uid, status }));
|
||||
} catch (error) {
|
||||
console.log("update mute failed", error);
|
||||
}
|
||||
}
|
||||
}),
|
||||
updateMuteSetting: builder.mutation<void, MuteDTO>({
|
||||
query: (data) => ({
|
||||
url: `/user/mute`,
|
||||
@@ -152,6 +222,7 @@ export const userApi = createApi({
|
||||
body: type == "file" ? JSON.stringify(content) : content
|
||||
}),
|
||||
async onQueryStarted(param1, param2) {
|
||||
// @ts-ignore
|
||||
await onMessageSendStarted.call(this, param1, param2, "user");
|
||||
}
|
||||
}),
|
||||
@@ -160,6 +231,7 @@ export const userApi = createApi({
|
||||
});
|
||||
|
||||
export const {
|
||||
useLazyGetUsersQuery,
|
||||
useGetUserByAdminQuery,
|
||||
useUpdateAvatarByAdminMutation,
|
||||
useUpdateAutoDeleteMsgMutation,
|
||||
@@ -169,10 +241,13 @@ export const {
|
||||
useLazyDeleteUserQuery,
|
||||
useUpdateInfoMutation,
|
||||
useUpdateAvatarMutation,
|
||||
useGetUsersQuery,
|
||||
useLazyGetUsersQuery,
|
||||
useLazyGetContactsQuery,
|
||||
useSendMsgMutation,
|
||||
useCreateBotAPIKeyMutation,
|
||||
useLazyDeleteBotAPIKeyQuery,
|
||||
useGetBotAPIKeysQuery
|
||||
useGetBotAPIKeysQuery,
|
||||
useSearchUserMutation,
|
||||
useUpdateContactStatusMutation,
|
||||
usePinChatMutation,
|
||||
useUnpinChatMutation
|
||||
} = userApi;
|
||||
|
||||
Reference in New Issue
Block a user