コード例 #1
0
        private Evbpc.Framework.Integrations.StackExchange.API.Models.User GetUserInformation(string accessToken)
        {
            var config    = StackExchangeApiConfiguration.Load();
            var apiConfig = new Evbpc.Framework.Integrations.StackExchange.API.Configuration()
            {
                Key         = config.Key,
                AccessToken = accessToken
            };

            var request = new Evbpc.Framework.Integrations.StackExchange.API.Requests.MeAssociatedRequest()
            {
                PageSize = 100, Types = Evbpc.Framework.Integrations.StackExchange.API.Requests.MeAssociatedRequest.AccountTypes.MainSite
            };
            var handler  = new Evbpc.Framework.Integrations.StackExchange.API.Handler(apiConfig);
            var response = handler.SubmitRequest(request);

            var myUsers = handler.ProcessResponse <Evbpc.Framework.Integrations.StackExchange.API.Models.NetworkUser>(response).Items;

            var meRequest = new Evbpc.Framework.Integrations.StackExchange.API.Requests.MeRequest()
            {
                Site = new Uri(myUsers[0].SiteUrl).Host
            };
            var user = handler.ProcessResponse <Evbpc.Framework.Integrations.StackExchange.API.Models.User>(handler.SubmitRequest(meRequest)).Items[0];

            user.Reputation = myUsers.Sum(x => x.Reputation);

            return(user);
        }
コード例 #2
0
        private List <SEAPI.Models.Site> GetSites()
        {
            var apiConfig = StackExchangeApiConfiguration.Load();
            var config    = new SEAPI.Configuration()
            {
                Key = apiConfig.Key
            };
            var request = new SEAPI.Requests.SitesRequest()
            {
                PageSize = 1000
            };
            var handler  = new SEAPI.Handler(config);
            var response = handler.ProcessResponse <SEAPI.Models.Site>(handler.SubmitRequest(request));

            return(response.Items);
        }
コード例 #3
0
        private List <SEAPI.Models.NetworkUser> GetAssociatedUsers(ApplicationUser user)
        {
            var apiConfig = StackExchangeApiConfiguration.Load();
            var config    = new SEAPI.Configuration()
            {
                AccessToken = user.AccessToken, Key = apiConfig.Key
            };
            var request = new SEAPI.Requests.MeAssociatedRequest()
            {
                PageSize = 100, Types = SEAPI.Requests.MeAssociatedRequest.AccountTypes.MainSite
            };
            var handler  = new SEAPI.Handler(config);
            var response = handler.ProcessResponse <SEAPI.Models.NetworkUser>(handler.SubmitRequest(request));

            return(response.Items);
        }
コード例 #4
0
        private string GetAccessToken(string code)
        {
            var config = StackExchangeApiConfiguration.Load();

            var request = new Evbpc.Framework.Integrations.StackExchange.OAuth.Requests.AccessTokenRequest();

            request.ClientId     = config.ClientId;
            request.ClientSecret = config.ClientSecret;
            request.RedirectUri  = StackExchangeApiConfiguration.EncodedRedirectUri;
            request.Code         = code;

            var apiConfig = new Evbpc.Framework.Integrations.StackExchange.OAuth.Configuration()
            {
                ClientId = config.ClientId, ClientSecret = config.ClientSecret, Key = config.Key
            };
            var handler  = new Evbpc.Framework.Integrations.StackExchange.OAuth.Handler(apiConfig);
            var response = handler.SubmitRequest(request);

            return(response);
        }
コード例 #5
0
        private List <SEAPI.Models.Question> GetQuestions(string site, string accessToken, int page = 1)
        {
            var apiConfig = StackExchangeApiConfiguration.Load();
            var config    = new SEAPI.Configuration()
            {
                AccessToken = accessToken, Key = apiConfig.Key
            };
            var request = new SEAPI.Requests.QuestionsRequest()
            {
                Filter   = _mainFilter,
                Order    = SEAPI.OrderType.Descending,
                Site     = site,
                Sort     = SEAPI.Requests.QuestionsRequest.SortType.Creation,
                Page     = page,
                PageSize = 10,
            };
            var handler      = new SEAPI.Handler(config);
            var response     = handler.SubmitRequest(request);
            var responseType = handler.ProcessResponse <SEAPI.Models.Question>(response);

            return(responseType.Items);
        }
