All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2014-2016 Open Whisper Systems
 *
 * Licensed according to the LICENSE file in this repository.
 */

package org.whispersystems.signalservice.api.messages.multidevice;

import com.google.protobuf.ByteString;

import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupDetails;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DeviceGroupsOutputStream extends ChunkedOutputStream {

  public DeviceGroupsOutputStream(OutputStream out) {
    super(out);
  }

  public void write(DeviceGroup group) throws IOException {
    writeGroupDetails(group);
    writeAvatarImage(group);
  }

  public void close() throws IOException {
    out.close();
  }

  private void writeAvatarImage(DeviceGroup contact) throws IOException {
    if (contact.getAvatar().isPresent()) {
      writeStream(contact.getAvatar().get().getInputStream());
    }
  }

  private void writeGroupDetails(DeviceGroup group) throws IOException {
    GroupDetails.Builder groupDetails = GroupDetails.newBuilder();
    groupDetails.setId(ByteString.copyFrom(group.getId()));

    if (group.getName().isPresent()) {
      groupDetails.setName(group.getName().get());
    }

    if (group.getAvatar().isPresent()) {
      GroupDetails.Avatar.Builder avatarBuilder = GroupDetails.Avatar.newBuilder();
      avatarBuilder.setContentType(group.getAvatar().get().getContentType());
      avatarBuilder.setLength((int)group.getAvatar().get().getLength());
      groupDetails.setAvatar(avatarBuilder);
    }

    if (group.getExpirationTimer().isPresent()) {
      groupDetails.setExpireTimer(group.getExpirationTimer().get());
    }

    if (group.getColor().isPresent()) {
      groupDetails.setColor(group.getColor().get());
    }

    List members     = new ArrayList<>(group.getMembers().size());
    List              membersE164 = new ArrayList<>(group.getMembers().size());

    for (SignalServiceAddress address : group.getMembers()) {
      GroupDetails.Member.Builder builder = GroupDetails.Member.newBuilder();

      if (address.getUuid().isPresent()) {
        builder.setUuid(address.getUuid().get().toString());
      }

      if (address.getNumber().isPresent()) {
        builder.setE164(address.getNumber().get());
        membersE164.add(address.getNumber().get());
      }

      members.add(builder.build());
    }

    groupDetails.addAllMembers(members);
    groupDetails.addAllMembersE164(membersE164);
    groupDetails.setActive(group.isActive());
    groupDetails.setBlocked(group.isBlocked());

    byte[] serializedContactDetails = groupDetails.build().toByteArray();

    writeVarint32(serializedContactDetails.length);
    out.write(serializedContactDetails);
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy