chore: update prettier setting
This commit is contained in:
+146
-146
@@ -4,157 +4,157 @@ import baseQuery from "./base.query";
|
||||
import { setAuthData, updateToken, resetAuthData, updateInitialized } from "../slices/auth.data";
|
||||
import BASE_URL, { KEY_DEVICE_KEY } from "../config";
|
||||
const getDeviceId = () => {
|
||||
let d = localStorage.getItem(KEY_DEVICE_KEY);
|
||||
if (!d) {
|
||||
d = `web:${nanoid()}`;
|
||||
localStorage.setItem(KEY_DEVICE_KEY, d);
|
||||
}
|
||||
return d;
|
||||
let d = localStorage.getItem(KEY_DEVICE_KEY);
|
||||
if (!d) {
|
||||
d = `web:${nanoid()}`;
|
||||
localStorage.setItem(KEY_DEVICE_KEY, d);
|
||||
}
|
||||
return d;
|
||||
};
|
||||
export const authApi = createApi({
|
||||
reducerPath: "authApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
login: builder.mutation({
|
||||
query: (credentials) => ({
|
||||
url: "token/login",
|
||||
method: "POST",
|
||||
body: {
|
||||
credential: credentials,
|
||||
device: getDeviceId(),
|
||||
device_token: "test"
|
||||
}
|
||||
}),
|
||||
transformResponse: (data) => {
|
||||
const { avatar_updated_at } = data.user;
|
||||
data.user.avatar =
|
||||
avatar_updated_at == 0
|
||||
? ""
|
||||
: `${BASE_URL}/resource/avatar?uid=${data.user.uid}&t=${avatar_updated_at}`;
|
||||
return data;
|
||||
},
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
if (data) {
|
||||
console.log("login resp", data);
|
||||
dispatch(setAuthData(data));
|
||||
}
|
||||
} catch {
|
||||
console.log("login error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
// 更新token
|
||||
renew: builder.mutation({
|
||||
query: ({ token, refreshToken }) => ({
|
||||
url: "/token/renew",
|
||||
method: "POST",
|
||||
body: {
|
||||
token,
|
||||
refresh_token: refreshToken
|
||||
}
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
dispatch(updateToken(data));
|
||||
} catch {
|
||||
dispatch(resetAuthData());
|
||||
console.log("renew token error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
// 更新 device token
|
||||
updateDeviceToken: builder.mutation({
|
||||
query: (device_token) => ({
|
||||
url: "/token/device_token",
|
||||
method: "PUT",
|
||||
body: {
|
||||
device_token
|
||||
}
|
||||
})
|
||||
}),
|
||||
// 获取openid
|
||||
getOpenid: builder.mutation({
|
||||
query: ({ issuer, redirect_uri }) => ({
|
||||
url: "/token/openid/authorize",
|
||||
method: "POST",
|
||||
body: {
|
||||
issuer,
|
||||
redirect_uri
|
||||
}
|
||||
})
|
||||
}),
|
||||
reducerPath: "authApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
login: builder.mutation({
|
||||
query: (credentials) => ({
|
||||
url: "token/login",
|
||||
method: "POST",
|
||||
body: {
|
||||
credential: credentials,
|
||||
device: getDeviceId(),
|
||||
device_token: "test"
|
||||
}
|
||||
}),
|
||||
transformResponse: (data) => {
|
||||
const { avatar_updated_at } = data.user;
|
||||
data.user.avatar =
|
||||
avatar_updated_at == 0
|
||||
? ""
|
||||
: `${BASE_URL}/resource/avatar?uid=${data.user.uid}&t=${avatar_updated_at}`;
|
||||
return data;
|
||||
},
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
if (data) {
|
||||
console.log("login resp", data);
|
||||
dispatch(setAuthData(data));
|
||||
}
|
||||
} catch {
|
||||
console.log("login error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
// 更新token
|
||||
renew: builder.mutation({
|
||||
query: ({ token, refreshToken }) => ({
|
||||
url: "/token/renew",
|
||||
method: "POST",
|
||||
body: {
|
||||
token,
|
||||
refresh_token: refreshToken
|
||||
}
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
dispatch(updateToken(data));
|
||||
} catch {
|
||||
dispatch(resetAuthData());
|
||||
console.log("renew token error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
// 更新 device token
|
||||
updateDeviceToken: builder.mutation({
|
||||
query: (device_token) => ({
|
||||
url: "/token/device_token",
|
||||
method: "PUT",
|
||||
body: {
|
||||
device_token
|
||||
}
|
||||
})
|
||||
}),
|
||||
// 获取openid
|
||||
getOpenid: builder.mutation({
|
||||
query: ({ issuer, redirect_uri }) => ({
|
||||
url: "/token/openid/authorize",
|
||||
method: "POST",
|
||||
body: {
|
||||
issuer,
|
||||
redirect_uri
|
||||
}
|
||||
})
|
||||
}),
|
||||
|
||||
checkInviteTokenValid: builder.mutation({
|
||||
query: (token) => ({
|
||||
url: "user/check_invite_magic_token",
|
||||
method: "POST",
|
||||
body: { magic_token: token }
|
||||
})
|
||||
}),
|
||||
updatePassword: builder.mutation({
|
||||
query: ({ old_password, new_password }) => ({
|
||||
url: "user/change_password",
|
||||
method: "POST",
|
||||
body: { old_password, new_password }
|
||||
})
|
||||
}),
|
||||
sendMagicLink: builder.mutation({
|
||||
query: (email) => ({
|
||||
url: "token/send_magic_link",
|
||||
method: "POST",
|
||||
body: { email }
|
||||
})
|
||||
}),
|
||||
getMetamaskNonce: builder.query({
|
||||
query: (address) => ({
|
||||
url: `/token/metamask/nonce?public_address=${address}`
|
||||
})
|
||||
}),
|
||||
getCredentials: builder.query({
|
||||
query: () => ({
|
||||
url: `/token/credentials`
|
||||
})
|
||||
}),
|
||||
logout: builder.query({
|
||||
query: () => ({ url: `token/logout` }),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
await queryFulfilled;
|
||||
dispatch(resetAuthData());
|
||||
} catch {
|
||||
console.log("logout error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
getInitialized: builder.query({
|
||||
query: () => ({
|
||||
url: `/admin/system/initialized`
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data: isInitialized } = await queryFulfilled;
|
||||
dispatch(updateInitialized(isInitialized));
|
||||
} catch {
|
||||
console.log("api initialized error");
|
||||
}
|
||||
}
|
||||
checkInviteTokenValid: builder.mutation({
|
||||
query: (token) => ({
|
||||
url: "user/check_invite_magic_token",
|
||||
method: "POST",
|
||||
body: { magic_token: token }
|
||||
})
|
||||
}),
|
||||
updatePassword: builder.mutation({
|
||||
query: ({ old_password, new_password }) => ({
|
||||
url: "user/change_password",
|
||||
method: "POST",
|
||||
body: { old_password, new_password }
|
||||
})
|
||||
}),
|
||||
sendMagicLink: builder.mutation({
|
||||
query: (email) => ({
|
||||
url: "token/send_magic_link",
|
||||
method: "POST",
|
||||
body: { email }
|
||||
})
|
||||
}),
|
||||
getMetamaskNonce: builder.query({
|
||||
query: (address) => ({
|
||||
url: `/token/metamask/nonce?public_address=${address}`
|
||||
})
|
||||
}),
|
||||
getCredentials: builder.query({
|
||||
query: () => ({
|
||||
url: `/token/credentials`
|
||||
})
|
||||
}),
|
||||
logout: builder.query({
|
||||
query: () => ({ url: `token/logout` }),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
await queryFulfilled;
|
||||
dispatch(resetAuthData());
|
||||
} catch {
|
||||
console.log("logout error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
getInitialized: builder.query({
|
||||
query: () => ({
|
||||
url: `/admin/system/initialized`
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data: isInitialized } = await queryFulfilled;
|
||||
dispatch(updateInitialized(isInitialized));
|
||||
} catch {
|
||||
console.log("api initialized error");
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
export const {
|
||||
useGetInitializedQuery,
|
||||
useSendMagicLinkMutation,
|
||||
useGetCredentialsQuery,
|
||||
useUpdateDeviceTokenMutation,
|
||||
useGetOpenidMutation,
|
||||
useRenewMutation,
|
||||
useLazyGetMetamaskNonceQuery,
|
||||
useLoginMutation,
|
||||
useLazyLogoutQuery,
|
||||
useCheckInviteTokenValidMutation,
|
||||
useUpdatePasswordMutation
|
||||
useGetInitializedQuery,
|
||||
useSendMagicLinkMutation,
|
||||
useGetCredentialsQuery,
|
||||
useUpdateDeviceTokenMutation,
|
||||
useGetOpenidMutation,
|
||||
useRenewMutation,
|
||||
useLazyGetMetamaskNonceQuery,
|
||||
useLoginMutation,
|
||||
useLazyLogoutQuery,
|
||||
useCheckInviteTokenValidMutation,
|
||||
useUpdatePasswordMutation
|
||||
} = authApi;
|
||||
|
||||
@@ -18,7 +18,7 @@ const whiteList = [
|
||||
"getMetamaskNonce",
|
||||
"renew",
|
||||
"getInitialized",
|
||||
"createAdmin",
|
||||
"createAdmin"
|
||||
];
|
||||
const baseQuery = fetchBaseQuery({
|
||||
baseUrl: BASE_URL,
|
||||
@@ -29,7 +29,7 @@ const baseQuery = fetchBaseQuery({
|
||||
headers.set(tokenHeader, token);
|
||||
}
|
||||
return headers;
|
||||
},
|
||||
}
|
||||
});
|
||||
let waitingForRenew = null;
|
||||
const baseQueryWithTokenCheck = async (args, api, extraOptions) => {
|
||||
@@ -37,16 +37,9 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => {
|
||||
await waitingForRenew;
|
||||
}
|
||||
// 先检查token是否过期,过期则renew
|
||||
const {
|
||||
token,
|
||||
refreshToken,
|
||||
expireTime = +new Date(),
|
||||
} = api.getState().authData;
|
||||
const { token, refreshToken, expireTime = +new Date() } = api.getState().authData;
|
||||
let result = null;
|
||||
if (
|
||||
!whiteList.includes(api.endpoint) &&
|
||||
dayjs().isAfter(new Date(expireTime - 20 * 1000))
|
||||
) {
|
||||
if (!whiteList.includes(api.endpoint) && dayjs().isAfter(new Date(expireTime - 20 * 1000))) {
|
||||
// 快过期了,renew
|
||||
waitingForRenew = baseQuery(
|
||||
{
|
||||
@@ -54,8 +47,8 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => {
|
||||
method: "POST",
|
||||
body: {
|
||||
token,
|
||||
refresh_token: refreshToken,
|
||||
},
|
||||
refresh_token: refreshToken
|
||||
}
|
||||
},
|
||||
api,
|
||||
extraOptions
|
||||
|
||||
+31
-34
@@ -15,10 +15,10 @@ export const channelApi = createApi({
|
||||
refetchOnFocus: true,
|
||||
endpoints: (builder) => ({
|
||||
getChannels: builder.query({
|
||||
query: () => ({ url: `group` }),
|
||||
query: () => ({ url: `group` })
|
||||
}),
|
||||
getChannel: builder.query({
|
||||
query: (id) => ({ url: `group/${id}` }),
|
||||
query: (id) => ({ url: `group/${id}` })
|
||||
}),
|
||||
leaveChannel: builder.query({
|
||||
query: (id) => ({ url: `group/${id}/leave` }),
|
||||
@@ -29,25 +29,22 @@ export const channelApi = createApi({
|
||||
} catch {
|
||||
console.log("channel update failed");
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
createChannel: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: "group",
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
updateChannel: builder.mutation({
|
||||
query: ({ id, ...data }) => ({
|
||||
url: `group/${id}`,
|
||||
method: "PUT",
|
||||
body: data,
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(
|
||||
{ id, name, description },
|
||||
{ dispatch, queryFulfilled }
|
||||
) {
|
||||
async onQueryStarted({ id, name, description }, { dispatch, queryFulfilled }) {
|
||||
// id: who send to ,from_uid: who sent
|
||||
const patchResult = dispatch(updateChannel({ id, name, description }));
|
||||
try {
|
||||
@@ -56,13 +53,13 @@ export const channelApi = createApi({
|
||||
console.log("channel update failed");
|
||||
patchResult.undo();
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
getHistoryMessages: builder.query({
|
||||
query: ({ id, mid = null, limit = 100 }) => ({
|
||||
url: mid
|
||||
? `/group/${id}/history?before=${mid}&limit=${limit}`
|
||||
: `/group/${id}/history?limit=${limit}`,
|
||||
: `/group/${id}/history?limit=${limit}`
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, getState, queryFulfilled }) {
|
||||
const { data: messages } = await queryFulfilled;
|
||||
@@ -71,34 +68,34 @@ export const channelApi = createApi({
|
||||
handleChatMessage(msg, dispatch, getState());
|
||||
});
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
createInviteLink: builder.query({
|
||||
query: (gid) => ({
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
accept: "text/plain",
|
||||
accept: "text/plain"
|
||||
},
|
||||
url: `/group/${gid}/create_invite_link`,
|
||||
responseHandler: (response) => response.text(),
|
||||
responseHandler: (response) => response.text()
|
||||
}),
|
||||
transformResponse: (link) => {
|
||||
// 替换掉域名
|
||||
const invite = new URL(link);
|
||||
return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`;
|
||||
},
|
||||
}
|
||||
}),
|
||||
removeChannel: builder.query({
|
||||
query: (id) => ({
|
||||
url: `group/${id}`,
|
||||
method: "DELETE",
|
||||
method: "DELETE"
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, getState, queryFulfilled }) {
|
||||
const {
|
||||
channelMessage,
|
||||
ui: {
|
||||
remeberedNavs: { chat: remeberedPath },
|
||||
},
|
||||
remeberedNavs: { chat: remeberedPath }
|
||||
}
|
||||
} = getState();
|
||||
try {
|
||||
await queryFulfilled;
|
||||
@@ -115,7 +112,7 @@ export const channelApi = createApi({
|
||||
} catch {
|
||||
console.log("remove channel error");
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
sendChannelMsg: builder.mutation({
|
||||
query: ({ id, content, type = "text", properties = "" }) => ({
|
||||
@@ -123,38 +120,38 @@ export const channelApi = createApi({
|
||||
"content-type": ContentTypes[type],
|
||||
"X-Properties": properties
|
||||
? btoa(unescape(encodeURIComponent(JSON.stringify(properties))))
|
||||
: "",
|
||||
: ""
|
||||
},
|
||||
url: `group/${id}/send`,
|
||||
method: "POST",
|
||||
body: type == "file" ? JSON.stringify(content) : content,
|
||||
body: type == "file" ? JSON.stringify(content) : content
|
||||
}),
|
||||
async onQueryStarted(param1, param2) {
|
||||
await onMessageSendStarted.call(this, param1, param2, "channel");
|
||||
},
|
||||
}
|
||||
}),
|
||||
addMembers: builder.mutation({
|
||||
query: ({ id, members }) => ({
|
||||
url: `group/${id}/members/add`,
|
||||
method: "POST",
|
||||
body: members,
|
||||
}),
|
||||
body: members
|
||||
})
|
||||
}),
|
||||
removeMembers: builder.mutation({
|
||||
query: ({ id, members }) => ({
|
||||
url: `group/${id}/members/remove`,
|
||||
method: "POST",
|
||||
body: members,
|
||||
}),
|
||||
body: members
|
||||
})
|
||||
}),
|
||||
updateIcon: builder.mutation({
|
||||
query: ({ gid, image }) => ({
|
||||
headers: {
|
||||
"content-type": "image/png",
|
||||
"content-type": "image/png"
|
||||
},
|
||||
url: `/group/${gid}/avatar`,
|
||||
method: "POST",
|
||||
body: image,
|
||||
body: image
|
||||
}),
|
||||
async onQueryStarted({ gid }, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
@@ -162,15 +159,15 @@ export const channelApi = createApi({
|
||||
dispatch(
|
||||
updateChannel({
|
||||
id: gid,
|
||||
icon: `${BASE_URL}/resource/group_avatar?gid=${gid}&t=${+new Date()}`,
|
||||
icon: `${BASE_URL}/resource/group_avatar?gid=${gid}&t=${+new Date()}`
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("err", error);
|
||||
}
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
export const {
|
||||
@@ -186,5 +183,5 @@ export const {
|
||||
useSendChannelMsgMutation,
|
||||
useAddMembersMutation,
|
||||
useRemoveMembersMutation,
|
||||
useUpdateIconMutation,
|
||||
useUpdateIconMutation
|
||||
} = channelApi;
|
||||
|
||||
+21
-21
@@ -45,23 +45,23 @@ export const contactApi = createApi({
|
||||
} catch {
|
||||
console.log("get contact list error");
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
deleteContact: builder.query({
|
||||
query: (uid) => ({ url: `/admin/user/${uid}`, method: "DELETE" }),
|
||||
query: (uid) => ({ url: `/admin/user/${uid}`, method: "DELETE" })
|
||||
}),
|
||||
updateContact: builder.mutation({
|
||||
query: ({ id, ...rest }) => ({
|
||||
url: `/admin/user/${id}`,
|
||||
body: rest,
|
||||
method: "PUT",
|
||||
}),
|
||||
method: "PUT"
|
||||
})
|
||||
}),
|
||||
updateMuteSetting: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `/user/mute`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
@@ -70,31 +70,31 @@ export const contactApi = createApi({
|
||||
} catch (error) {
|
||||
console.log("update mute failed", error);
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
updateAvatar: builder.mutation({
|
||||
query: (data) => ({
|
||||
headers: {
|
||||
"content-type": "image/png",
|
||||
"content-type": "image/png"
|
||||
},
|
||||
url: `user/avatar`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
updateInfo: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `user`,
|
||||
method: "PUT",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
register: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `user/register`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
sendMsg: builder.mutation({
|
||||
query: ({ id, content, type = "text", properties = "" }) => ({
|
||||
@@ -102,21 +102,21 @@ export const contactApi = createApi({
|
||||
"content-type": ContentTypes[type],
|
||||
"X-Properties": properties
|
||||
? btoa(unescape(encodeURIComponent(JSON.stringify(properties))))
|
||||
: "",
|
||||
: ""
|
||||
},
|
||||
url: `user/${id}/send`,
|
||||
method: "POST",
|
||||
body: type == "file" ? JSON.stringify(content) : content,
|
||||
body: type == "file" ? JSON.stringify(content) : content
|
||||
}),
|
||||
async onQueryStarted(param1, param2) {
|
||||
await onMessageSendStarted.call(this, param1, param2, "user");
|
||||
},
|
||||
}
|
||||
}),
|
||||
getHistoryMessages: builder.query({
|
||||
query: ({ id, mid = null, limit = 100 }) => ({
|
||||
url: mid
|
||||
? `/user/${id}/history?before=${mid}&limit=${limit}`
|
||||
: `/user/${id}/history?limit=${limit}`,
|
||||
: `/user/${id}/history?limit=${limit}`
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, getState, queryFulfilled }) {
|
||||
const { data: messages } = await queryFulfilled;
|
||||
@@ -125,9 +125,9 @@ export const contactApi = createApi({
|
||||
handleChatMessage(msg, dispatch, getState());
|
||||
});
|
||||
}
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
export const {
|
||||
@@ -140,5 +140,5 @@ export const {
|
||||
useGetContactsQuery,
|
||||
useLazyGetContactsQuery,
|
||||
useSendMsgMutation,
|
||||
useRegisterMutation,
|
||||
useRegisterMutation
|
||||
} = contactApi;
|
||||
|
||||
@@ -12,7 +12,7 @@ export const onMessageSendStarted = async (
|
||||
type = "text",
|
||||
from_uid,
|
||||
reply_mid = null,
|
||||
properties = { local_id: +new Date() },
|
||||
properties = { local_id: +new Date() }
|
||||
},
|
||||
{ dispatch, queryFulfilled },
|
||||
from = "channel"
|
||||
@@ -42,11 +42,10 @@ export const onMessageSendStarted = async (
|
||||
properties,
|
||||
from_uid,
|
||||
reply_mid,
|
||||
sending: true,
|
||||
sending: true
|
||||
};
|
||||
const addContextMessage = from == "channel" ? addChannelMsg : addUserMsg;
|
||||
const removeContextMessage =
|
||||
from == "channel" ? removeChannelMsg : removeUserMsg;
|
||||
const removeContextMessage = from == "channel" ? removeChannelMsg : removeUserMsg;
|
||||
if (!ignoreLocal) {
|
||||
batch(() => {
|
||||
dispatch(addMessage({ mid: ts, ...tmpMsg }));
|
||||
|
||||
+205
-205
@@ -1,214 +1,214 @@
|
||||
import { createApi } from '@reduxjs/toolkit/query/react';
|
||||
import { ContentTypes } from '../config';
|
||||
import { updateReadChannels, updateReadUsers } from '../slices/footprint';
|
||||
import { createApi } from "@reduxjs/toolkit/query/react";
|
||||
import { ContentTypes } from "../config";
|
||||
import { updateReadChannels, updateReadUsers } from "../slices/footprint";
|
||||
import {
|
||||
fullfillFavorites,
|
||||
populateFavorite,
|
||||
addFavorite,
|
||||
deleteFavorite
|
||||
} from '../slices/favorites';
|
||||
import { onMessageSendStarted } from './handlers';
|
||||
import { normalizeArchiveData } from '../../common/utils';
|
||||
fullfillFavorites,
|
||||
populateFavorite,
|
||||
addFavorite,
|
||||
deleteFavorite
|
||||
} from "../slices/favorites";
|
||||
import { onMessageSendStarted } from "./handlers";
|
||||
import { normalizeArchiveData } from "../../common/utils";
|
||||
// import { updateMessage } from "../slices/message";
|
||||
import baseQuery from './base.query';
|
||||
import baseQuery from "./base.query";
|
||||
|
||||
export const messageApi = createApi({
|
||||
reducerPath: 'messageApi',
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
editMessage: builder.mutation({
|
||||
query: ({ mid, content, type = 'text' }) => ({
|
||||
headers: {
|
||||
'content-type': ContentTypes[type]
|
||||
},
|
||||
url: `/message/${mid}/edit`,
|
||||
method: 'PUT',
|
||||
body: content
|
||||
})
|
||||
// async onQueryStarted({mid,content},{dispatch}){
|
||||
// dispatch()
|
||||
// }
|
||||
}),
|
||||
reactMessage: builder.mutation({
|
||||
query: ({ mid, action }) => ({
|
||||
url: `/message/${mid}/like`,
|
||||
method: 'PUT',
|
||||
body: { action }
|
||||
})
|
||||
}),
|
||||
deleteMessage: builder.query({
|
||||
query: (mid) => ({
|
||||
url: `/message/${mid}`,
|
||||
method: 'DELETE'
|
||||
})
|
||||
}),
|
||||
prepareUploadFile: builder.mutation({
|
||||
query: (meta = {}) => ({
|
||||
url: `/resource/file/prepare`,
|
||||
method: 'POST',
|
||||
body: meta
|
||||
})
|
||||
}),
|
||||
createArchive: builder.mutation({
|
||||
query: (mids = []) => ({
|
||||
url: `/resource/archive`,
|
||||
method: 'POST',
|
||||
body: { mid_list: mids }
|
||||
})
|
||||
}),
|
||||
uploadFile: builder.mutation({
|
||||
query: (formData) => ({
|
||||
// headers: {
|
||||
// "content-type": ContentTypes.formData,
|
||||
// },
|
||||
url: `/resource/file/upload`,
|
||||
method: 'POST',
|
||||
body: formData
|
||||
}),
|
||||
transformResponse: (data) => {
|
||||
console.log('upload file response', data);
|
||||
return data ? data : {};
|
||||
}
|
||||
}),
|
||||
getOGInfo: builder.query({
|
||||
query: (url) => ({
|
||||
url: `/resource/open_graphic_parse?url=${encodeURIComponent(url)}`
|
||||
})
|
||||
}),
|
||||
getArchiveMessage: builder.query({
|
||||
query: (file_path) => ({
|
||||
url: `/resource/archive?file_path=${encodeURIComponent(file_path)}`
|
||||
})
|
||||
}),
|
||||
pinMessage: builder.mutation({
|
||||
query: ({ gid, mid }) => ({
|
||||
url: `/group/${gid}/pin`,
|
||||
method: 'POST',
|
||||
body: { mid }
|
||||
})
|
||||
}),
|
||||
unpinMessage: builder.mutation({
|
||||
query: ({ gid, mid }) => ({
|
||||
url: `/group/${gid}/unpin`,
|
||||
method: 'POST',
|
||||
body: { mid }
|
||||
})
|
||||
}),
|
||||
favoriteMessage: builder.mutation({
|
||||
query: (mids) => ({
|
||||
url: `/favorite`,
|
||||
method: 'POST',
|
||||
body: { mid_list: mids }
|
||||
}),
|
||||
async onQueryStarted(mids, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
const { created_at, id } = data;
|
||||
dispatch(addFavorite({ id, created_at }));
|
||||
dispatch(messageApi.endpoints.getFavoriteDetails.initiate(id));
|
||||
} catch (err) {
|
||||
console.log('get favorite list error', err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
removeFavorite: builder.query({
|
||||
query: (id) => ({
|
||||
url: `/favorite/${id}`,
|
||||
method: 'DELETE'
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
await queryFulfilled;
|
||||
dispatch(deleteFavorite(id));
|
||||
} catch (err) {
|
||||
console.log('get favorite list error', err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
getFavoriteDetails: builder.query({
|
||||
query: (id) => ({
|
||||
url: `/favorite/${id}`
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, queryFulfilled, getState }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
const loginUid = getState().authData.uid;
|
||||
const messages = normalizeArchiveData(data, id, loginUid);
|
||||
dispatch(populateFavorite({ id, messages }));
|
||||
} catch (err) {
|
||||
console.log('get favorite list error', err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
getFavorites: builder.query({
|
||||
query: () => ({
|
||||
url: `/favorite`
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data: favorites } = await queryFulfilled;
|
||||
dispatch(fullfillFavorites(favorites));
|
||||
for (const fav of favorites) {
|
||||
const { id } = fav;
|
||||
dispatch(messageApi.endpoints.getFavoriteDetails.initiate(id));
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('get favorite list error', err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
replyMessage: builder.mutation({
|
||||
query: ({ reply_mid, content, type = 'text' }) => ({
|
||||
headers: {
|
||||
'content-type': ContentTypes[type]
|
||||
},
|
||||
url: `/message/${reply_mid}/reply`,
|
||||
method: 'POST',
|
||||
body: content
|
||||
}),
|
||||
async onQueryStarted(param1, param2) {
|
||||
await onMessageSendStarted.call(this, param1, param2, param1.context);
|
||||
}
|
||||
}),
|
||||
readMessage: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `/user/read-index`,
|
||||
method: 'POST',
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
const { users = null, groups = null } = data;
|
||||
if (users) {
|
||||
dispatch(updateReadUsers(users));
|
||||
}
|
||||
if (groups) {
|
||||
dispatch(updateReadChannels(groups));
|
||||
}
|
||||
try {
|
||||
await queryFulfilled;
|
||||
} catch {
|
||||
// todo
|
||||
}
|
||||
}
|
||||
reducerPath: "messageApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
editMessage: builder.mutation({
|
||||
query: ({ mid, content, type = "text" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type]
|
||||
},
|
||||
url: `/message/${mid}/edit`,
|
||||
method: "PUT",
|
||||
body: content
|
||||
})
|
||||
// async onQueryStarted({mid,content},{dispatch}){
|
||||
// dispatch()
|
||||
// }
|
||||
}),
|
||||
reactMessage: builder.mutation({
|
||||
query: ({ mid, action }) => ({
|
||||
url: `/message/${mid}/like`,
|
||||
method: "PUT",
|
||||
body: { action }
|
||||
})
|
||||
}),
|
||||
deleteMessage: builder.query({
|
||||
query: (mid) => ({
|
||||
url: `/message/${mid}`,
|
||||
method: "DELETE"
|
||||
})
|
||||
}),
|
||||
prepareUploadFile: builder.mutation({
|
||||
query: (meta = {}) => ({
|
||||
url: `/resource/file/prepare`,
|
||||
method: "POST",
|
||||
body: meta
|
||||
})
|
||||
}),
|
||||
createArchive: builder.mutation({
|
||||
query: (mids = []) => ({
|
||||
url: `/resource/archive`,
|
||||
method: "POST",
|
||||
body: { mid_list: mids }
|
||||
})
|
||||
}),
|
||||
uploadFile: builder.mutation({
|
||||
query: (formData) => ({
|
||||
// headers: {
|
||||
// "content-type": ContentTypes.formData,
|
||||
// },
|
||||
url: `/resource/file/upload`,
|
||||
method: "POST",
|
||||
body: formData
|
||||
}),
|
||||
transformResponse: (data) => {
|
||||
console.log("upload file response", data);
|
||||
return data ? data : {};
|
||||
}
|
||||
}),
|
||||
getOGInfo: builder.query({
|
||||
query: (url) => ({
|
||||
url: `/resource/open_graphic_parse?url=${encodeURIComponent(url)}`
|
||||
})
|
||||
}),
|
||||
getArchiveMessage: builder.query({
|
||||
query: (file_path) => ({
|
||||
url: `/resource/archive?file_path=${encodeURIComponent(file_path)}`
|
||||
})
|
||||
}),
|
||||
pinMessage: builder.mutation({
|
||||
query: ({ gid, mid }) => ({
|
||||
url: `/group/${gid}/pin`,
|
||||
method: "POST",
|
||||
body: { mid }
|
||||
})
|
||||
}),
|
||||
unpinMessage: builder.mutation({
|
||||
query: ({ gid, mid }) => ({
|
||||
url: `/group/${gid}/unpin`,
|
||||
method: "POST",
|
||||
body: { mid }
|
||||
})
|
||||
}),
|
||||
favoriteMessage: builder.mutation({
|
||||
query: (mids) => ({
|
||||
url: `/favorite`,
|
||||
method: "POST",
|
||||
body: { mid_list: mids }
|
||||
}),
|
||||
async onQueryStarted(mids, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
const { created_at, id } = data;
|
||||
dispatch(addFavorite({ id, created_at }));
|
||||
dispatch(messageApi.endpoints.getFavoriteDetails.initiate(id));
|
||||
} catch (err) {
|
||||
console.log("get favorite list error", err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
removeFavorite: builder.query({
|
||||
query: (id) => ({
|
||||
url: `/favorite/${id}`,
|
||||
method: "DELETE"
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
await queryFulfilled;
|
||||
dispatch(deleteFavorite(id));
|
||||
} catch (err) {
|
||||
console.log("get favorite list error", err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
getFavoriteDetails: builder.query({
|
||||
query: (id) => ({
|
||||
url: `/favorite/${id}`
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, queryFulfilled, getState }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
const loginUid = getState().authData.uid;
|
||||
const messages = normalizeArchiveData(data, id, loginUid);
|
||||
dispatch(populateFavorite({ id, messages }));
|
||||
} catch (err) {
|
||||
console.log("get favorite list error", err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
getFavorites: builder.query({
|
||||
query: () => ({
|
||||
url: `/favorite`
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
const { data: favorites } = await queryFulfilled;
|
||||
dispatch(fullfillFavorites(favorites));
|
||||
for (const fav of favorites) {
|
||||
const { id } = fav;
|
||||
dispatch(messageApi.endpoints.getFavoriteDetails.initiate(id));
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("get favorite list error", err);
|
||||
}
|
||||
}
|
||||
}),
|
||||
replyMessage: builder.mutation({
|
||||
query: ({ reply_mid, content, type = "text" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type]
|
||||
},
|
||||
url: `/message/${reply_mid}/reply`,
|
||||
method: "POST",
|
||||
body: content
|
||||
}),
|
||||
async onQueryStarted(param1, param2) {
|
||||
await onMessageSendStarted.call(this, param1, param2, param1.context);
|
||||
}
|
||||
}),
|
||||
readMessage: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `/user/read-index`,
|
||||
method: "POST",
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
const { users = null, groups = null } = data;
|
||||
if (users) {
|
||||
dispatch(updateReadUsers(users));
|
||||
}
|
||||
if (groups) {
|
||||
dispatch(updateReadChannels(groups));
|
||||
}
|
||||
try {
|
||||
await queryFulfilled;
|
||||
} catch {
|
||||
// todo
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
export const {
|
||||
useLazyRemoveFavoriteQuery,
|
||||
useUnpinMessageMutation,
|
||||
useLazyGetFavoritesQuery,
|
||||
useFavoriteMessageMutation,
|
||||
usePinMessageMutation,
|
||||
useLazyGetArchiveMessageQuery,
|
||||
useGetArchiveMessageQuery,
|
||||
useLazyGetOGInfoQuery,
|
||||
usePrepareUploadFileMutation,
|
||||
useUploadFileMutation,
|
||||
useEditMessageMutation,
|
||||
useReactMessageMutation,
|
||||
useReplyMessageMutation,
|
||||
useLazyDeleteMessageQuery,
|
||||
useReadMessageMutation,
|
||||
useCreateArchiveMutation
|
||||
useLazyRemoveFavoriteQuery,
|
||||
useUnpinMessageMutation,
|
||||
useLazyGetFavoritesQuery,
|
||||
useFavoriteMessageMutation,
|
||||
usePinMessageMutation,
|
||||
useLazyGetArchiveMessageQuery,
|
||||
useGetArchiveMessageQuery,
|
||||
useLazyGetOGInfoQuery,
|
||||
usePrepareUploadFileMutation,
|
||||
useUploadFileMutation,
|
||||
useEditMessageMutation,
|
||||
useReactMessageMutation,
|
||||
useReplyMessageMutation,
|
||||
useLazyDeleteMessageQuery,
|
||||
useReadMessageMutation,
|
||||
useCreateArchiveMutation
|
||||
} = messageApi;
|
||||
|
||||
+46
-46
@@ -20,7 +20,7 @@ export const serverApi = createApi({
|
||||
} catch {
|
||||
console.log("get server info error");
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
getThirdPartySecret: builder.query({
|
||||
query: () => ({
|
||||
@@ -29,142 +29,142 @@ export const serverApi = createApi({
|
||||
// accept: "text/plain",
|
||||
// },
|
||||
url: `/admin/system/third_party_secret`,
|
||||
responseHandler: (response) => response.text(),
|
||||
responseHandler: (response) => response.text()
|
||||
}),
|
||||
keepUnusedDataFor: 0,
|
||||
keepUnusedDataFor: 0
|
||||
}),
|
||||
updateThirdPartySecret: builder.mutation({
|
||||
query: () => ({
|
||||
url: `/admin/system/third_party_secret`,
|
||||
method: "POST",
|
||||
responseHandler: (response) => response.text(),
|
||||
}),
|
||||
responseHandler: (response) => response.text()
|
||||
})
|
||||
}),
|
||||
getMetrics: builder.query({
|
||||
query: () => ({ url: `/admin/system/metrics` }),
|
||||
query: () => ({ url: `/admin/system/metrics` })
|
||||
}),
|
||||
getServerVersion: builder.query({
|
||||
query: () => ({
|
||||
headers: {
|
||||
// "content-type": "text/plain",
|
||||
accept: "text/plain",
|
||||
accept: "text/plain"
|
||||
},
|
||||
url: `/admin/system/version`,
|
||||
responseHandler: (response) => response.text(),
|
||||
}),
|
||||
responseHandler: (response) => response.text()
|
||||
})
|
||||
}),
|
||||
getFirebaseConfig: builder.query({
|
||||
query: () => ({ url: `admin/fcm/config` }),
|
||||
query: () => ({ url: `admin/fcm/config` })
|
||||
}),
|
||||
getGoogleAuthConfig: builder.query({
|
||||
query: () => ({ url: `admin/google_auth/config` }),
|
||||
query: () => ({ url: `admin/google_auth/config` })
|
||||
}),
|
||||
updateGoogleAuthConfig: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/google_auth/config`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
getGithubAuthConfig: builder.query({
|
||||
query: () => ({ url: `admin/github_auth/config` }),
|
||||
query: () => ({ url: `admin/github_auth/config` })
|
||||
}),
|
||||
updateGithubAuthConfig: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/github_auth/config`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
sendTestEmail: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `/admin/system/send_mail`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
updateFirebaseConfig: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/fcm/config`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
getAgoraConfig: builder.query({
|
||||
query: () => ({ url: `admin/agora/config` }),
|
||||
query: () => ({ url: `admin/agora/config` })
|
||||
}),
|
||||
updateAgoraConfig: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/agora/config`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
getSMTPConfig: builder.query({
|
||||
query: () => ({ url: `admin/smtp/config` }),
|
||||
query: () => ({ url: `admin/smtp/config` })
|
||||
}),
|
||||
getSMTPStatus: builder.query({
|
||||
query: () => ({ url: `/admin/smtp/enabled` }),
|
||||
query: () => ({ url: `/admin/smtp/enabled` })
|
||||
}),
|
||||
updateSMTPConfig: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/smtp/config`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
getLoginConfig: builder.query({
|
||||
query: () => ({ url: `admin/login/config` }),
|
||||
query: () => ({ url: `admin/login/config` })
|
||||
}),
|
||||
updateLoginConfig: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/login/config`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
updateLogo: builder.mutation({
|
||||
query: (data) => ({
|
||||
headers: {
|
||||
"content-type": "image/png",
|
||||
"content-type": "image/png"
|
||||
},
|
||||
url: `admin/system/organization/logo`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
await queryFulfilled;
|
||||
dispatch(
|
||||
updateInfo({
|
||||
logo: `${BASE_URL}/resource/organization/logo?t=${+new Date()}`,
|
||||
logo: `${BASE_URL}/resource/organization/logo?t=${+new Date()}`
|
||||
})
|
||||
);
|
||||
} catch {
|
||||
console.log("update server logo error");
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
createInviteLink: builder.query({
|
||||
query: (expired_in = defaultExpireDuration) => ({
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
accept: "text/plain",
|
||||
accept: "text/plain"
|
||||
},
|
||||
url: `/admin/system/create_invite_link?expired_in=${expired_in}`,
|
||||
responseHandler: (response) => response.text(),
|
||||
responseHandler: (response) => response.text()
|
||||
}),
|
||||
transformResponse: (link) => {
|
||||
// 替换掉域名
|
||||
const invite = new URL(link);
|
||||
return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`;
|
||||
},
|
||||
}
|
||||
}),
|
||||
updateServer: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `admin/system/organization`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(data, { dispatch, queryFulfilled, getState }) {
|
||||
const { name: prevName, description: prevDesc } = getState().server;
|
||||
@@ -174,21 +174,21 @@ export const serverApi = createApi({
|
||||
} catch {
|
||||
dispatch(updateInfo({ name: prevName, description: prevDesc }));
|
||||
}
|
||||
},
|
||||
}
|
||||
}),
|
||||
createAdmin: builder.mutation({
|
||||
query: (data) => ({
|
||||
url: `/admin/system/create_admin`,
|
||||
method: "POST",
|
||||
body: data,
|
||||
}),
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
getInitialized: builder.query({
|
||||
query: () => ({
|
||||
url: `/admin/system/initialized`,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
url: `/admin/system/initialized`
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
export const {
|
||||
@@ -217,5 +217,5 @@ export const {
|
||||
useGetThirdPartySecretQuery,
|
||||
useUpdateThirdPartySecretMutation,
|
||||
useCreateAdminMutation,
|
||||
useGetInitializedQuery,
|
||||
useGetInitializedQuery
|
||||
} = serverApi;
|
||||
|
||||
Reference in New Issue
Block a user