Example #1
0
        private async void SendExecute()
        {
            var chat = _chat;

            if (chat == null)
            {
                return;
            }

            var member = _member;

            if (member == null)
            {
                return;
            }

            var supergroup = chat.Type as ChatTypeSupergroup;

            if (supergroup == null)
            {
                return;
            }

            var status = new ChatMemberStatusRestricted
            {
                IsMember            = true,
                RestrictedUntilDate = _untilDate,
                Permissions         = new ChatPermissions
                {
                    CanChangeInfo         = _canChangeInfo,
                    CanPinMessages        = _canPinMessages,
                    CanInviteUsers        = _canInviteUsers,
                    CanAddWebPagePreviews = _canAddWebPagePreviews,
                    CanSendPolls          = _canSendPolls,
                    CanSendOtherMessages  = _canSendOtherMessages,
                    CanSendMediaMessages  = _canSendMediaMessages,
                    CanSendMessages       = _canSendMessages,
                }
            };

            var response = await ProtoService.SendAsync(new SetChatMemberStatus(chat.Id, member.UserId, status));

            if (response is Ok)
            {
                NavigationService.GoBack();
                NavigationService.Frame.ForwardStack.Clear();
            }
            else
            {
                // TODO: ...
            }
        }
Example #2
0
        private async void SendExecute()
        {
            var chat = _chat;

            if (chat == null)
            {
                return;
            }

            var member = _member;

            if (member == null)
            {
                return;
            }

            var supergroup = chat.Type as ChatTypeSupergroup;

            if (supergroup == null)
            {
                return;
            }

            var status = new ChatMemberStatusRestricted
            {
                IsMember = _canViewMessages,
                CanAddWebPagePreviews = _canAddWebPagePreviews,
                CanSendOtherMessages  = _canSendOtherMessages,
                CanSendMediaMessages  = _canSendMediaMessages,
                CanSendMessages       = _canSendMessages
            };

            var response = await ProtoService.SendAsync(new SetChatMemberStatus(chat.Id, member.UserId, status));

            if (response is Ok)
            {
                NavigationService.GoBack();
                NavigationService.Frame.ForwardStack.Clear();
            }
            else
            {
                // TODO: ...
            }
        }
 public static bool IsForever(this ChatMemberStatusRestricted restricted)
 {
     return(restricted.RestrictedUntilDate == 0 || Math.Abs(restricted.RestrictedUntilDate - DateTime.Now.ToTimestamp() / 1000) > 5 * 365 * 24 * 60 * 60);
 }