コード例 #6
0
        public ActionResult Vote(int id, string site, string direction)
        {
            using (var db = new ApplicationDbContext())
            {
                var user = db.Users.Find(User.Identity.GetUserId());

                switch (direction)
                {
                case "Up":
                    // Submit a /questions/id/upvote API request to SE
                    try
                    {
                        var apiConfig = StackExchangeApiConfiguration.Load();
                        var config    = new SEAPI.Configuration()
                        {
                            AccessToken = user.AccessToken, Key = apiConfig.Key
                        };
                        var request = new SEAPI.Requests.VoteRequest {
                            Filter = _mainFilter, Id = id, Site = site, VoteAction = SEAPI.Requests.VoteRequest.VoteType.Upvote, Key = apiConfig.Key, AccessToken = user.AccessToken
                        };
                        var handler = new SEAPI.Handler(config);
                        var result  = handler.ProcessResponse <SEAPI.Models.Question>(handler.SubmitRequest(request));

                        user.Upvotes++;
                        db.SaveChanges();

                        if (!result.Items[0].Upvoted.Value)
                        {
                            // Failure, they either cannot upvote (15 rep?) or they are at their vote limit (40)
                            return(RedirectToAction("OutOfVotes"));
                        }
                    }
                    catch (WebException e)
                    {
                        using (var sr = new StreamReader(e.Response.GetResponseStream()))
                        {
                            var response = sr.ReadToEnd();
                            var result   = DataContractJsonSerialization.Deserialize <SEAPI.Models.Error>(response);

                            // Failure to cast up-vote
                            if (result.ErrorId == 407)
                            {
                                return(RedirectToAction("OutOfVotes", new { message = result.ErrorMessage, name = result.ErrorName, id = result.ErrorId }));
                            }
                        }
                        // TODO: replace this with an attempt to reauthenticate through OAuth, the majority of the times this gets hit seem to be issues with the access_token needing to be revalidated
                        return(RedirectToAction("Login", "Account"));
                    }
                    break;

                case "Skip":
                    // We don't really do anything for this. Adding the 'Skip' action to the DB handles it.
                    user.Skips++;
                    db.SaveChanges();
                    break;

                case "Down":
                    // Submit a /questions/id/downvote API request to SE
                    try
                    {
                        var apiConfig = StackExchangeApiConfiguration.Load();
                        var config    = new SEAPI.Configuration()
                        {
                            AccessToken = user.AccessToken, Key = apiConfig.Key
                        };
                        var request = new SEAPI.Requests.VoteRequest {
                            Filter = _mainFilter, Id = id, Site = site, VoteAction = SEAPI.Requests.VoteRequest.VoteType.Downvote, Key = apiConfig.Key, AccessToken = user.AccessToken
                        };
                        var handler = new SEAPI.Handler(config);
                        var result  = handler.ProcessResponse <SEAPI.Models.Question>(handler.SubmitRequest(request));

                        user.Downvotes++;
                        db.SaveChanges();

                        if (!result.Items[0].Downvoted.Value)
                        {
                            // Failure, they either don't have 125 rep or they are at their vote limit (40)
                            return(RedirectToAction("OutOfVotes"));
                        }
                    }
                    catch (WebException e)
                    {
                        using (var sr = new StreamReader(e.Response.GetResponseStream()))
                        {
                            var response = sr.ReadToEnd();
                            var result   = DataContractJsonSerialization.Deserialize <SEAPI.Models.Error>(response);

                            // Failure to cast up-vote
                            if (result.ErrorId == 407)
                            {
                                return(RedirectToAction("OutOfVotes", new { message = result.ErrorMessage, name = result.ErrorName, id = result.ErrorId }));
                            }
                        }
                        // TODO: replace this with an attempt to reauthenticate through OAuth, the majority of the times this gets hit seem to be issues with the access_token needing to be revalidated
                        return(RedirectToAction("Login", "Account"));
                    }
                    break;
                }

                // Action is **always** seen, it's possible to track what questions/answers a user upvotes/downvotes here but that takes away anonymity (sp?)
                // I think *at some point* I want to drop the 'Action' column altogether
                var userQuestionModel = new UserQuestionModel {
                    UserId = user.Id, QuestionId = id, Site = site, Action = "Seen"
                };
                db.UserQuestions.Add(userQuestionModel);
                db.SaveChanges();
            }

            return(RedirectToAction("ViewSite", new { site = site }));
        }