public static string Convert(TLMessageService serviceMessage, bool useActiveLinks = false, bool noName = false) { var fromId = serviceMessage.FromId; var fromUser = IoC.Get <ICacheService>().GetUser(fromId); var fromUserFullName = fromUser != null ? fromUser.FullName2 : AppResources.User; //var stateService = IoC.Get<IStateService>(); //if (fromId.Value == stateService.CurrentUserId) //{ // fromUserFullName = AppResources.You; //} var action = serviceMessage.Action; if (serviceMessage.ToId is TLPeerChannel) { var channel = IoC.Get <ICacheService>().GetChat(serviceMessage.ToId.Id) as TLChannel; var isMegaGroup = channel != null && channel.IsMegaGroup; var actionPinMessage = action as TLMessageActionPinMessage; if (actionPinMessage != null) { var serviceMessage49 = serviceMessage as TLMessageService49; if (serviceMessage49 != null) { var replyToMsgId = serviceMessage49.ReplyToMsgId; if (replyToMsgId != null && channel != null) { var reply = IoC.Get <ICacheService>().GetMessage(serviceMessage49.ReplyToMsgId, channel.Id) as TLMessage; if (reply != null) { if (!isMegaGroup && fromUser == null) { useActiveLinks = false; fromUserFullName = channel.FullName; } var mediaGame = reply.Media as TLMessageMediaGame; if (mediaGame != null) { return(string.Format(AppResources.MessageActionPinGame, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var text = reply.Message.ToString(); if (text.Length > 0) { if (text.Length > 20) { return(string.Format(AppResources.MessageActionPinText, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), text.Substring(0, 20).Replace("\r\n", "\n").Replace("\n", " ") + "...")); } return(string.Format(AppResources.MessageActionPinText, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), text)); } var mediaPhoto = reply.Media as TLMessageMediaPhoto; if (mediaPhoto != null) { return(string.Format(AppResources.MessageActionPinPhoto, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var mediaDocument = reply.Media as TLMessageMediaDocument; if (mediaDocument != null) { if (TLMessageBase.IsSticker(mediaDocument.Document)) { return(string.Format(AppResources.MessageActionPinSticker, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } if (TLMessageBase.IsVoice(mediaDocument.Document)) { return(string.Format(AppResources.MessageActionPinVoiceMessage, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } if (TLMessageBase.IsMusic(mediaDocument.Document)) { return(string.Format(AppResources.MessageActionPinTrack, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } if (TLMessageBase.IsVideo(mediaDocument.Document)) { return(string.Format(AppResources.MessageActionPinVideo, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } if (TLMessageBase.IsGif(mediaDocument.Document)) { return(string.Format(AppResources.MessageActionPinGif, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } return(string.Format(AppResources.MessageActionPinFile, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var mediaContact = reply.Media as TLMessageMediaContact; if (mediaContact != null) { return(string.Format(AppResources.MessageActionPinContact, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var mediaGeoLive = reply.Media as TLMessageMediaGeoLive; if (mediaGeoLive != null) { return(string.Format(AppResources.MessageActionPinGeoLive, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var mediaGeo = reply.Media as TLMessageMediaGeo; if (mediaGeo != null) { return(string.Format(AppResources.MessageActionPinMap, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var mediaAudio = reply.Media as TLMessageMediaAudio; if (mediaAudio != null) { return(string.Format(AppResources.MessageActionPinVoiceMessage, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } var mediaVideo = reply.Media as TLMessageMediaVideo; if (mediaVideo != null) { return(string.Format(AppResources.MessageActionPinVideo, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } } } return(string.Format(AppResources.MessageActionPinMessage, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName))); } } var actionChatAddUser41 = action as TLMessageActionChatAddUser41; if (actionChatAddUser41 != null) { var users = ((TLMessageActionChatAddUser41)action).Users; var userFullName = new List <string>(); foreach (var userId in users) { var user = IoC.Get <ICacheService>().GetUser(userId); if (user != null) { userFullName.Add(GetUserFullName(user, useActiveLinks, noName)); } } if (users.Count == 1 && users[0].Value == fromId.Value) { if (fromId.Value == IoC.Get <IStateService>().CurrentUserId) { return(AppResources.MessageActionChatJoinSelf); } return(string.Format(AppResources.MessageActionChatJoin, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), string.Join(", ", userFullName))); } return(string.Format(AppResources.MessageActionChatAddUser, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), string.Join(", ", userFullName))); } var actionChannelCreate = action as TLMessageActionChannelCreate; if (actionChannelCreate != null) { return(isMegaGroup ? string.Format(AppResources.MessageActionChatCreate, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), ((TLMessageActionChannelCreate)action).Title) : string.Format(AppResources.MessageActionChannelCreate, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), ((TLMessageActionChannelCreate)action).Title)); } var actionChatEditPhoto = action as TLMessageActionChatEditPhoto; if (actionChatEditPhoto != null) { return(isMegaGroup ? string.Format(AppResources.MessageActionChatEditPhoto, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName)) : AppResources.MessageActionChannelEditPhoto); } var actionChatDeletePhoto = action as TLMessageActionChatDeletePhoto; if (actionChatDeletePhoto != null) { return(isMegaGroup ? string.Format(AppResources.MessageActionChatDeletePhoto, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName)) : AppResources.MessageActionChannelDeletePhoto); } var actionChantEditTitle = action as TLMessageActionChatEditTitle; if (actionChantEditTitle != null) { return(isMegaGroup ? string.Format(AppResources.MessageActionChatEditTitle, GetUserFullNameString(fromUserFullName, fromId.Value, useActiveLinks, noName), actionChantEditTitle.Title) : string.Format(AppResources.MessageActionChannelEditTitle, actionChantEditTitle.Title)); } } if (action != null && _actionsCache.ContainsKey(action.GetType())) { return(_actionsCache[action.GetType()](serviceMessage, action, fromId.Value, fromUserFullName, useActiveLinks, noName)); } #if DEBUG return(string.Format("{0} msg_id={1}", action != null ? action.ToString() : AppResources.MessageActionEmpty, serviceMessage.Id)); #endif return(AppResources.MessageActionEmpty); }
public void OpenMedia(TLMessageBase messageBase) #endif { if (messageBase == null) { return; } if (messageBase.Status == MessageStatus.Failed || messageBase.Status == MessageStatus.Sending) { return; } var serviceMessage = messageBase as TLMessageService; if (serviceMessage != null) { var editPhotoAction = serviceMessage.Action as TLMessageActionChatEditPhoto; if (editPhotoAction != null) { var photo = editPhotoAction.Photo; if (photo != null) { //StateService.CurrentPhoto = photo; //StateService.CurrentChat = With as TLChatBase; //NavigationService.UriFor<ProfilePhotoViewerViewModel>().Navigate(); } } var phoneCallAction = serviceMessage.Action as TLMessageActionPhoneCall; if (phoneCallAction != null) { TLUser user = null; if (serviceMessage.Out.Value) { user = CacheService.GetUser(serviceMessage.ToId.Id) as TLUser; } else { user = serviceMessage.From as TLUser; } ShellViewModel.StartVoiceCall(user, IoC.Get <IVoIPService>(), IoC.Get <ICacheService>()); } return; } var message = messageBase as TLMessage; if (message == null) { return; } var mediaPhoto = message.Media as TLMessageMediaPhoto; if (mediaPhoto != null) { if (!message.Out.Value && message.HasTTL()) { var ttlMessageMedia = mediaPhoto as ITTLMessageMedia; if (ttlMessageMedia != null && ttlMessageMedia.TTLParams == null) { ttlMessageMedia.TTLParams = new TTLParams { StartTime = DateTime.Now, IsStarted = true, Total = ttlMessageMedia.TTLSeconds.Value }; mediaPhoto.NotifyOfPropertyChange(() => ttlMessageMedia.TTLParams); AddToTTLQueue(message as TLMessage70, ttlMessageMedia.TTLParams, result => { SplitGroupedMessages(new List <TLInt> { message.Id }); }); } ReadMessageContents(message as TLMessage25); } var photo = mediaPhoto.Photo as TLPhoto; if (photo != null) { var width = 311.0; TLPhotoSize size = null; var sizes = photo.Sizes.OfType <TLPhotoSize>(); foreach (var photoSize in sizes) { if (size == null || Math.Abs(width - size.W.Value) > Math.Abs(width - photoSize.W.Value)) { size = photoSize; } } if (size != null) { var location = size.Location as TLFileLocation; if (location != null) { var fileName = String.Format("{0}_{1}_{2}.jpg", location.VolumeId, location.LocalId, location.Secret); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.FileExists(fileName)) { message.Media.IsCanceled = false; message.Media.DownloadingProgress = 0.01; Execute.BeginOnThreadPool(() => DownloadFileManager.DownloadFile(location, photo, size.Size)); return; } } } } } if (mediaPhoto.IsCanceled) { mediaPhoto.IsCanceled = false; mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Photo); mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Self); return; } StateService.CurrentPhotoMessage = message; StateService.CurrentMediaMessages = message.HasTTL() ? new List <TLMessage> { message } : UngroupEnumerator(Items).OfType <TLMessage>() .Where(x => { if (TLMessageBase.HasTTL(x.Media)) { return(false); } return(x.Media is TLMessageMediaPhoto || x.Media is TLMessageMediaVideo || x.IsVideo()); }) .ToList(); OpenImageViewer(); return; } var mediaWebPage = message.Media as TLMessageMediaWebPage; if (mediaWebPage != null) { var webPage = mediaWebPage.WebPage as TLWebPage; if (webPage != null && webPage.Type != null) { if (webPage.Type != null) { var type = webPage.Type.ToString(); if (string.Equals(type, "photo", StringComparison.OrdinalIgnoreCase)) { StateService.CurrentPhotoMessage = message; OpenImageViewer(); return; } if (string.Equals(type, "video", StringComparison.OrdinalIgnoreCase)) { if (!TLString.Equals(webPage.SiteName, new TLString("youtube"), StringComparison.OrdinalIgnoreCase) && !TLString.IsNullOrEmpty(webPage.EmbedUrl)) { var launcher = new MediaPlayerLauncher { Location = MediaLocationType.Data, Media = new Uri(webPage.EmbedUrl.ToString(), UriKind.Absolute), Orientation = MediaPlayerOrientation.Portrait }; launcher.Show(); return; } if (!TLString.IsNullOrEmpty(webPage.Url)) { var webBrowserTask = new WebBrowserTask(); webBrowserTask.URL = HttpUtility.UrlEncode(webPage.Url.ToString()); webBrowserTask.Show(); } return; } if (string.Equals(type, "gif", StringComparison.OrdinalIgnoreCase)) { var webPage35 = webPage as TLWebPage35; if (webPage35 != null) { var document = webPage35.Document as TLDocument; if (document == null) { return; } var documentLocalFileName = document.GetFileName(); var store = IsolatedStorageFile.GetUserStoreForApplication(); #if WP81 var documentFile = mediaWebPage.File ?? await GetStorageFile(mediaWebPage); #endif if (!store.FileExists(documentLocalFileName) #if WP81 && documentFile == null #endif ) { mediaWebPage.IsCanceled = false; mediaWebPage.DownloadingProgress = mediaWebPage.LastProgress > 0.0 ? mediaWebPage.LastProgress : 0.001; DownloadDocumentFileManager.DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), message, document.Size, progress => { if (progress > 0.0) { mediaWebPage.DownloadingProgress = progress; } }); } } return; } } } } var mediaGame = message.Media as TLMessageMediaGame; if (mediaGame != null) { var game = mediaGame.Game; if (game != null) { var document = game.Document as TLDocument; if (document == null) { return; } var documentLocalFileName = document.GetFileName(); var store = IsolatedStorageFile.GetUserStoreForApplication(); #if WP81 var documentFile = mediaGame.File ?? await GetStorageFile(mediaGame); #endif if (!store.FileExists(documentLocalFileName) #if WP81 && documentFile == null #endif ) { mediaGame.IsCanceled = false; mediaGame.DownloadingProgress = mediaGame.LastProgress > 0.0 ? mediaGame.LastProgress : 0.001; DownloadDocumentFileManager.DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), message, document.Size, progress => { if (progress > 0.0) { mediaGame.DownloadingProgress = progress; } }); } } return; } var mediaGeo = message.Media as TLMessageMediaGeo; if (mediaGeo != null) { OpenLocation(message); return; } var mediaContact = message.Media as TLMessageMediaContact; if (mediaContact != null) { var phoneNumber = mediaContact.PhoneNumber.ToString(); if (mediaContact.UserId.Value == 0) { OpenPhoneContact(mediaContact); } else { var user = mediaContact.User; OpenMediaContact(mediaContact.UserId, user, new TLString(phoneNumber)); } return; } var mediaVideo = message.Media as TLMessageMediaVideo; if (mediaVideo != null) { var video = mediaVideo.Video as TLVideo; if (video == null) { return; } var videoFileName = video.GetFileName(); var store = IsolatedStorageFile.GetUserStoreForApplication(); #if WP81 var file = mediaVideo.File ?? await GetStorageFile(mediaVideo); #endif if (!store.FileExists(videoFileName) #if WP81 && file == null #endif ) { mediaVideo.IsCanceled = false; mediaVideo.DownloadingProgress = mediaVideo.LastProgress > 0.0 ? mediaVideo.LastProgress : 0.001; DownloadVideoFileManager.DownloadFileAsync( video.DCId, video.ToInputFileLocation(), message, video.Size, progress => { if (progress > 0.0) { mediaVideo.DownloadingProgress = progress; } }); } else { //ReadMessageContents(message); #if WP81 //var localFile = await GetFileFromLocalFolder(videoFileName); var videoProperties = await file.Properties.GetVideoPropertiesAsync(); var musicProperties = await file.Properties.GetMusicPropertiesAsync(); if (file != null) { Launcher.LaunchFileAsync(file); return; } #endif var launcher = new MediaPlayerLauncher { Location = MediaLocationType.Data, Media = new Uri(videoFileName, UriKind.Relative) }; launcher.Show(); } return; } var mediaAudio = message.Media as TLMessageMediaAudio; if (mediaAudio != null) { var audio = mediaAudio.Audio as TLAudio; if (audio == null) { return; } var store = IsolatedStorageFile.GetUserStoreForApplication(); var audioFileName = audio.GetFileName(); if (TLString.Equals(audio.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase)) { if (!store.FileExists(audioFileName)) { mediaAudio.IsCanceled = false; mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001; BeginOnThreadPool(() => { DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size); }); } else { ReadMessageContents(message as TLMessage25); } return; } var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav"; if (!store.FileExists(wavFileName)) { mediaAudio.IsCanceled = false; mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001; BeginOnThreadPool(() => { DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size); }); } else { ReadMessageContents(message as TLMessage25); } if (mediaAudio.IsCanceled) { mediaAudio.IsCanceled = false; mediaAudio.NotifyOfPropertyChange(() => mediaPhoto.ThumbSelf); return; } return; } var mediaDocument = message.Media as TLMessageMediaDocument; if (mediaDocument != null) { var document = mediaDocument.Document as TLDocument22; if (document != null) { if (TLMessageBase.IsSticker(document)) { var inputStickerSet = document.StickerSet; if (inputStickerSet != null && !(inputStickerSet is TLInputStickerSetEmpty)) { AddToStickers(message); } } else if (TLMessageBase.IsVoice(document)) { var store = IsolatedStorageFile.GetUserStoreForApplication(); var audioFileName = string.Format("audio{0}_{1}.mp3", document.Id, document.AccessHash); if (TLString.Equals(document.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase)) { if (!store.FileExists(audioFileName)) { mediaDocument.IsCanceled = false; mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0 ? mediaDocument.LastProgress : 0.001; BeginOnThreadPool(() => { DownloadAudioFileManager.DownloadFile(document.DCId, document.ToInputFileLocation(), message, document.Size); }); } else { ReadMessageContents(message as TLMessage25); } return; } var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav"; if (!store.FileExists(wavFileName)) { mediaDocument.IsCanceled = false; mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0 ? mediaDocument.LastProgress : 0.001; BeginOnThreadPool(() => { DownloadAudioFileManager.DownloadFile(document.DCId, document.ToInputFileLocation(), message, document.Size); }); } else { ReadMessageContents(message as TLMessage25); } if (mediaDocument.IsCanceled) { mediaDocument.IsCanceled = false; mediaDocument.NotifyOfPropertyChange(() => mediaDocument.ThumbSelf); return; } } else if (TLMessageBase.IsVideo(document)) { var video = mediaDocument.Document as TLDocument; if (video == null) { return; } var videoFileName = video.GetFileName(); var store = IsolatedStorageFile.GetUserStoreForApplication(); #if WP81 var file = mediaDocument.File ?? await GetStorageFile(mediaDocument); #endif if (!store.FileExists(videoFileName) #if WP81 && file == null #endif ) { mediaDocument.IsCanceled = false; mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0 ? mediaDocument.LastProgress : 0.001; DownloadVideoFileManager.DownloadFileAsync( video.DCId, video.ToInputFileLocation(), message, video.Size, progress => { if (progress > 0.0) { mediaDocument.DownloadingProgress = progress; } }); } else { if (!message.Out.Value && message.HasTTL()) { var ttlMessageMedia = mediaDocument as ITTLMessageMedia; if (ttlMessageMedia != null && ttlMessageMedia.TTLParams == null) { ttlMessageMedia.TTLParams = new TTLParams { StartTime = DateTime.Now, IsStarted = true, Total = ttlMessageMedia.TTLSeconds.Value }; mediaDocument.NotifyOfPropertyChange(() => ttlMessageMedia.TTLParams); AddToTTLQueue(message as TLMessage70, ttlMessageMedia.TTLParams, result => { SplitGroupedMessages(new List <TLInt> { message.Id }); }); } ReadMessageContents(message as TLMessage25); } else if (message.IsRoundVideo()) { ReadMessageContents(message as TLMessage25); } #if WP81 if (file != null) { Launcher.LaunchFileAsync(file); return; } #endif var launcher = new MediaPlayerLauncher { Location = MediaLocationType.Data, Media = new Uri(videoFileName, UriKind.Relative) }; launcher.Show(); } return; } else { OpenDocumentCommon(message, StateService, DownloadDocumentFileManager, () => { StateService.CurrentPhotoMessage = message; if (AnimatedImageViewer == null) { AnimatedImageViewer = new AnimatedImageViewerViewModel(StateService); NotifyOfPropertyChange(() => AnimatedImageViewer); } Execute.BeginOnUIThread(() => AnimatedImageViewer.OpenViewer()); }); } } else { OpenDocumentCommon(message, StateService, DownloadDocumentFileManager, () => { StateService.CurrentPhotoMessage = message; if (AnimatedImageViewer == null) { AnimatedImageViewer = new AnimatedImageViewerViewModel(StateService); NotifyOfPropertyChange(() => AnimatedImageViewer); } Execute.BeginOnUIThread(() => AnimatedImageViewer.OpenViewer()); }); } return; } Execute.ShowDebugMessage("tap on media"); }