private void AddSeparator(bool useSeparator)
        {
            for (var i = 0; i < Items.Count; i++)
            {
                var serviceMessage = Items[i] as TLMessageService;
                if (serviceMessage != null && serviceMessage.Action is TLMessageActionUnreadMessages)
                {
                    Items.RemoveAt(i--);
                }
            }

            if (useSeparator)
            {
                var separator = new TLMessageService17
                {
                    FromId = new TLInt(StateService.CurrentUserId),
                    ToId   = TLUtils.InputPeerToPeer(Peer, StateService.CurrentUserId),
                    Status = With is TLBroadcastChat
                        ? MessageStatus.Broadcast
                        : MessageStatus.Sending,
                    Out = new TLBool {
                        Value = true
                    },
                    Unread = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    Action = new TLMessageActionUnreadMessages(),
                    //IsAnimated = true,
                    RandomId = TLLong.Random()
                };
                Items.Insert(0, separator);
            }
        }
Пример #2
0
        public void AddComments()
        {
            var broadcast = With as TLBroadcastChat;

            if (broadcast == null)
            {
                return;
            }

            var broadcastPeer = new TLPeerBroadcast {
                Id = broadcast.Id
            };

            var count = new Random().Next(1, 5);

            var group = new TLMessageGroup
            {
                Count = new TLInt(count),
                Date  = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                MaxId = new TLInt(int.MaxValue),
                MinId = new TLInt(0)
            };

            var action = new TLMessageActionMessageGroup
            {
                Group = group
            };

            var serviceMessage = new TLMessageService17
            {
                FromId = new TLInt(StateService.CurrentUserId),
                ToId   = broadcastPeer,
                Status = MessageStatus.Confirmed,
                Out    = new TLBool {
                    Value = true
                },
                Date     = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                RandomId = TLLong.Random(),
                Action   = action
            };

            serviceMessage.SetUnread(TLBool.False);

            Items.Insert(0, serviceMessage);
            CacheService.SyncMessage(serviceMessage, broadcastPeer,
                                     message =>
            {
            });
        }
        public override void Create()
        {
            if (string.IsNullOrEmpty(Title))
            {
                MessageBox.Show(AppResources.PleaseEnterGroupSubject, AppResources.Error, MessageBoxButton.OK);
                return;
            }

            var participants = new TLVector <TLInputUserBase>();

            foreach (var item in SelectedUsers)
            {
                participants.Add(item.ToInputUser());
            }

            if (participants.Count == 0)
            {
                MessageBox.Show(AppResources.PleaseChooseAtLeastOneParticipant, AppResources.Error, MessageBoxButton.OK);
                return;
            }

            var broadcastChat = new TLBroadcastChat
            {
                Id             = TLInt.Random(),
                Photo          = new TLChatPhotoEmpty(),
                Title          = new TLString(Title),
                ParticipantIds = new TLVector <TLInt> {
                    Items = SelectedUsers.Select(x => x.Id).ToList()
                }
            };

            CacheService.SyncBroadcast(broadcastChat, result =>
            {
                var broadcastPeer = new TLPeerBroadcast {
                    Id = broadcastChat.Id
                };
                var serviceMessage = new TLMessageService17
                {
                    FromId = new TLInt(StateService.CurrentUserId),
                    ToId   = broadcastPeer,
                    Status = MessageStatus.Confirmed,
                    Out    = new TLBool {
                        Value = true
                    },
                    Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    //IsAnimated = true,
                    RandomId = TLLong.Random(),
                    Action   = new TLMessageActionChatCreate
                    {
                        Title = broadcastChat.Title,
                        Users = broadcastChat.ParticipantIds
                    }
                };
                serviceMessage.SetUnread(new TLBool(false));

                CacheService.SyncMessage(serviceMessage,
                                         message =>
                {
                    StateService.With            = broadcastChat;
                    StateService.RemoveBackEntry = true;
                    NavigationService.UriFor <DialogDetailsViewModel>().Navigate();
                });
            });
        }
