/// <summary>
 /// 创建 <see cref="SubscriptionController"/>
 /// </summary>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 public SubscriptionController(CachedDataProvider cachedData, KeylolDbContext dbContext,
                               KeylolUserManager userManager)
 {
     _cachedData  = cachedData;
     _dbContext   = dbContext;
     _userManager = userManager;
 }
        /// <summary>
        ///     当用户与机器人不再为好友时,通过此方法通知协调器
        /// </summary>
        /// <param name="userSteamId">用户 Steam ID</param>
        /// <param name="botId">机器人 ID</param>
        public async Task OnUserBotRelationshipNone(string userSteamId, string botId)
        {
            using (var dbContext = new KeylolDbContext())
            {
                var userManager = new KeylolUserManager(dbContext);
                var user        = await userManager.FindBySteamIdAsync(userSteamId);

                if (user == null)
                {
                    // 非会员不再为好友时,如果存在已绑定的 SteamBindingToken 则清除之
                    var bindingTokens = await dbContext.SteamBindingTokens
                                        .Where(t => t.SteamId == userSteamId && t.BotId == botId)
                                        .ToListAsync();

                    dbContext.SteamBindingTokens.RemoveRange(bindingTokens);
                    await dbContext.SaveChangesAsync(KeylolDbContext.ConcurrencyStrategy.DatabaseWin);
                }
                else if (user.SteamBotId == botId)
                {
                    // 会员与自己的机器人不再为好友时,解除机器人绑定
                    user.SteamBotId = null;
                    await dbContext.SaveChangesAsync(KeylolDbContext.ConcurrencyStrategy.DatabaseWin);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 获取一条动态
 /// </summary>
 /// <param name="authorIdCode">作者识别码</param>
 /// <param name="sidForAuthor">动态在作者名下的序号</param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 /// <returns><see cref="ActivityPage"/></returns>
 public static async Task <ActivityPage> Get(string authorIdCode, int sidForAuthor,
                                             [Injected] KeylolDbContext dbContext, [Injected] CachedDataProvider cachedData,
                                             [Injected] KeylolUserManager userManager)
 {
     return(await CreateAsync(authorIdCode, sidForAuthor, StateTreeHelper.GetCurrentUserId(),
                              StateTreeHelper.GetCurrentUser().IsInRole(KeylolRoles.Operator), dbContext, cachedData, userManager));
 }
Exemplo n.º 4
0
 /// <summary>
 /// 获取用户个人层级状态树
 /// </summary>
 /// <param name="entrance">要获取的页面</param>
 /// <param name="userIdCode">用户识别码</param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 /// <returns><see cref="UserLevel"/></returns>
 public static async Task <UserLevel> Get(string entrance, string userIdCode,
                                          [Injected] KeylolDbContext dbContext, [Injected] CachedDataProvider cachedData,
                                          [Injected] KeylolUserManager userManager)
 {
     return(await CreateAsync(StateTreeHelper.GetCurrentUserId(), userIdCode,
                              entrance.ToEnum <EntrancePage>(), dbContext, cachedData, userManager));
 }
Exemplo n.º 5
0
 /// <summary>
 /// 创建 <see cref="ActivityCommentController"/>
 /// </summary>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 public ActivityCommentController(KeylolDbContext dbContext, CachedDataProvider cachedData,
                                  KeylolUserManager userManager)
 {
     _dbContext   = dbContext;
     _cachedData  = cachedData;
     _userManager = userManager;
 }
Exemplo n.º 6
0
 /// <summary>
 ///     创建 <see cref="ArticleCommentController" />
 /// </summary>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="mqChannel"><see cref="IModel"/></param>
 public ArticleCommentController(KeylolDbContext dbContext, KeylolUserManager userManager,
                                 CachedDataProvider cachedData, IModel mqChannel)
 {
     _dbContext   = dbContext;
     _userManager = userManager;
     _cachedData  = cachedData;
     _mqChannel   = mqChannel;
 }
Exemplo n.º 7
0
 /// <summary>
 /// 创建 <see cref="ActivityController"/>
 /// </summary>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="mqChannel"><see cref="IModel"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 public ActivityController(KeylolDbContext dbContext, IModel mqChannel, KeylolUserManager userManager,
                           CachedDataProvider cachedData)
 {
     _dbContext   = dbContext;
     _mqChannel   = mqChannel;
     _userManager = userManager;
     _cachedData  = cachedData;
 }
Exemplo n.º 8
0
 /// <summary>
 ///     创建 <see cref="LikeController" />
 /// </summary>
 /// <param name="coupon">
 ///     <see cref="CouponProvider" />
 /// </param>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 /// <param name="cachedData">
 ///     <see cref="CachedDataProvider"/>
 /// </param>
 /// <param name="mqChannel"><see cref="IModel"/></param>
 public LikeController(CouponProvider coupon, KeylolDbContext dbContext,
                       KeylolUserManager userManager, CachedDataProvider cachedData, IModel mqChannel)
 {
     _coupon      = coupon;
     _dbContext   = dbContext;
     _userManager = userManager;
     _cachedData  = cachedData;
     _mqChannel   = mqChannel;
 }
Exemplo n.º 9
0
 /// <summary>
 ///     创建 <see cref="ArticleController" />
 /// </summary>
 /// <param name="mqChannel">
 ///     <see cref="IModel" />
 /// </param>
 /// <param name="coupon">
 ///     <see cref="CouponProvider" />
 /// </param>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 public ArticleController(IModel mqChannel, CouponProvider coupon, KeylolDbContext dbContext,
                          KeylolUserManager userManager, CachedDataProvider cachedData)
 {
     _mqChannel   = mqChannel;
     _coupon      = coupon;
     _dbContext   = dbContext;
     _userManager = userManager;
     _cachedData  = cachedData;
 }
