refactor: more TS code
This commit is contained in:
+34
-28
@@ -3,7 +3,13 @@ import { nanoid } from "@reduxjs/toolkit";
|
||||
import baseQuery from "./base.query";
|
||||
import { setAuthData, updateToken, resetAuthData, updateInitialized } from "../slices/auth.data";
|
||||
import BASE_URL, { KEY_DEVICE_KEY, KEY_LOCAL_MAGIC_TOKEN } from "../config";
|
||||
import { AuthData, LoginCredential } from "../../types/auth";
|
||||
import {
|
||||
AuthData,
|
||||
CredentialResponse,
|
||||
LoginCredential,
|
||||
RenewTokenDTO,
|
||||
RenewTokenResponse
|
||||
} from "../../types/auth";
|
||||
|
||||
const getDeviceId = () => {
|
||||
let d = localStorage.getItem(KEY_DEVICE_KEY);
|
||||
@@ -59,14 +65,11 @@ export const authApi = createApi({
|
||||
})
|
||||
}),
|
||||
// 更新token
|
||||
renew: builder.mutation({
|
||||
query: ({ token, refreshToken }) => ({
|
||||
renew: builder.mutation<RenewTokenResponse, RenewTokenDTO>({
|
||||
query: (data) => ({
|
||||
url: "/token/renew",
|
||||
method: "POST",
|
||||
body: {
|
||||
token,
|
||||
refresh_token: refreshToken
|
||||
}
|
||||
body: data
|
||||
}),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
@@ -79,7 +82,7 @@ export const authApi = createApi({
|
||||
}
|
||||
}),
|
||||
// 更新 device token
|
||||
updateDeviceToken: builder.mutation({
|
||||
updateDeviceToken: builder.mutation<void, { device_token: string }>({
|
||||
query: (device_token) => ({
|
||||
url: "/token/device_token",
|
||||
method: "PUT",
|
||||
@@ -89,66 +92,69 @@ export const authApi = createApi({
|
||||
})
|
||||
}),
|
||||
// 获取openid
|
||||
getOpenid: builder.mutation({
|
||||
query: ({ issuer, redirect_uri }) => ({
|
||||
getOpenid: builder.mutation<{ url: string }, { issuer: string; redirect_uri: string }>({
|
||||
query: (data) => ({
|
||||
url: "/token/openid/authorize",
|
||||
method: "POST",
|
||||
body: {
|
||||
issuer,
|
||||
redirect_uri
|
||||
}
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
|
||||
checkMagicTokenValid: builder.mutation({
|
||||
checkMagicTokenValid: builder.mutation<boolean, string>({
|
||||
query: (token) => ({
|
||||
url: "user/check_magic_token",
|
||||
method: "POST",
|
||||
body: { magic_token: token }
|
||||
})
|
||||
}),
|
||||
updatePassword: builder.mutation({
|
||||
query: ({ old_password, new_password }) => ({
|
||||
updatePassword: builder.mutation<void, { old_password: string; new_password: string }>({
|
||||
query: (data) => ({
|
||||
url: "user/change_password",
|
||||
method: "POST",
|
||||
body: { old_password, new_password }
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
sendLoginMagicLink: builder.mutation({
|
||||
sendLoginMagicLink: builder.mutation<string, string>({
|
||||
query: (email) => ({
|
||||
headers: {
|
||||
// "content-type": "text/plain",
|
||||
accept: "text/plain"
|
||||
},
|
||||
url: `user/send_login_magic_link?email=${encodeURIComponent(email)}`,
|
||||
method: "POST",
|
||||
responseHandler: (response: Response) => response.text()
|
||||
// body: { email }
|
||||
})
|
||||
}),
|
||||
sendRegMagicLink: builder.mutation({
|
||||
sendRegMagicLink: builder.mutation<
|
||||
{
|
||||
new_magic_token: "string";
|
||||
mail_is_sent: boolean;
|
||||
},
|
||||
{
|
||||
magic_token: "string";
|
||||
email: "string";
|
||||
password: "string";
|
||||
}
|
||||
>({
|
||||
query: (data) => ({
|
||||
url: `user/send_reg_magic_link`,
|
||||
method: "POST",
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
getMetamaskNonce: builder.query({
|
||||
getMetamaskNonce: builder.query<string, string>({
|
||||
query: (address) => ({
|
||||
url: `/token/metamask/nonce?public_address=${address}`
|
||||
})
|
||||
}),
|
||||
checkEmail: builder.query({
|
||||
checkEmail: builder.query<boolean, string>({
|
||||
query: (email) => ({
|
||||
url: `/user/check_email?email=${encodeURIComponent(email)}`
|
||||
})
|
||||
}),
|
||||
// todo: check return type
|
||||
getCredentials: builder.query<any, void>({
|
||||
getCredentials: builder.query<CredentialResponse, void>({
|
||||
query: () => ({ url: "/token/credentials" })
|
||||
}),
|
||||
// todo: check return type
|
||||
logout: builder.query<any, void>({
|
||||
logout: builder.query<void, void>({
|
||||
query: () => ({ url: "token/logout" }),
|
||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||
try {
|
||||
|
||||
@@ -9,9 +9,10 @@ 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 { Channel, ChannelDTO, CreateChannelDTO } from "../../types/channel";
|
||||
import { RootState } from "../store";
|
||||
import { ContentTypeKey } from "../../types/message";
|
||||
import { ContentTypeKey, ChatMessage } from "../../types/message";
|
||||
|
||||
export const channelApi = createApi({
|
||||
reducerPath: "channelApi",
|
||||
baseQuery,
|
||||
@@ -41,7 +42,7 @@ export const channelApi = createApi({
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
updateChannel: builder.mutation({
|
||||
updateChannel: builder.mutation<void, ChannelDTO>({
|
||||
query: ({ id, ...data }) => ({
|
||||
url: `group/${id}`,
|
||||
method: "PUT",
|
||||
@@ -49,7 +50,7 @@ export const channelApi = createApi({
|
||||
}),
|
||||
async onQueryStarted({ id, name, description }, { dispatch, queryFulfilled }) {
|
||||
// id: who send to ,from_uid: who sent
|
||||
const patchResult = dispatch(updateChannel({ id, name, description }));
|
||||
const patchResult = dispatch(updateChannel({ gid: id, name, description }));
|
||||
try {
|
||||
await queryFulfilled;
|
||||
} catch {
|
||||
@@ -58,7 +59,7 @@ export const channelApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
getHistoryMessages: builder.query({
|
||||
getHistoryMessages: builder.query<ChatMessage[], { id: number; mid?: number; limit: number }>({
|
||||
query: ({ id, mid = null, limit = 100 }) => ({
|
||||
url: mid
|
||||
? `/group/${id}/history?before=${mid}&limit=${limit}`
|
||||
@@ -145,21 +146,21 @@ export const channelApi = createApi({
|
||||
await onMessageSendStarted.call(this, param1, param2, "channel");
|
||||
}
|
||||
}),
|
||||
addMembers: builder.mutation({
|
||||
addMembers: builder.mutation<void, { id: number; members: number[] }>({
|
||||
query: ({ id, members }) => ({
|
||||
url: `group/${id}/members/add`,
|
||||
method: "POST",
|
||||
body: members
|
||||
})
|
||||
}),
|
||||
removeMembers: builder.mutation({
|
||||
removeMembers: builder.mutation<void, { id: number; members: number[] }>({
|
||||
query: ({ id, members }) => ({
|
||||
url: `group/${id}/members/remove`,
|
||||
method: "POST",
|
||||
body: members
|
||||
})
|
||||
}),
|
||||
updateIcon: builder.mutation({
|
||||
updateIcon: builder.mutation<void, { gid: number; image: File }>({
|
||||
query: ({ gid, image }) => ({
|
||||
headers: {
|
||||
"content-type": "image/png"
|
||||
|
||||
@@ -25,18 +25,6 @@ export const onMessageSendStarted = async (
|
||||
console.log("handlers data", content, type, properties, ignoreLocal, id);
|
||||
const isImage = properties.content_type?.startsWith("image");
|
||||
const ts = properties.local_id || +new Date();
|
||||
// let imageData = null;
|
||||
// if (type == "image") {
|
||||
// if (typeof content == "string" && content.startsWith("data:image")) {
|
||||
// // base64
|
||||
// // const resp = await fetch(content);
|
||||
// // const blob = await resp.blob();
|
||||
// // imageData = new File([blob], "tmp.png", { type: "image/png" });
|
||||
// imageData = content;
|
||||
// } else {
|
||||
// imageData = URL.createObjectURL(content);
|
||||
// }
|
||||
// }
|
||||
const tmpMsg = {
|
||||
content: isImage ? content.path : content,
|
||||
content_type: ContentTypes[type],
|
||||
|
||||
+15
-19
@@ -4,23 +4,16 @@ import { updateReadChannels, updateReadUsers } from "../slices/footprint";
|
||||
import { fillFavorites, 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 { OG } from "../../types/common";
|
||||
type UploadFileResponse = {
|
||||
path: string;
|
||||
size: number;
|
||||
hash: string;
|
||||
image_properties: {
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
};
|
||||
import { Archive, FavoriteArchive, OG } from "../../types/resource";
|
||||
import { ContentTypeKey, UploadFileResponse } from "../../types/message";
|
||||
import { RootState } from "../store";
|
||||
|
||||
export const messageApi = createApi({
|
||||
reducerPath: "messageApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
editMessage: builder.mutation({
|
||||
editMessage: builder.mutation<number, { mid: number; content: string; type: ContentTypeKey }>({
|
||||
query: ({ mid, content, type = "text" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type]
|
||||
@@ -50,7 +43,7 @@ export const messageApi = createApi({
|
||||
body: meta
|
||||
})
|
||||
}),
|
||||
createArchive: builder.mutation({
|
||||
createArchive: builder.mutation<string, number[]>({
|
||||
query: (mids = []) => ({
|
||||
url: `/resource/archive`,
|
||||
method: "POST",
|
||||
@@ -73,7 +66,7 @@ export const messageApi = createApi({
|
||||
url: `/resource/open_graphic_parse?url=${encodeURIComponent(url)}`
|
||||
})
|
||||
}),
|
||||
getArchiveMessage: builder.query({
|
||||
getArchiveMessage: builder.query<Archive, string>({
|
||||
query: (file_path) => ({
|
||||
url: `/resource/archive?file_path=${encodeURIComponent(file_path)}`
|
||||
})
|
||||
@@ -92,7 +85,7 @@ export const messageApi = createApi({
|
||||
body: { mid }
|
||||
})
|
||||
}),
|
||||
favoriteMessage: builder.mutation({
|
||||
favoriteMessage: builder.mutation<FavoriteArchive, number[]>({
|
||||
query: (mids) => ({
|
||||
url: `/favorite`,
|
||||
method: "POST",
|
||||
@@ -123,14 +116,14 @@ export const messageApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
getFavoriteDetails: builder.query({
|
||||
getFavoriteDetails: builder.query<Archive, number>({
|
||||
query: (id) => ({
|
||||
url: `/favorite/${id}`
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, queryFulfilled, getState }) {
|
||||
try {
|
||||
const { data } = await queryFulfilled;
|
||||
const loginUid = getState().authData.user.uid;
|
||||
const loginUid = (getState() as RootState).authData.user.uid;
|
||||
const messages = normalizeArchiveData(data, id, loginUid);
|
||||
dispatch(populateFavorite({ id, messages }));
|
||||
} catch (err) {
|
||||
@@ -138,7 +131,7 @@ export const messageApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
getFavorites: builder.query({
|
||||
getFavorites: builder.query<FavoriteArchive[], void>({
|
||||
query: () => ({
|
||||
url: `/favorite`
|
||||
}),
|
||||
@@ -155,7 +148,10 @@ export const messageApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
replyMessage: builder.mutation({
|
||||
replyMessage: builder.mutation<
|
||||
number,
|
||||
{ reply_mid: number; content: string; type: ContentTypeKey }
|
||||
>({
|
||||
query: ({ reply_mid, content, type = "text" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type]
|
||||
|
||||
@@ -3,7 +3,7 @@ import BASE_URL from "../config";
|
||||
import { updateInfo } from "../slices/server";
|
||||
import baseQuery from "./base.query";
|
||||
import { RootState } from "../store";
|
||||
import { User } from "../../types/auth";
|
||||
import { User } from "../../types/user";
|
||||
import {
|
||||
FirebaseConfig,
|
||||
GoogleAuthConfig,
|
||||
@@ -51,7 +51,6 @@ export const serverApi = createApi({
|
||||
getServerVersion: builder.query<string, void>({
|
||||
query: () => ({
|
||||
headers: {
|
||||
// "content-type": "text/plain",
|
||||
accept: "text/plain"
|
||||
},
|
||||
url: `/admin/system/version`,
|
||||
@@ -128,7 +127,7 @@ export const serverApi = createApi({
|
||||
body: data
|
||||
})
|
||||
}),
|
||||
updateLogo: builder.mutation({
|
||||
updateLogo: builder.mutation<void, File>({
|
||||
query: (data) => ({
|
||||
headers: {
|
||||
"content-type": "image/png"
|
||||
|
||||
@@ -9,13 +9,14 @@ import BASE_URL, { ContentTypes } from "../config";
|
||||
import { onMessageSendStarted } from "./handlers";
|
||||
import handleChatMessage from "../../common/hook/useStreaming/chat.handler";
|
||||
import { User, UserDTO, UserForAdmin, UserForAdminDTO } from "../../types/user";
|
||||
import { ContentType, MuteDTO } from "../../types/message";
|
||||
import { ChatMessage, ContentTypeKey, MuteDTO } from "../../types/message";
|
||||
import { RootState } from "../store";
|
||||
|
||||
export const userApi = createApi({
|
||||
reducerPath: "userApi",
|
||||
baseQuery,
|
||||
endpoints: (builder) => ({
|
||||
getUsers: builder.query({
|
||||
getUsers: builder.query<User[], void>({
|
||||
query: () => ({ url: `user` }),
|
||||
transformResponse: (data: User[]) => {
|
||||
return data.map((user) => {
|
||||
@@ -74,7 +75,7 @@ export const userApi = createApi({
|
||||
}
|
||||
}
|
||||
}),
|
||||
updateAvatar: builder.mutation({
|
||||
updateAvatar: builder.mutation<void, File>({
|
||||
query: (data) => ({
|
||||
headers: {
|
||||
"content-type": "image/png"
|
||||
@@ -92,7 +93,10 @@ export const userApi = createApi({
|
||||
})
|
||||
}),
|
||||
|
||||
sendMsg: builder.mutation({
|
||||
sendMsg: builder.mutation<
|
||||
number,
|
||||
{ id: number; content: string; type: ContentTypeKey; properties?: object }
|
||||
>({
|
||||
query: ({ id, content, type = "text", properties = "" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type],
|
||||
@@ -108,7 +112,7 @@ export const userApi = createApi({
|
||||
await onMessageSendStarted.call(this, param1, param2, "user");
|
||||
}
|
||||
}),
|
||||
getHistoryMessages: builder.query({
|
||||
getHistoryMessages: builder.query<ChatMessage[], { id: number; mid?: number; limit: number }>({
|
||||
query: ({ id, mid = null, limit = 100 }) => ({
|
||||
url: mid
|
||||
? `/user/${id}/history?before=${mid}&limit=${limit}`
|
||||
@@ -118,7 +122,7 @@ export const userApi = createApi({
|
||||
const { data: messages } = await queryFulfilled;
|
||||
if (messages?.length) {
|
||||
messages.forEach((msg) => {
|
||||
handleChatMessage(msg, dispatch, getState());
|
||||
handleChatMessage(msg, dispatch, getState() as RootState);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
KEY_TOKEN,
|
||||
KEY_UID
|
||||
} from "../config";
|
||||
import { AuthData, AuthToken, User } from "../../types/auth";
|
||||
import { AuthData, AuthToken, RenewTokenResponse, User } from "../../types/auth";
|
||||
|
||||
interface State {
|
||||
initialized: boolean;
|
||||
@@ -67,7 +67,7 @@ const authDataSlice = createSlice({
|
||||
updateInitialized(state, action: PayloadAction<boolean>) {
|
||||
state.initialized = action.payload;
|
||||
},
|
||||
updateToken(state, action: PayloadAction<AuthToken>) {
|
||||
updateToken(state, action: PayloadAction<RenewTokenResponse>) {
|
||||
const { token, refresh_token, expired_in } = action.payload;
|
||||
state.token = token;
|
||||
const et = +new Date() + Number(expired_in) * 1000;
|
||||
|
||||
@@ -53,7 +53,7 @@ const channelsSlice = createSlice({
|
||||
},
|
||||
updateChannel(state, action: PayloadAction<UpdateChannelDTO>) {
|
||||
const ignoreInPublic = ["add_member", "remove_member"];
|
||||
const { gid, operation, members = [], ...rest } = action.payload;
|
||||
const { gid, operation = "", members = [], ...rest } = action.payload;
|
||||
const currChannel = state.byId[gid];
|
||||
if (!currChannel || (currChannel.is_public && ignoreInPublic.includes(operation))) return;
|
||||
switch (operation) {
|
||||
|
||||
@@ -59,7 +59,7 @@ const uiSlice = createSlice({
|
||||
},
|
||||
updateRememberedNavs(
|
||||
state,
|
||||
action: PayloadAction<{ key?: "chat" | "user"; path: string | null } | undefined>
|
||||
action: PayloadAction<{ key?: "chat" | "user"; path?: string | null } | undefined>
|
||||
) {
|
||||
const { key = "chat", path = null } = action.payload || {};
|
||||
state.rememberedNavs[key] = path;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import { isNull, omitBy } from "lodash";
|
||||
import BASE_URL from "../config";
|
||||
import { User } from "../../types/auth";
|
||||
import { User } from "../../types/user";
|
||||
import { UserLog, UserState } from "../../types/sse";
|
||||
|
||||
export interface StoredUser extends User {
|
||||
|
||||
Reference in New Issue
Block a user