Search in sources :

Example 1 with GroupActionResult

use of org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult in project Signal-Android by WhisperSystems.

the class GroupManagerV1 method createGroup.

@NonNull
static GroupActionResult createGroup(@NonNull Context context, @NonNull Set<RecipientId> memberIds, @Nullable byte[] avatarBytes, @Nullable String name, boolean mms) {
    final GroupDatabase groupDatabase = SignalDatabase.groups();
    final SecureRandom secureRandom = new SecureRandom();
    final GroupId groupId = mms ? GroupId.createMms(secureRandom) : GroupId.createV1(secureRandom);
    final RecipientId groupRecipientId = SignalDatabase.recipients().getOrInsertFromGroupId(groupId);
    final Recipient groupRecipient = Recipient.resolved(groupRecipientId);
    memberIds.add(Recipient.self().getId());
    if (groupId.isV1()) {
        GroupId.V1 groupIdV1 = groupId.requireV1();
        groupDatabase.create(groupIdV1, name, memberIds, null, null);
        try {
            AvatarHelper.setAvatar(context, groupRecipientId, avatarBytes != null ? new ByteArrayInputStream(avatarBytes) : null);
        } catch (IOException e) {
            Log.w(TAG, "Failed to save avatar!", e);
        }
        groupDatabase.onAvatarUpdated(groupIdV1, avatarBytes != null);
        SignalDatabase.recipients().setProfileSharing(groupRecipient.getId(), true);
        return sendGroupUpdate(context, groupIdV1, memberIds, name, avatarBytes, memberIds.size() - 1);
    } else {
        groupDatabase.create(groupId.requireMms(), name, memberIds);
        try {
            AvatarHelper.setAvatar(context, groupRecipientId, avatarBytes != null ? new ByteArrayInputStream(avatarBytes) : null);
        } catch (IOException e) {
            Log.w(TAG, "Failed to save avatar!", e);
        }
        groupDatabase.onAvatarUpdated(groupId, avatarBytes != null);
        long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient, ThreadDatabase.DistributionTypes.CONVERSATION);
        return new GroupActionResult(groupRecipient, threadId, memberIds.size() - 1, Collections.emptyList());
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) GroupActionResult(org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) NonNull(androidx.annotation.NonNull)

Example 2 with GroupActionResult

use of org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult in project Signal-Android by WhisperSystems.

the class GroupManagerV1 method sendGroupUpdate.

