feat user restriction

This commit is contained in:
Quan HL
2023-11-07 14:06:49 +07:00
parent 28ddf0faf2
commit b3a1965be2
3 changed files with 135 additions and 85 deletions

View File

@@ -3,6 +3,7 @@ import {
FetchError, FetchError,
FetchTransport, FetchTransport,
Queue, Queue,
RegisteredUser,
StatusCodes, StatusCodes,
} from "./types"; } from "./types";
import { MSG_SOMETHING_WRONG } from "./constants"; import { MSG_SOMETHING_WRONG } from "./constants";
@@ -144,3 +145,10 @@ export const getQueues = () => {
`${advancedSettings.apiServer}/Accounts/${advancedSettings.accountSid}/Queues` `${advancedSettings.apiServer}/Accounts/${advancedSettings.accountSid}/Queues`
); );
}; };
export const getSelfRegisteredUser = (username: string) => {
const advancedSettings = getAdvancedSettings();
return getFetch<RegisteredUser>(
`${advancedSettings.apiServer}/Accounts/${advancedSettings.accountSid}/RegisteredSipUsers/${username}`
);
};

View File

@@ -34,3 +34,14 @@ export interface Queue {
name: string; name: string;
length: number; length: number;
} }
export interface RegisteredUser {
name: string;
contact: string;
expiryTime: number;
protocol: string;
allow_direct_app_calling: boolean;
allow_direct_queue_calling: boolean;
allow_direct_user_calling: boolean;
registered_status: string;
}

View File

