فهرست منبع

can-send-message api

Viktoriia 3 ماه پیش
والد
کامیت
c62d2107a7

+ 7 - 1
src/modules/api/chat/chat-api.ts

@@ -65,6 +65,10 @@ export interface PostCanCreateGroupReturn extends ResponseType {
   authorized: 0 | 1;
 }
 
+export interface PostCanSendMessageReturn extends ResponseType {
+  need_authentication_or_friend?: 0 | 1;
+}
+
 interface Attachement {
   id: number;
   filename: string;
@@ -517,5 +521,7 @@ export const chatApi = {
     }),
   editGroupMessage: (data: PostEditGroupMessage) =>
     request.postForm<ResponseType>(API.EDIT_GROUP_MESSAGE, data),
-  editMessage: (data: PostEditMessage) => request.postForm<ResponseType>(API.EDIT_MESSAGE, data)
+  editMessage: (data: PostEditMessage) => request.postForm<ResponseType>(API.EDIT_MESSAGE, data),
+  canSendMessage: (token: string, to_uid: number) =>
+    request.postForm<PostCanSendMessageReturn>(API.CAN_SEND_MESSAGE, { token, to_uid })
 };

+ 2 - 1
src/modules/api/chat/chat-query-keys.tsx

@@ -55,5 +55,6 @@ export const chatQueryKeys = {
     ['getPinnedGroup', token, group_token] as const,
   setPinGroupMessage: () => ['setPinGroupMessage'] as const,
   editGroupMessage: () => ['editGroupMessage'] as const,
-  editMessage: () => ['editMessage'] as const
+  editMessage: () => ['editMessage'] as const,
+  canSendMessage: (token: string, to_uid: number) => ['canSendMessage', token, to_uid] as const
 };

+ 1 - 0
src/modules/api/chat/queries/index.ts

@@ -41,3 +41,4 @@ export * from './use-post-get-pinned-group-message';
 export * from './use-post-set-pin-group-message';
 export * from './use-post-edit-group-message';
 export * from './use-post-edit-message';
+export * from './use-post-can-send-message';

+ 17 - 0
src/modules/api/chat/queries/use-post-can-send-message.tsx

@@ -0,0 +1,17 @@
+import { useQuery } from '@tanstack/react-query';
+
+import { chatQueryKeys } from '../chat-query-keys';
+import { chatApi, type PostCanSendMessageReturn } from '../chat-api';
+
+import type { BaseAxiosError } from '../../../../types';
+
+export const usePostCanSendMessageQuery = (token: string, to_uid: number, enabled: boolean) => {
+  return useQuery<PostCanSendMessageReturn, BaseAxiosError>({
+    queryKey: chatQueryKeys.canSendMessage(token, to_uid),
+    queryFn: async () => {
+      const response = await chatApi.canSendMessage(token, to_uid);
+      return response.data;
+    },
+    enabled
+  });
+};

+ 47 - 7
src/screens/InAppScreens/MessagesScreen/ChatScreen/index.tsx

@@ -48,7 +48,8 @@ import {
   usePostMessagesReadMutation,
   usePostReactToMessageMutation,
   usePostSendMessageMutation,
-  usePostEditMessageMutation
+  usePostEditMessageMutation,
+  usePostCanSendMessageQuery
 } from '@api/chat';
 import { CustomMessage, Message, Reaction } from '../types';
 import { API_HOST, APP_VERSION, WEBSOCKET_URL } from 'src/constants';
@@ -106,6 +107,7 @@ const ChatScreen = ({ route }: { route: any }) => {
   const [messages, setMessages] = useState<CustomMessage[] | null>();
   const navigation = useNavigation();
   const [prevThenMessageId, setPrevThenMessageId] = useState<number>(-1);
+  const { data: canSendMessage } = usePostCanSendMessageQuery(token, id, true);
   const {
     data: chatData,
     refetch,
@@ -182,6 +184,39 @@ const ChatScreen = ({ route }: { route: any }) => {
     });
   }, []);
 
