refactor: rename contact with user
This commit is contained in:
@@ -3,6 +3,7 @@ import toast from "react-hot-toast";
|
||||
import dayjs from "dayjs";
|
||||
import { updateToken, resetAuthData } from "../slices/auth.data";
|
||||
import BASE_URL, { tokenHeader } from "../config";
|
||||
import { RootState } from "../store";
|
||||
|
||||
const whiteList = [
|
||||
"login",
|
||||
@@ -28,7 +29,7 @@ const baseQuery = fetchBaseQuery({
|
||||
baseUrl: BASE_URL,
|
||||
prepareHeaders: (headers, { getState, endpoint }) => {
|
||||
console.log("req", endpoint);
|
||||
const { token } = getState().authData;
|
||||
const { token } = (getState() as RootState).authData;
|
||||
if (token && !whiteList.includes(endpoint)) {
|
||||
headers.set(tokenHeader, token);
|
||||
}
|
||||
|
||||
+12
-11
@@ -9,19 +9,20 @@ import { removeChannelSession } from "../slices/message.channel";
|
||||
import { removeReactionMessage } from "../slices/message.reaction";
|
||||
import { onMessageSendStarted } from "./handlers";
|
||||
import handleChatMessage from "../../common/hook/useStreaming/chat.handler";
|
||||
|
||||
import { Channel, CreateChannelDTO } from "../../types/channel";
|
||||
import { RootState } from "../store";
|
||||
export const channelApi = createApi({
|
||||
reducerPath: "channelApi",
|
||||
baseQuery,
|
||||
refetchOnFocus: true,
|
||||
endpoints: (builder) => ({
|
||||
getChannels: builder.query({
|
||||
getChannels: builder.query<Channel[], void>({
|
||||
query: () => ({ url: `group` })
|
||||
}),
|
||||
getChannel: builder.query({
|
||||
getChannel: builder.query<Channel, number>({
|
||||
query: (id) => ({ url: `group/${id}` })
|
||||
}),
|
||||
leaveChannel: builder.query({
|
||||
leaveChannel: builder.query<void, number>({
|
||||
query: (id) => ({ url: `group/${id}/leave` }),
|
||||
async onQueryStarted(gid, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
@@ -32,7 +33,7 @@ export const channelApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
createChannel: builder.mutation({
|
||||
createChannel: builder.mutation<number, CreateChannelDTO>({
|
||||
query: (data) => ({
|
||||
url: "group",
|
||||
method: "POST",
|
||||
@@ -66,7 +67,7 @@ export const channelApi = createApi({
|
||||
const { data: messages } = await queryFulfilled;
|
||||
if (messages?.length) {
|
||||
messages.forEach((msg) => {
|
||||
handleChatMessage(msg, dispatch, getState());
|
||||
handleChatMessage(msg, dispatch, getState() as RootState);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -80,15 +81,15 @@ export const channelApi = createApi({
|
||||
url: gid
|
||||
? `/group/create_reg_magic_link?expired_in=3600&max_times=1&gid=${gid}`
|
||||
: `/group/create_reg_magic_link?expired_in=3600&max_times=1`,
|
||||
responseHandler: (response) => response.text()
|
||||
responseHandler: (response: Response) => response.text()
|
||||
}),
|
||||
transformResponse: (link) => {
|
||||
transformResponse: (link: string) => {
|
||||
// 替换掉域名
|
||||
const invite = new URL(link);
|
||||
return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`;
|
||||
}
|
||||
}),
|
||||
removeChannel: builder.query({
|
||||
removeChannel: builder.query<void, number>({
|
||||
query: (id) => ({
|
||||
url: `group/${id}`,
|
||||
method: "DELETE"
|
||||
@@ -99,7 +100,7 @@ export const channelApi = createApi({
|
||||
ui: {
|
||||
remeberedNavs: { chat: remeberedPath }
|
||||
}
|
||||
} = getState();
|
||||
} = getState() as RootState;
|
||||
try {
|
||||
await queryFulfilled;
|
||||
// 删掉该channel下的所有消息&reaction
|
||||
@@ -161,7 +162,7 @@ export const channelApi = createApi({
|
||||
await queryFulfilled;
|
||||
dispatch(
|
||||
updateChannel({
|
||||
id: gid,
|
||||
gid,
|
||||
icon: `${BASE_URL}/resource/group_avatar?gid=${gid}&t=${+new Date()}`
|
||||
})
|
||||
);
|
||||
|
||||
+24
-18
@@ -11,7 +11,16 @@ import { onMessageSendStarted } from "./handlers";
|
||||
import { normalizeArchiveData } from "../../common/utils";
|
||||
// import { updateMessage } from "../slices/message";
|
||||
import baseQuery from "./base.query";
|
||||
|
||||
import { OG } from "../../types/common";
|
||||
type UploadFileResponse = {
|
||||
path: string;
|
||||
size: number;
|
||||
hash: string;
|
||||
image_properties: {
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
};
|
||||
export const messageApi = createApi({
|
||||
reducerPath: "messageApi",
|
||||
baseQuery,
|
||||
@@ -25,25 +34,22 @@ export const messageApi = createApi({
|
||||
method: "PUT",
|
||||
body: content
|
||||
})
|
||||
// async onQueryStarted({mid,content},{dispatch}){
|
||||
// dispatch()
|
||||
// }
|
||||
}),
|
||||
reactMessage: builder.mutation({
|
||||
reactMessage: builder.mutation<number, { mid: number; action: string }>({
|
||||
query: ({ mid, action }) => ({
|
||||
url: `/message/${mid}/like`,
|
||||
method: "PUT",
|
||||
body: { action }
|
||||
})
|
||||
}),
|
||||
deleteMessage: builder.query({
|
||||
deleteMessage: builder.query<number, number>({
|
||||
query: (mid) => ({
|
||||
url: `/message/${mid}`,
|
||||
method: "DELETE"
|
||||
})
|
||||
}),
|
||||
prepareUploadFile: builder.mutation({
|
||||
query: (meta = {}) => ({
|
||||
prepareUploadFile: builder.mutation<string, { content_type: string; filename: string }>({
|
||||
query: (meta = { content_type: "", filename: "" }) => ({
|
||||
url: `/resource/file/prepare`,
|
||||
method: "POST",
|
||||
body: meta
|
||||
@@ -56,21 +62,18 @@ export const messageApi = createApi({
|
||||
body: { mid_list: mids }
|
||||
})
|
||||
}),
|
||||
uploadFile: builder.mutation({
|
||||
uploadFile: builder.mutation<UploadFileResponse | {}, FormData>({
|
||||
query: (formData) => ({
|
||||
// headers: {
|
||||
// "content-type": ContentTypes.formData,
|
||||
// },
|
||||
url: `/resource/file/upload`,
|
||||
method: "POST",
|
||||
body: formData
|
||||
}),
|
||||
transformResponse: (data) => {
|
||||
transformResponse: (data: UploadFileResponse | null) => {
|
||||
console.log("upload file response", data);
|
||||
return data ? data : {};
|
||||
}
|
||||
}),
|
||||
getOGInfo: builder.query({
|
||||
getOGInfo: builder.query<OG, string>({
|
||||
query: (url) => ({
|
||||
url: `/resource/open_graphic_parse?url=${encodeURIComponent(url)}`
|
||||
})
|
||||
@@ -80,14 +83,14 @@ export const messageApi = createApi({
|
||||
url: `/resource/archive?file_path=${encodeURIComponent(file_path)}`
|
||||
})
|
||||
}),
|
||||
pinMessage: builder.mutation({
|
||||
pinMessage: builder.mutation<void, { gid: number; mid: number }>({
|
||||
query: ({ gid, mid }) => ({
|
||||
url: `/group/${gid}/pin`,
|
||||
method: "POST",
|
||||
body: { mid }
|
||||
})
|
||||
}),
|
||||
unpinMessage: builder.mutation({
|
||||
unpinMessage: builder.mutation<void, { gid: number; mid: number }>({
|
||||
query: ({ gid, mid }) => ({
|
||||
url: `/group/${gid}/unpin`,
|
||||
method: "POST",
|
||||
@@ -111,7 +114,7 @@ export const messageApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
removeFavorite: builder.query({
|
||||
removeFavorite: builder.query<void, number>({
|
||||
query: (id) => ({
|
||||
url: `/favorite/${id}`,
|
||||
method: "DELETE"
|
||||
@@ -170,7 +173,10 @@ export const messageApi = createApi({
|
||||
await onMessageSendStarted.call(this, param1, param2, param1.context);
|
||||
}
|
||||
}),
|
||||
readMessage: builder.mutation({
|
||||
readMessage: builder.mutation<
|
||||
void,
|
||||
{ users?: [{ uid: number; mid: number }]; groups?: [{ gid: number; mid: number }] }
|
||||
>({
|
||||
query: (data) => ({
|
||||
url: `/user/read-index`,
|
||||
method: "POST",
|
||||
|
||||
@@ -4,14 +4,14 @@ import { KEY_UID } from "../config";
|
||||
import baseQuery from "./base.query";
|
||||
import { resetAuthData } from "../slices/auth.data";
|
||||
import { updateMute } from "../slices/footprint";
|
||||
import { fullfillContacts } from "../slices/contacts";
|
||||
import { fullfillContacts } from "../slices/users";
|
||||
import BASE_URL, { ContentTypes } from "../config";
|
||||
import { onMessageSendStarted } from "./handlers";
|
||||
import handleChatMessage from "../../common/hook/useStreaming/chat.handler";
|
||||
import { User } from "../../types/auth";
|
||||
|
||||
export const contactApi = createApi({
|
||||
reducerPath: "contactApi",
|
||||
export const userApi = createApi({
|
||||
reducerPath: "userApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
getContacts: builder.query({
|
||||
@@ -30,21 +30,21 @@ export const contactApi = createApi({
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
const local_uid = localStorage.getItem(KEY_UID);
|
||||
try {
|
||||
const { data: contacts } = await queryFulfilled;
|
||||
const matchedUser = contacts.find((c) => c.uid == local_uid);
|
||||
console.log("wtf", contacts, matchedUser);
|
||||
const { data: users } = await queryFulfilled;
|
||||
const matchedUser = users.find((c) => c.uid == local_uid);
|
||||
console.log("wtf", users, matchedUser);
|
||||
if (!matchedUser) {
|
||||
// 用户已注销或被禁用
|
||||
console.log("no matched user, redirect to login");
|
||||
dispatch(resetAuthData());
|
||||
} else {
|
||||
const markedContacts = contacts.map((u) => {
|
||||
const markedContacts = users.map((u) => {
|
||||
return u.uid == matchedUser.uid ? { ...u, online: true } : u;
|
||||
});
|
||||
dispatch(fullfillContacts(markedContacts));
|
||||
}
|
||||
} catch {
|
||||
console.log("get contact list error");
|
||||
console.log("get user list error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
@@ -135,4 +135,4 @@ export const {
|
||||
useGetContactsQuery,
|
||||
useLazyGetContactsQuery,
|
||||
useSendMsgMutation
|
||||
} = contactApi;
|
||||
} = userApi;
|
||||
Reference in New Issue
Block a user