|
| 1 | +import { UserProps } from "../context/reducers/usersReducer"; |
| 2 | +import axios from "./Axios"; |
| 3 | +import { ConfigsProps } from "../context/reducers/appReducer"; |
| 4 | +import { RoomProps } from "../context/reducers/roomsReducer"; |
| 5 | + |
| 6 | +interface AuthKeysProps { |
| 7 | + apiKey: string; |
| 8 | + subscriptionKey: string; |
| 9 | +} |
| 10 | + |
| 11 | +export interface InitiateProps extends AuthKeysProps { |
| 12 | + tenantId: string; |
| 13 | +} |
| 14 | + |
| 15 | +interface OnlineInfoProps extends AuthKeysProps { |
| 16 | + userIds: string; |
| 17 | +} |
| 18 | + |
| 19 | +type OnlineInfoReturnProps = Pick< |
| 20 | + UserProps, |
| 21 | + "chatAuthId" | "socketId" | "online" |
| 22 | +>; |
| 23 | + |
| 24 | +interface ReadTokenProps extends AuthKeysProps { |
| 25 | + roomIds: string; |
| 26 | +} |
| 27 | + |
| 28 | +type ReadTokenReturnProps = Pick< |
| 29 | + RoomProps, |
| 30 | + "chatRoomAuthId" | "lastMessageReadToken" |
| 31 | +>; |
| 32 | + |
| 33 | +export const initiate = async (props: InitiateProps) => { |
| 34 | + const { tenantId, apiKey, subscriptionKey } = props; |
| 35 | + |
| 36 | + try { |
| 37 | + const response = await axios.get<ConfigsProps>( |
| 38 | + `/api/v1/tenant/config/${tenantId}`, |
| 39 | + { |
| 40 | + params: { |
| 41 | + tenantId, |
| 42 | + plugin: true, // This is for backend compatibility |
| 43 | + }, |
| 44 | + headers: { |
| 45 | + apiKey, |
| 46 | + subscriptionKey, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + if (response.data.tenantId === tenantId) { |
| 52 | + return response.data; |
| 53 | + } |
| 54 | + |
| 55 | + return; |
| 56 | + } catch { |
| 57 | + // TODO: Log error |
| 58 | + /* empty */ |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +export const getUsersOnlineInfo = async (props: OnlineInfoProps) => { |
| 63 | + const { userIds, apiKey, subscriptionKey } = props; |
| 64 | + |
| 65 | + try { |
| 66 | + const response = await axios.get<OnlineInfoReturnProps[]>( |
| 67 | + `/api/v1/user/active-users`, |
| 68 | + { |
| 69 | + params: { |
| 70 | + userIds, |
| 71 | + }, |
| 72 | + headers: { |
| 73 | + apiKey, |
| 74 | + subscriptionKey, |
| 75 | + }, |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + return response.data; |
| 80 | + } catch { |
| 81 | + // TODO: Log error |
| 82 | + /* empty */ |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +export const getRoomsReadToken = async (props: ReadTokenProps) => { |
| 87 | + const { roomIds, apiKey, subscriptionKey } = props; |
| 88 | + |
| 89 | + try { |
| 90 | + const response = await axios.get<ReadTokenReturnProps[]>( |
| 91 | + `/api/v1/room/active-rooms`, |
| 92 | + { |
| 93 | + params: { |
| 94 | + roomIds, |
| 95 | + }, |
| 96 | + headers: { |
| 97 | + apiKey, |
| 98 | + subscriptionKey, |
| 99 | + }, |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + return response.data; |
| 104 | + } catch { |
| 105 | + // TODO: Log error |
| 106 | + /* empty */ |
| 107 | + } |
| 108 | +}; |
0 commit comments