示例#1
0
        public void DeleteMember(TLUserBase user)
        {
            var channel = CurrentItem as TLChannel;

            if (channel == null)
            {
                return;
            }

            if (user == null)
            {
                return;
            }

            var rights = new TLChannelAdminRights {
                Flags = new TLInt(0)
            };

            IsWorking = true;
            MTProtoService.EditAdminAsync(channel, user.ToInputUser(), rights,
                                          result => BeginOnUIThread(() =>
            {
                IsWorking = false;

                Items.Remove(user);
            }),
                                          error => BeginOnUIThread(() =>
            {
                IsWorking = false;

                Execute.ShowDebugMessage("channels.editAdmin error " + error);
            }));
        }
示例#2
0
        public void EditAdminAsync(TLChannel channel, TLInputUserBase userId, TLChannelAdminRights rights, Action <TLUpdatesBase> callback, Action <TLRPCError> faultCallback = null)
        {
            var obj = new TLChannelsEditAdmin {
                Channel = channel.ToInputChannel(), UserId = userId, AdminRights = rights
            };

            const string caption = "channels.editAdmin";

            SendInformativeMessage <TLUpdatesBase>(caption, obj,
                                                   result =>
            {
                var multiPts = result as ITLMultiPts;
                if (multiPts != null)
                {
                    _updatesService.SetState(multiPts, caption);
                }
                else
                {
                    ProcessUpdates(result, null);
                }

                GetFullChannelAsync(channel.ToInputChannel(),
                                    messagesChatFull => callback?.Invoke(result),
                                    faultCallback);
            },
                                                   faultCallback);
        }
示例#3
0
        public static TLChannelAdminRights GetDefaultAdminRights(bool supergroup, TLChannelParticipantRoleBase role)
        {
            if (role is TLChannelRoleEditor)
            {
                var rights = new TLChannelAdminRights {
                    Flags = new TLInt(0)
                };
                if (supergroup)
                {
                    rights.ChannelInfo    = true;
                    rights.DeleteMessages = true;
                    rights.BanUsers       = true;
                    rights.InviteLinks    = true;
                    rights.PinMessages    = true;
                    rights.AddAdmins      = false;
                }
                else
                {
                    rights.ChannelInfo    = true;
                    rights.PostMessages   = true;
                    rights.EditMessages   = true;
                    rights.DeleteMessages = true;
                    rights.InviteUsers    = true;
                    rights.AddAdmins      = false;
                }
            }

            return(new TLChannelAdminRights {
                Flags = new TLInt(0)
            });
        }
示例#4
0
        private async void DismissExecute()
        {
            var rights = new TLChannelAdminRights();

            var response = await ProtoService.EditAdminAsync(_channel, _item.User.ToInputUser(), rights);

            if (response.IsSucceeded)
            {
                NavigationService.GoBack();
                NavigationService.Frame.ForwardStack.Clear();
            }
        }
示例#5
0
        public SearchContactsViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            EventAggregator.Subscribe(this);

            Items  = new ObservableCollection <TLObject>();
            Status = string.Empty;

            _currentChat             = StateService.CurrentChat;
            StateService.CurrentChat = null;

            if (StateService.CurrentAdminRights != null)
            {
                _currentAdminRights             = StateService.CurrentAdminRights;
                StateService.CurrentAdminRights = null;
            }

            if (StateService.CurrentRole != null)
            {
                _currentRole             = StateService.CurrentRole;
                StateService.CurrentRole = null;
            }

            if (StateService.RequestForwardingCount)
            {
                RequestForwardingCount = true;
                StateService.RequestForwardingCount = false;
            }

            if (StateService.NavigateToDialogDetails)
            {
                NavigateToDialogDetails = true;
                StateService.NavigateToDialogDetails = false;
            }

            if (StateService.NavigateToSecretChat)
            {
                NavigateToSecretChat = true;
                StateService.NavigateToSecretChat = false;
                _dhConfig             = StateService.DHConfig;
                StateService.DHConfig = null;

                if (_dhConfig == null)
                {
                    Execute.BeginOnThreadPool(GetDHConfig);
                }
            }
        }
        private async void ParticipantDismissExecute(TLChannelParticipantBase participant)
        {
            if (_item == null)
            {
                return;
            }

            if (participant.User == null)
            {
                return;
            }

            var rights = new TLChannelAdminRights();

            var response = await ProtoService.EditAdminAsync(_item, participant.User.ToInputUser(), rights);

            if (response.IsSucceeded)
            {
                Participants.Remove(participant);
            }
        }
示例#7
0
        private async void SendExecute()
        {
            var rights = new TLChannelAdminRights
            {
                IsChangeInfo     = _isChangeInfo,
                IsPostMessages   = _isPostMessages,
                IsEditMessages   = _isEditMessages,
                IsDeleteMessages = _isDeleteMessages,
                IsBanUsers       = _isBanUsers,
                IsInviteUsers    = _isInviteUsers,
                IsInviteLink     = _isInviteLink,
                IsPinMessages    = _isPinnedMessages,
                IsAddAdmins      = _isAddAdmins
            };

            var response = await ProtoService.EditAdminAsync(_channel, _item.User.ToInputUser(), rights);

            if (response.IsSucceeded)
            {
                NavigationService.GoBack();
                NavigationService.Frame.ForwardStack.Clear();
            }
        }
