private void deliver(MasterSecret masterSecret, SendReq message)
      throws IOException, RecipientFormattingException, InvalidNumberException,
          EncapsulatedExceptions {
    TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret);
    byte[] groupId = GroupUtil.getDecodedId(message.getTo()[0].getString());
    Recipients recipients =
        DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, false);
    List<PushAddress> addresses = getPushAddresses(recipients);
    List<TextSecureAttachment> attachments = getAttachments(masterSecret, message);

    if (MmsSmsColumns.Types.isGroupUpdate(message.getDatabaseMessageBox())
        || MmsSmsColumns.Types.isGroupQuit(message.getDatabaseMessageBox())) {
      String content = PartParser.getMessageText(message.getBody());

      if (content != null && !content.trim().isEmpty()) {
        PushMessageProtos.PushMessageContent.GroupContext groupContext =
            PushMessageProtos.PushMessageContent.GroupContext.parseFrom(Base64.decode(content));
        TextSecureAttachment avatar = attachments.isEmpty() ? null : attachments.get(0);
        TextSecureGroup.Type type =
            MmsSmsColumns.Types.isGroupQuit(message.getDatabaseMessageBox())
                ? TextSecureGroup.Type.QUIT
                : TextSecureGroup.Type.UPDATE;
        TextSecureGroup group =
            new TextSecureGroup(
                type, groupId, groupContext.getName(), groupContext.getMembersList(), avatar);
        TextSecureMessage groupMessage =
            new TextSecureMessage(message.getSentTimestamp(), group, null, null);

        messageSender.sendMessage(addresses, groupMessage);
      }
    } else {
      String body = PartParser.getMessageText(message.getBody());
      TextSecureGroup group = new TextSecureGroup(groupId);
      TextSecureMessage groupMessage =
          new TextSecureMessage(message.getSentTimestamp(), group, attachments, body);

      messageSender.sendMessage(addresses, groupMessage);
    }
  }