Search in sources :

Example 1 with GroupInviteLinkUrl

use of org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl in project Signal-Android by WhisperSystems.

the class LinkPreviewRepository method fetchGroupLinkPreview.

private static RequestController fetchGroupLinkPreview(@NonNull Context context, @NonNull String groupUrl, @NonNull Callback callback) {
    SignalExecutors.UNBOUNDED.execute(() -> {
        try {
            GroupInviteLinkUrl groupInviteLinkUrl = GroupInviteLinkUrl.fromUri(groupUrl);
            if (groupInviteLinkUrl == null) {
                throw new AssertionError();
            }
            GroupMasterKey groupMasterKey = groupInviteLinkUrl.getGroupMasterKey();
            GroupId.V2 groupId = GroupId.v2(groupMasterKey);
            Optional<GroupDatabase.GroupRecord> group = SignalDatabase.groups().getGroup(groupId);
            if (group.isPresent()) {
                Log.i(TAG, "Creating preview for locally available group");
                GroupDatabase.GroupRecord groupRecord = group.get();
                String title = groupRecord.getTitle();
                int memberCount = groupRecord.getMembers().size();
                String description = getMemberCountDescription(context, memberCount);
                Optional<Attachment> thumbnail = Optional.absent();
                if (AvatarHelper.hasAvatar(context, groupRecord.getRecipientId())) {
                    Recipient recipient = Recipient.resolved(groupRecord.getRecipientId());
                    Bitmap bitmap = AvatarUtil.loadIconBitmapSquareNoCache(context, recipient, 512, 512);
                    thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.WEBP, MediaUtil.IMAGE_WEBP);
                }
                callback.onSuccess(new LinkPreview(groupUrl, title, description, 0, thumbnail));
            } else {
                Log.i(TAG, "Group is not locally available for preview generation, fetching from server");
                DecryptedGroupJoinInfo joinInfo = GroupManager.getGroupJoinInfoFromServer(context, groupMasterKey, groupInviteLinkUrl.getPassword());
                String description = getMemberCountDescription(context, joinInfo.getMemberCount());
                Optional<Attachment> thumbnail = Optional.absent();
                byte[] avatarBytes = AvatarGroupsV2DownloadJob.downloadGroupAvatarBytes(context, groupMasterKey, joinInfo.getAvatar());
                if (avatarBytes != null) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length);
                    thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.WEBP, MediaUtil.IMAGE_WEBP);
                    if (bitmap != null)
                        bitmap.recycle();
                }
                callback.onSuccess(new LinkPreview(groupUrl, joinInfo.getTitle(), description, 0, thumbnail));
            }
        } catch (ExecutionException | InterruptedException | IOException | VerificationFailedException e) {
            Log.w(TAG, "Failed to fetch group link preview.", e);
            callback.onError(Error.PREVIEW_NOT_AVAILABLE);
        } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
            Log.w(TAG, "Bad group link.", e);
            callback.onError(Error.PREVIEW_NOT_AVAILABLE);
        } catch (GroupLinkNotActiveException e) {
            Log.w(TAG, "Group link not active.", e);
            callback.onError(Error.GROUP_LINK_INACTIVE);
        }
    });
    return () -> Log.i(TAG, "Cancelled group link preview fetch -- no effect.");
}
Also used : Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Bitmap(android.graphics.Bitmap) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) ExecutionException(java.util.concurrent.ExecutionException) DecryptedGroupJoinInfo(org.signal.storageservice.protos.groups.local.DecryptedGroupJoinInfo) GroupInviteLinkUrl(org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl) GroupMasterKey(org.signal.zkgroup.groups.GroupMasterKey) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) GroupId(org.thoughtcrime.securesms.groups.GroupId) VerificationFailedException(org.signal.zkgroup.VerificationFailedException) GroupLinkNotActiveException(org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException)

Example 2 with GroupInviteLinkUrl

use of org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl in project Signal-Android by WhisperSystems.

the class CommunicationActions method handlePotentialGroupLinkUrl.

/**
 * If the url is a group link it will handle it.
 * If the url is a malformed group link, it will assume Signal needs to update.
 * Otherwise returns false, indicating was not a group link.
 */
