{"action":"created","comment":{"url":"https://api.github.com/repos/bem/bem-forum/pulls/comments/30897975","id":30897975,"diff_hunk":"@@ -0,0 +1,461 @@\n+/**\n+ * 1. The main model to retrieve data and work with them.\n+ * 2. The model works with two data sources: Github archive and, if specified in the config app.\n+ * 3. For caching data from the Github module uses MemoryStorage that stores data in computer memory.\n+ */\n+\n+var _ = require('lodash'),\n+    vow = require('vow'),\n+    inherit = require('inherit'),\n+    Archive = require('./archive.js'),\n+    Github = require('../services/github.js'),\n+    MemoryStorage = require('../services/memoryStorage.js'),\n+    Logger = require('bem-site-logger'),\n+    MainModel;\n+\n+module.exports = MainModel = inherit({\n+    /**\n+     * The constructor of the Model class\n+     * @param config {Object} - app config\n+     * @private\n+     */\n+    __constructor: function (config) {\n+        this._config = config;\n+        this._logger = Logger.setOptions(this._config['logger']).createLogger(module);\n+        this._github = Github.getInstance(config);\n+        this._storage = MemoryStorage.getInstance(config);\n+        this._archive = Archive.getInstance(config);\n+    },\n+\n+    /**\n+     * The handler successfully receive data from sources\n+     * 1. If the source replied 304, returns data from the memory,\n+     * in this case, the elapsed request to Github is not considered.\n+     * 2. If the data has changed and the source sent 200,\n+     * the data written in the memory and response to the new data.\n+     * @param options {Object}\n+     * @param data {Object} - data from source\n+     * @returns {Promise}\n+     * @private\n+     */\n+    _onSuccess: function (options, data) {\n+        /*\n+          If the option was given promise object,\n+          resolve data without additional action.\n+        */\n+        if (options.resolve || options.isArchive) {\n+            return options.resolve(data);\n+        }\n+\n+        var meta = data.meta,\n+            stData = options.stData,\n+            optionsData = options.optionsData,\n+            cb = options.cb,\n+            result;\n+\n+        if (stData && this._isDataNotChanged(meta)) {\n+            result = stData;\n+        } else {\n+            result = data;\n+            this._logger.info('x-ratelimit-remaining: %s', data.meta['x-ratelimit-remaining']);\n+            this._storage.setData('etag', optionsData, meta.etag);\n+            this._storage.setData('data', optionsData, data);\n+        }\n+\n+        /*\n+          If the passed callback - that is called after data processing.\n+          This is needed for cases when you need to perform custom processing\n+          on the data before the response\n+        */\n+        if (cb && _.isFunction(cb)) {\n+            result = cb.call(this, result);\n+        }\n+\n+        return options.def.resolve(result);\n+    },\n+\n+    /**\n+     * The handler failed receive data from sources\n+     * @param def {Object} - vow.defer() object\n+     * @param err {Object}\n+     * @returns {Promise}\n+     * @private\n+     */\n+    _onError: function (def, err) {\n+        this._logger.error('Error occur: %s', err.message);\n+        return def.reject(err);\n+    },\n+\n+    /**\n+     * If the response status from Github contains code 304,\n+     * then the data has not changed\n+     * @param meta {Object} - meta response data from the data source\n+     * @returns {boolean}\n+     * @private\n+     */\n+    _isDataNotChanged: function (meta) {\n+        return meta.status.indexOf('304') > -1;\n+    },\n+\n+    /**\n+     * Get the list of all labels in the Github repository\n+     * @param req {Object}\n+     * @param token {Number} - user token\n+     * @returns {Promise} - promise with array of labels\n+     */\n+    getLabels: function (req, token) {\n+        var def = vow.defer(),\n+            lang = req.lang,\n+            options = {\n+                type: 'labels',\n+                setRepoStorage: true,\n+                per_page: 100,\n+                lang: lang,\n+                page: 1\n+            },\n+            labels = this._storage.getData('data', options),\n+            eTag = this._storage.getData('etag', options);\n+\n+        options = this._extendEtagHeaders(options, eTag);\n+\n+        /**\n+         * The filtered labels that result in missed service label removed\n+         * Used as a callback for post-processing of the data got from Github\n+         * @param data {Array} - list of labels\n+         * @returns {*}\n+         */\n+        function skipRemovedLabel(data) {\n+            return data.filter(function (label) {\n+                return !label.name || label.name !== 'removed';\n+            });\n+        }\n+\n+        this._github.getLabels(token, options)\n+            .then(this._onSuccess.bind(this, {\n+                def: def,\n+                stData: labels,\n+                optionsData: options,\n+                cb: skipRemovedLabel\n+            }))\n+            .fail(this._onError.bind(this, def));\n+\n+        return def.promise();\n+    },\n+\n+    /**\n+     * Extends object field headers,\n+     * and added 'If-None-Match' if the specified eTag\n+     * @param obj {Object}\n+     * @param eTag {Number}\n+     * @private\n+     */\n+    _extendEtagHeaders: function (obj, eTag) {\n+        return _.extend(obj, { headers: eTag ? { 'If-None-Match': eTag } : {} });\n+    },\n+\n+    /**\n+     * Getting data about the user based on the token passed\n+     * @param req {Object}\n+     * @param token {Number} - user token\n+     * @param name {String} - user name\n+     * @returns {Promise} - promise with object of user data\n+     */\n+    getAuthUser: function (req, token, name) {\n+        var def = vow.defer();\n+\n+        if (!token) {\n+            def.resolve();","path":"server/models/main.js","position":167,"original_position":167,"commit_id":"140365c8c0f7b40c3300ca6b3dfbe785a095f0a4","original_commit_id":"140365c8c0f7b40c3300ca6b3dfbe785a095f0a4","user":{"login":"tavriaforever","id":975906,"avatar_url":"https://avatars.githubusercontent.com/u/975906?v=3","gravatar_id":"","url":"https://api.github.com/users/tavriaforever","html_url":"https://github.com/tavriaforever","followers_url":"https://api.github.com/users/tavriaforever/followers","following_url":"https://api.github.com/users/tavriaforever/following{/other_user}","gists_url":"https://api.github.com/users/tavriaforever/gists{/gist_id}","starred_url":"https://api.github.com/users/tavriaforever/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tavriaforever/subscriptions","organizations_url":"https://api.github.com/users/tavriaforever/orgs","repos_url":"https://api.github.com/users/tavriaforever/repos","events_url":"https://api.github.com/users/tavriaforever/events{/privacy}","received_events_url":"https://api.github.com/users/tavriaforever/received_events","type":"User","site_admin":false},"body":"no no, If we do this - we do not return the right promise","created_at":"2015-05-22T14:13:08Z","updated_at":"2015-05-22T14:13:08Z","html_url":"https://github.com/bem/bem-forum/pull/99#discussion_r30897975","pull_request_url":"https://api.github.com/repos/bem/bem-forum/pulls/99","_links":{"self":{"href":"https://api.github.com/repos/bem/bem-forum/pulls/comments/30897975"},"html":{"href":"https://github.com/bem/bem-forum/pull/99#discussion_r30897975"},"pull_request":{"href":"https://api.github.com/repos/bem/bem-forum/pulls/99"}}},"pull_request":{"url":"https://api.github.com/repos/bem/bem-forum/pulls/99","id":35853060,"html_url":"https://github.com/bem/bem-forum/pull/99","diff_url":"https://github.com/bem/bem-forum/pull/99.diff","patch_url":"https://github.com/bem/bem-forum/pull/99.patch","issue_url":"https://api.github.com/repos/bem/bem-forum/issues/99","number":99,"state":"open","locked":false,"title":"bem-forum v1.0.0","user":{"login":"tavriaforever","id":975906,"avatar_url":"https://avatars.githubusercontent.com/u/975906?v=3","gravatar_id":"","url":"https://api.github.com/users/tavriaforever","html_url":"https://github.com/tavriaforever","followers_url":"https://api.github.com/users/tavriaforever/followers","following_url":"https://api.github.com/users/tavriaforever/following{/other_user}","gists_url":"https://api.github.com/users/tavriaforever/gists{/gist_id}","starred_url":"https://api.github.com/users/tavriaforever/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tavriaforever/subscriptions","organizations_url":"https://api.github.com/users/tavriaforever/orgs","repos_url":"https://api.github.com/users/tavriaforever/repos","events_url":"https://api.github.com/users/tavriaforever/events{/privacy}","received_events_url":"https://api.github.com/users/tavriaforever/received_events","type":"User","site_admin":false},"body":"@tadatuta ","created_at":"2015-05-20T16:52:58Z","updated_at":"2015-05-22T14:13:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bem/bem-forum/pulls/99/commits","review_comments_url":"https://api.github.com/repos/bem/bem-forum/pulls/99/comments","review_comment_url":"https://api.github.com/repos/bem/bem-forum/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bem/bem-forum/issues/99/comments","statuses_url":"https://api.github.com/repos/bem/bem-forum/statuses/140365c8c0f7b40c3300ca6b3dfbe785a095f0a4","head":{"label":"bem:v1","ref":"v1","sha":"140365c8c0f7b40c3300ca6b3dfbe785a095f0a4","user":{"login":"bem","id":223412,"avatar_url":"https://avatars.githubusercontent.com/u/223412?v=3","gravatar_id":"","url":"https://api.github.com/users/bem","html_url":"https://github.com/bem","followers_url":"https://api.github.com/users/bem/followers","following_url":"https://api.github.com/users/bem/following{/other_user}","gists_url":"https://api.github.com/users/bem/gists{/gist_id}","starred_url":"https://api.github.com/users/bem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bem/subscriptions","organizations_url":"https://api.github.com/users/bem/orgs","repos_url":"https://api.github.com/users/bem/repos","events_url":"https://api.github.com/users/bem/events{/privacy}","received_events_url":"https://api.github.com/users/bem/received_events","type":"Organization","site_admin":false},"repo":{"id":14571945,"name":"bem-forum","full_name":"bem/bem-forum","owner":{"login":"bem","id":223412,"avatar_url":"https://avatars.githubusercontent.com/u/223412?v=3","gravatar_id":"","url":"https://api.github.com/users/bem","html_url":"https://github.com/bem","followers_url":"https://api.github.com/users/bem/followers","following_url":"https://api.github.com/users/bem/following{/other_user}","gists_url":"https://api.github.com/users/bem/gists{/gist_id}","starred_url":"https://api.github.com/users/bem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bem/subscriptions","organizations_url":"https://api.github.com/users/bem/orgs","repos_url":"https://api.github.com/users/bem/repos","events_url":"https://api.github.com/users/bem/events{/privacy}","received_events_url":"https://api.github.com/users/bem/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bem/bem-forum","description":"BEM forum on top of github issues","fork":false,"url":"https://api.github.com/repos/bem/bem-forum","forks_url":"https://api.github.com/repos/bem/bem-forum/forks","keys_url":"https://api.github.com/repos/bem/bem-forum/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bem/bem-forum/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bem/bem-forum/teams","hooks_url":"https://api.github.com/repos/bem/bem-forum/hooks","issue_events_url":"https://api.github.com/repos/bem/bem-forum/issues/events{/number}","events_url":"https://api.github.com/repos/bem/bem-forum/events","assignees_url":"https://api.github.com/repos/bem/bem-forum/assignees{/user}","branches_url":"https://api.github.com/repos/bem/bem-forum/branches{/branch}","tags_url":"https://api.github.com/repos/bem/bem-forum/tags","blobs_url":"https://api.github.com/repos/bem/bem-forum/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bem/bem-forum/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bem/bem-forum/git/refs{/sha}","trees_url":"https://api.github.com/repos/bem/bem-forum/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bem/bem-forum/statuses/{sha}","languages_url":"https://api.github.com/repos/bem/bem-forum/languages","stargazers_url":"https://api.github.com/repos/bem/bem-forum/stargazers","contributors_url":"https://api.github.com/repos/bem/bem-forum/contributors","subscribers_url":"https://api.github.com/repos/bem/bem-forum/subscribers","subscription_url":"https://api.github.com/repos/bem/bem-forum/subscription","commits_url":"https://api.github.com/repos/bem/bem-forum/commits{/sha}","git_commits_url":"https://api.github.com/repos/bem/bem-forum/git/commits{/sha}","comments_url":"https://api.github.com/repos/bem/bem-forum/comments{/number}","issue_comment_url":"https://api.github.com/repos/bem/bem-forum/issues/comments{/number}","contents_url":"https://api.github.com/repos/bem/bem-forum/contents/{+path}","compare_url":"https://api.github.com/repos/bem/bem-forum/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bem/bem-forum/merges","archive_url":"https://api.github.com/repos/bem/bem-forum/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bem/bem-forum/downloads","issues_url":"https://api.github.com/repos/bem/bem-forum/issues{/number}","pulls_url":"https://api.github.com/repos/bem/bem-forum/pulls{/number}","milestones_url":"https://api.github.com/repos/bem/bem-forum/milestones{/number}","notifications_url":"https://api.github.com/repos/bem/bem-forum/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bem/bem-forum/labels{/name}","releases_url":"https://api.github.com/repos/bem/bem-forum/releases{/id}","created_at":"2013-11-20T23:08:46Z","updated_at":"2015-04-24T14:19:48Z","pushed_at":"2015-05-20T16:52:59Z","git_url":"git://github.com/bem/bem-forum.git","ssh_url":"git@github.com:bem/bem-forum.git","clone_url":"https://github.com/bem/bem-forum.git","svn_url":"https://github.com/bem/bem-forum","homepage":null,"size":2790,"stargazers_count":14,"watchers_count":14,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":17,"forks":4,"open_issues":17,"watchers":14,"default_branch":"master"}},"base":{"label":"bem:master","ref":"master","sha":"63e6a00eb81fb5e742234a90a1bb860f37ebcf2e","user":{"login":"bem","id":223412,"avatar_url":"https://avatars.githubusercontent.com/u/223412?v=3","gravatar_id":"","url":"https://api.github.com/users/bem","html_url":"https://github.com/bem","followers_url":"https://api.github.com/users/bem/followers","following_url":"https://api.github.com/users/bem/following{/other_user}","gists_url":"https://api.github.com/users/bem/gists{/gist_id}","starred_url":"https://api.github.com/users/bem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bem/subscriptions","organizations_url":"https://api.github.com/users/bem/orgs","repos_url":"https://api.github.com/users/bem/repos","events_url":"https://api.github.com/users/bem/events{/privacy}","received_events_url":"https://api.github.com/users/bem/received_events","type":"Organization","site_admin":false},"repo":{"id":14571945,"name":"bem-forum","full_name":"bem/bem-forum","owner":{"login":"bem","id":223412,"avatar_url":"https://avatars.githubusercontent.com/u/223412?v=3","gravatar_id":"","url":"https://api.github.com/users/bem","html_url":"https://github.com/bem","followers_url":"https://api.github.com/users/bem/followers","following_url":"https://api.github.com/users/bem/following{/other_user}","gists_url":"https://api.github.com/users/bem/gists{/gist_id}","starred_url":"https://api.github.com/users/bem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bem/subscriptions","organizations_url":"https://api.github.com/users/bem/orgs","repos_url":"https://api.github.com/users/bem/repos","events_url":"https://api.github.com/users/bem/events{/privacy}","received_events_url":"https://api.github.com/users/bem/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bem/bem-forum","description":"BEM forum on top of github issues","fork":false,"url":"https://api.github.com/repos/bem/bem-forum","forks_url":"https://api.github.com/repos/bem/bem-forum/forks","keys_url":"https://api.github.com/repos/bem/bem-forum/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bem/bem-forum/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bem/bem-forum/teams","hooks_url":"https://api.github.com/repos/bem/bem-forum/hooks","issue_events_url":"https://api.github.com/repos/bem/bem-forum/issues/events{/number}","events_url":"https://api.github.com/repos/bem/bem-forum/events","assignees_url":"https://api.github.com/repos/bem/bem-forum/assignees{/user}","branches_url":"https://api.github.com/repos/bem/bem-forum/branches{/branch}","tags_url":"https://api.github.com/repos/bem/bem-forum/tags","blobs_url":"https://api.github.com/repos/bem/bem-forum/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bem/bem-forum/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bem/bem-forum/git/refs{/sha}","trees_url":"https://api.github.com/repos/bem/bem-forum/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bem/bem-forum/statuses/{sha}","languages_url":"https://api.github.com/repos/bem/bem-forum/languages","stargazers_url":"https://api.github.com/repos/bem/bem-forum/stargazers","contributors_url":"https://api.github.com/repos/bem/bem-forum/contributors","subscribers_url":"https://api.github.com/repos/bem/bem-forum/subscribers","subscription_url":"https://api.github.com/repos/bem/bem-forum/subscription","commits_url":"https://api.github.com/repos/bem/bem-forum/commits{/sha}","git_commits_url":"https://api.github.com/repos/bem/bem-forum/git/commits{/sha}","comments_url":"https://api.github.com/repos/bem/bem-forum/comments{/number}","issue_comment_url":"https://api.github.com/repos/bem/bem-forum/issues/comments{/number}","contents_url":"https://api.github.com/repos/bem/bem-forum/contents/{+path}","compare_url":"https://api.github.com/repos/bem/bem-forum/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bem/bem-forum/merges","archive_url":"https://api.github.com/repos/bem/bem-forum/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bem/bem-forum/downloads","issues_url":"https://api.github.com/repos/bem/bem-forum/issues{/number}","pulls_url":"https://api.github.com/repos/bem/bem-forum/pulls{/number}","milestones_url":"https://api.github.com/repos/bem/bem-forum/milestones{/number}","notifications_url":"https://api.github.com/repos/bem/bem-forum/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bem/bem-forum/labels{/name}","releases_url":"https://api.github.com/repos/bem/bem-forum/releases{/id}","created_at":"2013-11-20T23:08:46Z","updated_at":"2015-04-24T14:19:48Z","pushed_at":"2015-05-20T16:52:59Z","git_url":"git://github.com/bem/bem-forum.git","ssh_url":"git@github.com:bem/bem-forum.git","clone_url":"https://github.com/bem/bem-forum.git","svn_url":"https://github.com/bem/bem-forum","homepage":null,"size":2790,"stargazers_count":14,"watchers_count":14,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":17,"forks":4,"open_issues":17,"watchers":14,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bem/bem-forum/pulls/99"},"html":{"href":"https://github.com/bem/bem-forum/pull/99"},"issue":{"href":"https://api.github.com/repos/bem/bem-forum/issues/99"},"comments":{"href":"https://api.github.com/repos/bem/bem-forum/issues/99/comments"},"review_comments":{"href":"https://api.github.com/repos/bem/bem-forum/pulls/99/comments"},"review_comment":{"href":"https://api.github.com/repos/bem/bem-forum/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bem/bem-forum/pulls/99/commits"},"statuses":{"href":"https://api.github.com/repos/bem/bem-forum/statuses/140365c8c0f7b40c3300ca6b3dfbe785a095f0a4"}}}}