Example #4
0
            private MessageContent GetMessageContent(ChatEvent item)
            {
                if (item.Action is ChatEventDescriptionChanged descriptionChanged)
                {
                    var text    = new FormattedText(descriptionChanged.NewDescription, new TextEntity[0]);
                    var webPage = string.IsNullOrEmpty(descriptionChanged.OldDescription) ? null : new WebPage {
                        SiteName = Strings.Resources.EventLogPreviousGroupDescription, Description = descriptionChanged.OldDescription
                    };

                    return(new MessageText(text, webPage));
                }
                else if (item.Action is ChatEventUsernameChanged usernameChanged)
                {
                    var link = string.IsNullOrEmpty(usernameChanged.NewUsername) ? string.Empty : MeUrlPrefixConverter.Convert(_protoService, usernameChanged.NewUsername);

                    var text    = new FormattedText(link, new[] { new TextEntity(0, link.Length, new TextEntityTypeUrl()) });
                    var webPage = string.IsNullOrEmpty(usernameChanged.OldUsername) ? null : new WebPage {
                        SiteName = Strings.Resources.EventLogPreviousLink, Description = MeUrlPrefixConverter.Convert(_protoService, usernameChanged.OldUsername)
                    };

                    return(new MessageText(text, webPage));
                }
                else if (item.Action is ChatEventMemberRestricted memberRestricted)
                {
                    var text     = string.Empty;
                    var entities = new List <TextEntity>();

                    var whoUser = _protoService.GetUser(memberRestricted.UserId);
                    ChatMemberStatusRestricted o = null;
                    ChatMemberStatusRestricted n = null;

                    if (memberRestricted.OldStatus is ChatMemberStatusRestricted oldRestricted)
                    {
                        o = oldRestricted;
                    }
                    else if (memberRestricted.OldStatus is ChatMemberStatusBanned oldBanned)
                    {
                        o = new ChatMemberStatusRestricted(false, oldBanned.BannedUntilDate, false, false, false, false);
                    }
                    else if (memberRestricted.OldStatus is ChatMemberStatusMember)
                    {
                        o = new ChatMemberStatusRestricted(true, 0, true, true, true, true);
                    }

                    if (memberRestricted.NewStatus is ChatMemberStatusRestricted newRestricted)
                    {
                        n = newRestricted;
                    }
                    else if (memberRestricted.NewStatus is ChatMemberStatusBanned newBanned)
                    {
                        n = new ChatMemberStatusRestricted(false, newBanned.BannedUntilDate, false, false, false, false);
                    }
                    else if (memberRestricted.NewStatus is ChatMemberStatusMember)
                    {
                        n = new ChatMemberStatusRestricted(true, 0, true, true, true, true);
                    }

                    if (!_channel && (n == null || n != null && o != null /*&& n.RestrictedUntilDate != o.RestrictedUntilDate*/))
                    {
                        StringBuilder rights;
                        String        bannedDuration;
                        if (n != null && !n.IsForever())
                        {
                            bannedDuration = "";
                            int duration = n.RestrictedUntilDate - 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 = Locale.Declension("Days", days);
                                        count++;
                                    }
                                }
                                else if (a == 1)
                                {
                                    if (hours != 0)
                                    {
                                        addStr = Locale.Declension("Hours", hours);
                                        count++;
                                    }
                                }
                                else
                                {
                                    if (minutes != 0)
                                    {
                                        addStr = Locale.Declension("Minutes", minutes);
                                        count++;
                                    }
                                }
                                if (addStr != null)
                                {
                                    if (bannedDuration.Length > 0)
                                    {
                                        bannedDuration += ", ";
                                    }
                                    bannedDuration += addStr;
                                }
                                if (count == 2)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            bannedDuration = Strings.Resources.UserRestrictionsUntilForever;
                        }

                        var str = Strings.Resources.EventLogRestrictedUntil;
                        rights = new StringBuilder(String.Format(str, GetUserName(whoUser, entities, str.IndexOf("{0}")), bannedDuration));
                        var added = false;
                        if (o == null)
                        {
                            o = new ChatMemberStatusRestricted(true, 0, true, true, true, true);
                        }
                        if (n == null)
                        {
                            n = new ChatMemberStatusRestricted(true, 0, true, true, true, true);
                        }

                        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.Resources.EventLogRestrictedReadMessages);
                        //}
                        if (o.CanSendMessages != n.CanSendMessages)
                        {
                            AppendChange(n.CanSendMessages, Strings.Resources.EventLogRestrictedSendMessages);
                        }
                        if (o.CanSendOtherMessages != n.CanSendOtherMessages)
                        {
                            AppendChange(n.CanSendOtherMessages, Strings.Resources.EventLogRestrictedSendStickers);
                        }
                        if (o.CanSendMediaMessages != n.CanSendMediaMessages)
                        {
                            AppendChange(n.CanSendMediaMessages, Strings.Resources.EventLogRestrictedSendMedia);
                        }
                        if (o.CanAddWebPagePreviews != n.CanAddWebPagePreviews)
                        {
                            AppendChange(n.CanAddWebPagePreviews, Strings.Resources.EventLogRestrictedSendEmbed);
                        }

                        text = rights.ToString();
                    }
                    else
                    {
                        String str;
                        if (o == null || memberRestricted.NewStatus is ChatMemberStatusBanned)
                        {
                            str = Strings.Resources.EventLogChannelRestricted;
                        }
                        else
                        {
                            str = Strings.Resources.EventLogChannelUnrestricted;
                        }

                        text = String.Format(str, GetUserName(whoUser, entities, str.IndexOf("{0}")));
                    }

                    return(new MessageText(new FormattedText(text, entities), null));
                }
                else if (item.Action is ChatEventMemberPromoted memberPromoted)
                {
                    var entities = new List <TextEntity>();

                    var whoUser  = _protoService.GetUser(memberPromoted.UserId);
                    var str      = Strings.Resources.EventLogPromoted;
                    var userName = GetUserName(whoUser, entities, str.IndexOf("{0}"));
                    var builder  = new StringBuilder(string.Format(str, userName));
                    var added    = false;

                    ChatMemberStatusAdministrator o = null;
                    ChatMemberStatusAdministrator n = null;

                    if (memberPromoted.OldStatus is ChatMemberStatusAdministrator oldAdmin)
                    {
                        o = oldAdmin;
                    }
                    if (memberPromoted.NewStatus is ChatMemberStatusAdministrator newAdmin)
                    {
                        n = newAdmin;
                    }

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

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

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

                    if (o.CanChangeInfo != n.CanChangeInfo)
                    {
                        AppendChange(n.CanChangeInfo, _channel ? Strings.Resources.EventLogPromotedChangeChannelInfo : Strings.Resources.EventLogPromotedChangeGroupInfo);
                    }

                    if (_channel)
                    {
                        if (o.CanPostMessages != n.CanPostMessages)
                        {
                            AppendChange(n.CanPostMessages, Strings.Resources.EventLogPromotedPostMessages);
                        }
                        if (o.CanEditMessages != n.CanEditMessages)
                        {
                            AppendChange(n.CanEditMessages, Strings.Resources.EventLogPromotedEditMessages);
                        }
                    }
                    if (o.CanDeleteMessages != n.CanDeleteMessages)
                    {
                        AppendChange(n.CanDeleteMessages, Strings.Resources.EventLogPromotedDeleteMessages);
                    }
                    if (o.CanPromoteMembers != n.CanPromoteMembers)
                    {
                        AppendChange(n.CanPromoteMembers, Strings.Resources.EventLogPromotedAddAdmins);
                    }
                    if (!_channel)
                    {
                        if (o.CanRestrictMembers != n.CanRestrictMembers)
                        {
                            AppendChange(n.CanRestrictMembers, Strings.Resources.EventLogPromotedBanUsers);
                        }
                    }
                    if (o.CanInviteUsers != n.CanInviteUsers)
                    {
                        AppendChange(n.CanInviteUsers, Strings.Resources.EventLogPromotedAddUsers);
                    }
                    if (!_channel)
                    {
                        if (o.CanPinMessages != n.CanPinMessages)
                        {
                            AppendChange(n.CanPinMessages, Strings.Resources.EventLogPromotedPinMessages);
                        }
                    }

                    return(new MessageText(new FormattedText(builder.ToString(), entities), null));
                }

                return(new MessageChatEvent(item, true));
            }
Example #5
0
 public static bool IsForever(this ChatMemberStatusRestricted restricted)
 {
     return(Math.Abs(restricted.RestrictedUntilDate - Utils.CurrentTimestamp / 1000) > 5 * 365 * 24 * 60 * 60);
 }