@@ -52,9 +52,15 @@ import {
import { OutGoingCall } from "./outgoing-call"; import { OutGoingCall } from "./outgoing-call";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import IconButtonMenu, { IconButtonMenuItems } from "src/components/menu"; import IconButtonMenu, { IconButtonMenuItems } from "src/components/menu";
import { getApplications, getQueues, getRegisteredUser } from "src/api"; import {
getApplications,
getQueues,
getRegisteredUser,
getSelfRegisteredUser,
} from "src/api";
import JambonzSwitch from "src/components/switch"; import JambonzSwitch from "src/components/switch";
import { DEFAULT_TOAST_DURATION } from "src/common/constants"; import { DEFAULT_TOAST_DURATION } from "src/common/constants";
import { RegisteredUser } from "src/api/types";
type PhoneProbs = { type PhoneProbs = {
sipDomain: string; sipDomain: string;
@@ -100,6 +106,13 @@ export const Phone = ({
const sipDisplayNameRef = useRef(""); const sipDisplayNameRef = useRef("");
const [isForceChangeUaStatus, setIsForceChangeUaStatus] = useState(false); const [isForceChangeUaStatus, setIsForceChangeUaStatus] = useState(false);
const isInputNumberFocusRef = useRef(false); const isInputNumberFocusRef = useRef(false);
const [registeredUser, setRegisteredUser] = useState<Partial<RegisteredUser>>(
{
allow_direct_app_calling: false,
allow_direct_queue_calling: false,
allow_direct_user_calling: false,
}
);
const toast = useToast(); const toast = useToast();
useEffect(() => { useEffect(() => {
@@ -162,6 +175,17 @@ export const Phone = ({
} }
}, [status]); }, [status]);
useEffect(() => {
getSelfRegisteredUser(sipUsernameRef.current).then(({ json }) => {
setRegisteredUser(json);
});
setInterval(() => {
getSelfRegisteredUser(sipUsernameRef.current).then(({ json }) => {
setRegisteredUser(json);
});
}, 20000);
}, []);
// useEffect(() => { // useEffect(() => {
// chrome.runtime.onMessage.addListener(function (request) { // chrome.runtime.onMessage.addListener(function (request) {
// const msg = request as Message<any>; // const msg = request as Message<any>;
@@ -478,91 +502,98 @@ export const Phone = ({
> >
{isAdvanceMode && isSipClientIdle(callStatus) && ( {isAdvanceMode && isSipClientIdle(callStatus) && (
<HStack spacing={2} align="start" w="full"> <HStack spacing={2} align="start" w="full">
<IconButtonMenu {registeredUser.allow_direct_user_calling && (
icon={<Users />} <IconButtonMenu
tooltip="Call an online user" icon={<Users />}
noResultLabel="No one else is online" tooltip="Call an online user"
onClick={(_, value) => { noResultLabel="No one else is online"
setInputNumber(value); onClick={(_, value) => {
makeOutboundCall(value); setInputNumber(value);
}} makeOutboundCall(value);
onOpen={() => { }}
return new Promise<IconButtonMenuItems[]>( onOpen={() => {
(resolve, reject) => { return new Promise<IconButtonMenuItems[]>(
getRegisteredUser() (resolve, reject) => {
.then(({ json }) => { getRegisteredUser()
resolve( .then(({ json }) => {
json resolve(
.filter((u) => !u.includes(sipUsername)) json
.map((u) => { .filter((u) => !u.includes(sipUsername))
const uName = u.match(/(^.*)@.*/); .map((u) => {
return { const uName = u.match(/(^.*)@.*/);
name: uName ? uName[1] : u, return {
value: uName ? uName[1] : u, name: uName ? uName[1] : u,
}; value: uName ? uName[1] : u,
}) };
); })
}) );
.catch((err) => reject(err)); })
} .catch((err) => reject(err));
); }
}} );
/> }}
<IconButtonMenu />
icon={<List />} )}
tooltip="Take a call from queue"
noResultLabel="No calls in queue"
onClick={(name, value) => {
setAppName(`Queue ${name}`);
const calledQueue = `queue-${value}`;
setInputNumber("");
makeOutboundCall(calledQueue, `Queue ${name}`);
}}
onOpen={() => {
return new Promise<IconButtonMenuItems[]>(
(resolve, reject) => {
getQueues()
.then(({ json }) => {
resolve(
json.map((q) => ({
name: `${q.name} (${q.length})`,
value: q.name,
}))
);
})
.catch((err) => reject(err));
}
);
}}
/>
<IconButtonMenu {registeredUser.allow_direct_queue_calling && (
icon={<GitMerge />} <IconButtonMenu
tooltip="Call an application" icon={<List />}
noResultLabel="No applications" tooltip="Take a call from queue"
onClick={(name, value) => { noResultLabel="No calls in queue"
setAppName(`App ${name}`); onClick={(name, value) => {
const calledAppId = `app-${value}`; setAppName(`Queue ${name}`);
setInputNumber(""); const calledQueue = `queue-${value}`;
makeOutboundCall(calledAppId, `App ${name}`); setInputNumber("");
}} makeOutboundCall(calledQueue, `Queue ${name}`);
onOpen={() => { }}
return new Promise<IconButtonMenuItems[]>( onOpen={() => {
(resolve, reject) => { return new Promise<IconButtonMenuItems[]>(
getApplications() (resolve, reject) => {
.then(({ json }) => { getQueues()
resolve( .then(({ json }) => {
json.map((a) => ({ resolve(
name: a.name, json.map((q) => ({
value: a.application_sid, name: `${q.name} (${q.length})`,
})) value: q.name,
); }))
}) );
.catch((err) => reject(err)); })
} .catch((err) => reject(err));
); }
}} );
/> }}
/>
)}
{registeredUser.allow_direct_app_calling && (
<IconButtonMenu
icon={<GitMerge />}
tooltip="Call an application"
noResultLabel="No applications"
onClick={(name, value) => {
setAppName(`App ${name}`);
const calledAppId = `app-${value}`;
setInputNumber("");
makeOutboundCall(calledAppId, `App ${name}`);
}}
onOpen={() => {
return new Promise<IconButtonMenuItems[]>(
(resolve, reject) => {
getApplications()
.then(({ json }) => {
resolve(
json.map((a) => ({
name: a.name,
value: a.application_sid,
}))
);
})
.catch((err) => reject(err));
}
);
}}
/>
)}
</HStack> </HStack>
)} )}