Exemplo n.º 10
0
 /// <summary>
 ///     创建 <see cref="UserController" />
 /// </summary>
 /// <param name="coupon">
 ///     <see cref="CouponProvider" />
 /// </param>
 /// <param name="owinContextProvider">
 ///     <see cref="OwinContextProvider" />
 /// </param>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 /// <param name="oneTimeToken">
 ///     <see cref="OneTimeTokenProvider" />
 /// </param>
 /// <param name="roleManager"><see cref="KeylolRoleManager"/></param>
 public UserController(CouponProvider coupon, OwinContextProvider owinContextProvider, KeylolDbContext dbContext,
                       KeylolUserManager userManager, OneTimeTokenProvider oneTimeToken, KeylolRoleManager roleManager)
 {
     _coupon       = coupon;
     _owinContext  = owinContextProvider.Current;
     _dbContext    = dbContext;
     _userManager  = userManager;
     _oneTimeToken = oneTimeToken;
     _roleManager  = roleManager;
 }
        /// <summary>
        ///     判断指定 Steam 账户是不是其乐用户并且匹配指定机器人
        /// </summary>
        /// <param name="steamId">Steam ID</param>
        /// <param name="botId">机器人 ID</param>
        /// <returns><c>true</c> 表示是其乐用户并于目标机器人匹配,<c>false</c> 表示不是</returns>
        public async Task <bool> IsKeylolUser(string steamId, string botId)
        {
            using (var dbContext = new KeylolDbContext())
            {
                var userManager = new KeylolUserManager(dbContext);
                var user        = await userManager.FindBySteamIdAsync(steamId);

                return(user != null && user.SteamBotId == botId);
            }
        }
        private static async Task <bool> UpdateUserSteamFrinedsAsync([NotNull] string userId, KeylolDbContext dbContext,
                                                                     KeylolUserManager userManager, RedisProvider redis)
        {
            var cacheKey    = UserSteamFriendRecordsCrawlerStampCacheKey(userId);
            var redisDb     = redis.GetDatabase();
            var cacheResult = await redisDb.StringGetAsync(cacheKey);

            if (cacheResult.HasValue)
            {
                return(false);
            }
            await redisDb.StringSetAsync(cacheKey, DateTime.Now.ToTimestamp(), UserSteamFriendsUpdatePeriod);

            try
            {
                var steamId = new SteamID();
                steamId.SetFromSteam3String(await userManager.GetSteamIdAsync(userId));
                var result = JObject.Parse(await HttpClient.GetStringAsync(
                                               $"http://api.steampowered.com/ISteamUser/GetFriendList/v1/?key={ApiKey}&format=json&steamid={steamId.ConvertToUInt64()}&relationship=friend"));
                var oldRecords = (await dbContext.UserSteamFriendRecords.Where(r => r.UserId == userId).ToListAsync())
                                 .ToDictionary(r => r.FriendSteamId, r => r);
                foreach (var friend in result["friendslist"]["friends"])
                {
                    var friendSteamId = (new SteamID(ulong.Parse((string)friend["steamid"]))).Render(true);
                    UserSteamFriendRecord record;
                    if (oldRecords.TryGetValue(friendSteamId, out record))
                    {
                        oldRecords.Remove(friendSteamId);
                    }
                    else
                    {
                        record = new UserSteamFriendRecord
                        {
                            UserId        = userId,
                            FriendSteamId = friendSteamId
                        };
                        dbContext.UserSteamFriendRecords.Add(record);
                        if (friend["friend_since"] != null)
                        {
                            record.FriendSince = Helpers.DateTimeFromTimeStamp((ulong)friend["friend_since"]);
                        }
                    }
                }
                dbContext.UserSteamFriendRecords.RemoveRange(oldRecords.Values);
                await dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                await redisDb.KeyExpireAsync(cacheKey, SilenceTime);

                return(false);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// 创建 <see cref="DetailPage"/>
        /// </summary>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns><see cref="DetailPage"/></returns>
        public static async Task <DetailPage> CreateAsync(string currentUserId, KeylolDbContext dbContext,
                                                          KeylolUserManager userManager)
        {
            var couponLogs = await CouponLogList.CreateAsync(currentUserId, 1, true, dbContext, userManager);

            return(new DetailPage
            {
                CouponLogPageCount = couponLogs.Item2,
                CouponLogs = couponLogs.Item1
            });
        }
Exemplo n.º 14
0
        /// <summary>
        /// 获取指定用户的编辑页
        /// </summary>
        /// <param name="userIdCode">用户识别码</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns><see cref="EditPage"/></returns>
        public static async Task <EditPage> Get(string userIdCode, [Injected] KeylolDbContext dbContext,
                                                [Injected] KeylolUserManager userManager)
        {
            var user = await userManager.FindByIdCodeAsync(userIdCode);

            if (user == null)
            {
                return(new EditPage());
            }
            return(await CreateAsync(user, StateTreeHelper.GetCurrentUserId(), dbContext, userManager));
        }
 /// <summary>
 /// 异步抓取指定用户的 Steam 好友列表 (fire-and-forget)
 /// </summary>
 /// <param name="userId">用户 ID</param>
 public static void UpdateUserSteamFrineds(string userId)
 {
     Task.Run(async() =>
     {
         using (var dbContext = new KeylolDbContext())
             using (var userManager = new KeylolUserManager(dbContext))
             {
                 var redis = Global.Container.GetInstance <RedisProvider>();
                 await UpdateUserSteamFrinedsAsync(userId, dbContext, userManager, redis);
             }
     });
 }
        /// <summary>
        ///     更新指定用户的属性
        /// </summary>
        /// <param name="steamId">要更新的用户 Steam ID</param>
        /// <param name="profileName">Steam 昵称,<c>null</c> 表示不更新</param>
        public async Task UpdateUser(string steamId, string profileName)
        {
            using (var dbContext = new KeylolDbContext())
            {
                var userManager = new KeylolUserManager(dbContext);
                var user        = await userManager.FindBySteamIdAsync(steamId);

                if (user == null)
                {
                    return;
                }
                if (profileName != null)
                {
                    user.SteamProfileName = profileName;
                }
                await dbContext.SaveChangesAsync(KeylolDbContext.ConcurrencyStrategy.DatabaseWin);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// 创建 <see cref="EditPage"/>
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns><see cref="EditPage"/></returns>
        public static async Task <EditPage> CreateAsync(KeylolUser user, string currentUserId, KeylolDbContext dbContext,
                                                        KeylolUserManager userManager)
        {
            if (currentUserId != user.Id && !await userManager.IsInRoleAsync(currentUserId, KeylolRoles.Operator))
            {
                return(new EditPage());
            }

            var editPage = new EditPage
            {
                Email                        = user.Email,
                LockoutEnabled               = user.LockoutEnabled,
                OpenInNewWindow              = user.OpenInNewWindow,
                UseEnglishPointName          = user.PreferredPointName == PreferredPointName.English,
                NotifyOnArticleReplied       = user.NotifyOnArticleReplied,
                NotifyOnCommentReplied       = user.NotifyOnCommentReplied,
                NotifyOnActivityReplied      = user.NotifyOnActivityReplied,
                NotifyOnArticleLiked         = user.NotifyOnArticleLiked,
                NotifyOnCommentLiked         = user.NotifyOnCommentLiked,
                NotifyOnActivityLiked        = user.NotifyOnActivityLiked,
                NotifyOnSubscribed           = user.NotifyOnSubscribed,
                SteamNotifyOnArticleReplied  = user.SteamNotifyOnArticleReplied,
                SteamNotifyOnCommentReplied  = user.SteamNotifyOnCommentReplied,
                SteamNotifyOnActivityReplied = user.SteamNotifyOnActivityReplied,
                SteamNotifyOnArticleLiked    = user.SteamNotifyOnArticleLiked,
                SteamNotifyOnCommentLiked    = user.SteamNotifyOnCommentLiked,
                SteamNotifyOnActivityLiked   = user.SteamNotifyOnActivityLiked,
                SteamNotifyOnSubscribed      = user.SteamNotifyOnSubscribed,
                SteamNotifyOnSpotlighted     = user.SteamNotifyOnSpotlighted,
                SteamNotifyOnMissive         = user.SteamNotifyOnMissive
            };

            if (user.SteamBotId == null)
            {
                editPage.SteamBotLost    = true;
                editPage.SteamBotSteamId = (await SteamBindingHub.GetNextBotAsync(dbContext)).SteamId;
            }
            else
            {
                editPage.SteamBotName    = $"其乐机器人 Keylol.com #{user.SteamBot.Sid}";
                editPage.SteamBotSteamId = user.SteamBot.SteamId;
            }
            return(editPage);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 创建 <see cref="CurrentUser"/>
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="coupon"><see cref="CouponProvider"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns><see cref="CurrentUser"/></returns>
        public static async Task <CurrentUser> CreateAsync(KeylolUser user, KeylolUserManager userManager,
                                                           KeylolDbContext dbContext, CouponProvider coupon, CachedDataProvider cachedData)
        {
            // 每日访问奖励
            if (DateTime.Now.Date > user.LastDailyRewardTime.Date)
            {
                user.LastDailyRewardTime = DateTime.Now;
                user.FreeLike            = 5; // 免费认可重置
                try
                {
                    await dbContext.SaveChangesAsync();

                    await coupon.UpdateAsync(user, CouponEvent.每日访问);
                }
                catch (DbUpdateConcurrencyException)
                {
                }
            }

            // Steam 游戏记录更新
            SteamCrawlerProvider.UpdateUserSteamGameRecords(user.Id);

            // Steam 好友列表更新
            SteamCrawlerProvider.UpdateUserSteamFrineds(user.Id);

            return(new CurrentUser
            {
                Id = user.Id,
                UserName = user.UserName,
                IdCode = user.IdCode,
                Roles = (await userManager.GetRolesAsync(user.Id)).ToList(),
                AvatarImage = user.AvatarImage,
                MessageCount = await cachedData.Messages.GetUserUnreadMessageCountAsync(user.Id),
                Coupon = user.Coupon,
                PreferredPointName = user.PreferredPointName,
                OpenInNewWindow = user.OpenInNewWindow
            });
        }
Exemplo n.º 19
0
        /// <summary>
        /// 创建 <see cref="CouponLogList"/>
        /// </summary>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="page">分页页码</param>
        /// <param name="returnPageCount">是否返回总页数</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns>Item1 表示 <see cref="CouponLogList"/>,Item2 表示总页数</returns>
        public static async Task <Tuple <CouponLogList, int> > CreateAsync(string currentUserId, int page,
                                                                           bool returnPageCount, KeylolDbContext dbContext, KeylolUserManager userManager)
        {
            var conditionQuery = from log in dbContext.CouponLogs
                                 where log.UserId == currentUserId
                                 orderby log.Sid descending
                                 select log;
            var queryResult = await conditionQuery.Select(l => new
            {
                Count = returnPageCount ? conditionQuery.Count() : 1,
                l.Event,
                l.Change,
                l.Balance,
                l.CreateTime,
                l.Description
            }).TakePage(page, RecordsPerPage).ToListAsync();

            var result = new CouponLogList(queryResult.Count);

            foreach (var l in queryResult)
            {
                result.Add(new CouponLog
                {
                    Event       = l.Event,
                    Change      = l.Change,
                    Balance     = l.Balance,
                    CreateTime  = l.CreateTime,
                    Description = await ParseDescriptionAsync(l.Description, dbContext, userManager)
                });
            }
            var firstRecord = queryResult.FirstOrDefault();

            return(new Tuple <CouponLogList, int>(
                       result,
                       (int)Math.Ceiling(firstRecord?.Count / (double)RecordsPerPage ?? 1)));
        }
Exemplo n.º 20
0
 /// <summary>
 ///     创建 <see cref="UserGameRecordController" />
 /// </summary>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 public UserGameRecordController(KeylolDbContext dbContext, KeylolUserManager userManager)
 {
     _dbContext   = dbContext;
     _userManager = userManager;
 }
Exemplo n.º 21
0
        /// <summary>
        /// 创建 <see cref="DossierPage"/>
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="currentUserId">当前登录用户 ID</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns><see cref="DossierPage"/></returns>
        public static async Task <DossierPage> CreateAsync(KeylolUser user, string currentUserId,
                                                           KeylolDbContext dbContext, CachedDataProvider cachedData, KeylolUserManager userManager)
        {
            var subscribedPoints =
                await SubscribedPointList.CreateAsync(currentUserId, user.Id, 1, 3, true, dbContext, cachedData);

            var selectedArticles =
                await SelectedArticleList.CreateAsync(user.Id, 1, 8, true, currentUserId, dbContext, cachedData);

            var dossierPage = new DossierPage
            {
                Coupon          = user.Coupon,
                LikeCount       = await cachedData.Likes.GetUserLikeCountAsync(user.Id),
                GameCount       = await dbContext.UserSteamGameRecords.CountAsync(r => r.UserId == user.Id),
                PlayedGameCount =
                    await dbContext.UserSteamGameRecords.CountAsync(r => r.UserId == user.Id && r.TotalPlayedTime > 0),
                SpotlightCount       = await dbContext.Articles.CountAsync(a => a.AuthorId == user.Id && a.Spotlighted),
                IsOperator           = await userManager.IsInRoleAsync(user.Id, KeylolRoles.Operator),
                SubscribedPointCount = subscribedPoints.Item2,
                SubscribedPoints     = subscribedPoints.Item1,
                ArticleCount         = selectedArticles.Item2,
                SelectedArticles     = selectedArticles.Item1
            };

            return(dossierPage);
        }
Exemplo n.º 22
0
 /// <summary>
 ///     创建新 <see cref="CouponProvider" />
 /// </summary>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 public CouponProvider(KeylolDbContext dbContext, KeylolUserManager userManager)
 {
     _dbContext   = dbContext;
     _userManager = userManager;
 }
Exemplo n.º 23
0
 /// <summary>
 /// 创建 <see cref="StorePage"/>
 /// </summary>
 /// <param name="currentUserId">当前登录用户 ID</param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 /// <param name="coupon"></param>
 /// <returns><see cref="StorePage"/></returns>
 public static async Task <StorePage> CreateAsync(string currentUserId, [Injected] KeylolDbContext dbContext,
                                                  [Injected] CachedDataProvider cachedData, [Injected] KeylolUserManager userManager, [Injected] CouponProvider coupon)
 {
     return(new StorePage
     {
         Gifts = await CouponGiftList.CreateAsync(currentUserId, dbContext, cachedData, userManager, coupon)
     });
 }
Exemplo n.º 24
0
 /// <summary>
 /// 获取文券商店页面
 /// </summary>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
 /// <param name="userManager"></param>
 /// <param name="coupon"></param>
 /// <returns><see cref="StorePage"/></returns>
 public static async Task <StorePage> Get([Injected] KeylolDbContext dbContext,
                                          [Injected] CachedDataProvider cachedData, [Injected] KeylolUserManager userManager, [Injected] CouponProvider coupon)
 {
     return(await CreateAsync(StateTreeHelper.GetCurrentUserId(), dbContext, cachedData, userManager, coupon));
 }
Exemplo n.º 25
0
 /// <summary>
 ///     创建 <see cref="MessageController" />
 /// </summary>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 public MessageController(KeylolDbContext dbContext, KeylolUserManager userManager)
 {
     _dbContext = dbContext;
 }
 /// <summary>
 ///     创建 <see cref="SteamBotController" />
 /// </summary>
 /// <param name="dbContext">
 ///     <see cref="KeylolDbContext" />
 /// </param>
 /// <param name="userManager">
 ///     <see cref="KeylolUserManager" />
 /// </param>
 public SteamBotController(KeylolDbContext dbContext, KeylolUserManager userManager)
 {
     _dbContext   = dbContext;
     _userManager = userManager;
 }
Exemplo n.º 27
0
        /// <summary>
        /// 获取新的完整状态树
        /// </summary>
        /// <param name="state">当前 UI 状态</param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="coupon"><see cref="CouponProvider"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <param name="pointIdCode">据点识别码</param>
        /// <param name="authorIdCode">作者识别码</param>
        /// <param name="userIdCode">用户识别码</param>
        /// <param name="sidForAuthor">文章在作者名下的序号</param>
        /// <param name="keyword">搜索关键字</param>
        /// <returns>完整状态树</returns>
        public static async Task <Root> Locate(string state, [Injected] KeylolUserManager userManager,
                                               [Injected] KeylolDbContext dbContext, [Injected] CouponProvider coupon,
                                               [Injected] CachedDataProvider cachedData, string pointIdCode = null, string authorIdCode = null,
                                               string userIdCode = null, int sidForAuthor = 0, string keyword = null)
        {
            var root          = new Root();
            var currentUserId = StateTreeHelper.GetCurrentUserId();
            var isOperator    = StateTreeHelper.GetCurrentUser().IsInRole(KeylolRoles.Operator);

            if (await StateTreeHelper.CanAccessAsync <Root>(nameof(CurrentUser)))
            {
                var user = await userManager.FindByIdAsync(currentUserId);

                root.CurrentUser = await CurrentUser.CreateAsync(user, userManager, dbContext, coupon, cachedData);
            }

            switch (state)
            {
            case "entrance":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Auto, dbContext, cachedData);

                break;

            case "entrance.discovery":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Discovery, dbContext, cachedData);

                break;

            case "entrance.points":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Points, dbContext, cachedData);

                break;

            case "entrance.timeline":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Timeline, dbContext, cachedData);

                break;

            case "aggregation.point":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Auto, dbContext, cachedData)
                };
                break;

            case "aggregation.point.frontpage":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Frontpage, dbContext, cachedData)
                };
                break;

            case "aggregation.point.intel":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Intel, dbContext, cachedData)
                };
                break;

            case "aggregation.point.product":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Product, dbContext, cachedData)
                };
                break;

            case "aggregation.point.timeline":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Timeline, dbContext, cachedData)
                };
                break;

            case "aggregation.point.edit.info":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.EditInfo, dbContext, cachedData)
                };
                break;

            case "aggregation.point.edit.style":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.EditStyle, dbContext, cachedData)
                };
                break;

            case "aggregation.user":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Auto,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.dossier":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Dossier,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.people":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.People,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.timeline":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Timeline,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.edit":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Edit,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "content.article":
                root.Content = new ContentLevel
                {
                    Article =
                        await
                        States.Content.Article.ArticlePage.CreateAsync(authorIdCode, sidForAuthor, currentUserId,
                                                                       isOperator, dbContext, cachedData, userManager)
                };
                break;

            case "content.activity":
                root.Content = new ContentLevel
                {
                    Activity = await ActivityPage.CreateAsync(authorIdCode, sidForAuthor, currentUserId,
                                                              isOperator, dbContext, cachedData, userManager)
                };
                break;

            case "post-office.unread":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        Unread = await UnreadPage.CreateAsync(currentUserId, dbContext, cachedData)
                    }
                }
                ;
                break;

            case "post-office.social-activity.comment":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        SocialActivity = new SocialActivityLevel
                        {
                            Comment = await CommentPage.CreateAsync(currentUserId, dbContext, cachedData)
                        }
                    }
                }
                ;
                break;

            case "post-office.social-activity.like":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        SocialActivity = new SocialActivityLevel
                        {
                            Like = await LikePage.CreateAsync(currentUserId, dbContext, cachedData)
                        }
                    }
                }
                ;
                break;

            case "post-office.social-activity.subscriber":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        SocialActivity = new SocialActivityLevel
                        {
                            Subscriber = await SubscriberPage.CreateAsync(currentUserId, dbContext, cachedData)
                        }
                    }
                }
                ;
                break;

            case "post-office.missive":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        Missive = await MissivePage.CreateAsync(currentUserId, dbContext, cachedData)
                    }
                }
                ;
                break;

            case "coupon.detail":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(Coupon)))
                {
                    root.Coupon = new CouponLevel
                    {
                        Detail = await DetailPage.CreateAsync(currentUserId, dbContext, userManager)
                    }
                }
                ;
                break;

            case "coupon.store":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(Coupon)))
                {
                    root.Coupon = new CouponLevel
                    {
                        Store =
                            await StorePage.CreateAsync(currentUserId, dbContext, cachedData, userManager, coupon)
                    }
                }
                ;
                break;

            case "coupon.ranking":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(Coupon)))
                {
                    root.Coupon = new CouponLevel
                    {
                        Ranking = await RankingPage.CreateAsync(currentUserId, dbContext, cachedData)
                    }
                }
                ;
                break;

            case "search.point":
                root.Search = new SearchLevel
                {
                    Point = await PointPage.CreateAsync(currentUserId, keyword, dbContext, cachedData)
                };
                break;

            case "search.article":
                root.Search = new SearchLevel
                {
                    Article = await ArticlePage.CreateAsync(keyword, dbContext, cachedData)
                };
                break;

            case "search.user":
                root.Search = new SearchLevel
                {
                    User = await UserPage.CreateAsync(currentUserId, keyword, dbContext, cachedData)
                };
                break;

            default:
                throw new NotSupportedException("Not supported state.");
            }
            return(root);
        }
