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) { public void setContactBlocked(RecipientId recipientId, boolean blocked) {
var contact = account.getContactStore().getContact(recipientId); var contact = account.getContactStore().getContact(recipientId);
final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact); final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
if (blocked) {
builder.withProfileSharingEnabled(false);
}
account.getContactStore().storeContact(recipientId, builder.withBlocked(blocked).build()); 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.groups.NotAGroupMemberException;
import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.groups.GroupInfo; 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.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientId; import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.sendLog.MessageSendLogEntry; import org.asamk.signal.manager.storage.sendLog.MessageSendLogEntry;
@ -73,10 +74,20 @@ public class SendHelper {
public SendMessageResult sendMessage( public SendMessageResult sendMessage(
final SignalServiceDataMessage.Builder messageBuilder, final RecipientId recipientId final SignalServiceDataMessage.Builder messageBuilder, final RecipientId recipientId
) throws IOException { ) throws IOException {
final var contact = account.getContactStore().getContact(recipientId); var contact = account.getContactStore().getContact(recipientId);
final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0; 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.withExpiration(expirationTime);
messageBuilder.withProfileKey(account.getProfileKey().serialize());
if (!contact.isBlocked()) {
final var profileKey = account.getProfileKey().serialize();
messageBuilder.withProfileKey(profileKey);
}
final var message = messageBuilder.build(); final var message = messageBuilder.build();
return sendMessage(message, recipientId); return sendMessage(message, recipientId);

View file

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

View file

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

View file

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

View file

@ -26,6 +26,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -75,7 +76,8 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
r.contact.color, r.contact.color,
r.contact.messageExpirationTime, r.contact.messageExpirationTime,
r.contact.blocked, r.contact.blocked,
r.contact.archived); r.contact.archived,
r.contact.profileSharingEnabled);
} }
ProfileKey profileKey = null; ProfileKey profileKey = null;
@ -149,10 +151,6 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
this.lastId = lastId; this.lastId = lastId;
} }
public boolean isBulkUpdating() {
return isBulkUpdating;
}
public void setBulkUpdating(final boolean bulkUpdating) { public void setBulkUpdating(final boolean bulkUpdating) {
isBulkUpdating = bulkUpdating; isBulkUpdating = bulkUpdating;
if (!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 @Override
public RecipientId resolveRecipient(ServiceId serviceId) { public RecipientId resolveRecipient(ServiceId serviceId) {
return resolveRecipient(new RecipientAddress(serviceId.uuid()), false, false); return resolveRecipient(new RecipientAddress(serviceId.uuid()), false, false);
@ -545,7 +552,8 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
recipientContact.getColor(), recipientContact.getColor(),
recipientContact.getMessageExpirationTime(), recipientContact.getMessageExpirationTime(),
recipientContact.isBlocked(), recipientContact.isBlocked(),
recipientContact.isArchived()); recipientContact.isArchived(),
recipientContact.isProfileSharingEnabled());
final var recipientProfile = recipient.getProfile(); final var recipientProfile = recipient.getProfile();
final var profile = recipientProfile == null final var profile = recipientProfile == null
? null ? null
@ -599,7 +607,12 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
) { ) {
private record Contact( 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( private record Profile(

View file

@ -554,7 +554,7 @@ public class DbusManagerImpl implements Manager {
return null; return null;
} }
return new Pair<>(new RecipientAddress(null, n), 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(); }).filter(Objects::nonNull).toList();
} }