Exemple #1
0
        public static async Task <StorageFile> GetStorageFile(TLDecryptedMessageMediaBase media)
        {
            if (media == null)
            {
                return(null);
            }
            if (media.StorageFile != null)
            {
                if (File.Exists(media.StorageFile.Path))
                {
                    return(media.StorageFile);
                }
            }

            var mediaDocument = media as TLDecryptedMessageMediaDocument;

            if (mediaDocument != null)
            {
                var file = media.File as TLEncryptedFile;
                if (file == null)
                {
                    return(null);
                }

                var fileName = String.Format("{0}_{1}_{2}.{3}",
                                             file.Id,
                                             file.DCId,
                                             file.AccessHash,
                                             mediaDocument.FileExt);

                return(await DialogDetailsViewModel.GetFileFromLocalFolder(fileName));
            }

            var mediaVideo = media as TLDecryptedMessageMediaVideo;

            if (mediaVideo != null)
            {
                var file = media.File as TLEncryptedFile;
                if (file == null)
                {
                    return(null);
                }

                var fileName = String.Format("{0}_{1}_{2}.{3}",
                                             file.Id,
                                             file.DCId,
                                             file.AccessHash,
                                             "mp4");

                return(await DialogDetailsViewModel.GetFileFromLocalFolder(fileName));
            }

            return(null);
        }
        public void Send()
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (!CanSend)
            {
                return;
            }

            var text = DialogDetailsViewModel.GetTrimmedText(Text);

            if (ProcessSpecialCommands(text))
            {
                return;
            }

            //check maximum message length
            if (text.Length > Constants.MaximumMessageLength)
            {
                MessageBox.Show(
                    String.Format(AppResources.MaximumMessageLengthExceeded, Constants.MaximumMessageLength),
                    AppResources.Error, MessageBoxButton.OK);

                return;
            }

            var decryptedTuple = GetDecryptedMessageAndObject(new TLString(text), new TLDecryptedMessageMediaEmpty(), chat);

            Items.Insert(0, decryptedTuple.Item1);
            RaiseScrollToBottom();
            NotifyOfPropertyChange(() => DescriptionVisibility);
            Text = string.Empty;

            SendEncrypted(chat, decryptedTuple.Item2, MTProtoService, CacheService);
        }
        public async void SendDocument(StorageFile file)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (file == null)
            {
                return;
            }

            var properties = await file.GetBasicPropertiesAsync();

            var size = properties.Size;

            if (!DialogDetailsViewModel.CheckDocumentSize(size))
            {
                MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK);
                return;
            }

            DialogDetailsViewModel.AddFileToFutureAccessList(file);

            var thumb = await DialogDetailsViewModel.GetFileThumbAsync(file) as TLPhotoSize;

            var dcId       = TLInt.Random();
            var id         = TLLong.Random();
            var accessHash = TLLong.Random();

            var fileLocation = new TLEncryptedFile
            {
                Id             = id,
                AccessHash     = accessHash,
                DCId           = dcId,
                Size           = new TLInt((int)size),
                KeyFingerprint = new TLInt(0),
                FileName       = new TLString(Path.GetFileName(file.Name))
            };

            var keyIV = GenerateKeyIV();

            var decryptedMediaDocumentBase = GetDecryptedMediaDocument(file, chat, thumb, size, keyIV, fileLocation);
            var decryptedMediaDocument45   = decryptedMediaDocumentBase as TLDecryptedMessageMediaDocument45;

            if (decryptedMediaDocument45 != null)
            {
                if (string.Equals(decryptedMediaDocument45.FileExt, "webp", StringComparison.OrdinalIgnoreCase) &&
                    decryptedMediaDocument45.DocumentSize < Telegram.Api.Constants.StickerMaxSize)
                {
                    decryptedMediaDocument45.MimeType = new TLString("image/webp");
                    decryptedMediaDocument45.Attributes.Add(new TLDocumentAttributeSticker29 {
                        Alt = TLString.Empty, Stickerset = new TLInputStickerSetEmpty()
                    });

                    var fileName = decryptedMediaDocument45.GetFileName();
                    await file.CopyAsync(ApplicationData.Current.LocalFolder, fileName, NameCollisionOption.ReplaceExisting);
                }
                else if (string.Equals(decryptedMediaDocument45.FileExt, "mp3", StringComparison.OrdinalIgnoreCase))
                {
                    var musicProperties = await file.Properties.GetMusicPropertiesAsync();

                    if (musicProperties != null)
                    {
                        var documentAttributeAudio = new TLDocumentAttributeAudio32 {
                            Duration = new TLInt(0), Title = TLString.Empty, Performer = TLString.Empty
                        };
                        documentAttributeAudio.Duration = new TLInt((int)musicProperties.Duration.TotalSeconds);
                        if (!string.IsNullOrEmpty(musicProperties.Title))
                        {
                            documentAttributeAudio.Title = new TLString(musicProperties.Title);
                        }
                        if (!string.IsNullOrEmpty(musicProperties.Artist))
                        {
                            documentAttributeAudio.Performer = new TLString(musicProperties.Artist);
                        }
                        decryptedMediaDocument45.Attributes.Add(documentAttributeAudio);
                    }
                }
            }
            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaDocumentBase, chat, true);

            BeginOnUIThread(() =>
            {
                InsertSendingMessage(decryptedTuple.Item1);
                RaiseScrollToBottom();
                NotifyOfPropertyChange(() => DescriptionVisibility);

                BeginOnThreadPool(() =>
                                  CacheService.SyncDecryptedMessage(decryptedTuple.Item1, chat,
                                                                    cachedMessage => SendDocumentInternal(file, decryptedTuple.Item2)));
            });
        }
        public void Send()
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (!CanSend)
            {
                return;
            }

            var text = DialogDetailsViewModel.GetTrimmedText(Text);

            if (ProcessSpecialCommands(text))
            {
                return;
            }

            //check maximum message length
            if (text.Length > Constants.MaximumMessageLength)
            {
                MessageBox.Show(
                    String.Format(AppResources.MaximumMessageLengthExceeded, Constants.MaximumMessageLength),
                    AppResources.Error, MessageBoxButton.OK);

                return;
            }

            var decryptedTuple = GetDecryptedMessageAndObject(new TLString(text), new TLDecryptedMessageMediaEmpty(), chat);

            var chat17 = chat as TLEncryptedChat17;

            if (chat17 != null && chat17.Layer.Value >= Constants.MinSecretChatWithRepliesLayer)
            {
                if (Reply != null && IsWebPagePreview(Reply))
                {
                    var webPageMedia = ((TLDecryptedMessagesContainter)Reply).WebPageMedia as TLMessageMediaWebPage;
                    if (webPageMedia != null)
                    {
                        var webPage = webPageMedia.WebPage as TLWebPage;
                        if (webPage != null)
                        {
                            ((TLDecryptedMessage45)decryptedTuple.Item1).Media = new TLDecryptedMessageMediaWebPage {
                                Url = new TLString(Text), WebPage = webPage
                            };
                            ((TLDecryptedMessage45)decryptedTuple.Item1).SetMedia();
                            Reply = _previousReply;
                        }
                    }
                }
            }

            InsertSendingMessage(decryptedTuple.Item1);
            RaiseScrollToBottom();
            NotifyOfPropertyChange(() => DescriptionVisibility);
            Text = string.Empty;

            SendEncrypted(chat, decryptedTuple.Item2, MTProtoService, CacheService);
        }