Пример #4
0
        public override void Create()
        {
            if (IsWorking)
            {
                return;
            }
            if (_newChannel == null)
            {
                return;
            }

            var participants = new TLVector <TLInputUserBase>();

            foreach (var item in SelectedUsers)
            {
                participants.Add(item.ToInputUser());
            }
            participants.Add(new TLInputUserContact {
                UserId = new TLInt(StateService.CurrentUserId)
            });

            if (participants.Count == 0)
            {
                MessageBox.Show(AppResources.PleaseChooseAtLeastOneParticipant, AppResources.Error, MessageBoxButton.OK);
                return;
            }

            _newChannel.ParticipantIds = new TLVector <TLInt> {
                Items = SelectedUsers.Select(x => x.Id).ToList()
            };

#if LAYER_40
            IsWorking = true;
            MTProtoService.InviteToChannelAsync(_newChannel.ToInputChannel(), participants,
                                                result => Execute.BeginOnUIThread(() =>
            {
                IsWorking = false;

                StateService.With = _newChannel;
                StateService.RemoveBackEntries = true;
                NavigationService.UriFor <DialogDetailsViewModel>().Navigate();
            }),
                                                error => Execute.BeginOnUIThread(() =>
            {
                IsWorking = false;
                Execute.ShowDebugMessage("channels.inviteToChannel error " + error);
            }));
#else
            CacheService.SyncBroadcast(_newChannel, result =>
            {
                var broadcastPeer = new TLPeerBroadcast {
                    Id = _newChannel.Id
                };
                var serviceMessage = new TLMessageService17
                {
                    FromId = new TLInt(StateService.CurrentUserId),
                    ToId   = broadcastPeer,
                    Status = MessageStatus.Confirmed,
                    Out    = TLBool.True,
                    Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    //IsAnimated = true,
                    RandomId = TLLong.Random(),
                    Action   = new TLMessageActionChannelCreate
                    {
                        Title = _newChannel.Title,
                    }
                };
                serviceMessage.SetUnread(TLBool.False);

                CacheService.SyncMessage(serviceMessage, broadcastPeer,
                                         message =>
                {
                    if (_newChannel.Photo is TLChatPhoto)
                    {
                        var serviceMessage2 = new TLMessageService17
                        {
                            FromId = new TLInt(StateService.CurrentUserId),
                            ToId   = broadcastPeer,
                            Status = MessageStatus.Confirmed,
                            Out    = TLBool.True,
                            Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                            //IsAnimated = true,
                            RandomId = TLLong.Random(),
                            Action   = new TLMessageActionChatEditPhoto
                            {
                                Photo = _newChannel.Photo,
                            }
                        };
                        serviceMessage2.SetUnread(TLBool.False);

                        CacheService.SyncMessage(serviceMessage2, broadcastPeer, message2 =>
                        {
                            StateService.With = _newChannel;
                            StateService.RemoveBackEntries = true;
                            NavigationService.UriFor <DialogDetailsViewModel>().Navigate();
                        });
                        return;
                    }
                    StateService.With = _newChannel;
                    StateService.RemoveBackEntries = true;
                    NavigationService.UriFor <DialogDetailsViewModel>().Navigate();
                });
            });
#endif
        }
Пример #5
0
        protected override void OnActivate()
        {
            if (_stateService != null &&
                _stateService.Participant != null)
            {
                var participant = _stateService.Participant;
                _stateService.Participant = null;

                var forwardingMessagesCount = _stateService.ForwardingMessagesCount;
                _stateService.ForwardingMessagesCount = 0;

                var broadcastChat = Chat as TLBroadcastChat;
                if (broadcastChat != null)
                {
                    var serviceMessage = new TLMessageService17();
                    serviceMessage.ToId = new TLPeerBroadcast {
                        Id = Chat.Id
                    };
                    serviceMessage.FromId = new TLInt(_stateService.CurrentUserId);
                    serviceMessage.Out    = new TLBool(true);
                    serviceMessage.SetUnread(new TLBool(false));
                    serviceMessage.Date   = TLUtils.DateToUniversalTimeTLInt(_mtProtoService.ClientTicksDelta, DateTime.Now);
                    serviceMessage.Action = new TLMessageActionChatAddUser {
                        UserId = participant.Id
                    };

                    broadcastChat.ParticipantIds.Add(participant.Id);

                    _cacheService.SyncBroadcast(broadcastChat,
                                                result =>
                    {
                        _eventAggregator.Publish(serviceMessage);
                    });
                    //ChatDetails.UpdateTitles();
                }
                else
                {
                    _mtProtoService.AddChatUserAsync(Chat.Id, participant.ToInputUser(), new TLInt(forwardingMessagesCount),
                                                     statedMessage =>
                    {
                        var updates = statedMessage as TLUpdates;
                        if (updates != null)
                        {
                            var updateNewMessage = updates.Updates.FirstOrDefault(x => x is TLUpdateNewMessage) as TLUpdateNewMessage;
                            if (updateNewMessage != null)
                            {
                                _eventAggregator.Publish(updateNewMessage.Message);
                            }
                        }

                        ChatDetails.UpdateTitles();
                    },
                                                     error => Execute.BeginOnUIThread(() =>
                    {
                        if (error.TypeEquals(ErrorType.PEER_FLOOD))
                        {
                            MessageBox.Show(AppResources.PeerFloodAddContact, AppResources.Error, MessageBoxButton.OK);
                        }

                        Telegram.Api.Helpers.Execute.ShowDebugMessage("messages.addChatUser error " + error);
                    }));
                }
            }

            if (_stateService != null &&
                _stateService.SelectedTimerSpan != null)
            {
                ChatDetails.SelectedSpan        = _stateService.SelectedTimerSpan;
                _stateService.SelectedTimerSpan = null;
            }

            ChatDetails.StartTimer();

            base.OnActivate();
        }