+  useEffect(() => {
+    if (canSendMessage && canSendMessage.need_authentication_or_friend === 1) {
+      setModalInfo({
+        visible: true,
+        type: 'success',
+        message: (
+          <Text>
+            Only legit NomadManias and friends can send messages.{'\n'}Befriend this traveller first
+            or{' '}
+            <Text
+              style={{
+                color: Colors.ORANGE,
+                textDecorationLine: 'underline',
+                textDecorationColor: Colors.ORANGE
+              }}
+              onPress={() =>
+                Linking.openURL('https://nomadmania.com/badges/').catch((err) =>
+                  console.error('Failed to open auth URL:', err)
+                )
+              }
+            >
+              become Legit
+            </Text>
+            !
+          </Text>
+        ),
+        action: () => {},
+        buttonTitle: 'OK',
+        title: 'Oops!'
+      });
+    }
+  }, [canSendMessage]);
+
   const onSendMedia = useCallback(
     async (files: { uri: string; type: 'image' | 'video' }[]) => {
       for (const file of files) {
@@ -981,7 +1016,11 @@ const ChatScreen = ({ route }: { route: any }) => {
           setHasMoreMessages(false);
         }
 
-        if (mappedMessages.length === 0 && !modalInfo.visible) {
+        if (
+          mappedMessages.length === 0 &&
+          !modalInfo.visible &&
+          !canSendMessage?.need_authentication_or_friend
+        ) {
           setTimeout(() => {
             textInputRef.current?.focus();
           }, 500);
@@ -993,7 +1032,11 @@ const ChatScreen = ({ route }: { route: any }) => {
   );
 
   useEffect(() => {
-    if (messages?.length === 0 && !modalInfo.visible) {
+    if (
+      messages?.length === 0 &&
+      !modalInfo.visible &&
+      !canSendMessage?.need_authentication_or_friend
+    ) {
       setTimeout(() => {
         textInputRef.current?.focus();
       }, 500);
@@ -1440,10 +1483,7 @@ const ChatScreen = ({ route }: { route: any }) => {
                     !
                   </Text>
                 ),
-                action: () =>
-                  Linking.openURL('https://nomadmania.com/badges/').catch((err) =>
-                    console.error('Failed to open auth URL:', err)
-                  ),
+                action: () => {},
                 buttonTitle: 'OK',
                 title: 'Oops!'
               });

+ 4 - 2
src/types/api.ts

@@ -184,7 +184,8 @@ export enum API_ENDPOINT {
   GET_PINNED_GROUP_MESSAGE = 'get-pinned-group-message',
   SET_PIN_GROUP_MESSAGE = 'set-pin-group-message',
   EDIT_GROUP_MESSAGE = 'edit-group-message',
-  EDIT_MESSAGE = 'edit-message'
+  EDIT_MESSAGE = 'edit-message',
+  CAN_SEND_MESSAGE = 'can-send-message'
 }
 
 export enum API {
@@ -343,7 +344,8 @@ export enum API {
   GET_PINNED_GROUP_MESSAGE = `${API_ROUTE.CHAT}/${API_ENDPOINT.GET_PINNED_GROUP_MESSAGE}`,
   SET_PIN_GROUP_MESSAGE = `${API_ROUTE.CHAT}/${API_ENDPOINT.SET_PIN_GROUP_MESSAGE}`,
   EDIT_GROUP_MESSAGE = `${API_ROUTE.CHAT}/${API_ENDPOINT.EDIT_GROUP_MESSAGE}`,
-  EDIT_MESSAGE = `${API_ROUTE.CHAT}/${API_ENDPOINT.EDIT_MESSAGE}`
+  EDIT_MESSAGE = `${API_ROUTE.CHAT}/${API_ENDPOINT.EDIT_MESSAGE}`,
+  CAN_SEND_MESSAGE = `${API_ROUTE.CHAT}/${API_ENDPOINT.CAN_SEND_MESSAGE}`
 }
 
 export type BaseAxiosError = AxiosError;