Skip to content

Commit

Permalink
[CommunicationIdentifier] Added support for rawId ⟷ CommunicationIden…
Browse files Browse the repository at this point in the history
…tifier conversion (#1116)

* Added support for rawId ⟷ CommunicationIdentifier conversion

* fix checkstyle

* remove raw id from phone number identifier

* additional test cases

* teams user raw id can not be changed after set

* Update CHANGELOG.md

* fix code review suggestions

* removing override hashCode

* Revert "removing override hashCode"

This reverts commit 014e8d4.
  • Loading branch information
iaulakh authored Jun 30, 2022
1 parent 1ce296a commit 6d3b1dc
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 52 deletions.
1 change: 1 addition & 0 deletions sdk/communication/azure-communication-common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release History

## 1.1.0-beta.1 (Unreleased)
- Added `String getRawId()`, and `static CommunicationIdentifier fromRawId(String rawId)` to `CommunicationIdentifier` to translate between a `CommunicationIdentifier` and its underlying canonical rawId representation. Developers can now use the rawId as an encoded format for identifiers to store in their databases or as stable keys in general.

### Features Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,75 @@
* Common communication identifier for Communication Services
*/
public abstract class CommunicationIdentifier {
protected String rawId;

/**
* When storing rawIds, use this function to restore the identifier that was encoded in the rawId.
*
* @param rawId raw id.
* @return CommunicationIdentifier
* @throws IllegalArgumentException raw id is null or empty.
*/
public static CommunicationIdentifier fromRawId(String rawId) {
if (rawId == null || rawId.trim().length() == 0) {
throw new IllegalArgumentException("The parameter [rawId] cannot be null to empty.");
}

if (rawId.startsWith("4:")) {
return new PhoneNumberIdentifier("+" + rawId.substring("4:".length()));
}
final String[] segments = rawId.split(":");
if (segments.length < 3) {
return new UnknownIdentifier(rawId);
}

final String prefix = segments[0] + ":" + segments[1] + ":";
final String suffix = rawId.substring(prefix.length());

if ("8:teamsvisitor:".equals(prefix)) {
return new MicrosoftTeamsUserIdentifier(suffix, true);
} else if ("8:orgid:".equals(prefix)) {
return new MicrosoftTeamsUserIdentifier(suffix, false);
} else if ("8:dod:".equals(prefix)) {
return new MicrosoftTeamsUserIdentifier(suffix, false)
.setCloudEnvironment(CommunicationCloudEnvironment.DOD);
} else if ("8:gcch:".equals(prefix)) {
return new MicrosoftTeamsUserIdentifier(suffix, false)
.setCloudEnvironment(CommunicationCloudEnvironment.GCCH);
} else if ("8:acs:".equals(prefix) || "8:spool:".equals(prefix)
|| "8:dod-acs:".equals(prefix) || "8:gcch-acs:".equals(prefix)) {
return new CommunicationUserIdentifier(rawId);
}

return new UnknownIdentifier(rawId);
}

/**
* Returns the rawId for a given CommunicationIdentifier.
* You can use the rawId for encoding the identifier and then use it as a key in a database.
*
* @return raw id
*/
public String getRawId() {
return rawId;
}

@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}

if (!(that instanceof CommunicationIdentifier)) {
return false;
}

CommunicationIdentifier thatId = (CommunicationIdentifier) that;
return this.getRawId().equals(thatId.getRawId());
}

@Override
public int hashCode() {
return getRawId().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CommunicationUserIdentifier(String id) {
throw new IllegalArgumentException("The initialization parameter [id] cannot be null or empty.");
}
this.id = id;
this.rawId = id;
}

