Convert Contact to record

This commit is contained in:
AsamK 2023-11-11 11:09:24 +01:00
parent 9741c93ce9
commit 9f4a2b3e26
7 changed files with 35 additions and 116 deletions

View file

@ -2,50 +2,24 @@ package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.internal.util.Util; import org.whispersystems.signalservice.internal.util.Util;
import java.util.Objects; public record Contact(
String givenName,
public class Contact { String familyName,
String color,
private final String givenName; int messageExpirationTime,
boolean isBlocked,
private final String familyName; boolean isArchived,
boolean isProfileSharingEnabled
private final String color; ) {
private final int messageExpirationTime;
private final boolean blocked;
private final boolean archived;
private final boolean profileSharingEnabled;
public Contact(
final String givenName,
final String familyName,
final String color,
final int messageExpirationTime,
final boolean blocked,
final boolean archived,
final boolean profileSharingEnabled
) {
this.givenName = givenName;
this.familyName = familyName;
this.color = color;
this.messageExpirationTime = messageExpirationTime;
this.blocked = blocked;
this.archived = archived;
this.profileSharingEnabled = profileSharingEnabled;
}
private Contact(final Builder builder) { private Contact(final Builder builder) {
givenName = builder.givenName; this(builder.givenName,
familyName = builder.familyName; builder.familyName,
color = builder.color; builder.color,
messageExpirationTime = builder.messageExpirationTime; builder.messageExpirationTime,
blocked = builder.blocked; builder.blocked,
archived = builder.archived; builder.archived,
profileSharingEnabled = builder.profileSharingEnabled; builder.profileSharingEnabled);
} }
public static Builder newBuilder() { public static Builder newBuilder() {
@ -54,10 +28,10 @@ public class Contact {
public static Builder newBuilder(final Contact copy) { public static Builder newBuilder(final Contact copy) {
Builder builder = new Builder(); Builder builder = new Builder();
builder.givenName = copy.getGivenName(); builder.givenName = copy.givenName();
builder.familyName = copy.getFamilyName(); builder.familyName = copy.familyName();
builder.color = copy.getColor(); builder.color = copy.color();
builder.messageExpirationTime = copy.getMessageExpirationTime(); builder.messageExpirationTime = copy.messageExpirationTime();
builder.blocked = copy.isBlocked(); builder.blocked = copy.isBlocked();
builder.archived = copy.isArchived(); builder.archived = copy.isArchived();
builder.profileSharingEnabled = copy.isProfileSharingEnabled(); builder.profileSharingEnabled = copy.isProfileSharingEnabled();
@ -79,59 +53,6 @@ public class Contact {
return givenName + " " + familyName; return givenName + " " + familyName;
} }
public String getGivenName() {
return givenName;
}
public String getFamilyName() {
return familyName;
}
public String getColor() {
return color;
}
public int getMessageExpirationTime() {
return messageExpirationTime;
}
public boolean isBlocked() {
return blocked;
}
public boolean isArchived() {
return archived;
}
public boolean isProfileSharingEnabled() {
return profileSharingEnabled;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Contact contact = (Contact) o;
return messageExpirationTime == contact.messageExpirationTime
&& blocked == contact.blocked
&& archived == contact.archived
&& profileSharingEnabled == contact.profileSharingEnabled
&& Objects.equals(givenName, contact.givenName)
&& Objects.equals(familyName, contact.familyName)
&& Objects.equals(color, contact.color);
}
@Override
public int hashCode() {
return Objects.hash(givenName,
familyName,
color,
messageExpirationTime,
blocked,
archived,
profileSharingEnabled);
}
public static final class Builder { public static final class Builder {
private String givenName; private String givenName;

View file

@ -31,7 +31,7 @@ public class ContactHelper {
public void setExpirationTimer(RecipientId recipientId, int messageExpirationTimer) { public void setExpirationTimer(RecipientId recipientId, int messageExpirationTimer) {
var contact = account.getContactStore().getContact(recipientId); var contact = account.getContactStore().getContact(recipientId);
if (contact != null && contact.getMessageExpirationTime() == messageExpirationTimer) { if (contact != null && contact.messageExpirationTime() == messageExpirationTimer) {
return; return;
} }
final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact); final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);

View file

@ -84,7 +84,7 @@ public class SendHelper {
account.getContactStore().storeContact(recipientId, contact); account.getContactStore().storeContact(recipientId, contact);
} }
final var expirationTime = contact.getMessageExpirationTime(); final var expirationTime = contact.messageExpirationTime();
messageBuilder.withExpiration(expirationTime); messageBuilder.withExpiration(expirationTime);
if (!contact.isBlocked()) { if (!contact.isBlocked()) {
@ -177,7 +177,7 @@ public class SendHelper {
) { ) {
final var recipientId = account.getSelfRecipientId(); final var recipientId = account.getSelfRecipientId();
final var contact = account.getContactStore().getContact(recipientId); final var contact = account.getContactStore().getContact(recipientId);
final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0; final var expirationTime = contact != null ? contact.messageExpirationTime() : 0;
messageBuilder.withExpiration(expirationTime); messageBuilder.withExpiration(expirationTime);
var message = messageBuilder.build(); var message = messageBuilder.build();

View file

@ -116,8 +116,8 @@ public class StorageHelper {
final var blocked = contact != null && contact.isBlocked(); final var blocked = contact != null && contact.isBlocked();
final var profileShared = contact != null && contact.isProfileSharingEnabled(); final var profileShared = contact != null && contact.isProfileSharingEnabled();
final var archived = contact != null && contact.isArchived(); final var archived = contact != null && contact.isArchived();
final var contactGivenName = contact == null ? null : contact.getGivenName(); final var contactGivenName = contact == null ? null : contact.givenName();
final var contactFamilyName = contact == null ? null : contact.getFamilyName(); final var contactFamilyName = contact == null ? null : contact.familyName();
if (blocked != contactRecord.isBlocked() if (blocked != contactRecord.isBlocked()
|| profileShared != contactRecord.isProfileSharingEnabled() || profileShared != contactRecord.isProfileSharingEnabled()
|| archived != contactRecord.isArchived() || archived != contactRecord.isArchived()

View file

@ -148,11 +148,11 @@ public class SyncHelper {
out.write(new DeviceContact(address, out.write(new DeviceContact(address,
Optional.ofNullable(contact.getName()), Optional.ofNullable(contact.getName()),
createContactAvatarAttachment(new RecipientAddress(address)), createContactAvatarAttachment(new RecipientAddress(address)),
Optional.ofNullable(contact.getColor()), Optional.ofNullable(contact.color()),
Optional.ofNullable(verifiedMessage), Optional.ofNullable(verifiedMessage),
Optional.ofNullable(profileKey), Optional.ofNullable(profileKey),
contact.isBlocked(), contact.isBlocked(),
Optional.of(contact.getMessageExpirationTime()), Optional.of(contact.messageExpirationTime()),
Optional.empty(), Optional.empty(),
contact.isArchived())); contact.isArchived()));
} }

View file

@ -589,11 +589,11 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
""" """
).formatted(TABLE_RECIPIENT); ).formatted(TABLE_RECIPIENT);
try (final var statement = connection.prepareStatement(sql)) { try (final var statement = connection.prepareStatement(sql)) {
statement.setString(1, contact == null ? null : contact.getGivenName()); statement.setString(1, contact == null ? null : contact.givenName());
statement.setString(2, contact == null ? null : contact.getFamilyName()); statement.setString(2, contact == null ? null : contact.familyName());
statement.setInt(3, contact == null ? 0 : contact.getMessageExpirationTime()); statement.setInt(3, contact == null ? 0 : contact.messageExpirationTime());
statement.setBoolean(4, contact != null && contact.isProfileSharingEnabled()); statement.setBoolean(4, contact != null && contact.isProfileSharingEnabled());
statement.setString(5, contact == null ? null : contact.getColor()); statement.setString(5, contact == null ? null : contact.color());
statement.setBoolean(6, contact != null && contact.isBlocked()); statement.setBoolean(6, contact != null && contact.isBlocked());
statement.setBoolean(7, contact != null && contact.isArchived()); statement.setBoolean(7, contact != null && contact.isArchived());
statement.setLong(8, recipientId.id()); statement.setLong(8, recipientId.id());

View file

@ -62,11 +62,9 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
contact.getName(), contact.getName(),
profile.getDisplayName(), profile.getDisplayName(),
r.getAddress().username().orElse(""), r.getAddress().username().orElse(""),
contact.getColor(), contact.color(),
contact.isBlocked(), contact.isBlocked(),
contact.getMessageExpirationTime() == 0 contact.messageExpirationTime() == 0 ? "disabled" : contact.messageExpirationTime() + "s");
? "disabled"
: contact.getMessageExpirationTime() + "s");
} }
} }
case JsonWriter writer -> { case JsonWriter writer -> {
@ -77,9 +75,9 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
address.uuid().map(UUID::toString).orElse(null), address.uuid().map(UUID::toString).orElse(null),
address.username().orElse(null), address.username().orElse(null),
contact.getName(), contact.getName(),
contact.getColor(), contact.color(),
contact.isBlocked(), contact.isBlocked(),
contact.getMessageExpirationTime(), contact.messageExpirationTime(),
r.getProfile() == null r.getProfile() == null
? null ? null
: new JsonContact.JsonProfile(r.getProfile().getLastUpdateTimestamp(), : new JsonContact.JsonProfile(r.getProfile().getLastUpdateTimestamp(),