Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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 }));
        }