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 java.util.Objects;
public class Contact {
private final String givenName;
private final String familyName;
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
public record Contact(
String givenName,
String familyName,
String color,
int messageExpirationTime,
boolean isBlocked,
boolean isArchived,
boolean isProfileSharingEnabled
) {
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) {
givenName = builder.givenName;
familyName = builder.familyName;
color = builder.color;
messageExpirationTime = builder.messageExpirationTime;
blocked = builder.blocked;
archived = builder.archived;
profileSharingEnabled = builder.profileSharingEnabled;
this(builder.givenName,
builder.familyName,
builder.color,
builder.messageExpirationTime,
builder.blocked,
builder.archived,
builder.profileSharingEnabled);
}
public static Builder newBuilder() {
@ -54,10 +28,10 @@ public class Contact {
public static Builder newBuilder(final Contact copy) {
Builder builder = new Builder();
builder.givenName = copy.getGivenName();
builder.familyName = copy.getFamilyName();
builder.color = copy.getColor();
builder.messageExpirationTime = copy.getMessageExpirationTime();
builder.givenName = copy.givenName();
builder.familyName = copy.familyName();
builder.color = copy.color();
builder.messageExpirationTime = copy.messageExpirationTime();
builder.blocked = copy.isBlocked();
builder.archived = copy.isArchived();
builder.profileSharingEnabled = copy.isProfileSharingEnabled();
@ -79,59 +53,6 @@ public class Contact {
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 {
private String givenName;

View file

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

View file

@ -84,7 +84,7 @@ public class SendHelper {
account.getContactStore().storeContact(recipientId, contact);
}
final var expirationTime = contact.getMessageExpirationTime();
final var expirationTime = contact.messageExpirationTime();
messageBuilder.withExpiration(expirationTime);
if (!contact.isBlocked()) {
@ -177,7 +177,7 @@ public class SendHelper {
) {
final var recipientId = account.getSelfRecipientId();
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);
var message = messageBuilder.build();

View file

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

View file

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

View file

@ -589,11 +589,11 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
"""
).formatted(TABLE_RECIPIENT);
try (final var statement = connection.prepareStatement(sql)) {
statement.setString(1, contact == null ? null : contact.getGivenName());
statement.setString(2, contact == null ? null : contact.getFamilyName());
statement.setInt(3, contact == null ? 0 : contact.getMessageExpirationTime());
statement.setString(1, contact == null ? null : contact.givenName());
statement.setString(2, contact == null ? null : contact.familyName());
statement.setInt(3, contact == null ? 0 : contact.messageExpirationTime());
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(7, contact != null && contact.isArchived());
statement.setLong(8, recipientId.id());

View file

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