/// <summary> /// Determines what event has happened and triggers event as appropriate. /// </summary> /// <param name="eventKey">The key for the event.</param> /// <param name="json">The JSON source for the event.</param> public void Dispatch(string eventKey, string json) { switch (eventKey) { case PushEvent.WebhookEventName: OnPushReceived(new EventArgs <PushEvent>(DataContractJsonSerialization.Deserialize <PushEvent>(json))); break; case IssueCommentCreatedEvent.WebhookEventName: OnIssueCommentCreatedReceived(new EventArgs <IssueCommentCreatedEvent>(DataContractJsonSerialization.Deserialize <IssueCommentCreatedEvent>(json))); break; case IssueCreatedEvent.WebhookEventName: OnIssueCreatedEventReceived(new EventArgs <IssueCreatedEvent>(DataContractJsonSerialization.Deserialize <IssueCreatedEvent>(json))); break; case IssueUpdatedEvent.WebhookEventName: OnIssueUpdatedEventReceived(new EventArgs <IssueUpdatedEvent>(DataContractJsonSerialization.Deserialize <IssueUpdatedEvent>(json))); break; } }
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 })); }
/// <summary> /// Processes a request from the SE API. /// </summary> /// <typeparam name="T">The type of the object to be returned. This should be inferred from the <see cref="IRequest{T}"/>.</typeparam> /// <param name="response">The JSON response from the API.</param> /// <returns>A <see cref="Wrapper{T}"/> representing the JSON data.</returns> public T ProcessResponse <T>(string response) where T : IBaseModel => DataContractJsonSerialization.Deserialize <T>(response);
/// <summary> /// Processes a request from the SE API. /// </summary> /// <typeparam name="T">The type of the object to be returned. This should be inferred from the <see cref="IRequest{T}"/>.</typeparam> /// <param name="response">The JSON response from the API.</param> /// <returns>A <see cref="Wrapper{T}"/> representing the JSON data.</returns> public Wrapper <T> ProcessResponse <T>(string response) where T : IBaseModel { return(DataContractJsonSerialization.Deserialize <Wrapper <T> >(response)); }
/// <summary> /// Determines what event has happened and triggers event as appropriate. /// </summary> /// <param name="eventKey">The key for the event.</param> /// <param name="json">The JSON source for the event.</param> public void Dispatch(string eventKey, string json) { switch (eventKey) { case CommitCommentEvent.WebhookEventName: OnCommitCommentReceived(new EventArgs <CommitCommentEvent>(DataContractJsonSerialization.Deserialize <CommitCommentEvent>(json))); break; case CreateEvent.WebhookEventName: OnCreateReceived(new EventArgs <CreateEvent>(DataContractJsonSerialization.Deserialize <CreateEvent>(json))); break; case DeleteEvent.WebhookEventName: OnDeleteReceived(new EventArgs <DeleteEvent>(DataContractJsonSerialization.Deserialize <DeleteEvent>(json))); break; case DeploymentEvent.WebhookEventName: OnDeploymentReceived(new EventArgs <DeploymentEvent>(DataContractJsonSerialization.Deserialize <DeploymentEvent>(json))); break; case DeploymentStatusEvent.WebhookEventName: OnDeploymentStatusReceived(new EventArgs <DeploymentStatusEvent>(DataContractJsonSerialization.Deserialize <DeploymentStatusEvent>(json))); break; case ForkEvent.WebhookEventName: OnForkReceived(new EventArgs <ForkEvent>(DataContractJsonSerialization.Deserialize <ForkEvent>(json))); break; case GollumEvent.WebhookEventName: OnGollumReceived(new EventArgs <GollumEvent>(DataContractJsonSerialization.Deserialize <GollumEvent>(json))); break; case IssueCommentEvent.WebhookEventName: OnIssueCommentReceived(new EventArgs <IssueCommentEvent>(DataContractJsonSerialization.Deserialize <IssueCommentEvent>(json))); break; case IssuesEvent.WebhookEventName: OnIssuesReceived(new EventArgs <IssuesEvent>(DataContractJsonSerialization.Deserialize <IssuesEvent>(json))); break; case MemberEvent.WebhookEventName: OnMemberReceived(new EventArgs <MemberEvent>(DataContractJsonSerialization.Deserialize <MemberEvent>(json))); break; case MembershipEvent.WebhookEventName: OnMembershipReceived(new EventArgs <MembershipEvent>(DataContractJsonSerialization.Deserialize <MembershipEvent>(json))); break; case PageBuildEvent.WebhookEventName: OnPageBuildReceived(new EventArgs <PageBuildEvent>(DataContractJsonSerialization.Deserialize <PageBuildEvent>(json))); break; case PublicEvent.WebhookEventName: OnPublicReceived(new EventArgs <PublicEvent>(DataContractJsonSerialization.Deserialize <PublicEvent>(json))); break; case PullRequestEvent.WebhookEventName: OnPullRequestReceived(new EventArgs <PullRequestEvent>(DataContractJsonSerialization.Deserialize <PullRequestEvent>(json))); break; case PullRequestReviewCommentEvent.WebhookEventName: OnPullRequestReviewCommentReceived(new EventArgs <PullRequestReviewCommentEvent>(DataContractJsonSerialization.Deserialize <PullRequestReviewCommentEvent>(json))); break; case PushEvent.WebhookEventName: OnPushReceived(new EventArgs <PushEvent>(DataContractJsonSerialization.Deserialize <PushEvent>(json))); break; case ReleaseEvent.WebhookEventName: OnReleaseReceived(new EventArgs <ReleaseEvent>(DataContractJsonSerialization.Deserialize <ReleaseEvent>(json))); break; case RepositoryEvent.WebhookEventName: OnRepositoryReceived(new EventArgs <RepositoryEvent>(DataContractJsonSerialization.Deserialize <RepositoryEvent>(json))); break; case StatusEvent.WebhookEventName: OnStatusReceived(new EventArgs <StatusEvent>(DataContractJsonSerialization.Deserialize <StatusEvent>(json))); break; case TeamAddEvent.WebhookEventName: OnTeamAddReceived(new EventArgs <TeamAddEvent>(DataContractJsonSerialization.Deserialize <TeamAddEvent>(json))); break; case WatchEvent.WebhookEventName: OnWatchReceived(new EventArgs <WatchEvent>(DataContractJsonSerialization.Deserialize <WatchEvent>(json))); break; } }