示例#8
0
        public override TLChatBase this[long index]
        {
            get
            {
                if (TryGetValue(index, out TLChatBase value))
                {
                    return(value);
                }

                Statement statement;
                Sqlite3.sqlite3_prepare_v2(_database, $"SELECT {_fields} FROM `Chats` WHERE `index` = {index}", out statement);

                TLChatBase result = null;
                if (Sqlite3.sqlite3_step(statement) == SQLiteResult.Row)
                {
                    var id      = Sqlite3.sqlite3_column_int64(statement, 0);
                    var title   = Sqlite3.sqlite3_column_text(statement, 4);
                    var version = Sqlite3.sqlite3_column_int(statement, 6);
                    var date    = Sqlite3.sqlite3_column_int(statement, 8);

                    TLChatPhotoBase photo     = null;
                    var             photoType = Sqlite3.sqlite3_column_type(statement, 10);
                    if (photoType == 1) // SQLITE_INTEGER
                    {
                        var photo_small_local_id  = Sqlite3.sqlite3_column_int(statement, 10);
                        var photo_small_secret    = Sqlite3.sqlite3_column_int64(statement, 11);
                        var photo_small_volume_id = Sqlite3.sqlite3_column_int64(statement, 12);
                        var photo_small_dc_id     = Sqlite3.sqlite3_column_int(statement, 13);

                        var photo_big_local_id  = Sqlite3.sqlite3_column_int(statement, 14);
                        var photo_big_secret    = Sqlite3.sqlite3_column_int64(statement, 15);
                        var photo_big_volume_id = Sqlite3.sqlite3_column_int64(statement, 16);
                        var photo_big_dc_id     = Sqlite3.sqlite3_column_int(statement, 17);

                        photo = new TLChatPhoto
                        {
                            PhotoSmall = new TLFileLocation
                            {
                                LocalId  = photo_small_local_id,
                                Secret   = photo_small_secret,
                                VolumeId = photo_small_volume_id,
                                DCId     = photo_small_dc_id
                            },
                            PhotoBig = new TLFileLocation
                            {
                                LocalId  = photo_big_local_id,
                                Secret   = photo_big_secret,
                                VolumeId = photo_big_volume_id,
                                DCId     = photo_big_dc_id
                            }
                        };
                    }
                    else
                    {
                        photo = new TLChatPhotoEmpty();
                    }

                    if (((ulong)id & PeerIdTypeMask) == PeerIdChatShift) // CHAT
                    {
                        var flags = (TLChat.Flag)Sqlite3.sqlite3_column_int(statement, 3);
                        var participants_count = Sqlite3.sqlite3_column_int(statement, 7);

                        TLInputChannelBase migratedTo = null;
                        if (flags.HasFlag(TLChat.Flag.MigratedTo))
                        {
                            var channel_id  = Sqlite3.sqlite3_column_int(statement, 18);
                            var access_hash = Sqlite3.sqlite3_column_int64(statement, 19);

                            migratedTo = new TLInputChannel {
                                ChannelId = channel_id, AccessHash = access_hash
                            };
                        }

                        result = new TLChat
                        {
                            Id                = (int)(uint)((ulong)id & PeerIdMask),
                            Flags             = flags,
                            Title             = title,
                            Version           = version,
                            ParticipantsCount = participants_count,
                            Date              = date,
                            Photo             = photo,
                            MigratedTo        = migratedTo
                        };
                    }
                    else if (((ulong)id & PeerIdTypeMask) == PeerIdChannelShift) // CHANNEL
                    {
                        var flags              = (TLChannel.Flag)Sqlite3.sqlite3_column_int(statement, 3);
                        var access_hash        = Sqlite3.sqlite3_column_int64(statement, 2);
                        var username           = Sqlite3.sqlite3_column_text(statement, 5);
                        var restriction_reason = Sqlite3.sqlite3_column_text(statement, 9);

                        TLChannelAdminRights adminRights = null;
                        if (flags.HasFlag(TLChannel.Flag.AdminRights))
                        {
                            adminRights = new TLChannelAdminRights {
                                Flags = (TLChannelAdminRights.Flag)Sqlite3.sqlite3_column_int(statement, 20)
                            };
                        }

                        TLChannelBannedRights bannedRights = null;
                        if (flags.HasFlag(TLChannel.Flag.AdminRights))
                        {
                            bannedRights = new TLChannelBannedRights {
                                Flags = (TLChannelBannedRights.Flag)Sqlite3.sqlite3_column_int(statement, 21)
                            };
                        }

                        result = new TLChannel
                        {
                            Id                = (int)(uint)((ulong)id & PeerIdMask),
                            AccessHash        = access_hash,
                            Flags             = flags,
                            Title             = title,
                            Username          = username,
                            Version           = version,
                            Date              = date,
                            RestrictionReason = restriction_reason,
                            Photo             = photo,
                            AdminRights       = adminRights,
                            BannedRights      = bannedRights
                        };
                    }

                    base[index] = result;
                }

                Sqlite3.sqlite3_finalize(statement);
                return(result);
            }
            set
            {
                base[index] = value;

                Statement statement;
                Sqlite3.sqlite3_prepare_v2(_database, $"INSERT OR REPLACE INTO `Chats` ({_fields}) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", out statement);

                if (value is TLChat chat)
                {
                    Sqlite3.sqlite3_bind_int64(statement, 1, (long)(PeerIdChatShift | (ulong)(uint)chat.Id));
                    Sqlite3.sqlite3_bind_int(statement, 2, chat.Id);
                    Sqlite3.sqlite3_bind_null(statement, 3);
                    Sqlite3.sqlite3_bind_int(statement, 4, (int)chat.Flags);
                    Sqlite3.sqlite3_bind_text(statement, 5, chat.Title, -1);
                    Sqlite3.sqlite3_bind_null(statement, 6);
                    Sqlite3.sqlite3_bind_int(statement, 7, chat.Version);
                    Sqlite3.sqlite3_bind_int(statement, 8, chat.ParticipantsCount);
                    Sqlite3.sqlite3_bind_int(statement, 9, chat.Date);
                    Sqlite3.sqlite3_bind_null(statement, 10);

                    if (chat.Photo is TLChatPhoto photo && photo.PhotoSmall is TLFileLocation small && photo.PhotoBig is TLFileLocation big)
                    {
                        Sqlite3.sqlite3_bind_int(statement, 11, small.LocalId);
                        Sqlite3.sqlite3_bind_int64(statement, 12, small.Secret);
                        Sqlite3.sqlite3_bind_int64(statement, 13, small.VolumeId);
                        Sqlite3.sqlite3_bind_int(statement, 14, small.DCId);
                        Sqlite3.sqlite3_bind_int(statement, 15, big.LocalId);
                        Sqlite3.sqlite3_bind_int64(statement, 16, big.Secret);
                        Sqlite3.sqlite3_bind_int64(statement, 17, big.VolumeId);
                        Sqlite3.sqlite3_bind_int(statement, 18, big.DCId);
                    }
                    else
                    {
                        Sqlite3.sqlite3_bind_null(statement, 11);
                        Sqlite3.sqlite3_bind_null(statement, 12);
                        Sqlite3.sqlite3_bind_null(statement, 13);
                        Sqlite3.sqlite3_bind_null(statement, 14);
                        Sqlite3.sqlite3_bind_null(statement, 15);
                        Sqlite3.sqlite3_bind_null(statement, 16);
                        Sqlite3.sqlite3_bind_null(statement, 17);
                        Sqlite3.sqlite3_bind_null(statement, 18);
                    }

                    if (chat.HasMigratedTo && chat.MigratedTo is TLInputChannel inputChannel)
                    {
                        Sqlite3.sqlite3_bind_int(statement, 19, inputChannel.ChannelId);
                        Sqlite3.sqlite3_bind_int64(statement, 20, inputChannel.AccessHash);
                    }
                    else
                    {
                        Sqlite3.sqlite3_bind_null(statement, 19);
                        Sqlite3.sqlite3_bind_null(statement, 20);
                    }

                    Sqlite3.sqlite3_bind_null(statement, 21);
                    Sqlite3.sqlite3_bind_null(statement, 22);
                }
                else if (value is TLChannel channel)
示例#9
0
            public override async Task <IList <TLMessageBase> > LoadDataAsync()
            {
                _viewModel.IsLoading = true;
                _hasMore             = false;

                var maxId = Count > 0 ? _minEventId : 0;

                var response = await _protoService.GetAdminLogAsync(_inputChannel, null, null, null, maxId, 0, 50);

                if (response.IsSucceeded)
                {
                    _viewModel.IsLoading = false;

                    var result = new List <TLMessageBase>();

                    foreach (var item in response.Result.Events)
                    {
                        _minEventId = Math.Min(_minEventId, item.Id);

                        if (item.Action is TLChannelAdminLogEventActionChangeTitle changeTitle)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangeAbout changeAbout)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId   = item.UserId;
                            message.ToId     = _channel.ToPeer();
                            message.Date     = item.Date;
                            message.Message  = changeAbout.NewValue;
                            message.HasMedia = true;

                            if (string.IsNullOrEmpty(changeAbout.PrevValue))
                            {
                                message.Media = new TLMessageMediaEmpty();
                            }
                            else
                            {
                                message.Media = new TLMessageMediaWebPage
                                {
                                    WebPage = new TLWebPage
                                    {
                                        SiteName       = Strings.Android.EventLogPreviousGroupDescription,
                                        Description    = changeAbout.PrevValue,
                                        HasSiteName    = true,
                                        HasDescription = true
                                    }
                                };
                            }

                            result.Insert(0, message);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangeUsername changeUsername)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId      = item.UserId;
                            message.ToId        = _channel.ToPeer();
                            message.Date        = item.Date;
                            message.Message     = string.IsNullOrEmpty(changeUsername.NewValue) ? string.Empty : MeUrlPrefixConverter.Convert(changeUsername.NewValue);
                            message.Entities    = new TLVector <TLMessageEntityBase>();
                            message.HasMedia    = true;
                            message.HasEntities = true;

                            message.Entities.Add(new TLMessageEntityUrl {
                                Offset = 0, Length = message.Message.Length
                            });

                            if (string.IsNullOrEmpty(changeUsername.PrevValue))
                            {
                                message.Media = new TLMessageMediaEmpty();
                            }
                            else
                            {
                                message.Media = new TLMessageMediaWebPage
                                {
                                    WebPage = new TLWebPage
                                    {
                                        SiteName       = Strings.Android.EventLogPreviousLink,
                                        Description    = MeUrlPrefixConverter.Convert(changeUsername.PrevValue),
                                        HasSiteName    = true,
                                        HasDescription = true
                                    }
                                };
                            }

                            result.Insert(0, message);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangePhoto changePhoto)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionToggleInvites toggleInvites)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionToggleSignatures toggleSignatures)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionTogglePreHistoryHidden togglePreHistoryHidden)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionUpdatePinned updatePinned)
                        {
                            // Patch for view
                            if (updatePinned.Message is TLMessageCommonBase messageCommon)
                            {
                                messageCommon.ReplyToMsgId = null;
                                messageCommon.IsOut        = false;
                                messageCommon.IsPost       = false;
                            }

                            if (!(updatePinned.Message is TLMessageEmpty))
                            {
                                result.Insert(0, updatePinned.Message);
                            }

                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionEditMessage editMessage)
                        {
                            // TODO: the actual message
                            if (editMessage.NewMessage is TLMessageCommonBase messageCommon)
                            {
                                messageCommon.ReplyToMsgId = editMessage.PrevMessage.Id;
                                messageCommon.IsOut        = false;
                                messageCommon.IsPost       = false;
                            }
                            if (editMessage.NewMessage is TLMessage message)
                            {
                                message.Reply       = editMessage.PrevMessage;
                                message.EditDate    = null;
                                message.HasEditDate = false;
                            }

                            result.Insert(0, editMessage.NewMessage);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionDeleteMessage deleteMessage)
                        {
                            // Patch for view
                            if (deleteMessage.Message is TLMessageCommonBase messageCommon)
                            {
                                messageCommon.ReplyToMsgId = null;
                                messageCommon.IsOut        = false;
                                messageCommon.IsPost       = false;
                            }

                            result.Insert(0, deleteMessage.Message);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantJoin participantJoin)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantLeave participantLeave)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantInvite participantInvite)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantToggleBan participantToggleBan)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId = item.UserId;
                            message.ToId   = _channel.ToPeer();
                            message.Date   = item.Date;
                            //message.Message = from.ReadString();
                            message.Entities = new TLVector <TLMessageEntityBase>();

                            message.HasFromId   = true;
                            message.HasEntities = true;

                            var whoUser             = participantToggleBan.PrevParticipant.User;
                            TLChannelBannedRights o = null;
                            TLChannelBannedRights n = null;

                            if (participantToggleBan.PrevParticipant is TLChannelParticipantBanned prevBanned)
                            {
                                o = prevBanned.BannedRights;
                            }
                            if (participantToggleBan.NewParticipant is TLChannelParticipantBanned newBanned)
                            {
                                n = newBanned.BannedRights;
                            }

                            if (_channel.IsMegaGroup && (n == null || !n.IsViewMessages || n != null && o != null && n.UntilDate != o.UntilDate))
                            {
                                StringBuilder rights;
                                String        bannedDuration;
                                if (n != null && !AdminLogHelper.IsBannedForever(n.UntilDate))
                                {
                                    bannedDuration = "";
                                    int duration = n.UntilDate - item.Date;
                                    int days     = duration / 60 / 60 / 24;
                                    duration -= days * 60 * 60 * 24;
                                    int hours = duration / 60 / 60;
                                    duration -= hours * 60 * 60;
                                    int minutes = duration / 60;
                                    int count   = 0;
                                    for (int a = 0; a < 3; a++)
                                    {
                                        String addStr = null;
                                        if (a == 0)
                                        {
                                            if (days != 0)
                                            {
                                                addStr = LocaleHelper.Declension("Days", days);
                                                count++;
                                            }
                                        }
                                        else if (a == 1)
                                        {
                                            if (hours != 0)
                                            {
                                                addStr = LocaleHelper.Declension("Hours", hours);
                                                count++;
                                            }
                                        }
                                        else
                                        {
                                            if (minutes != 0)
                                            {
                                                addStr = LocaleHelper.Declension("Minutes", minutes);
                                                count++;
                                            }
                                        }
                                        if (addStr != null)
                                        {
                                            if (bannedDuration.Length > 0)
                                            {
                                                bannedDuration += ", ";
                                            }
                                            bannedDuration += addStr;
                                        }
                                        if (count == 2)
                                        {
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    bannedDuration = Strings.Android.UserRestrictionsUntilForever;
                                }

                                var str = Strings.Android.EventLogRestrictedUntil;
                                rights = new StringBuilder(String.Format(str, GetUserName(whoUser, message.Entities, str.IndexOf("{0}")), bannedDuration));
                                var added = false;
                                if (o == null)
                                {
                                    o = new TLChannelBannedRights();
                                }
                                if (n == null)
                                {
                                    n = new TLChannelBannedRights();
                                }

                                void AppendChange(bool value, string label)
                                {
                                    if (!added)
                                    {
                                        rights.Append('\n');
                                        added = true;
                                    }

                                    rights.Append('\n').Append(!value ? '+' : '-').Append(' ');
                                    rights.Append(label);
                                }

                                if (o.IsViewMessages != n.IsViewMessages)
                                {
                                    AppendChange(n.IsViewMessages, Strings.Android.EventLogRestrictedReadMessages);
                                }
                                if (o.IsSendMessages != n.IsSendMessages)
                                {
                                    AppendChange(n.IsSendMessages, Strings.Android.EventLogRestrictedSendMessages);
                                }
                                if (o.IsSendStickers != n.IsSendStickers || o.IsSendInline != n.IsSendInline || o.IsSendGifs != n.IsSendGifs || o.IsSendGames != n.IsSendGames)
                                {
                                    AppendChange(n.IsSendStickers, Strings.Android.EventLogRestrictedSendStickers);
                                }
                                if (o.IsSendMedia != n.IsSendMedia)
                                {
                                    AppendChange(n.IsSendMedia, Strings.Android.EventLogRestrictedSendMedia);
                                }
                                if (o.IsEmbedLinks != n.IsEmbedLinks)
                                {
                                    AppendChange(n.IsEmbedLinks, Strings.Android.EventLogRestrictedSendEmbed);
                                }

                                message.Message = rights.ToString();
                            }
                            else
                            {
                                String str;
                                if (n != null && (o == null || n.IsViewMessages))
                                {
                                    str = Strings.Android.EventLogChannelRestricted;
                                }
                                else
                                {
                                    str = Strings.Android.EventLogChannelUnrestricted;
                                }

                                message.Message = String.Format(str, GetUserName(whoUser, message.Entities, str.IndexOf("{0}")));
                            }

                            result.Insert(0, message);
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantToggleAdmin participantToggleAdmin)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId = item.UserId;
                            message.ToId   = _channel.ToPeer();
                            message.Date   = item.Date;
                            //message.Message = from.ReadString();
                            message.Entities = new TLVector <TLMessageEntityBase>();

                            message.HasFromId   = true;
                            message.HasEntities = true;

                            var whoUser  = participantToggleAdmin.PrevParticipant.User;
                            var str      = Strings.Android.EventLogPromoted;
                            var userName = GetUserName(whoUser, message.Entities, str.IndexOf("{0}"));
                            var builder  = new StringBuilder(string.Format(str, userName));
                            var added    = false;

                            TLChannelAdminRights o = null;
                            TLChannelAdminRights n = null;

                            if (participantToggleAdmin.PrevParticipant is TLChannelParticipantAdmin prevAdmin)
                            {
                                o = prevAdmin.AdminRights;
                            }
                            if (participantToggleAdmin.NewParticipant is TLChannelParticipantAdmin newAdmin)
                            {
                                n = newAdmin.AdminRights;
                            }

                            if (o == null)
                            {
                                o = new TLChannelAdminRights();
                            }
                            if (n == null)
                            {
                                n = new TLChannelAdminRights();
                            }

                            void AppendChange(bool value, string label)
                            {
                                if (!added)
                                {
                                    builder.Append('\n');
                                    added = true;
                                }

                                builder.Append('\n').Append(value ? '+' : '-').Append(' ');
                                builder.Append(label);
                            }

                            if (o.IsChangeInfo != n.IsChangeInfo)
                            {
                                AppendChange(n.IsChangeInfo, _channel.IsMegaGroup ? Strings.Android.EventLogPromotedChangeGroupInfo : Strings.Android.EventLogPromotedChangeChannelInfo);
                            }

                            if (!_channel.IsMegaGroup)
                            {
                                if (o.IsPostMessages != n.IsPostMessages)
                                {
                                    AppendChange(n.IsPostMessages, Strings.Android.EventLogPromotedPostMessages);
                                }
                                if (o.IsEditMessages != n.IsEditMessages)
                                {
                                    AppendChange(n.IsEditMessages, Strings.Android.EventLogPromotedEditMessages);
                                }
                            }
                            if (o.IsDeleteMessages != n.IsDeleteMessages)
                            {
                                AppendChange(n.IsDeleteMessages, Strings.Android.EventLogPromotedDeleteMessages);
                            }
                            if (o.IsAddAdmins != n.IsAddAdmins)
                            {
                                AppendChange(n.IsAddAdmins, Strings.Android.EventLogPromotedAddAdmins);
                            }
                            if (_channel.IsMegaGroup)
                            {
                                if (o.IsBanUsers != n.IsBanUsers)
                                {
                                    AppendChange(n.IsBanUsers, Strings.Android.EventLogPromotedBanUsers);
                                }
                            }
                            if (o.IsInviteUsers != n.IsInviteUsers)
                            {
                                AppendChange(n.IsInviteUsers, Strings.Android.EventLogPromotedAddUsers);
                            }
                            if (_channel.IsMegaGroup)
                            {
                                if (o.IsPinMessages != n.IsPinMessages)
                                {
                                    AppendChange(n.IsPinMessages, Strings.Android.EventLogPromotedPinMessages);
                                }
                            }

                            message.Message = builder.ToString();

                            result.Insert(0, message);
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangeStickerSet changeStickerSet)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                    }

                    if (response.Result.Events.Count < 50)
                    {
                        _hasMore = false;
                    }

                    return(result);
                }

                _viewModel.IsLoading = false;

                return(new TLMessageBase[0]);
            }
            public override async Task <IList <TLMessageBase> > LoadDataAsync()
            {
                _viewModel.IsLoading = true;

                var maxId = Count > 0 ? _minEventId : 0;

                var response = await _protoService.GetAdminLogAsync(_inputChannel, null, null, null, maxId, 0, 50);

                if (response.IsSucceeded)
                {
                    _viewModel.IsLoading = false;

                    var result = new List <TLMessageBase>();

                    foreach (var item in response.Result.Events)
                    {
                        _minEventId = Math.Min(_minEventId, item.Id);

                        if (item.Action is TLChannelAdminLogEventActionChangeTitle changeTitle)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangeAbout changeAbout)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId   = item.UserId;
                            message.ToId     = _channel.ToPeer();
                            message.Date     = item.Date;
                            message.Message  = changeAbout.NewValue;
                            message.HasMedia = true;

                            if (string.IsNullOrEmpty(changeAbout.PrevValue))
                            {
                                message.Media = new TLMessageMediaEmpty();
                            }
                            else
                            {
                                message.Media = new TLMessageMediaWebPage
                                {
                                    WebPage = new TLWebPage
                                    {
                                        SiteName       = AppResources.EventLogPreviousGroupDescription,
                                        Description    = changeAbout.PrevValue,
                                        HasSiteName    = true,
                                        HasDescription = true
                                    }
                                };
                            }

                            result.Insert(0, message);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangeUsername changeUsername)
                        {
                            var config = InMemoryCacheService.Current.GetConfig();
                            if (config == null)
                            {
                                continue;
                            }

                            var linkPrefix = config.MeUrlPrefix;
                            if (linkPrefix.EndsWith("/"))
                            {
                                linkPrefix = linkPrefix.Substring(0, linkPrefix.Length - 1);
                            }
                            if (linkPrefix.StartsWith("https://"))
                            {
                                linkPrefix = linkPrefix.Substring(8);
                            }
                            else if (linkPrefix.StartsWith("http://"))
                            {
                                linkPrefix = linkPrefix.Substring(7);
                            }

                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId      = item.UserId;
                            message.ToId        = _channel.ToPeer();
                            message.Date        = item.Date;
                            message.Message     = string.IsNullOrEmpty(changeUsername.NewValue) ? string.Empty : $"https://{linkPrefix}/{changeUsername.NewValue}";
                            message.Entities    = new TLVector <TLMessageEntityBase>();
                            message.HasMedia    = true;
                            message.HasEntities = true;

                            message.Entities.Add(new TLMessageEntityUrl {
                                Offset = 0, Length = message.Message.Length
                            });

                            if (string.IsNullOrEmpty(changeUsername.PrevValue))
                            {
                                message.Media = new TLMessageMediaEmpty();
                            }
                            else
                            {
                                message.Media = new TLMessageMediaWebPage
                                {
                                    WebPage = new TLWebPage
                                    {
                                        SiteName       = AppResources.EventLogPreviousLink,
                                        Description    = $"https://{linkPrefix}/{changeUsername.PrevValue}",
                                        HasSiteName    = true,
                                        HasDescription = true
                                    }
                                };
                            }

                            result.Insert(0, message);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionChangePhoto changePhoto)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionToggleInvites toggleInvites)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionToggleSignatures toggleSignatures)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionUpdatePinned updatePinned)
                        {
                            // Patch for view
                            if (updatePinned.Message is TLMessageCommonBase messageCommon)
                            {
                                messageCommon.ReplyToMsgId = null;
                                messageCommon.IsOut        = false;
                                messageCommon.IsPost       = false;
                            }

                            if (!(updatePinned.Message is TLMessageEmpty))
                            {
                                result.Insert(0, updatePinned.Message);
                            }

                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionEditMessage editMessage)
                        {
                            // TODO: the actual message
                            if (editMessage.NewMessage is TLMessageCommonBase messageCommon)
                            {
                                messageCommon.ReplyToMsgId = editMessage.PrevMessage.Id;
                                messageCommon.IsOut        = false;
                                messageCommon.IsPost       = false;
                            }
                            if (editMessage.NewMessage is TLMessage message)
                            {
                                message.Reply       = editMessage.PrevMessage;
                                message.EditDate    = null;
                                message.HasEditDate = false;
                            }

                            result.Insert(0, editMessage.NewMessage);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionDeleteMessage deleteMessage)
                        {
                            // Patch for view
                            if (deleteMessage.Message is TLMessageCommonBase messageCommon)
                            {
                                messageCommon.ReplyToMsgId = null;
                                messageCommon.IsOut        = false;
                                messageCommon.IsPost       = false;
                            }

                            result.Insert(0, deleteMessage.Message);
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantJoin participantJoin)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantLeave participantLeave)
                        {
                            result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantInvite participantInvite)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId = item.UserId;
                            message.ToId   = _channel.ToPeer();
                            message.Date   = item.Date;
                            //message.Message = from.ReadString();
                            message.Entities = new TLVector <TLMessageEntityBase>();

                            message.HasFromId   = true;
                            message.HasEntities = true;

                            var whoUser  = participantInvite.Participant.User;
                            var str      = AppResources.EventLogInvited;
                            var userName = GetUserName(whoUser, message.Entities, str.IndexOf("{0}"));
                            var builder  = new StringBuilder(string.Format(str, userName));

                            message.Message = string.Format(str, userName);
                            result.Insert(0, message);

                            //result.Insert(0, GetServiceMessage(item));
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantToggleBan participantToggleBan)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId = item.UserId;
                            message.ToId   = _channel.ToPeer();
                            message.Date   = item.Date;
                            //message.Message = from.ReadString();
                            message.Entities = new TLVector <TLMessageEntityBase>();

                            message.HasFromId   = true;
                            message.HasEntities = true;

                            var whoUser             = participantToggleBan.PrevParticipant.User;
                            TLChannelBannedRights o = null;
                            TLChannelBannedRights n = null;

                            if (participantToggleBan.PrevParticipant is TLChannelParticipantBanned prevBanned)
                            {
                                o = prevBanned.BannedRights;
                            }
                            if (participantToggleBan.NewParticipant is TLChannelParticipantBanned newBanned)
                            {
                                n = newBanned.BannedRights;
                            }
                            if (!_channel.IsMegaGroup || (n != null && n.IsViewMessages && (n == null || o == null || n.UntilDate == o.UntilDate)))
                            {
                                string str;
                                if (n == null || !(o == null || n.IsViewMessages))
                                {
                                    str = AppResources.EventLogChannelUnrestricted;
                                }
                                else
                                {
                                    str = AppResources.EventLogChannelRestricted;
                                }

                                var userName = GetUserName(whoUser, message.Entities, str.IndexOf("{0}"));
                                message.Message = string.Format(str, userName);
                            }
                            else
                            {
                                StringBuilder builder;
                                if (n == null || AdminLogHelper.IsBannedForever(n.UntilDate))
                                {
                                    var str      = AppResources.EventLogRestricted;
                                    var userName = GetUserName(whoUser, message.Entities, str.IndexOf("{0}"));
                                    builder = new StringBuilder(String.Format(str, userName));
                                }
                                else
                                {
                                    var bannedDuration = "";
                                    int duration       = n.UntilDate - item.Date;
                                    int days           = ((duration / 60) / 60) / 24;
                                    duration -= ((days * 60) * 60) * 24;
                                    int hours   = (duration / 60) / 60;
                                    int minutes = (duration - ((hours * 60) * 60)) / 60;
                                    int count   = 0;
                                    for (int a = 0; a < 3; a++)
                                    {
                                        String addStr = null;
                                        if (a == 0)
                                        {
                                            if (days != 0)
                                            {
                                                //addStr = LocaleController.formatPluralString("Days", days);
                                                addStr = $"{days} days";
                                                count++;
                                            }
                                        }
                                        else if (a == 1)
                                        {
                                            if (hours != 0)
                                            {
                                                //addStr = LocaleController.formatPluralString("Hours", hours);
                                                addStr = $"{hours} hours";
                                                count++;
                                            }
                                        }
                                        else if (minutes != 0)
                                        {
                                            //addStr = LocaleController.formatPluralString("Minutes", minutes);
                                            addStr = $"{minutes} minutes";
                                            count++;
                                        }
                                        if (addStr != null)
                                        {
                                            if (bannedDuration.Length > 0)
                                            {
                                                bannedDuration = bannedDuration + ", ";
                                            }
                                            bannedDuration = bannedDuration + addStr;
                                        }
                                        if (count == 2)
                                        {
                                            break;
                                        }
                                    }

                                    var str      = AppResources.EventLogRestrictedUntil;
                                    var userName = GetUserName(whoUser, message.Entities, str.IndexOf("{0}"));
                                    builder = new StringBuilder(String.Format(str, userName, bannedDuration));
                                }

                                var added = false;
                                if (o == null)
                                {
                                    o = new TLChannelBannedRights();
                                }
                                if (n == null)
                                {
                                    n = new TLChannelBannedRights();
                                }

                                void AppendChange(bool value, string label)
                                {
                                    if (!added)
                                    {
                                        builder.Append('\n');
                                        added = true;
                                    }

                                    builder.Append('\n').Append(!value ? '+' : '-').Append(' ');
                                    builder.Append(label);
                                }

                                if (o.IsViewMessages != n.IsViewMessages)
                                {
                                    AppendChange(n.IsViewMessages, AppResources.EventLogRestrictedReadMessages);
                                }
                                if (o.IsSendMessages != n.IsSendMessages)
                                {
                                    AppendChange(n.IsSendMessages, AppResources.EventLogRestrictedSendMessages);
                                }
                                if (!(o.IsSendStickers == n.IsSendStickers && o.IsSendInline == n.IsSendInline && o.IsSendGifs == n.IsSendGifs && o.IsSendGames == n.IsSendGames))
                                {
                                    AppendChange(n.IsSendStickers, AppResources.EventLogRestrictedSendStickers);
                                }
                                if (o.IsSendMedia != n.IsSendMedia)
                                {
                                    AppendChange(n.IsSendMedia, AppResources.EventLogRestrictedSendMedia);
                                }
                                if (o.IsEmbedLinks != n.IsEmbedLinks)
                                {
                                    AppendChange(n.IsEmbedLinks, AppResources.EventLogRestrictedSendEmbed);
                                }

                                message.Message = builder.ToString();
                            }

                            result.Insert(0, message);
                        }
                        else if (item.Action is TLChannelAdminLogEventActionParticipantToggleAdmin participantToggleAdmin)
                        {
                            var message = new TLMessage();
                            //message.Id = item.Id;
                            message.FromId = item.UserId;
                            message.ToId   = _channel.ToPeer();
                            message.Date   = item.Date;
                            //message.Message = from.ReadString();
                            message.Entities = new TLVector <TLMessageEntityBase>();

                            message.HasFromId   = true;
                            message.HasEntities = true;

                            var whoUser  = participantToggleAdmin.PrevParticipant.User;
                            var str      = AppResources.EventLogPromoted;
                            var userName = GetUserName(whoUser, message.Entities, str.IndexOf("{0}"));
                            var builder  = new StringBuilder(string.Format(str, userName));
                            var added    = false;

                            TLChannelAdminRights o = null;
                            TLChannelAdminRights n = null;

                            if (participantToggleAdmin.PrevParticipant is TLChannelParticipantAdmin prevAdmin)
                            {
                                o = prevAdmin.AdminRights;
                            }
                            if (participantToggleAdmin.NewParticipant is TLChannelParticipantAdmin newAdmin)
                            {
                                n = newAdmin.AdminRights;
                            }

                            if (o == null)
                            {
                                o = new TLChannelAdminRights();
                            }
                            if (n == null)
                            {
                                n = new TLChannelAdminRights();
                            }

                            void AppendChange(bool value, string label)
                            {
                                if (!added)
                                {
                                    builder.Append('\n');
                                    added = true;
                                }

                                builder.Append('\n').Append(value ? '+' : '-').Append(' ');
                                builder.Append(label);
                            }

                            if (o.IsChangeInfo != n.IsChangeInfo)
                            {
                                AppendChange(n.IsChangeInfo, _channel.IsMegaGroup ? AppResources.EventLogPromotedChangeGroupInfo : AppResources.EventLogPromotedChangeChannelInfo);
                            }

                            if (!_channel.IsMegaGroup)
                            {
                                if (o.IsPostMessages != n.IsPostMessages)
                                {
                                    AppendChange(n.IsPostMessages, AppResources.EventLogPromotedPostMessages);
                                }
                                if (o.IsEditMessages != n.IsEditMessages)
                                {
                                    AppendChange(n.IsEditMessages, AppResources.EventLogPromotedEditMessages);
                                }
                            }
                            if (o.IsDeleteMessages != n.IsDeleteMessages)
                            {
                                AppendChange(n.IsDeleteMessages, AppResources.EventLogPromotedDeleteMessages);
                            }
                            if (o.IsAddAdmins != n.IsAddAdmins)
                            {
                                AppendChange(n.IsAddAdmins, AppResources.EventLogPromotedAddAdmins);
                            }
                            if (_channel.IsMegaGroup)
                            {
                                if (o.IsBanUsers != n.IsBanUsers)
                                {
                                    AppendChange(n.IsBanUsers, AppResources.EventLogPromotedBanUsers);
                                }
                                if (o.IsInviteUsers != n.IsInviteUsers)
                                {
                                    AppendChange(n.IsInviteUsers, AppResources.EventLogPromotedAddUsers);
                                }
                                if (o.IsPinMessages != n.IsPinMessages)
                                {
                                    AppendChange(n.IsPinMessages, AppResources.EventLogPromotedPinMessages);
                                }
                            }

                            message.Message = builder.ToString();

                            result.Insert(0, message);
                        }
                    }

                    if (response.Result.Events.Count < 50)
                    {
                        _hasMore = false;
                    }

                    return(result);
                }

                _viewModel.IsLoading = false;

                return(new TLMessageBase[0]);
            }
        public AddChatParticipantViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            if (StateService.CurrentAdminRights != null)
            {
                _currentAdminRights             = StateService.CurrentAdminRights;
                StateService.CurrentAdminRights = null;
            }

            if (StateService.CurrentRole != null)
            {
                _currentRole             = StateService.CurrentRole;
                StateService.CurrentRole = null;
            }

            if (StateService.CurrentChat != null)
            {
                _currentChat             = StateService.CurrentChat;
                StateService.CurrentChat = null;
            }

            if (StateService.RemovedUsers != null)
            {
                foreach (var user in StateService.RemovedUsers)
                {
                    _removedUsers[user.Index] = user;
                }
                StateService.RemovedUsers = null;
            }

            if (StateService.RequestForwardingCount)
            {
                _requestForwardingCount             = true;
                StateService.RequestForwardingCount = false;
            }

            _inviteVisibility            = StateService.IsInviteVisible ? Visibility.Visible : Visibility.Collapsed;
            StateService.IsInviteVisible = false;

            BeginOnThreadPool(() =>
            {
                var firstSlice      = new List <TLUserBase>();
                var secondSlice     = new List <TLUserBase>();
                var firstSliceCount = 10;
                var isAuthorized    = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (isAuthorized)
                {
                    var contacts = CacheService.GetContacts();
                    foreach (var contact in contacts.OrderBy(x => x.FullName))
                    {
                        if (!(contact is TLUserEmpty) && contact.Index != StateService.CurrentUserId && !_removedUsers.ContainsKey(contact.Index))
                        {
                            if (firstSlice.Count < firstSliceCount)
                            {
                                firstSlice.Add(contact);
                            }
                            else
                            {
                                secondSlice.Add(contact);
                            }
                        }
                    }
                }

                BeginOnUIThread(() =>
                {
                    Status = string.Empty;
                    Items.Clear();
                    foreach (var user in firstSlice)
                    {
                        Items.Add(user);
                    }
                    if (secondSlice.Count > 0)
                    {
                        BeginOnUIThread(() =>
                        {
                            foreach (var user in secondSlice)
                            {
                                Items.Add(user);
                            }
                        });
                    }

                    if (StateService.RemoveBackEntry)
                    {
                        NavigationService.RemoveBackEntry();
                        StateService.RemoveBackEntry = false;
                    }
                });
            });
        }