{"action":"created","comment":{"url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/comments/32382613","id":32382613,"diff_hunk":"@@ -278,8 +297,50 @@ class CTFOServer {\n   }\n \n   void OnMidichloriansEvent(const LogEntry& entry) {\n-    // TODO(mzhurovich): update answers here.\n-    static_cast<void>(entry);\n+    std::unique_ptr<MidichloriansEvent> event;\n+    if (entry.m == \"POST\") {\n+      try {\n+        ParseJSON(entry.b, event);\n+        UpdateStateOnEvent(event);\n+      } catch (const bricks::ParseJSONException&) {\n+        // TODO(mzhurovich): Error logging.\n+        std::cerr << \"Parse JSON failed.\" << std::endl;\n+      }\n+    }\n+  }\n+\n+  void UpdateStateOnEvent(const std::unique_ptr<MidichloriansEvent>& event) {\n+    try {\n+      const iOSGenericEvent& ge = dynamic_cast<const iOSGenericEvent&>(*event.get());\n+      if (valid_answers_.find(ge.event) != valid_answers_.end()) {\n+        const ANSWER answer = valid_answers_.at(ge.event);\n+        const UID uid = StringToUID(ge.fields.at(\"uid\"));\n+        const CID cid = StringToCID(ge.fields.at(\"cid\"));\n+        const std::string token = ge.fields.at(\"token\");\n+        if (uid != UID::INVALID && cid != CID::INVALID) {\n+          storage_.Transaction([&uid, &cid, &token, &answer](StorageAPI::T_DATA data) {\n+                                 const auto auth_token_accessor = Matrix<AuthKeyTokenPair>::Accessor(data);\n+                                 bool token_is_valid = false;\n+                                 if (auth_token_accessor.Cols().Has(token)) {\n+                                   // Something went terribly wrong\n+                                   // if we have more than one authentication key for token.\n+                                   assert(auth_token_accessor[token].size() == 1);\n+                                   if (auth_token_accessor[token].begin()->valid) {\n+                                     token_is_valid = true;\n+                                   }\n+                                 }\n+                                 if (token_is_valid) {\n+                                   auto answers_mutator = Matrix<Answer>::Mutator(data);\n+                                   if (!answers_mutator.Has(uid, cid)) {  // Do not overwrite existing answers.\n+                                     data.Add(Answer(uid, cid, answer));\n+                                   }\n+                                 }\n+                               }).Go();\n+        }\n+      }\n+    } catch (const std::bad_cast&) {","path":"server.h","position":156,"original_position":156,"commit_id":"b166432e6524cf47acbd017f4a7f2a07538131c0","original_commit_id":"b166432e6524cf47acbd017f4a7f2a07538131c0","user":{"login":"dkorolev","id":2159447,"avatar_url":"https://avatars.githubusercontent.com/u/2159447?v=3","gravatar_id":"","url":"https://api.github.com/users/dkorolev","html_url":"https://github.com/dkorolev","followers_url":"https://api.github.com/users/dkorolev/followers","following_url":"https://api.github.com/users/dkorolev/following{/other_user}","gists_url":"https://api.github.com/users/dkorolev/gists{/gist_id}","starred_url":"https://api.github.com/users/dkorolev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkorolev/subscriptions","organizations_url":"https://api.github.com/users/dkorolev/orgs","repos_url":"https://api.github.com/users/dkorolev/repos","events_url":"https://api.github.com/users/dkorolev/events{/privacy}","received_events_url":"https://api.github.com/users/dkorolev/received_events","type":"User","site_admin":false},"body":"`std::out_of_range` is the exception to catch if you go with `map::at`. By the way, this route would save you a top-level `if (valid_answers_.find(ge.event) != valid_answers_.end())`.\r\n\r\nAlthough I'd catch on every step and return descriptive error messages. Nikolay will thank us for it!","created_at":"2015-06-14T18:22:39Z","updated_at":"2015-06-14T18:22:39Z","html_url":"https://github.com/KnowSheet/CTFO_BE/pull/5#discussion_r32382613","pull_request_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/comments/32382613"},"html":{"href":"https://github.com/KnowSheet/CTFO_BE/pull/5#discussion_r32382613"},"pull_request":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5","id":37645447,"html_url":"https://github.com/KnowSheet/CTFO_BE/pull/5","diff_url":"https://github.com/KnowSheet/CTFO_BE/pull/5.diff","patch_url":"https://github.com/KnowSheet/CTFO_BE/pull/5.patch","issue_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues/5","number":5,"state":"open","locked":false,"title":"Updated version with parsing events.","user":{"login":"mzhurovich","id":11226961,"avatar_url":"https://avatars.githubusercontent.com/u/11226961?v=3","gravatar_id":"","url":"https://api.github.com/users/mzhurovich","html_url":"https://github.com/mzhurovich","followers_url":"https://api.github.com/users/mzhurovich/followers","following_url":"https://api.github.com/users/mzhurovich/following{/other_user}","gists_url":"https://api.github.com/users/mzhurovich/gists{/gist_id}","starred_url":"https://api.github.com/users/mzhurovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mzhurovich/subscriptions","organizations_url":"https://api.github.com/users/mzhurovich/orgs","repos_url":"https://api.github.com/users/mzhurovich/repos","events_url":"https://api.github.com/users/mzhurovich/events{/privacy}","received_events_url":"https://api.github.com/users/mzhurovich/received_events","type":"User","site_admin":false},"body":"@dkorolev PTAL.","created_at":"2015-06-14T17:54:03Z","updated_at":"2015-06-14T18:22:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6cf515e2f2c903276f4eae8979dbc743ba91dc67","assignee":{"login":"dkorolev","id":2159447,"avatar_url":"https://avatars.githubusercontent.com/u/2159447?v=3","gravatar_id":"","url":"https://api.github.com/users/dkorolev","html_url":"https://github.com/dkorolev","followers_url":"https://api.github.com/users/dkorolev/followers","following_url":"https://api.github.com/users/dkorolev/following{/other_user}","gists_url":"https://api.github.com/users/dkorolev/gists{/gist_id}","starred_url":"https://api.github.com/users/dkorolev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkorolev/subscriptions","organizations_url":"https://api.github.com/users/dkorolev/orgs","repos_url":"https://api.github.com/users/dkorolev/repos","events_url":"https://api.github.com/users/dkorolev/events{/privacy}","received_events_url":"https://api.github.com/users/dkorolev/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5/commits","review_comments_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5/comments","review_comment_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/comments{/number}","comments_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues/5/comments","statuses_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/statuses/b166432e6524cf47acbd017f4a7f2a07538131c0","head":{"label":"mzhurovich:master","ref":"master","sha":"b166432e6524cf47acbd017f4a7f2a07538131c0","user":{"login":"mzhurovich","id":11226961,"avatar_url":"https://avatars.githubusercontent.com/u/11226961?v=3","gravatar_id":"","url":"https://api.github.com/users/mzhurovich","html_url":"https://github.com/mzhurovich","followers_url":"https://api.github.com/users/mzhurovich/followers","following_url":"https://api.github.com/users/mzhurovich/following{/other_user}","gists_url":"https://api.github.com/users/mzhurovich/gists{/gist_id}","starred_url":"https://api.github.com/users/mzhurovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mzhurovich/subscriptions","organizations_url":"https://api.github.com/users/mzhurovich/orgs","repos_url":"https://api.github.com/users/mzhurovich/repos","events_url":"https://api.github.com/users/mzhurovich/events{/privacy}","received_events_url":"https://api.github.com/users/mzhurovich/received_events","type":"User","site_admin":false},"repo":{"id":34410534,"name":"CTFO_BE","full_name":"mzhurovich/CTFO_BE","owner":{"login":"mzhurovich","id":11226961,"avatar_url":"https://avatars.githubusercontent.com/u/11226961?v=3","gravatar_id":"","url":"https://api.github.com/users/mzhurovich","html_url":"https://github.com/mzhurovich","followers_url":"https://api.github.com/users/mzhurovich/followers","following_url":"https://api.github.com/users/mzhurovich/following{/other_user}","gists_url":"https://api.github.com/users/mzhurovich/gists{/gist_id}","starred_url":"https://api.github.com/users/mzhurovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mzhurovich/subscriptions","organizations_url":"https://api.github.com/users/mzhurovich/orgs","repos_url":"https://api.github.com/users/mzhurovich/repos","events_url":"https://api.github.com/users/mzhurovich/events{/privacy}","received_events_url":"https://api.github.com/users/mzhurovich/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mzhurovich/CTFO_BE","description":"CTFO Backend","fork":true,"url":"https://api.github.com/repos/mzhurovich/CTFO_BE","forks_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/forks","keys_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/teams","hooks_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/hooks","issue_events_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/issues/events{/number}","events_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/events","assignees_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/assignees{/user}","branches_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/branches{/branch}","tags_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/tags","blobs_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/git/refs{/sha}","trees_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/statuses/{sha}","languages_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/languages","stargazers_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/stargazers","contributors_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/contributors","subscribers_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/subscribers","subscription_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/subscription","commits_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/commits{/sha}","git_commits_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/git/commits{/sha}","comments_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/comments{/number}","issue_comment_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/issues/comments{/number}","contents_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/contents/{+path}","compare_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/merges","archive_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/downloads","issues_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/issues{/number}","pulls_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/pulls{/number}","milestones_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/milestones{/number}","notifications_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/labels{/name}","releases_url":"https://api.github.com/repos/mzhurovich/CTFO_BE/releases{/id}","created_at":"2015-04-22T19:15:30Z","updated_at":"2015-06-04T06:29:46Z","pushed_at":"2015-06-14T17:52:31Z","git_url":"git://github.com/mzhurovich/CTFO_BE.git","ssh_url":"git@github.com:mzhurovich/CTFO_BE.git","clone_url":"https://github.com/mzhurovich/CTFO_BE.git","svn_url":"https://github.com/mzhurovich/CTFO_BE","homepage":null,"size":128,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"KnowSheet:master","ref":"master","sha":"c117970d82a2d3cab364fa7ac5ebb49c8bc7a460","user":{"login":"KnowSheet","id":9897562,"avatar_url":"https://avatars.githubusercontent.com/u/9897562?v=3","gravatar_id":"","url":"https://api.github.com/users/KnowSheet","html_url":"https://github.com/KnowSheet","followers_url":"https://api.github.com/users/KnowSheet/followers","following_url":"https://api.github.com/users/KnowSheet/following{/other_user}","gists_url":"https://api.github.com/users/KnowSheet/gists{/gist_id}","starred_url":"https://api.github.com/users/KnowSheet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KnowSheet/subscriptions","organizations_url":"https://api.github.com/users/KnowSheet/orgs","repos_url":"https://api.github.com/users/KnowSheet/repos","events_url":"https://api.github.com/users/KnowSheet/events{/privacy}","received_events_url":"https://api.github.com/users/KnowSheet/received_events","type":"Organization","site_admin":false},"repo":{"id":34405022,"name":"CTFO_BE","full_name":"KnowSheet/CTFO_BE","owner":{"login":"KnowSheet","id":9897562,"avatar_url":"https://avatars.githubusercontent.com/u/9897562?v=3","gravatar_id":"","url":"https://api.github.com/users/KnowSheet","html_url":"https://github.com/KnowSheet","followers_url":"https://api.github.com/users/KnowSheet/followers","following_url":"https://api.github.com/users/KnowSheet/following{/other_user}","gists_url":"https://api.github.com/users/KnowSheet/gists{/gist_id}","starred_url":"https://api.github.com/users/KnowSheet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KnowSheet/subscriptions","organizations_url":"https://api.github.com/users/KnowSheet/orgs","repos_url":"https://api.github.com/users/KnowSheet/repos","events_url":"https://api.github.com/users/KnowSheet/events{/privacy}","received_events_url":"https://api.github.com/users/KnowSheet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/KnowSheet/CTFO_BE","description":"CTFO Backend","fork":false,"url":"https://api.github.com/repos/KnowSheet/CTFO_BE","forks_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/forks","keys_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/keys{/key_id}","collaborators_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/teams","hooks_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/hooks","issue_events_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues/events{/number}","events_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/events","assignees_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/assignees{/user}","branches_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/branches{/branch}","tags_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/tags","blobs_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/git/refs{/sha}","trees_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/git/trees{/sha}","statuses_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/statuses/{sha}","languages_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/languages","stargazers_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/stargazers","contributors_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/contributors","subscribers_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/subscribers","subscription_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/subscription","commits_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/commits{/sha}","git_commits_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/git/commits{/sha}","comments_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/comments{/number}","issue_comment_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues/comments{/number}","contents_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/contents/{+path}","compare_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/compare/{base}...{head}","merges_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/merges","archive_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/downloads","issues_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues{/number}","pulls_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls{/number}","milestones_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/milestones{/number}","notifications_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/labels{/name}","releases_url":"https://api.github.com/repos/KnowSheet/CTFO_BE/releases{/id}","created_at":"2015-04-22T17:25:40Z","updated_at":"2015-06-06T20:13:23Z","pushed_at":"2015-06-14T17:54:05Z","git_url":"git://github.com/KnowSheet/CTFO_BE.git","ssh_url":"git@github.com:KnowSheet/CTFO_BE.git","clone_url":"https://github.com/KnowSheet/CTFO_BE.git","svn_url":"https://github.com/KnowSheet/CTFO_BE","homepage":null,"size":192,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5"},"html":{"href":"https://github.com/KnowSheet/CTFO_BE/pull/5"},"issue":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues/5"},"comments":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/KnowSheet/CTFO_BE/statuses/b166432e6524cf47acbd017f4a7f2a07538131c0"}}}}