Esempio n. 1
0
        internal async Task <IEnumerable <Ban> > GetBansAsync(int page = 1)
        {
            var request =
                new RestRequest("bans", Method.GET, DataFormat.Json).AddQueryParameter("page", page.ToString());
            var response = await Client.ExecuteAsync(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                FailedRequest(response);
            }

            var toReturn = new List <Ban>();
            var content  = JsonSerializer.Deserialize <JsonElement>(response.Content);

            foreach (var b in content.GetProperty("data").EnumerateArray())
            {
                var expiryString = b.GetProperty("unbanned_datetime").GetString() ??
                                   b.GetProperty("expiration_time").GetString();
                var toAdd = new Ban()
                {
                    BannedOn = DateTime.SpecifyKind(DateTime.Parse(b.GetProperty("bantime").GetString()),
                                                    DateTimeKind.Utc),
                    BannedBy   = b.GetProperty("a_ckey").GetString(),
                    UnbannedBy = b.GetProperty("unbanned_ckey").GetString(),
                    BanType    = b.GetProperty("roles").EnumerateArray().Select(x => x.GetString()).Contains("Server")
                        ? BanType.Server
                        : BanType.Job,
                    Expires = expiryString == null
                        ? null
                        : DateTime.SpecifyKind(DateTime.Parse(expiryString), DateTimeKind.Utc),
                    CKey             = b.GetProperty("ckey").GetString(),
                    Reason           = b.GetProperty("reason").GetString(),
                    BanID            = b.GetProperty("id").GetInt32().ToString(),
                    SourceNavigation = ParseBanSource(b.GetProperty("server_name").GetString())
                };

                if (toAdd.BanType == BanType.Job)
                {
                    toAdd.AddJobRange(b.GetProperty("roles").EnumerateArray().Select(x => x.GetString()));
                }

                if (b.GetProperty("global_ban").GetInt32() == 1)
                {
                    toAdd.AddAttribute(BanAttribute.BeeStationGlobal);
                }

                toReturn.Add(toAdd);
            }

            return(toReturn);
        }
Esempio n. 2
0
        public void Equals_SameBanDifferingAttributes_ReturnFalse()
        {
            var banA = new Ban()
            {
                Id     = 0,
                Source = 15
            };

            var banB = new Ban()
            {
                Id     = 0,
                Source = 15
            };

            banB.AddAttribute(BanAttribute.BeeStationGlobal);

            var comparer = BanEqualityComparer.Instance;

            Assert.False(comparer.Equals(banA, banB), "Bans should not be equal if they differ in attributes");
            Assert.False(comparer.GetHashCode(banA) == comparer.GetHashCode(banB), "Bans should not have the same hashcode if they differ in attributes");
        }
Esempio n. 3
0
        public void Equals_SameBanSameAttributes_ReturnTrue()
        {
            var banA = new Ban()
            {
                Id     = 0,
                Source = 15
            };

            banA.AddAttribute(BanAttribute.BeeStationGlobal);

            var banB = new Ban()
            {
                Id     = 0,
                Source = 15
            };

            banB.AddAttribute(BanAttribute.BeeStationGlobal);

            var comparer = BanEqualityComparer.Instance;

            Assert.True(comparer.Equals(banA, banB), "Bans should be equal when they are equal including attributes");
            Assert.True(comparer.GetHashCode(banA) == comparer.GetHashCode(banB), "Bans should have the same hashcode if they are equal including attributes");
        }