public static boolean handlePotentialGroupLinkUrl(@NonNull FragmentActivity activity, @NonNull String potentialGroupLinkUrl) {
    try {
        GroupInviteLinkUrl groupInviteLinkUrl = GroupInviteLinkUrl.fromUri(potentialGroupLinkUrl);
        if (groupInviteLinkUrl == null) {
            return false;
        }
        handleGroupLinkUrl(activity, groupInviteLinkUrl);
        return true;
    } catch (GroupInviteLinkUrl.InvalidGroupLinkException e) {
        Log.w(TAG, "Could not parse group URL", e);
        Toast.makeText(activity, R.string.GroupJoinUpdateRequiredBottomSheetDialogFragment_group_link_is_not_valid, Toast.LENGTH_SHORT).show();
        return true;
    } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
        Log.w(TAG, "Group link is for an advanced version", e);
        GroupJoinUpdateRequiredBottomSheetDialogFragment.show(activity.getSupportFragmentManager());
        return true;
    }
}
Also used : GroupInviteLinkUrl(org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl)

Example 3 with GroupInviteLinkUrl

use of org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl in project Signal-Android by signalapp.

the class CommunicationActions method handlePotentialGroupLinkUrl.

/**
 * If the url is a group link it will handle it.
 * If the url is a malformed group link, it will assume Signal needs to update.
 * Otherwise returns false, indicating was not a group link.
 */
public static boolean handlePotentialGroupLinkUrl(@NonNull FragmentActivity activity, @NonNull String potentialGroupLinkUrl) {
    try {
        GroupInviteLinkUrl groupInviteLinkUrl = GroupInviteLinkUrl.fromUri(potentialGroupLinkUrl);
        if (groupInviteLinkUrl == null) {
            return false;
        }
        handleGroupLinkUrl(activity, groupInviteLinkUrl);
        return true;
    } catch (GroupInviteLinkUrl.InvalidGroupLinkException e) {
        Log.w(TAG, "Could not parse group URL", e);
        Toast.makeText(activity, R.string.GroupJoinUpdateRequiredBottomSheetDialogFragment_group_link_is_not_valid, Toast.LENGTH_SHORT).show();
        return true;
    } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
        Log.w(TAG, "Group link is for an advanced version", e);
        GroupJoinUpdateRequiredBottomSheetDialogFragment.show(activity.getSupportFragmentManager());
        return true;
    }
}
Also used : GroupInviteLinkUrl(org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl)

Example 4 with GroupInviteLinkUrl

use of org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl in project Signal-Android by signalapp.

the class LinkPreviewRepository method fetchGroupLinkPreview.

private static RequestController fetchGroupLinkPreview(@NonNull Context context, @NonNull String groupUrl, @NonNull Callback callback) {
    SignalExecutors.UNBOUNDED.execute(() -> {
        try {
            GroupInviteLinkUrl groupInviteLinkUrl = GroupInviteLinkUrl.fromUri(groupUrl);
            if (groupInviteLinkUrl == null) {
                throw new AssertionError();
            }
            GroupMasterKey groupMasterKey = groupInviteLinkUrl.getGroupMasterKey();
            GroupId.V2 groupId = GroupId.v2(groupMasterKey);
            Optional<GroupDatabase.GroupRecord> group = SignalDatabase.groups().getGroup(groupId);
            if (group.isPresent()) {
                Log.i(TAG, "Creating preview for locally available group");
                GroupDatabase.GroupRecord groupRecord = group.get();
                String title = groupRecord.getTitle();
                int memberCount = groupRecord.getMembers().size();
                String description = getMemberCountDescription(context, memberCount);
                Optional<Attachment> thumbnail = Optional.absent();
                if (AvatarHelper.hasAvatar(context, groupRecord.getRecipientId())) {
                    Recipient recipient = Recipient.resolved(groupRecord.getRecipientId());
                    Bitmap bitmap = AvatarUtil.loadIconBitmapSquareNoCache(context, recipient, 512, 512);
                    thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.WEBP, MediaUtil.IMAGE_WEBP);
                }
                callback.onSuccess(new LinkPreview(groupUrl, title, description, 0, thumbnail));
            } else {
                Log.i(TAG, "Group is not locally available for preview generation, fetching from server");
                DecryptedGroupJoinInfo joinInfo = GroupManager.getGroupJoinInfoFromServer(context, groupMasterKey, groupInviteLinkUrl.getPassword());
                String description = getMemberCountDescription(context, joinInfo.getMemberCount());
                Optional<Attachment> thumbnail = Optional.absent();
                byte[] avatarBytes = AvatarGroupsV2DownloadJob.downloadGroupAvatarBytes(context, groupMasterKey, joinInfo.getAvatar());
                if (avatarBytes != null) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length);
                    thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.WEBP, MediaUtil.IMAGE_WEBP);
                    if (bitmap != null)
                        bitmap.recycle();
                }
                callback.onSuccess(new LinkPreview(groupUrl, joinInfo.getTitle(), description, 0, thumbnail));
            }
        } catch (ExecutionException | InterruptedException | IOException | VerificationFailedException e) {
            Log.w(TAG, "Failed to fetch group link preview.", e);
            callback.onError(Error.PREVIEW_NOT_AVAILABLE);
        } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
            Log.w(TAG, "Bad group link.", e);
            callback.onError(Error.PREVIEW_NOT_AVAILABLE);
        } catch (GroupLinkNotActiveException e) {
            Log.w(TAG, "Group link not active.", e);
            callback.onError(Error.GROUP_LINK_INACTIVE);
        }
    });
    return () -> Log.i(TAG, "Cancelled group link preview fetch -- no effect.");
}
Also used : Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Bitmap(android.graphics.Bitmap) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) ExecutionException(java.util.concurrent.ExecutionException) DecryptedGroupJoinInfo(org.signal.storageservice.protos.groups.local.DecryptedGroupJoinInfo) GroupInviteLinkUrl(org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl) GroupMasterKey(org.signal.zkgroup.groups.GroupMasterKey) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) GroupId(org.thoughtcrime.securesms.groups.GroupId) VerificationFailedException(org.signal.zkgroup.VerificationFailedException) GroupLinkNotActiveException(org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException)

