Store profile sharing enabled for contacts

Automatically enable it when sending direct messages
This commit is contained in:
AsamK 2022-05-18 15:26:34 +02:00
parent 06a9884e99
commit b1e56faab2
7 changed files with 71 additions and 21 deletions

View file

@ -36,6 +36,9 @@ public class ContactHelper {
public void setContactBlocked(RecipientId recipientId, boolean blocked) {
var contact = account.getContactStore().getContact(recipientId);
final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
if (blocked) {
builder.withProfileSharingEnabled(false);
}
account.getContactStore().storeContact(recipientId, builder.withBlocked(blocked).build());
}
}

View file

@ -11,6 +11,7 @@ import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.groups.GroupInfo;
import org.asamk.signal.manager.storage.recipients.Contact;
import org.asamk.signal.manager.storage.recipients.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.sendLog.MessageSendLogEntry;
@ -73,10 +74,20 @@ public class SendHelper {
public SendMessageResult sendMessage(
final SignalServiceDataMessage.Builder messageBuilder, final RecipientId recipientId
) throws IOException {
final var contact = account.getContactStore().getContact(recipientId);
final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0;
var contact = account.getContactStore().getContact(recipientId);
if (contact == null || !contact.isProfileSharingEnabled()) {
final var contactBuilder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
contact = contactBuilder.withProfileSharingEnabled(true).build();
account.getContactStore().storeContact(recipientId, contact);
}
final var expirationTime = contact.getMessageExpirationTime();
messageBuilder.withExpiration(expirationTime);
messageBuilder.withProfileKey(account.getProfileKey().serialize());
if (!contact.isBlocked()) {
final var profileKey = account.getProfileKey().serialize();
messageBuilder.withProfileKey(profileKey);
}
final var message = messageBuilder.build();
return sendMessage(message, recipientId);

View file

@ -88,13 +88,18 @@ public class StorageHelper {
final var recipientId = account.getRecipientStore().resolveRecipient(address);
final var contact = account.getContactStore().getContact(recipientId);
if (contactRecord.getGivenName().isPresent() || contactRecord.getFamilyName().isPresent() || (
(contact == null || !contact.isBlocked()) && contactRecord.isBlocked()
)) {
final var newContact = (contact == null ? Contact.newBuilder() : Contact.newBuilder(contact)).withBlocked(
contactRecord.isBlocked()).withName((
contactRecord.getGivenName().orElse("") + " " + contactRecord.getFamilyName().orElse("")
).trim()).build();
final var blocked = contact != null && contact.isBlocked();
final var profileShared = contact != null && contact.isProfileSharingEnabled();
if (contactRecord.getGivenName().isPresent()
|| contactRecord.getFamilyName().isPresent()
|| blocked != contactRecord.isBlocked()
|| profileShared != contactRecord.isProfileSharingEnabled()) {
final var contactBuilder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
final var name = contactRecord.getGivenName().orElse("") + " " + contactRecord.getFamilyName().orElse("");
final var newContact = contactBuilder.withBlocked(contactRecord.isBlocked())
.withName(name.trim())
.withProfileSharingEnabled(contactRecord.isProfileSharingEnabled())
.build();
account.getContactStore().storeContact(recipientId, newContact);
}

View file

@ -715,7 +715,8 @@ public class SignalAccount implements Closeable {
contact.color,
contact.messageExpirationTime,
contact.blocked,
contact.archived));
contact.archived,
false));
// Store profile keys only in profile store
var profileKeyString = contact.profileKey;

View file

@ -14,18 +14,22 @@ public class Contact {
private final boolean archived;
private final boolean profileSharingEnabled;
public Contact(
final String name,
final String color,
final int messageExpirationTime,
final boolean blocked,
final boolean archived
final boolean archived,
final boolean profileSharingEnabled
) {
this.name = name;
this.color = color;
this.messageExpirationTime = messageExpirationTime;
this.blocked = blocked;
this.archived = archived;
this.profileSharingEnabled = profileSharingEnabled;
}
private Contact(final Builder builder) {
@ -34,6 +38,7 @@ public class Contact {
messageExpirationTime = builder.messageExpirationTime;
blocked = builder.blocked;
archived = builder.archived;
profileSharingEnabled = builder.profileSharingEnabled;
}
public static Builder newBuilder() {
@ -47,6 +52,7 @@ public class Contact {
builder.messageExpirationTime = copy.getMessageExpirationTime();
builder.blocked = copy.isBlocked();
builder.archived = copy.isArchived();
builder.profileSharingEnabled = copy.isProfileSharingEnabled();
return builder;
}
@ -70,6 +76,10 @@ public class Contact {
return archived;
}
public boolean isProfileSharingEnabled() {
return profileSharingEnabled;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
@ -78,13 +88,14 @@ public class Contact {
return messageExpirationTime == contact.messageExpirationTime
&& blocked == contact.blocked
&& archived == contact.archived
&& profileSharingEnabled == contact.profileSharingEnabled
&& Objects.equals(name, contact.name)
&& Objects.equals(color, contact.color);
}
@Override
public int hashCode() {
return Objects.hash(name, color, messageExpirationTime, blocked, archived);
return Objects.hash(name, color, messageExpirationTime, blocked, archived, profileSharingEnabled);
}
public static final class Builder {
@ -94,6 +105,7 @@ public class Contact {
private int messageExpirationTime;
private boolean blocked;
private boolean archived;
private boolean profileSharingEnabled;
private Builder() {
}
@ -123,6 +135,11 @@ public class Contact {
return this;
}
public Builder withProfileSharingEnabled(final boolean val) {
profileSharingEnabled = val;
return this;
}
public Contact build() {
return new Contact(this);
}

View file

@ -26,6 +26,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -75,7 +76,8 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
r.contact.color,
r.contact.messageExpirationTime,
r.contact.blocked,
r.contact.archived);
r.contact.archived,
r.contact.profileSharingEnabled);
}
ProfileKey profileKey = null;
@ -149,10 +151,6 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
this.lastId = lastId;
}
public boolean isBulkUpdating() {
return isBulkUpdating;
}
public void setBulkUpdating(final boolean bulkUpdating) {
isBulkUpdating = bulkUpdating;
if (!bulkUpdating) {
@ -174,6 +172,15 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
}
}
public Collection<RecipientId> getRecipientIdsWithEnabledProfileSharing() {
synchronized (recipients) {
return recipients.values().stream().filter(r -> {
final var contact = r.getContact();
return contact != null && !contact.isBlocked() && contact.isProfileSharingEnabled();
}).map(Recipient::getRecipientId).toList();
}
}
@Override
public RecipientId resolveRecipient(ServiceId serviceId) {
return resolveRecipient(new RecipientAddress(serviceId.uuid()), false, false);
@ -545,7 +552,8 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
recipientContact.getColor(),
recipientContact.getMessageExpirationTime(),
recipientContact.isBlocked(),
recipientContact.isArchived());
recipientContact.isArchived(),
recipientContact.isProfileSharingEnabled());
final var recipientProfile = recipient.getProfile();
final var profile = recipientProfile == null
? null
@ -599,7 +607,12 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
) {
private record Contact(
String name, String color, int messageExpirationTime, boolean blocked, boolean archived
String name,
String color,
int messageExpirationTime,
boolean blocked,
boolean archived,
boolean profileSharingEnabled
) {}
private record Profile(

View file

@ -554,7 +554,7 @@ public class DbusManagerImpl implements Manager {
return null;
}
return new Pair<>(new RecipientAddress(null, n),
new Contact(contactName, null, 0, signal.isContactBlocked(n), false));
new Contact(contactName, null, 0, signal.isContactBlocked(n), false, false));
}).filter(Objects::nonNull).toList();
}