/// <summary>
        /// Get rate limits for the current user.
        /// </summary>
        /// <param name="rateLimitDb"></param>
        /// <param name="endpoint"></param>
        /// <param name="userManager"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static UserRateLimitInfo GetCurrentUserInfo(RateLimitDbContext rateLimitDb, TwitterAPIEndpoint endpoint, UserManager <ApplicationUser> userManager, ClaimsPrincipal user)
        {
            if (!user.Identity.IsAuthenticated || user.GetTwitterAccessToken() == null)
            {
                return(null);
            }
            var info = rateLimitDb.Find(typeof(UserRateLimitInfo), new object[] { userManager.GetUserId(user), endpoint }) as UserRateLimitInfo;

            if (info == null)
            {
                info = new UserRateLimitInfo()
                {
                    UserID = userManager.GetUserId(user), Type = endpoint
                };
                rateLimitDb.Add(info);
                rateLimitDb.SaveChanges();
            }
            else
            {
                info.ResetIfNeeded();
                rateLimitDb.Update(info);
                rateLimitDb.SaveChanges();
            }

            return(info);
        }
Exemple #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="controller"></param>
 /// <param name="rateLimitDb"></param>
 /// <param name="userManager"></param>
 /// <param name="maxConnectionsPerNode">The cap on the number of connected entities to any given entity. Lower to reduce lag during visualization.</param>
 protected DegreeLinkFinder(IConfiguration configuration, SearchController controller,
                            RateLimitDbContext rateLimitDb, UserManager <ApplicationUser> userManager, int maxConnectionsPerNode)
 {
     Configuration             = configuration;
     Controller                = controller;
     RateLimitDb               = rateLimitDb;
     UserManager               = userManager;
     maximumConnectionsPerNode = maxConnectionsPerNode;
 }
 public HashtagLinkFinder(IConfiguration configuration, SearchController controller, RateLimitDbContext rateLimitDb,
                          UserManager <ApplicationUser> userManager, int maxConnectionsPerNode)
     : base(configuration, controller, rateLimitDb, userManager, maxConnectionsPerNode)
 {
 }
 public RateLimitController(IConfiguration configuration, UserManager <ApplicationUser> userManager, RateLimitDbContext rateLimitDb)
 {
     this.configuration = configuration;
     this.userManager   = userManager;
     this.rateLimitDb   = rateLimitDb;
 }
 internal IDictionary <QueryType, IDictionary <AuthenticationType, int> > CurrentRateLimits(RateLimitDbContext rateLimitDb, UserManager <ApplicationUser> userManager, ClaimsPrincipal user)
 => Enum.GetValues(typeof(QueryType)).Cast <QueryType>()
 .ToDictionary(type => type, type => MinimumRateLimits(type, rateLimitDb, userManager, user));
 /// <summary>
 /// The maximum number of calls available for a given SixDegrees endpoint assuming all related Twitter API endpoints will be hit.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="dbContext"></param>
 /// <param name="userManager"></param>
 /// <param name="user">The user accessing the SixDegrees API.</param>
 /// <returns></returns>
 internal IDictionary <AuthenticationType, int> MinimumRateLimits(QueryType type, RateLimitDbContext dbContext, UserManager <ApplicationUser> userManager, ClaimsPrincipal user) => new Dictionary <AuthenticationType, int>()
 {
     { AuthenticationType.Application, Endpoints(type).Min(endpoint => { var info = cache[endpoint.Value]; info.ResetIfNeeded(); return(info.Limit as int?); }) ?? -1 },
     { AuthenticationType.User, Endpoints(type).Min(endpoint => RateLimitController.GetCurrentUserInfo(dbContext, endpoint.Value, userManager, user)?.Limit as int?) ?? -1 }
 };