Exemple #5
0
        public void LoadNextSlice()
        {
            if (IsWorking ||
                LazyItems.Count > 0 ||
                IsLastSliceLoaded
#if WP8
                || !_isUpdated
#endif
                )
            {
                return;
            }

            var offset = _offset;
            var limit  = Constants.DialogsSlice;
            //TLUtils.WriteLine(string.Format("{0} messages.getDialogs offset={1} limit={2}", DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture), offset, limit), LogSeverity.Error);

            var             offsetDate = 0;
            var             offsetId   = 0;
            TLInputPeerBase offsetPeer = new TLInputPeerEmpty();
            var             lastDialog = GetLastDialog(Items);
            if (lastDialog != null)
            {
                var lastMessage = lastDialog.TopMessage as TLMessageCommon;
                if (lastMessage != null)
                {
                    offsetDate = lastMessage.DateIndex;
                    offsetId   = lastMessage.Index;
                    if (lastMessage.ToId is TLPeerUser)
                    {
                        offsetPeer = !lastMessage.Out.Value
                            ? DialogDetailsViewModel.PeerToInputPeer(new TLPeerUser {
                            Id = lastMessage.FromId
                        })
                            : DialogDetailsViewModel.PeerToInputPeer(lastMessage.ToId);
                    }
                    else
                    {
                        offsetPeer = DialogDetailsViewModel.PeerToInputPeer(lastMessage.ToId);
                    }
                }
            }

            IsWorking = true;
            //TLObject.LogNotify = true;
            //TelegramEventAggregator.LogPublish = true;
            var stopwatch = Stopwatch.StartNew();
            MTProtoService.GetDialogsAsync(stopwatch,
                                           new TLInt(offsetDate),
                                           new TLInt(offsetId),
                                           offsetPeer,
                                           new TLInt(limit),
                                           new TLInt(0),
                                           result =>
            {
                //System.Diagnostics.Debug.WriteLine("messages.getDialogs end sync elapsed=" + stopwatch.Elapsed);

                BeginOnUIThread(() =>
                {
                    //System.Diagnostics.Debug.WriteLine("messages.getDialogs ui elapsed=" + stopwatch.Elapsed);
                    //TelegramEventAggregator.LogPublish = false;
                    //TLObject.LogNotify = false;
                    if (_offset != offset)
                    {
                        return;
                    }
                    _offset += Constants.DialogsSlice;

                    foreach (var dialog in result.Dialogs)
                    {
                        Items.Add(dialog);
                    }

                    ReorderDrafts(Items);

                    IsWorking         = false;
                    IsLastSliceLoaded = result.Dialogs.Count < limit;
                    Status            = LazyItems.Count > 0 || Items.Count > 0 ? string.Empty : Status;
                    //System.Diagnostics.Debug.WriteLine("messages.getDialogs end ui elapsed=" + stopwatch.Elapsed);
                    //TLUtils.WriteLine(string.Format("messages.getDialogs offset={0} limit={1} result={2}", offset, limit, result.Dialogs.Count), LogSeverity.Error);
                });
            },
                                           error => BeginOnUIThread(() =>
            {
                IsWorking = false;
                Status    = string.Empty;
                //TLUtils.WriteLine(string.Format("messages.getDialogs offset={0} limit={1} error={2}", offset, limit, error), LogSeverity.Error);
                Execute.ShowDebugMessage("messages.getDialogs error " + error);
            }));
        }
        public async Task <Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject> > GetPhotoMessage(StorageFile file)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return(null);
            }

            if (file == null)
            {
                return(null);
            }

            var properties = await file.GetBasicPropertiesAsync();

            var size = properties.Size;

            var thumb = await DialogDetailsViewModel.GetFileThumbAsync(file) as TLPhotoSize;

            var dcId       = TLInt.Random();
            var id         = TLLong.Random();
            var accessHash = TLLong.Random();

            var fileName = String.Format("{0}_{1}_{2}.jpg",
                                         id,
                                         dcId,
                                         accessHash);

            var stream = await file.OpenReadAsync();

            var resizedPhoto = await DialogDetailsViewModel.ResizeJpeg(stream, Constants.DefaultImageSize, file.DisplayName, fileName);

            var keyIV = GenerateKeyIV();

            var fileLocation = new TLEncryptedFile
            {
                Id             = id,
                AccessHash     = accessHash,
                DCId           = dcId,
                Size           = new TLInt(resizedPhoto.Bytes.Length),
                KeyFingerprint = new TLInt(0),
                FileName       = new TLString(Path.GetFileName(file.Name))
            };

            TLDecryptedMessageMediaPhoto decryptedMediaPhoto;
            var chat17 = chat as TLEncryptedChat17;

            if (chat17 != null && chat17.Layer.Value >= Constants.MinSecretChatWithCaptionsLayer)
            {
                decryptedMediaPhoto = new TLDecryptedMessageMediaPhoto45
                {
                    Thumb   = thumb != null ? thumb.Bytes : TLString.Empty,
                    ThumbW  = thumb != null ? thumb.W : new TLInt(0),
                    ThumbH  = thumb != null ? thumb.H : new TLInt(0),
                    Size    = new TLInt(resizedPhoto.Bytes.Length),
                    Key     = keyIV.Item1,
                    IV      = keyIV.Item2,
                    W       = new TLInt(resizedPhoto.Width),
                    H       = new TLInt(resizedPhoto.Height),
                    Caption = TLString.Empty,

                    File        = fileLocation,
                    StorageFile = resizedPhoto.File,

                    UploadingProgress = 0.001
                };
            }
            else
            {
                decryptedMediaPhoto = new TLDecryptedMessageMediaPhoto
                {
                    Thumb  = thumb != null ? thumb.Bytes : TLString.Empty,
                    ThumbW = thumb != null ? thumb.W : new TLInt(0),
                    ThumbH = thumb != null ? thumb.H : new TLInt(0),
                    Size   = new TLInt(resizedPhoto.Bytes.Length),
                    Key    = keyIV.Item1,
                    IV     = keyIV.Item2,
                    W      = new TLInt(resizedPhoto.Width),
                    H      = new TLInt(resizedPhoto.Height),

                    File        = fileLocation,
                    StorageFile = resizedPhoto.File,

                    UploadingProgress = 0.001
                };
            }

            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaPhoto, chat, true);

            return(decryptedTuple);
        }
        public async void SendDocument(StorageFile file)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (file == null)
            {
                return;
            }

            var properties = await file.GetBasicPropertiesAsync();

            var size = properties.Size;

            if (!DialogDetailsViewModel.CheckDocumentSize(size))
            {
                MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK);
                return;
            }

            DialogDetailsViewModel.AddFileToFutureAccessList(file);

            var thumb = await DialogDetailsViewModel.GetFileThumbAsync(file) as TLPhotoSize;

            var dcId       = TLInt.Random();
            var id         = TLLong.Random();
            var accessHash = TLLong.Random();

            var fileLocation = new TLEncryptedFile
            {
                Id             = id,
                AccessHash     = accessHash,
                DCId           = dcId,
                Size           = new TLInt((int)size),
                KeyFingerprint = new TLInt(0),
                FileName       = new TLString(Path.GetFileName(file.Name))
            };

            var keyIV = GenerateKeyIV();

            var decryptedMediaDocument = new TLDecryptedMessageMediaDocument
            {
                Thumb    = thumb != null? thumb.Bytes : TLString.Empty,
                ThumbW   = thumb != null? thumb.W : new TLInt(0),
                ThumbH   = thumb != null? thumb.H : new TLInt(0),
                FileName = new TLString(Path.GetFileName(file.Name)),
                MimeType = new TLString(file.ContentType),
                Size     = new TLInt((int)size),
                Key      = keyIV.Item1,
                IV       = keyIV.Item2,

                File        = fileLocation,
                StorageFile = file,

                UploadingProgress = 0.001
            };

            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaDocument, chat, true);

            BeginOnUIThread(() =>
            {
                Items.Insert(0, decryptedTuple.Item1);
                RaiseScrollToBottom();
                NotifyOfPropertyChange(() => DescriptionVisibility);

                BeginOnThreadPool(() =>
                                  CacheService.SyncDecryptedMessage(decryptedTuple.Item1, chat,
                                                                    cachedMessage => SendDocumentInternal(file, decryptedTuple.Item2)));
            });
        }
        public void LoadNextSlice()
        {
            if (LazyItems.Count > 0 || IsLastSliceLoaded || IsWorking)
            {
                return;
            }

            var             offsetDate = 0;
            var             offsetId   = 0;
            TLInputPeerBase offsetPeer = new TLInputPeerEmpty();
            var             lastDialog = Items.OfType <TLDialog>().Last(x => x.TopMessage != null && x.TopMessage.Index > 0);

            if (lastDialog != null)
            {
                var lastMessage = lastDialog.TopMessage as TLMessageCommon;
                if (lastMessage != null)
                {
                    offsetDate = lastMessage.DateIndex;
                    offsetId   = lastMessage.Index;
                    if (lastMessage.ToId is TLPeerUser)
                    {
                        offsetPeer = !lastMessage.Out.Value
                            ? DialogDetailsViewModel.PeerToInputPeer(new TLPeerUser {
                            Id = lastMessage.FromId
                        })
                            : DialogDetailsViewModel.PeerToInputPeer(lastMessage.ToId);
                    }
                    else
                    {
                        offsetPeer = DialogDetailsViewModel.PeerToInputPeer(lastMessage.ToId);
                    }
                }
            }

            IsWorking = true;
            var offset = Items.Count;
            var limit  = 30;

            MTProtoService.GetDialogsAsync(Stopwatch.StartNew(),
                                           new TLInt(offsetDate),
                                           new TLInt(offsetId),
                                           offsetPeer,
                                           new TLInt(limit),
                                           new TLInt(0),
                                           result => Execute.BeginOnUIThread(() =>
            {
                lastDialog = result.Dialogs.LastOrDefault(x => x.TopMessageId != null) as TLDialog;
                if (lastDialog != null)
                {
                    _maxId = lastDialog.TopMessageId.Value;
                }

                var itemsAdded = 0;
                foreach (var dialog in result.Dialogs)
                {
                    if (!SkipDialogForBot(_bot, dialog))
                    {
                        Items.Add(dialog);
                        itemsAdded++;
                    }
                }

                IsWorking         = false;
                IsLastSliceLoaded = result.Dialogs.Count < limit;
                Status            = LazyItems.Count > 0 || Items.Count > 0 ? string.Empty : Status;

                if (itemsAdded < (Constants.DialogsSlice / 2))
                {
                    LoadNextSlice();
                }
            }),
                                           error => Execute.BeginOnUIThread(() =>
            {
                IsWorking = false;
                Status    = string.Empty;
            }));
        }
Exemple #9
0
 private string GetSubtitle(TLUserBase user)
 {
     return(DialogDetailsViewModel.GetUserStatus(user));
 }