Search in sources :

Example 1 with DeviceGroupsOutputStream

use of org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream in project Signal-Android by WhisperSystems.

the class MultiDeviceGroupUpdateJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws Exception {
    SignalServiceMessageSender messageSender = messageSenderFactory.create();
    File contactDataFile = createTempFile("multidevice-contact-update");
    GroupDatabase.Reader reader = null;
    GroupDatabase.GroupRecord record;
    try {
        DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(contactDataFile));
        reader = DatabaseFactory.getGroupDatabase(context).getGroups();
        while ((record = reader.getNext()) != null) {
            out.write(new DeviceGroup(record.getId(), Optional.fromNullable(record.getTitle()), record.getMembers(), getAvatar(record.getAvatar()), record.isActive()));
        }
        out.close();
        if (contactDataFile.exists() && contactDataFile.length() > 0) {
            sendUpdate(messageSender, contactDataFile);
        } else {
            Log.w(TAG, "No groups present for sync message...");
        }
    } finally {
        if (contactDataFile != null)
            contactDataFile.delete();
        if (reader != null)
            reader.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) DeviceGroup(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) File(java.io.File) DeviceGroupsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)

Example 2 with DeviceGroupsOutputStream

use of org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream in project Signal-Android by signalapp.

the class MultiDeviceGroupUpdateJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws Exception {
    File contactDataFile = createTempFile("multidevice-contact-update");
    GroupDatabase.Reader reader = null;
    GroupDatabase.GroupRecord record;
    try {
        DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(contactDataFile));
        reader = DatabaseFactory.getGroupDatabase(context).getGroups();
        while ((record = reader.getNext()) != null) {
            if (!record.isMms()) {
                List<String> members = new LinkedList<>();
                for (Address member : record.getMembers()) {
                    members.add(member.serialize());
                }
                Recipient recipient = Recipient.from(context, Address.fromSerialized(GroupUtil.getEncodedId(record.getId(), record.isMms())), false);
                Optional<Integer> expirationTimer = recipient.getExpireMessages() > 0 ? Optional.of(recipient.getExpireMessages()) : Optional.absent();
                out.write(new DeviceGroup(record.getId(), Optional.fromNullable(record.getTitle()), members, getAvatar(record.getAvatar()), record.isActive(), expirationTimer));
            }
        }
        out.close();
        if (contactDataFile.exists() && contactDataFile.length() > 0) {
            sendUpdate(messageSender, contactDataFile);
        } else {
            Log.w(TAG, "No groups present for sync message...");
        }
    } finally {
        if (contactDataFile != null)
            contactDataFile.delete();
        if (reader != null)
            reader.close();
    }
}
Also used : Address(org.thoughtcrime.securesms.database.Address) DeviceGroup(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup) Recipient(org.thoughtcrime.securesms.recipients.Recipient) LinkedList(java.util.LinkedList) FileOutputStream(java.io.FileOutputStream) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) File(java.io.File) DeviceGroupsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)

Example 3 with DeviceGroupsOutputStream

use of org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream in project Signal-Android by WhisperSystems.

the class MultiDeviceGroupUpdateJob method onRun.

@Override
public void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!TextSecurePreferences.isMultiDevice(context)) {
        Log.i(TAG, "Not multi device, aborting...");
        return;
    }
    if (SignalStore.account().isLinkedDevice()) {
        Log.i(TAG, "Not primary device, aborting...");
        return;
    }
    ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);
    Uri uri = BlobProvider.getInstance().forData(inputStream, 0).withFileName("multidevice-group-update").createForSingleSessionOnDiskAsync(context, () -> Log.i(TAG, "Write successful."), e -> Log.w(TAG, "Error during write.", e));
    try (GroupDatabase.Reader reader = SignalDatabase.groups().getGroups()) {
        DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]));
        boolean hasData = false;
        GroupDatabase.GroupRecord record;
        while ((record = reader.getNext()) != null) {
            if (record.isV1Group()) {
                List<SignalServiceAddress> members = new LinkedList<>();
                List<Recipient> registeredMembers = RecipientUtil.getEligibleForSending(Recipient.resolvedList(record.getMembers()));
                for (Recipient member : registeredMembers) {
                    members.add(RecipientUtil.toSignalServiceAddress(context, member));
                }
                RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(record.getId());
                Recipient recipient = Recipient.resolved(recipientId);
                Optional<Integer> expirationTimer = recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent();
                Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
                Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
                out.write(new DeviceGroup(record.getId().getDecodedId(), Optional.fromNullable(record.getTitle()), members, getAvatar(record.getRecipientId()), record.isActive(), expirationTimer, Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), recipient.isBlocked(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
                hasData = true;
            }
        }
        out.close();
        if (hasData) {
            long length = BlobProvider.getInstance().calculateFileSize(context, uri);
            sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, uri), length);
        } else {
            Log.w(TAG, "No groups present for sync message. Sending an empty update.");
            sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), null, 0);
        }
    } finally {
        BlobProvider.getInstance().delete(context, uri);
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) InputStream(java.io.InputStream) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) DeviceGroup(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Uri(android.net.Uri) LinkedList(java.util.LinkedList) ParcelFileDescriptor(android.os.ParcelFileDescriptor) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) DeviceGroupsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)

Aggregations

GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)3 DeviceGroup (org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup)3 DeviceGroupsOutputStream (org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 LinkedList (java.util.LinkedList)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 Uri (android.net.Uri)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 InputStream (java.io.InputStream)1 Address (org.thoughtcrime.securesms.database.Address)1 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)1 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)1 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)1 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)1