private TextSecureDataMessage createTextSecureMessage(TextSecureEnvelope envelope, DataMessage content)
        {
            TextSecureGroup groupInfo = createGroupInfo(envelope, content);
            LinkedList <TextSecureAttachment> attachments = new LinkedList <TextSecureAttachment>();
            bool endSession = ((content.Flags & (uint)DataMessage.Types.Flags.END_SESSION) != 0);

            foreach (AttachmentPointer pointer in content.AttachmentsList)
            {
                attachments.AddLast(new TextSecureAttachmentPointer(pointer.Id,
                                                                    pointer.ContentType,
                                                                    pointer.Key.ToByteArray(),
                                                                    envelope.getRelay(),
                                                                    pointer.HasSize ? new May <uint>(pointer.Size) : May <uint> .NoValue,
                                                                    pointer.HasThumbnail ? new May <byte[]>(pointer.Thumbnail.ToByteArray()) : May <byte[]> .NoValue));
            }

            return(new TextSecureDataMessage(envelope.getTimestamp(), groupInfo, attachments,
                                             content.Body, endSession));
        }
        private async Task <GroupContext> createGroupContent(TextSecureGroup group)
        {
            GroupContext.Builder builder = GroupContext.CreateBuilder();
            builder.SetId(ByteString.CopyFrom(group.getGroupId()));

            if (group.getType() != TextSecureGroup.Type.DELIVER)
            {
                if (group.getType() == TextSecureGroup.Type.UPDATE)
                {
                    builder.SetType(GroupContext.Types.Type.UPDATE);
                }
                else if (group.getType() == TextSecureGroup.Type.QUIT)
                {
                    builder.SetType(GroupContext.Types.Type.QUIT);
                }
                else
                {
                    throw new Exception("Unknown type: " + group.getType());
                }

                if (group.getName().HasValue)
                {
                    builder.SetName(group.getName().ForceGetValue());
                }
                if (group.getMembers().HasValue)
                {
                    builder.AddRangeMembers(group.getMembers().ForceGetValue());
                }

                if (group.getAvatar().HasValue&& group.getAvatar().ForceGetValue().isStream())
                {
                    AttachmentPointer pointer = await createAttachmentPointer(group.getAvatar().ForceGetValue().asStream());

                    builder.SetAvatar(pointer);
                }
            }
            else
            {
                builder.SetType(GroupContext.Types.Type.DELIVER);
            }

            return(builder.Build());
        }