Example 5 with GroupInviteLinkUrl

use of org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl in project Signal-Android by WhisperSystems.

the class GroupLinkInviteRepository method enableGroupInviteLink.

void enableGroupInviteLink(boolean requireMemberApproval, @NonNull AsynchronousCallback.WorkerThread<GroupInviteLinkUrl, EnableInviteLinkError> callback) {
    SignalExecutors.UNBOUNDED.execute(() -> {
        try {
            GroupInviteLinkUrl groupInviteLinkUrl = GroupManager.setGroupLinkEnabledState(context, groupId, requireMemberApproval ? GroupManager.GroupLinkState.ENABLED_WITH_APPROVAL : GroupManager.GroupLinkState.ENABLED);
            if (groupInviteLinkUrl == null) {
                throw new AssertionError();
            }
            callback.onComplete(groupInviteLinkUrl);
        } catch (IOException e) {
            callback.onError(EnableInviteLinkError.NETWORK_ERROR);
        } catch (GroupChangeBusyException e) {
            callback.onError(EnableInviteLinkError.BUSY);
        } catch (GroupChangeFailedException e) {
            callback.onError(EnableInviteLinkError.FAILED);
        } catch (GroupInsufficientRightsException e) {
            callback.onError(EnableInviteLinkError.INSUFFICIENT_RIGHTS);
        } catch (GroupNotAMemberException e) {
            callback.onError(EnableInviteLinkError.NOT_IN_GROUP);
        }
    });
}
Also used : GroupChangeFailedException(org.thoughtcrime.securesms.groups.GroupChangeFailedException) GroupChangeBusyException(org.thoughtcrime.securesms.groups.GroupChangeBusyException) GroupNotAMemberException(org.thoughtcrime.securesms.groups.GroupNotAMemberException) GroupInviteLinkUrl(org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl) GroupInsufficientRightsException(org.thoughtcrime.securesms.groups.GroupInsufficientRightsException) IOException(java.io.IOException)

Aggregations

GroupInviteLinkUrl (org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl)6 IOException (java.io.IOException)4 Bitmap (android.graphics.Bitmap)2 ExecutionException (java.util.concurrent.ExecutionException)2 DecryptedGroupJoinInfo (org.signal.storageservice.protos.groups.local.DecryptedGroupJoinInfo)2 VerificationFailedException (org.signal.zkgroup.VerificationFailedException)2 GroupMasterKey (org.signal.zkgroup.groups.GroupMasterKey)2 Attachment (org.thoughtcrime.securesms.attachments.Attachment)2 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)2 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)2 GroupChangeBusyException (org.thoughtcrime.securesms.groups.GroupChangeBusyException)2 GroupChangeFailedException (org.thoughtcrime.securesms.groups.GroupChangeFailedException)2 GroupId (org.thoughtcrime.securesms.groups.GroupId)2 GroupInsufficientRightsException (org.thoughtcrime.securesms.groups.GroupInsufficientRightsException)2 GroupNotAMemberException (org.thoughtcrime.securesms.groups.GroupNotAMemberException)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 GroupLinkNotActiveException (org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException)2