private static GroupActionResult sendGroupUpdate(@NonNull Context context, @NonNull GroupId.V1 groupId, @NonNull Set<RecipientId> members, @Nullable String groupName, @Nullable byte[] avatar, int newMemberCount) {
    Attachment avatarAttachment = null;
    RecipientId groupRecipientId = SignalDatabase.recipients().getOrInsertFromGroupId(groupId);
    Recipient groupRecipient = Recipient.resolved(groupRecipientId);
    List<GroupContext.Member> uuidMembers = new ArrayList<>(members.size());
    List<String> e164Members = new ArrayList<>(members.size());
    for (RecipientId member : members) {
        Recipient recipient = Recipient.resolved(member);
        if (recipient.hasE164()) {
            e164Members.add(recipient.requireE164());
            uuidMembers.add(GroupV1MessageProcessor.createMember(recipient.requireE164()));
        }
    }
    GroupContext.Builder groupContextBuilder = GroupContext.newBuilder().setId(ByteString.copyFrom(groupId.getDecodedId())).setType(GroupContext.Type.UPDATE).addAllMembersE164(e164Members).addAllMembers(uuidMembers);
    if (groupName != null)
        groupContextBuilder.setName(groupName);
    GroupContext groupContext = groupContextBuilder.build();
    if (avatar != null) {
        Uri avatarUri = BlobProvider.getInstance().forData(avatar).createForSingleUseInMemory();
        avatarAttachment = new UriAttachment(avatarUri, MediaUtil.IMAGE_PNG, AttachmentDatabase.TRANSFER_PROGRESS_DONE, avatar.length, null, false, false, false, false, null, null, null, null, null);
    }
    OutgoingGroupUpdateMessage outgoingMessage = new OutgoingGroupUpdateMessage(groupRecipient, groupContext, avatarAttachment, System.currentTimeMillis(), 0, false, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    long threadId = MessageSender.send(context, outgoingMessage, -1, false, null, null);
    return new GroupActionResult(groupRecipient, threadId, newMemberCount, Collections.emptyList());
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ArrayList(java.util.ArrayList) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ByteString(com.google.protobuf.ByteString) Uri(android.net.Uri) OutgoingGroupUpdateMessage(org.thoughtcrime.securesms.mms.OutgoingGroupUpdateMessage) GroupActionResult(org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) GroupContext(org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext)

Example 3 with GroupActionResult

use of org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult in project Signal-Android by WhisperSystems.

the class GroupManagerV1 method updateGroup.

static GroupActionResult updateGroup(@NonNull Context context, @NonNull GroupId groupId, @NonNull Set<RecipientId> memberAddresses, @Nullable byte[] avatarBytes, @Nullable String name, int newMemberCount) {
    final GroupDatabase groupDatabase = SignalDatabase.groups();
    final RecipientId groupRecipientId = SignalDatabase.recipients().getOrInsertFromGroupId(groupId);
    memberAddresses.add(Recipient.self().getId());
    groupDatabase.updateMembers(groupId, new LinkedList<>(memberAddresses));
    if (groupId.isPush()) {
        GroupId.V1 groupIdV1 = groupId.requireV1();
        groupDatabase.updateTitle(groupIdV1, name);
        groupDatabase.onAvatarUpdated(groupIdV1, avatarBytes != null);
        try {
            AvatarHelper.setAvatar(context, groupRecipientId, avatarBytes != null ? new ByteArrayInputStream(avatarBytes) : null);
        } catch (IOException e) {
            Log.w(TAG, "Failed to save avatar!", e);
        }
        return sendGroupUpdate(context, groupIdV1, memberAddresses, name, avatarBytes, newMemberCount);
    } else {
        Recipient groupRecipient = Recipient.resolved(groupRecipientId);
        long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient);
        return new GroupActionResult(groupRecipient, threadId, newMemberCount, Collections.emptyList());
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ByteArrayInputStream(java.io.ByteArrayInputStream) GroupActionResult(org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException)

Example 4 with GroupActionResult

use of org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult in project Signal-Android by WhisperSystems.

the class GroupManagerV1 method updateGroup.

static GroupActionResult updateGroup(@NonNull Context context, @NonNull GroupId.Mms groupId, @Nullable byte[] avatarBytes, @Nullable String name) {
    GroupDatabase groupDatabase = SignalDatabase.groups();
    RecipientId groupRecipientId = SignalDatabase.recipients().getOrInsertFromGroupId(groupId);
    Recipient groupRecipient = Recipient.resolved(groupRecipientId);
    long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient);
    groupDatabase.updateTitle(groupId, name);
    groupDatabase.onAvatarUpdated(groupId, avatarBytes != null);
    try {
        AvatarHelper.setAvatar(context, groupRecipientId, avatarBytes != null ? new ByteArrayInputStream(avatarBytes) : null);
    } catch (IOException e) {
        Log.w(TAG, "Failed to save avatar!", e);
    }
    return new GroupActionResult(groupRecipient, threadId, 0, Collections.emptyList());
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ByteArrayInputStream(java.io.ByteArrayInputStream) GroupActionResult(org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException)

Aggregations

GroupActionResult (org.thoughtcrime.securesms.groups.GroupManager.GroupActionResult)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)4 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)3 Uri (android.net.Uri)1 NonNull (androidx.annotation.NonNull)1 ByteString (com.google.protobuf.ByteString)1 SecureRandom (java.security.SecureRandom)1 ArrayList (java.util.ArrayList)1 Attachment (org.thoughtcrime.securesms.attachments.Attachment)1 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)1 OutgoingGroupUpdateMessage (org.thoughtcrime.securesms.mms.OutgoingGroupUpdateMessage)1 GroupContext (org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext)1