Exemplo n.º 28
0
        /// <summary>
        /// 获取指定用户的人脉页
        /// </summary>
        /// <param name="userIdCode">用户识别码</param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <returns><see cref="PeoplePage"/></returns>
        public static async Task <PeoplePage> Get(string userIdCode, [Injected] KeylolDbContext dbContext,
                                                  [Injected] CachedDataProvider cachedData, [Injected] KeylolUserManager userManager)
        {
            var user = await userManager.FindByIdCodeAsync(userIdCode);

            if (user == null)
            {
                return(new PeoplePage());
            }
            return(await CreateAsync(user.Id, StateTreeHelper.GetCurrentUserId(), dbContext, cachedData));
        }
Exemplo n.º 29
0
        private static async Task <object> ParseDescriptionAsync(string descriptionText, KeylolDbContext dbContext,
                                                                 KeylolUserManager userManager)
        {
            var description = Helpers.SafeDeserialize <JObject>(descriptionText);

            if (description == null)
            {
                return(Helpers.SafeDeserialize <string>(descriptionText));
            }
            Func <object, JObject> jObject =
                o => JObject.FromObject(o, new JsonSerializer {
                NullValueHandling = NullValueHandling.Ignore
            });
            Func <string, string, Task> fillUser = async(field, newField) =>
            {
                if (description[field] != null)
                {
                    var user = await userManager.FindByIdAsync((string)description[field]);

                    if (user != null)
                    {
                        description.Remove(field);
                        description[newField] = jObject(new
                        {
                            user.UserName,
                            user.IdCode
                        });
                    }
                }
            };
            Func <string, string> truncateContent =
                content => content.Length > 15 ? $"{content.Substring(0, 15)} …" : content;

            if (description["ArticleId"] != null)
            {
                var article = await dbContext.Articles.FindAsync((string)description["ArticleId"]);

                if (article != null)
                {
                    description.Remove("ArticleId");
                    description["Article"] = jObject(new
                    {
                        article.Title,
                        article.SidForAuthor,
                        article.Author.IdCode
                    });
                }
            }
            if (description["CommentId"] != null || description["ArticleCommentId"] != null)
            {
                var commentId = description["ArticleCommentId"] ?? description["CommentId"];
                var comment   = await dbContext.ArticleComments.FindAsync((string)commentId);

                if (comment != null)
                {
                    description.Remove("ArticleCommentId");
                    description.Remove("CommentId");
                    description["ArticleComment"] = jObject(new
                    {
                        Content             = truncateContent(comment.UnstyledContent),
                        ArticleAuthorIdCode = comment.Article.Author.IdCode,
                        comment.Article.SidForAuthor,
                        comment.SidForArticle
                    });
                }
            }
            if (description["ActivityId"] != null)
            {
                var activity = await dbContext.Activities.FindAsync((string)description["ActivityId"]);

                if (activity != null)
                {
                    description.Remove("ActivityId");
                    description["Activity"] = jObject(new
                    {
                        Content = PostOffice.PostOfficeMessageList.CollapseActivityContent(activity, 15),
                        activity.SidForAuthor,
                        activity.Author.IdCode
                    });
                }
            }
            if (description["ActivityCommentId"] != null)
            {
                var comment = await dbContext.ActivityComments.FindAsync((string)description["ActivityCommentId"]);

                if (comment != null)
                {
                    description.Remove("ActivityCommentId");
                    description["ActivityComment"] = jObject(new
                    {
                        Content             = truncateContent(comment.Content),
                        ArticleAuthorIdCode = comment.Activity.Author.IdCode,
                        comment.Activity.SidForAuthor,
                        comment.SidForActivity
                    });
                }
            }
            if (description["CouponGiftId"] != null)
            {
                var gift = await dbContext.CouponGifts.FindAsync((string)description["CouponGiftId"]);

                if (gift != null)
                {
                    description.Remove("CouponGiftId");
                    description["CouponGift"] = jObject(new
                    {
                        gift.Id,
                        gift.Name
                    });
                }
            }
            await fillUser("OperatorId", "Operator");
            await fillUser("UserId", "User");
            await fillUser("InviterId", "Inviter");

            return(description);
        }
Exemplo n.º 30
0
 /// <summary>
 /// 获取文券记录列表
 /// </summary>
 /// <param name="page">分页页码</param>
 /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
 /// <param name="userManager"><see cref="KeylolUserManager"/></param>
 /// <returns><see cref="CouponLogList"/></returns>
 public static async Task <CouponLogList> Get(int page, [Injected] KeylolDbContext dbContext,
                                              [Injected] KeylolUserManager userManager)
 {
     return((await CreateAsync(StateTreeHelper.GetCurrentUserId(), page, false, dbContext, userManager)).Item1);
 }