refactor: replace image message with file message

This commit is contained in:
zerosoul
2022-04-12 09:37:46 +08:00
parent 1dcf757920
commit 5884c73bad
18 changed files with 285 additions and 262 deletions
+29 -1
View File
@@ -46,6 +46,33 @@ export const channelApi = createApi({
}
},
}),
getHistoryMessages: builder.query({
query: ({ gid, mid = 0, limit = 50 }) => ({
url: `/group/${gid}/history?before=${mid}&limit=${limit}`,
}),
// async onQueryStarted(id, { dispatch, getState, queryFulfilled }) {
// const {
// ui: { channelSetting },
// channelMessage,
// } = getState();
// try {
// await queryFulfilled;
// dispatch(removeChannel(id));
// if (id == channelSetting) {
// dispatch(toggleChannelSetting());
// }
// // 删掉该channel下的所有消息&reaction
// const mids = channelMessage[id];
// if (mids) {
// dispatch(removeChannelSession(id));
// dispatch(removeMessage(mids));
// dispatch(removeReactionMessage(mids));
// }
// } catch {
// console.log("remove channel error");
// }
// },
}),
removeChannel: builder.query({
query: (id) => ({
url: `group/${id}`,
@@ -82,7 +109,7 @@ export const channelApi = createApi({
},
url: `group/${id}/send`,
method: "POST",
body: content,
body: type == "file" ? JSON.stringify(content) : content,
}),
async onQueryStarted(param1, param2) {
await onMessageSendStarted.call(this, param1, param2, "channel");
@@ -106,6 +133,7 @@ export const channelApi = createApi({
});
export const {
useLazyGetHistoryMessagesQuery,
useGetChannelQuery,
useUpdateChannelMutation,
useLazyRemoveChannelQuery,
+1 -1
View File
@@ -95,7 +95,7 @@ export const contactApi = createApi({
},
url: `user/${id}/send`,
method: "POST",
body: content,
body: type == "file" ? JSON.stringify(content) : content,
}),
async onQueryStarted(param1, param2) {
await onMessageSendStarted.call(this, param1, param2, "user");
+3 -1
View File
@@ -17,6 +17,8 @@ export const onMessageSendStarted = async (
from = "channel"
) => {
// id: who send to ,from_uid: who sent
console.log("handlers data", content, type, properties);
const isImage = properties.file_type?.startsWith("image");
const ts = properties.local_id || new Date().getTime();
// let imageData = null;
// if (type == "image") {
@@ -31,7 +33,7 @@ export const onMessageSendStarted = async (
// }
// }
const tmpMsg = {
content: type == "image" ? URL.createObjectURL(content) : content,
content: isImage ? content.path : content,
content_type: ContentTypes[type],
created_at: ts,
properties,
+1 -28
View File
@@ -1,7 +1,7 @@
import { createApi } from "@reduxjs/toolkit/query/react";
// import { batch } from "react-redux";
import BASE_URL, { ContentTypes } from "../config";
import { ContentTypes } from "../config";
import { updateReadChannels, updateReadUsers } from "../slices/footprint";
import { onMessageSendStarted } from "./handlers";
@@ -59,19 +59,6 @@ export const messageApi = createApi({
return data ? data : {};
},
}),
uploadImage: builder.mutation({
query: (image) => ({
headers: {
"content-type": ContentTypes.image,
},
url: `/resource/image`,
method: "POST",
body: image,
}),
transformResponse: (image_id) => {
return `${BASE_URL}/resource/image?id=${encodeURIComponent(image_id)}`;
},
}),
replyMessage: builder.mutation({
query: ({ reply_mid, content, type = "text" }) => ({
headers: {
@@ -93,29 +80,16 @@ export const messageApi = createApi({
}),
async onQueryStarted(data, { dispatch, queryFulfilled }) {
const { users = null, groups = null } = data;
// const { readUsers, readChannels } = getState().footprint;
// const prevUsers = users.map(({ uid }) => {
// return { uid, mid: readUsers[uid] };
// });
// const prevChannels = users.map(({ gid }) => {
// return { gid, mid: readChannels[gid] };
// });
// batch(() => {
if (users) {
dispatch(updateReadUsers(users));
}
if (groups) {
dispatch(updateReadChannels(groups));
}
// });
try {
await queryFulfilled;
} catch {
// todo
// batch(() => {
// dispatch(updateReadChannels(prevChannels));
// dispatch(updateReadUsers(prevUsers));
// });
}
},
}),
@@ -125,7 +99,6 @@ export const messageApi = createApi({
export const {
usePrepareUploadFileMutation,
useUploadFileMutation,
useUploadImageMutation,
useEditMessageMutation,
useReactMessageMutation,
useReplyMessageMutation,