Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Azure.Communication.Common] Added support for rawId ⟷ CommunicationIdentifier conversion #28574

Merged
merged 14 commits into from
Jun 30, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ namespace Azure.Communication
public abstract partial class CommunicationIdentifier : System.IEquatable<Azure.Communication.CommunicationIdentifier>
{
protected CommunicationIdentifier() { }
public virtual string RawId { get { throw null; } }
public abstract bool Equals(Azure.Communication.CommunicationIdentifier other);
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override bool Equals(object obj) { throw null; }
public static Azure.Communication.CommunicationIdentifier FromRawId(string rawId) { throw null; }
public override int GetHashCode() { throw null; }
}
public sealed partial class CommunicationTokenCredential : System.IDisposable
{
Expand All @@ -44,6 +47,7 @@ public partial class CommunicationUserIdentifier : Azure.Communication.Communica
{
public CommunicationUserIdentifier(string id) { }
public string Id { get { throw null; } }
public override string RawId { get { throw null; } }
public override bool Equals(Azure.Communication.CommunicationIdentifier other) { throw null; }
public override int GetHashCode() { throw null; }
public override string ToString() { throw null; }
Expand All @@ -53,7 +57,7 @@ public partial class MicrosoftTeamsUserIdentifier : Azure.Communication.Communic
public MicrosoftTeamsUserIdentifier(string userId, bool isAnonymous = false, Azure.Communication.CommunicationCloudEnvironment? cloud = default(Azure.Communication.CommunicationCloudEnvironment?), string rawId = null) { }
public Azure.Communication.CommunicationCloudEnvironment Cloud { get { throw null; } }
public bool IsAnonymous { get { throw null; } }
public string RawId { get { throw null; } }
public override string RawId { get { throw null; } }
public string UserId { get { throw null; } }
public override bool Equals(Azure.Communication.CommunicationIdentifier other) { throw null; }
public override int GetHashCode() { throw null; }
Expand All @@ -63,7 +67,7 @@ public partial class PhoneNumberIdentifier : Azure.Communication.CommunicationId
{
public PhoneNumberIdentifier(string phoneNumber, string rawId = null) { }
public string PhoneNumber { get { throw null; } }
public string RawId { get { throw null; } }
public override string RawId { get { throw null; } }
public override bool Equals(Azure.Communication.CommunicationIdentifier other) { throw null; }
public override int GetHashCode() { throw null; }
public override string ToString() { throw null; }
Expand All @@ -72,6 +76,7 @@ public partial class UnknownIdentifier : Azure.Communication.CommunicationIdenti
{
public UnknownIdentifier(string id) { }
public string Id { get { throw null; } }
public override string RawId { get { throw null; } }
public override bool Equals(Azure.Communication.CommunicationIdentifier other) { throw null; }
public override int GetHashCode() { throw null; }
public override string ToString() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CommunicationCloudEnvironment(string value)
/// <summary> Determines if two <see cref="CommunicationCloudEnvironment"/> values are not the same. </summary>
public static bool operator !=(CommunicationCloudEnvironment left, CommunicationCloudEnvironment right) => !left.Equals(right);
/// <summary> Converts a string to a <see cref="CommunicationCloudEnvironment"/>. </summary>
public static implicit operator CommunicationCloudEnvironment(string value) => new CommunicationCloudEnvironment(value);
public static implicit operator CommunicationCloudEnvironment(string value) => new(value);

/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,61 @@

using System;
using System.ComponentModel;
using Azure.Core;

namespace Azure.Communication
{
/// <summary>Represents an identifier in Azure Communication Services.</summary>
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
public abstract class CommunicationIdentifier : IEquatable<CommunicationIdentifier>
#pragma warning restore CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
/// <summary>
/// Returns the RawId representation of the CommunicationIdentifier.
/// You can use the RawId for encoding the identifier and then use it as a key in a database.
/// </summary>
public virtual string RawId { get; }
petrsvihlik marked this conversation as resolved.
Show resolved Hide resolved

/// <inheritdoc />
public override int GetHashCode() => RawId.GetHashCode();

/// <inheritdoc />
public abstract bool Equals(CommunicationIdentifier other);

/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
=> obj is CommunicationIdentifier other && Equals(other);

/// <summary>
/// Creates a <see cref="CommunicationIdentifier"/> from a given rawId.
/// When storing rawIds, use this function to restore the identifier that was encoded in the rawId.
/// </summary>
/// <param name="rawId">The rawId to be translated to its identifier representation.</param>
/// <returns></returns>
public static CommunicationIdentifier FromRawId(string rawId)
{
Argument.AssertNotNullOrEmpty(rawId, nameof(rawId));

if (rawId.StartsWith("4:", StringComparison.OrdinalIgnoreCase))
{
return new PhoneNumberIdentifier($"+{rawId.Substring("4:".Length)}");
}

var segments = rawId.Split(':');
if (segments.Length < 3)
return new UnknownIdentifier(rawId);

var prefix = $"{segments[0]}:{segments[1]}:";
var suffix = rawId.Substring(prefix.Length);

return prefix switch
{
"8:teamsvisitor:" => new MicrosoftTeamsUserIdentifier(suffix, true),
petrsvihlik marked this conversation as resolved.
Show resolved Hide resolved
"8:orgid:" => new MicrosoftTeamsUserIdentifier(suffix, false, CommunicationCloudEnvironment.Public),
"8:dod:" => new MicrosoftTeamsUserIdentifier(suffix, false, CommunicationCloudEnvironment.Dod),
"8:gcch:" => new MicrosoftTeamsUserIdentifier(suffix, false, CommunicationCloudEnvironment.Gcch),
"8:acs:" or "8:spool:" or "8:dod-acs:" or "8:gcch-acs:" => new CommunicationUserIdentifier(rawId),
_ => new UnknownIdentifier(rawId),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class CommunicationUserIdentifier : CommunicationIdentifier
/// <summary>The id of the communication user.</summary>
public string Id { get; }

/// <inheritdoc />
public override string RawId => Id;

/// <summary>
/// Initializes a new instance of <see cref="CommunicationUserIdentifier"/>.
/// </summary>
Expand All @@ -31,10 +34,10 @@ public CommunicationUserIdentifier(string id)
public override string ToString() => Id;

/// <inheritdoc />
public override int GetHashCode() => Id.GetHashCode();
public override int GetHashCode() => RawId.GetHashCode();

/// <inheritdoc />
public override bool Equals(CommunicationIdentifier other)
=> other is CommunicationUserIdentifier otherId && otherId.Id == Id;
=> other is CommunicationUserIdentifier otherId && otherId.RawId == RawId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,34 @@ namespace Azure.Communication
/// <summary>Represents a Microsoft Teams user.</summary>
public class MicrosoftTeamsUserIdentifier : CommunicationIdentifier
{
private readonly string _rawId;

/// <summary>The optional raw id of the Microsoft Teams User identifier.</summary>
public string RawId { get; }
public override string RawId
{
get
{
if (_rawId != null)
return _rawId;

if (IsAnonymous)
{
return $"8:teamsvisitor:{UserId}";
}
else if (Cloud == CommunicationCloudEnvironment.Dod)
{
return $"8:dod:{UserId}";
}
else if (Cloud == CommunicationCloudEnvironment.Gcch)
{
return $"8:gcch:{UserId}";
}
else
{
return $"8:orgid:{UserId}";
}
}
}

/// <summary>The id of the Microsoft Teams user. If the user isn't anonymous, the id is the AAD object id of the user.</summary>
public string UserId { get; }
Expand Down Expand Up @@ -39,21 +65,18 @@ public MicrosoftTeamsUserIdentifier(string userId, bool isAnonymous = false, Com
UserId = userId;
IsAnonymous = isAnonymous;
Cloud = cloud ?? CommunicationCloudEnvironment.Public;
RawId = rawId;
_rawId = rawId;
}

/// <inheritdoc />
public override string ToString() => UserId;

/// <inheritdoc />
public override int GetHashCode() => UserId.GetHashCode();
public override int GetHashCode() => RawId.GetHashCode();

/// <inheritdoc />
public override bool Equals(CommunicationIdentifier other)
=> other is MicrosoftTeamsUserIdentifier otherId
&& otherId.UserId == UserId
&& otherId.IsAnonymous == IsAnonymous
&& otherId.Cloud == Cloud
&& (RawId is null || otherId.RawId is null || RawId == otherId.RawId);
&& otherId.RawId == RawId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ namespace Azure.Communication
/// <summary>Represents a Phone Number.</summary>
public class PhoneNumberIdentifier : CommunicationIdentifier
{
private readonly string _rawId;

/// <summary>The optional raw id of the phone number.</summary>
petrsvihlik marked this conversation as resolved.
Show resolved Hide resolved
public string RawId { get; }
public override string RawId
{
get
{
return _rawId ?? $"4:{PhoneNumber.TrimStart('+')}";
}
}

/// <summary>The phone number in E.164 format.</summary>
public string PhoneNumber { get; }
Expand All @@ -27,17 +35,17 @@ public PhoneNumberIdentifier(string phoneNumber, string rawId = null)
{
Argument.AssertNotNullOrEmpty(phoneNumber, nameof(phoneNumber));
PhoneNumber = phoneNumber;
RawId = rawId;
_rawId = rawId;
iaulakh marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc />
public override string ToString() => PhoneNumber;

/// <inheritdoc />
public override int GetHashCode() => PhoneNumber.GetHashCode();
public override int GetHashCode() => RawId.GetHashCode();
petrsvihlik marked this conversation as resolved.
Show resolved Hide resolved

/// <inheritdoc />
public override bool Equals(CommunicationIdentifier other)
=> other is PhoneNumberIdentifier otherId && otherId.PhoneNumber == PhoneNumber && (RawId is null || otherId.RawId is null || RawId == otherId.RawId);
=> other is PhoneNumberIdentifier otherId && otherId.RawId == RawId;
petrsvihlik marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class UnknownIdentifier : CommunicationIdentifier
/// <summary>The id of the endpoint.</summary>
public string Id { get; }

/// <inheritdoc />
public override string RawId => Id;

/// <summary>
/// Initializes a new instance of <see cref="UnknownIdentifier"/>.
/// </summary>
Expand All @@ -31,10 +34,10 @@ public UnknownIdentifier(string id)
public override string ToString() => Id;

/// <inheritdoc />
public override int GetHashCode() => Id.GetHashCode();
public override int GetHashCode() => RawId.GetHashCode();

/// <inheritdoc />
public override bool Equals(CommunicationIdentifier other)
=> other is UnknownIdentifier otherId && otherId.Id == Id;
=> other is UnknownIdentifier otherId && otherId.RawId == RawId;
}
}
Loading