/**
Expand All @@ -40,11 +41,11 @@ public boolean equals(Object that) {
return false;
}

return ((CommunicationUserIdentifier) that).getId().equals(id);
return ((CommunicationUserIdentifier) that).getRawId().equals(getRawId());
}

@Override
public int hashCode() {
return getId().hashCode();
return getRawId().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
public final class MicrosoftTeamsUserIdentifier extends CommunicationIdentifier {
private final String userId;
private final boolean isAnonymous;
private boolean rawIdSet = false;
private CommunicationCloudEnvironment cloudEnvironment = CommunicationCloudEnvironment.PUBLIC;

private String rawId;

/**
* Creates a MicrosoftTeamsUserIdentifier object
*
Expand All @@ -28,6 +27,7 @@ public MicrosoftTeamsUserIdentifier(String userId, boolean isAnonymous) {
}
this.userId = userId;
this.isAnonymous = isAnonymous;
generateRawId();
}

/**
Expand Down Expand Up @@ -64,6 +64,7 @@ public boolean isAnonymous() {
*/
public MicrosoftTeamsUserIdentifier setCloudEnvironment(CommunicationCloudEnvironment cloudEnvironment) {
this.cloudEnvironment = cloudEnvironment;
generateRawId();
return this;
}

Expand All @@ -75,21 +76,14 @@ public CommunicationCloudEnvironment getCloudEnvironment() {
return cloudEnvironment;
}

/**
* Get full id of the identifier. This id is optional.
* @return full id of the identifier
*/
public String getRawId() {
return rawId;
}

/**
* Set full id of the identifier
* @param rawId full id of the identifier
* @return CommunicationIdentifier object itself
*/
public MicrosoftTeamsUserIdentifier setRawId(String rawId) {
this.rawId = rawId;
rawIdSet = true;
return this;
}

Expand All @@ -104,10 +98,6 @@ public boolean equals(Object that) {
}

MicrosoftTeamsUserIdentifier thatId = (MicrosoftTeamsUserIdentifier) that;
if (!thatId.getUserId().equals(this.getUserId())
|| thatId.isAnonymous != this.isAnonymous) {
return false;
}

if (cloudEnvironment != null && !cloudEnvironment.equals(thatId.cloudEnvironment)) {
return false;
Expand All @@ -125,6 +115,20 @@ public boolean equals(Object that) {

@Override
public int hashCode() {
return userId.hashCode();
return getRawId().hashCode();
}

private void generateRawId() {
if (!rawIdSet) {
if (this.isAnonymous) {
this.rawId = "8:teamsvisitor:" + this.userId;
} else if (cloudEnvironment.equals(CommunicationCloudEnvironment.DOD)) {
this.rawId = "8:dod:" + this.userId;
} else if (cloudEnvironment.equals(CommunicationCloudEnvironment.GCCH)) {
this.rawId = "8:gcch:" + this.userId;
} else {
this.rawId = "8:orgid:" + this.userId;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
public final class PhoneNumberIdentifier extends CommunicationIdentifier {
private final String phoneNumber;
private String rawId;

/**
* Creates a PhoneNumberIdentifier object
Expand All @@ -21,6 +20,7 @@ public PhoneNumberIdentifier(String phoneNumber) {
throw new IllegalArgumentException("The initialization parameter [phoneNumber] cannot be null to empty.");
}
this.phoneNumber = phoneNumber;
this.rawId = "4:" + phoneNumber.replaceAll("^[+]", "");
}

/**
Expand All @@ -30,14 +30,6 @@ public String getPhoneNumber() {
return phoneNumber;
}

/**
* Get full id of the identifier. This id is optional.
* @return full id of the identifier
*/
public String getRawId() {
return rawId;
}

/**
* Set full id of the identifier
* @param rawId full id of the identifier
Expand All @@ -59,9 +51,6 @@ public boolean equals(Object that) {
}

PhoneNumberIdentifier phoneId = (PhoneNumberIdentifier) that;
if (!phoneNumber.equals(phoneId.phoneNumber)) {
return false;
}

return getRawId() == null
|| phoneId.getRawId() == null
Expand All @@ -70,6 +59,6 @@ public boolean equals(Object that) {

@Override
public int hashCode() {
return phoneNumber.hashCode();
return getRawId().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public UnknownIdentifier(String id) {
throw new IllegalArgumentException("The initialization parameter [id] cannot be null or empty.");
}
this.id = id;
this.rawId = id;
}

/**
Expand All @@ -41,11 +42,11 @@ public boolean equals(Object that) {
}

UnknownIdentifier thatId = (UnknownIdentifier) that;
return this.id.equals(thatId.id);
return this.getRawId().equals(thatId.getRawId());
}

@Override
public int hashCode() {
return id.hashCode();
return getRawId().hashCode();
}
}
Loading

0 comments on commit 6d3b1dc

Please sign in to comment.