Fix up syncing all contacts.

We were looking up contact from the phone book which we had no access to.
This commit is contained in:
Mikunj 2019-11-06 11:02:07 +11:00
parent ddafcf4530
commit 9541588383
2 changed files with 30 additions and 8 deletions

View File

@ -46,7 +46,9 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
@ -183,8 +185,8 @@ public class MultiDeviceContactUpdateJob extends BaseJob implements InjectableTy
File contactDataFile = createTempFile("multidevice-contact-update"); File contactDataFile = createTempFile("multidevice-contact-update");
try { try {
DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile)); DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile));
Collection<ContactData> contacts = ContactAccessor.getInstance().getContactsWithPush(context); List<ContactData> contacts = getAllContacts();
for (ContactData contactData : contacts) { for (ContactData contactData : contacts) {
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactData.id)); Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactData.id));
@ -192,7 +194,7 @@ public class MultiDeviceContactUpdateJob extends BaseJob implements InjectableTy
Recipient recipient = Recipient.from(context, address, false); Recipient recipient = Recipient.from(context, address, false);
Optional<IdentityDatabase.IdentityRecord> identity = DatabaseFactory.getIdentityDatabase(context).getIdentity(address); Optional<IdentityDatabase.IdentityRecord> identity = DatabaseFactory.getIdentityDatabase(context).getIdentity(address);
Optional<VerifiedMessage> verified = getVerifiedMessage(recipient, identity); Optional<VerifiedMessage> verified = getVerifiedMessage(recipient, identity);
Optional<String> name = Optional.fromNullable(DatabaseFactory.getLokiUserDatabase(context).getDisplayName(address.serialize())); Optional<String> name = Optional.fromNullable(contactData.name);
Optional<String> color = Optional.of(recipient.getColor().serialize()); Optional<String> color = Optional.of(recipient.getColor().serialize());
Optional<byte[]> profileKey = Optional.fromNullable(recipient.getProfileKey()); Optional<byte[]> profileKey = Optional.fromNullable(recipient.getProfileKey());
boolean blocked = recipient.isBlocked(); boolean blocked = recipient.isBlocked();
@ -222,6 +224,20 @@ public class MultiDeviceContactUpdateJob extends BaseJob implements InjectableTy
} }
} }
private List<ContactData> getAllContacts() {
List<Address> contactAddresses = DatabaseFactory.getRecipientDatabase(context).getRegistered();
List<ContactData> contacts = new ArrayList<>(contactAddresses.size());
for (Address address : contactAddresses) {
if (!address.isPhone()) { continue; }
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(Recipient.from(context, address, false));
String name = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(address.serialize());
ContactData contactData = new ContactData(threadId, name);
contactData.numbers.add(new ContactAccessor.NumberData("TextSecure", address.serialize()));
contacts.add(contactData);
}
return contacts;
}
private LokiThreadFriendRequestStatus getFriendRequestStatus(Recipient recipient) { private LokiThreadFriendRequestStatus getFriendRequestStatus(Recipient recipient) {
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient); long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient);
return DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadId); return DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadId);
@ -259,6 +275,9 @@ public class MultiDeviceContactUpdateJob extends BaseJob implements InjectableTy
} }
private Optional<SignalServiceAttachmentStream> getAvatar(@Nullable Uri uri) throws IOException { private Optional<SignalServiceAttachmentStream> getAvatar(@Nullable Uri uri) throws IOException {
return Optional.absent();
/* Loki - Disabled until we support custom avatars. This will need to be reworked
if (uri == null) { if (uri == null) {
return Optional.absent(); return Optional.absent();
} }
@ -312,6 +331,7 @@ public class MultiDeviceContactUpdateJob extends BaseJob implements InjectableTy
cursor.close(); cursor.close();
} }
} }
*/
} }
private Optional<VerifiedMessage> getVerifiedMessage(Recipient recipient, Optional<IdentityDatabase.IdentityRecord> identity) throws InvalidNumberException { private Optional<VerifiedMessage> getVerifiedMessage(Recipient recipient, Optional<IdentityDatabase.IdentityRecord> identity) throws InvalidNumberException {

View File

@ -667,7 +667,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
MessageSender.sendBackgroundFriendRequest(context, deviceContact.getNumber(), "This is an automated friend request. Still under testing!"); MessageSender.sendBackgroundFriendRequest(context, deviceContact.getNumber(), "This is an automated friend request. Still under testing!");
} else if (status == LokiThreadFriendRequestStatus.REQUEST_RECEIVED) { } else if (status == LokiThreadFriendRequestStatus.REQUEST_RECEIVED) {
// Accept the incoming friend request // Accept the incoming friend request
becomeFriendsWithContact(deviceContact.getNumber()); becomeFriendsWithContact(deviceContact.getNumber(), false);
} }
// TODO: Handle blocked - If user is not blocked then we should do the friend request logic otherwise add them to our block list // TODO: Handle blocked - If user is not blocked then we should do the friend request logic otherwise add them to our block list
@ -1131,10 +1131,10 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
private void acceptFriendRequestIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content) { private void acceptFriendRequestIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content) {
// If we get anything other than a friend request, we can assume that we have a session with the other user // If we get anything other than a friend request, we can assume that we have a session with the other user
if (envelope.isFriendRequest() || isGroupChatMessage(content)) { return; } if (envelope.isFriendRequest() || isGroupChatMessage(content)) { return; }
becomeFriendsWithContact(content.getSender()); becomeFriendsWithContact(content.getSender(), true);
} }
private void becomeFriendsWithContact(String pubKey) { private void becomeFriendsWithContact(String pubKey, boolean syncContact) {
LokiThreadDatabase lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context); LokiThreadDatabase lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context);
Recipient contactID = Recipient.from(context, Address.fromSerialized(pubKey), false); Recipient contactID = Recipient.from(context, Address.fromSerialized(pubKey), false);
long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(contactID); long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(contactID);
@ -1144,7 +1144,9 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
// it must be a friend request accepted message. Declining a friend request doesn't send a message. // it must be a friend request accepted message. Declining a friend request doesn't send a message.
lokiThreadDatabase.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS); lokiThreadDatabase.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS);
// Send out a contact sync message // Send out a contact sync message
MessageSender.syncContact(context, contactID.getAddress()); if (syncContact) {
MessageSender.syncContact(context, contactID.getAddress());
}
// Update the last message if needed // Update the last message if needed
LokiStorageAPI.shared.getPrimaryDevicePublicKey(pubKey).success(primaryDevice -> { LokiStorageAPI.shared.getPrimaryDevicePublicKey(pubKey).success(primaryDevice -> {
Util.runOnMain(() -> { Util.runOnMain(() -> {
@ -1161,7 +1163,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
boolean shouldBecomeFriends = PromiseUtil.get(MultiDeviceUtilities.shouldAutomaticallyBecomeFriendsWithDevice(content.getSender(), context), false); boolean shouldBecomeFriends = PromiseUtil.get(MultiDeviceUtilities.shouldAutomaticallyBecomeFriendsWithDevice(content.getSender(), context), false);
if (shouldBecomeFriends) { if (shouldBecomeFriends) {
// Become friends AND update the message they sent // Become friends AND update the message they sent
becomeFriendsWithContact(content.getSender()); becomeFriendsWithContact(content.getSender(), true);
// Send them an accept message back // Send them an accept message back
MessageSender.sendBackgroundMessage(context, content.getSender()); MessageSender.sendBackgroundMessage(context, content.getSender());
} else { } else {