Пример #6
0
        public static void CreateSecretChatCommon(TLUserBase user, TLDHConfig dhConfig, IMTProtoService mtProtoService, ICacheService cacheService, INavigationService navigationService, IStateService stateService, ITelegramEventAggregator eventAggregator)
        {
            if (user == null)
            {
                return;
            }

            var random   = new Random();
            var randomId = random.Next();

            mtProtoService.RequestEncryptionAsync(user.ToInputUser(), new TLInt(randomId), dhConfig.GA,
                                                  encryptedChat =>
            {
                var chatWaiting = encryptedChat as TLEncryptedChatWaiting;

                if (chatWaiting != null)
                {
                    var action   = new TLMessageActionChatCreate();
                    action.Title = TLString.Empty;
                    action.Users = new TLVector <TLInt> {
                        Items = new List <TLInt> {
                            user.Id
                        }
                    };

                    var dialog            = new TLDialog24();
                    dialog.ReadInboxMaxId = new TLInt(0);
                    dialog.With           = user;
                    dialog.Peer           = new TLPeerUser {
                        Id = user.Id
                    };

                    var topMessage    = new TLMessageService17();
                    topMessage.Date   = chatWaiting.Date;
                    topMessage.FromId = chatWaiting.AdminId;
                    topMessage.ToId   = new TLPeerUser {
                        Id = user.Id
                    };
                    topMessage.Out    = new TLBool(true);
                    topMessage.Action = action;
                    topMessage.SetUnread(new TLBool(false));

                    chatWaiting.A = dhConfig.A;
                    chatWaiting.P = dhConfig.P;
                    chatWaiting.G = dhConfig.G;
                }

                stateService.RemoveBackEntry = true;
                stateService.With            = chatWaiting;
                stateService.Participant     = user;

                Execute.BeginOnUIThread(() =>
                {
                    stateService.AnimateTitle = true;
                    navigationService.UriFor <SecretDialogDetailsViewModel>().Navigate();
                });


                //syncing chat and message
                cacheService.SyncEncryptedChat(chatWaiting, eventAggregator.Publish);

                var message = new TLDecryptedMessageService
                {
                    RandomId    = TLLong.Random(),
                    RandomBytes = new TLString(""),
                    ChatId      = chatWaiting.Id,
                    Action      = new TLDecryptedMessageActionEmpty(),
                    FromId      = new TLInt(stateService.CurrentUserId),
                    Date        = chatWaiting.Date,
                    Out         = new TLBool(false),
                    Unread      = new TLBool(false),
                    Status      = MessageStatus.Read
                };

                cacheService.SyncDecryptedMessage(message, chatWaiting, result => { });
            },
                                                  error =>
            {
                Execute.ShowDebugMessage("messages.requestEncryption error " + error);
            });
        }
Пример #7
0
        public void DeleteParticipant(TLUserBase user)
        {
            if (CurrentItem.IsForbidden)
            {
                return;
            }
            if (user == null)
            {
                return;
            }

            var broadcast = CurrentItem as TLBroadcastChat;
            var channel   = CurrentItem as TLChannel;

            if (broadcast != null && channel == null)
            {
                var broadcastChat = (TLBroadcastChat)CurrentItem;

                var serviceMessage = new TLMessageService17
                {
                    ToId = new TLPeerBroadcast {
                        Id = broadcastChat.Id
                    },
                    FromId = new TLInt(StateService.CurrentUserId),
                    Out    = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    Action = new TLMessageActionChatDeleteUser {
                        UserId = user.Id
                    }
                };
                serviceMessage.SetUnread(new TLBool(false));

                for (var i = 0; i < broadcastChat.ParticipantIds.Count; i++)
                {
                    if (user.Id.Value == broadcastChat.ParticipantIds[i].Value)
                    {
                        broadcastChat.ParticipantIds.RemoveAt(i);
                        break;
                    }
                }

                broadcastChat.ParticipantIds.Remove(user.Id);

                CacheService.SyncBroadcast(broadcastChat,
                                           result =>
                {
                    EventAggregator.Publish(serviceMessage);
                    UpdateTitles();
                });
            }
            else
            {
                if (user.Index == StateService.CurrentUserId)
                {
                    DeleteAndExitGroup();
                    return;
                }

                IsWorking = true;
                MTProtoService.DeleteChatUserAsync(CurrentItem.Id, user.ToInputUser(),
                                                   statedMessage =>
                {
                    IsWorking = false;
                    BeginOnUIThread(() => Items.Remove(user));

                    var updates = statedMessage as TLUpdates;
                    if (updates != null)
                    {
                        var updateNewMessage = updates.Updates.FirstOrDefault(x => x is TLUpdateNewMessage) as TLUpdateNewMessage;
                        if (updateNewMessage != null)
                        {
                            EventAggregator.Publish(updateNewMessage.Message);
                        }
                    }
                    UpdateTitles();
                },
                                                   error =>
                {
                    Execute.ShowDebugMessage("messages.deleteChatUser error " + error);
                    IsWorking = false;
                });
            }
        }