@azure/communication-common
Version:
Common package for Azure Communication services.
214 lines (213 loc) • 7.77 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var identifierModels_exports = {};
__export(identifierModels_exports, {
createIdentifierFromRawId: () => createIdentifierFromRawId,
getIdentifierKind: () => getIdentifierKind,
getIdentifierRawId: () => getIdentifierRawId,
isCommunicationUserIdentifier: () => isCommunicationUserIdentifier,
isMicrosoftTeamsAppIdentifier: () => isMicrosoftTeamsAppIdentifier,
isMicrosoftTeamsUserIdentifier: () => isMicrosoftTeamsUserIdentifier,
isPhoneNumberIdentifier: () => isPhoneNumberIdentifier,
isTeamsExtensionUserIdentifier: () => isTeamsExtensionUserIdentifier,
isUnknownIdentifier: () => isUnknownIdentifier
});
module.exports = __toCommonJS(identifierModels_exports);
const isCommunicationUserIdentifier = (identifier) => {
return typeof identifier.communicationUserId === "string";
};
const isPhoneNumberIdentifier = (identifier) => {
return typeof identifier.phoneNumber === "string";
};
const isMicrosoftTeamsUserIdentifier = (identifier) => {
return typeof identifier.microsoftTeamsUserId === "string";
};
const isMicrosoftTeamsAppIdentifier = (identifier) => {
return typeof identifier.teamsAppId === "string";
};
const isTeamsExtensionUserIdentifier = (identifier) => {
const userIdExists = typeof identifier.userId === "string";
const tenantIdExists = typeof identifier.tenantId === "string";
const resourceIdExists = typeof identifier.resourceId === "string";
return userIdExists && tenantIdExists && resourceIdExists;
};
const isUnknownIdentifier = (identifier) => {
return typeof identifier.id === "string";
};
const getIdentifierKind = (identifier) => {
if (isCommunicationUserIdentifier(identifier)) {
return { ...identifier, kind: "communicationUser" };
}
if (isPhoneNumberIdentifier(identifier)) {
return { ...identifier, kind: "phoneNumber" };
}
if (isMicrosoftTeamsUserIdentifier(identifier)) {
return { ...identifier, kind: "microsoftTeamsUser" };
}
if (isMicrosoftTeamsAppIdentifier(identifier)) {
return { ...identifier, kind: "microsoftTeamsApp" };
}
if (isTeamsExtensionUserIdentifier(identifier)) {
return { ...identifier, kind: "teamsExtensionUser" };
}
return { ...identifier, kind: "unknown" };
};
const getIdentifierRawId = (identifier) => {
const identifierKind = getIdentifierKind(identifier);
switch (identifierKind.kind) {
case "communicationUser":
return identifierKind.communicationUserId;
case "microsoftTeamsUser": {
const { microsoftTeamsUserId, rawId, cloud, isAnonymous } = identifierKind;
if (rawId) return rawId;
if (isAnonymous) return `8:teamsvisitor:${microsoftTeamsUserId}`;
switch (cloud) {
case "dod":
return `8:dod:${microsoftTeamsUserId}`;
case "gcch":
return `8:gcch:${microsoftTeamsUserId}`;
case "public":
return `8:orgid:${microsoftTeamsUserId}`;
}
return `8:orgid:${microsoftTeamsUserId}`;
}
case "microsoftTeamsApp": {
const { teamsAppId, rawId, cloud } = identifierKind;
if (rawId) return rawId;
switch (cloud) {
case "dod":
return `28:dod:${teamsAppId}`;
case "gcch":
return `28:gcch:${teamsAppId}`;
}
return `28:orgid:${teamsAppId}`;
}
case "phoneNumber": {
const { phoneNumber, rawId } = identifierKind;
if (rawId) return rawId;
return `4:${phoneNumber}`;
}
case "teamsExtensionUser": {
const { userId, tenantId, resourceId, rawId, cloud } = identifierKind;
if (rawId) return rawId;
switch (cloud) {
case "dod":
return `8:dod-acs:${resourceId}_${tenantId}_${userId}`;
case "gcch":
return `8:gcch-acs:${resourceId}_${tenantId}_${userId}`;
}
return `8:acs:${resourceId}_${tenantId}_${userId}`;
}
case "unknown": {
return identifierKind.id;
}
}
};
const buildMicrosoftTeamsAppIdentifier = (teamsAppId, cloud) => {
return {
kind: "microsoftTeamsApp",
teamsAppId,
cloud
};
};
const buildMicrosoftTeamsUserIdentifier = (id, cloud, isAnonymous) => {
return {
kind: "microsoftTeamsUser",
microsoftTeamsUserId: id,
isAnonymous,
cloud
};
};
const buildTeamsExtensionUserOrCommunicationUserIdentifier = (rawId, suffix, cloud) => {
const segments = suffix.split("_");
if (segments.length !== 3) {
return { kind: "communicationUser", communicationUserId: rawId };
}
const resourceId = segments[0];
const tenantId = segments[1];
const userId = segments[2];
return {
kind: "teamsExtensionUser",
userId,
tenantId,
resourceId,
cloud
};
};
const buildPhoneNumberIdentifier = (rawId) => {
const phoneNumber = rawId.substring("4:".length);
const isAnonymous = phoneNumber === "anonymous";
const assertedIdIndex = isAnonymous ? -1 : phoneNumber.lastIndexOf("_") + 1;
const hasAssertedId = assertedIdIndex > 0 && assertedIdIndex < phoneNumber.length;
const assertedId = hasAssertedId ? phoneNumber.substring(assertedIdIndex) : void 0;
return {
kind: "phoneNumber",
phoneNumber,
isAnonymous,
assertedId
};
};
const createIdentifierFromRawId = (rawId) => {
if (rawId.startsWith("4:")) {
return buildPhoneNumberIdentifier(rawId);
}
const segments = rawId.split(":");
if (segments.length !== 3) {
return { kind: "unknown", id: rawId };
}
const prefix = `${segments[0]}:${segments[1]}:`;
const suffix = segments[2];
switch (prefix) {
case "8:teamsvisitor:":
return { kind: "microsoftTeamsUser", microsoftTeamsUserId: suffix, isAnonymous: true };
case "8:orgid:":
return buildMicrosoftTeamsUserIdentifier(suffix, "public", false);
case "8:dod:":
return buildMicrosoftTeamsUserIdentifier(suffix, "dod", false);
case "8:gcch:":
return buildMicrosoftTeamsUserIdentifier(suffix, "gcch", false);
case "8:acs:":
return buildTeamsExtensionUserOrCommunicationUserIdentifier(rawId, suffix, "public");
case "8:dod-acs:":
return buildTeamsExtensionUserOrCommunicationUserIdentifier(rawId, suffix, "dod");
case "8:gcch-acs:":
return buildTeamsExtensionUserOrCommunicationUserIdentifier(rawId, suffix, "gcch");
case "8:spool:":
return { kind: "communicationUser", communicationUserId: rawId };
case "28:orgid:":
return buildMicrosoftTeamsAppIdentifier(suffix, "public");
case "28:gcch:":
return buildMicrosoftTeamsAppIdentifier(suffix, "gcch");
case "28:dod:":
return buildMicrosoftTeamsAppIdentifier(suffix, "dod");
}
return { kind: "unknown", id: rawId };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createIdentifierFromRawId,
getIdentifierKind,
getIdentifierRawId,
isCommunicationUserIdentifier,
isMicrosoftTeamsAppIdentifier,
isMicrosoftTeamsUserIdentifier,
isPhoneNumberIdentifier,
isTeamsExtensionUserIdentifier,
isUnknownIdentifier
});
//# sourceMappingURL=identifierModels.js.map