示例#1
0
        /// <summary>
        /// Gets or creates the global transformation protection data for the given user.
        /// </summary>
        /// <param name="discordUser">The user.</param>
        /// <returns>Global protection data for the given user.</returns>
        public async Task <RetrieveEntityResult <GlobalUserProtection> > GetOrCreateGlobalUserProtectionAsync
        (
            IUser discordUser
        )
        {
            var protection = await _database.GlobalUserProtections.AsQueryable()
                             .FirstOrDefaultAsync(p => p.User.DiscordID == (long)discordUser.Id);

            if (!(protection is null))
            {
                return(RetrieveEntityResult <GlobalUserProtection> .FromSuccess(protection));
            }

            var getUserResult = await _users.GetOrRegisterUserAsync(discordUser);

            if (!getUserResult.IsSuccess)
            {
                return(RetrieveEntityResult <GlobalUserProtection> .FromError(getUserResult));
            }

            var user = getUserResult.Entity;

            protection = new GlobalUserProtection(user);

            _database.GlobalUserProtections.Update(protection);

            await _database.SaveChangesAsync();

            return(RetrieveEntityResult <GlobalUserProtection> .FromSuccess(protection));
        }
            public async Task CreatedObjectRespectsGlobalDefaults()
            {
                var user = (await this.Users.GetOrRegisterUserAsync(_user)).Entity;

                var globalSetting = new GlobalUserProtection(user)
                {
                    DefaultOptIn = true,
                    DefaultType  = ProtectionType.Whitelist
                };

                this.Database.GlobalUserProtections.Update(globalSetting);
                await this.Database.SaveChangesAsync();

                var localSetting = await this.Transformations.GetOrCreateServerUserProtectionAsync
                                   (
                    _user,
                    _guild
                                   );

                Assert.Equal(globalSetting.DefaultOptIn, localSetting.Entity.HasOptedIn);
                Assert.Equal(globalSetting.DefaultType, localSetting.Entity.Type);
                Assert.Same(globalSetting.User, localSetting.Entity.User);
            }
 /// <summary>
 /// Initializes a new instance of the <see cref="UserProtectionEntry"/> class.
 /// </summary>
 /// <param name="globalProtection">The global protection entry.</param>
 /// <param name="user">The target user.</param>
 public UserProtectionEntry(GlobalUserProtection globalProtection, User user)
 {
     this.GlobalProtection = globalProtection;
     this.User             = user;
 }