{"action":"created","comment":{"url":"https://api.github.com/repos/sulu-io/sulu/pulls/comments/32395184","id":32395184,"diff_hunk":"@@ -0,0 +1,146 @@\n+<?php\n+/*\n+ * This file is part of the Sulu CMS.\n+ *\n+ * (c) MASSIVE ART WebServices GmbH\n+ *\n+ * This source file is subject to the MIT license that is bundled\n+ * with this source code in the file LICENSE.\n+ */\n+\n+namespace Sulu\\Bundle\\ContentBundle\\Collaboration;\n+\n+use Ratchet\\ConnectionInterface;\n+use Psr\\Log\\LoggerInterface;\n+use Sulu\\Component\\Websocket\\Exception\\MissingParameterException;\n+use Sulu\\Component\\Websocket\\MessageDispatcher\\MessageHandlerContext;\n+use Sulu\\Component\\Websocket\\MessageDispatcher\\MessageHandlerException;\n+use Sulu\\Component\\Websocket\\MessageDispatcher\\MessageHandlerInterface;\n+\n+/**\n+ * Handles messages for collaboration.\n+ *\n+ * @example {cmd: enter, webspaceKey: sulu_io, user: 1, content: 123-123-123}\n+ *\n+ * The example calls the enter action and passes the current page and the user id \n+ * as message parameter\n+ */\n+class CollaborationMessageHandler implements MessageHandlerInterface\n+{\n+    /**\n+     * @var LoggerInterface\n+     */\n+    private $logger;\n+\n+    /**\n+     * An array which contains all the users for a certain identifier (representing a page, product, ...)\n+     * @var array\n+     */\n+    private $users = array();\n+\n+    public function __construct(\n+        LoggerInterface $logger\n+    ) {\n+        $this->logger = $logger;\n+    }\n+\n+    /**\n+     * {@inheritdoc}\n+     */\n+    public function handle(ConnectionInterface $conn, array $message, MessageHandlerContext $context)\n+    {\n+        try {\n+            return $this->execute($conn, $context, $message);\n+        } catch (\\Exception $ex) {\n+            // TODO Christian Bader: create exception class\n+            throw new MessageHandlerException($ex);\n+        }\n+    }\n+\n+    /**\n+     * Executes command.\n+     *\n+     * @param ConnectionInterface $conn\n+     * @param MessageHandlerContext $context\n+     * @param array $msg\n+     *\n+     * @return mixed|null\n+     *\n+     * @throws MissingParameterException\n+     */\n+    private function execute(ConnectionInterface $conn, MessageHandlerContext $context, $msg)\n+    {\n+        if (!array_key_exists('command', $msg)) {\n+            throw new MissingParameterException('command');\n+        }\n+        $command = $msg['command'];\n+        $result = null;\n+\n+        switch ($command) {\n+            case 'enter':\n+                $result = $this->enter($conn, $context, $msg);\n+                break;\n+            case 'leave':\n+                $result = $this->leave($conn, $context, $msg);\n+                break;\n+            default:\n+                throw new \\InvalidArgumentException(sprintf('Command \"%s\" not known', $command));\n+                break;\n+        }\n+\n+        return $result;\n+    }\n+\n+    /**\n+     * Called when the user has entered the page.\n+     *\n+     * @param ConnectionInterface $conn\n+     * @param MessageHandlerContext $context\n+     * @param array $msg\n+     *\n+     * @return array\n+     */\n+    private function enter(ConnectionInterface $conn, MessageHandlerContext $context, $msg)\n+    {\n+        // TODO check msg keys\n+        $this->addUser($msg['type'] . $msg['id'], $msg['userId']);\n+\n+        return array(\n+            'type' => $msg['type'],\n+            'id' => $msg['id'],\n+            'users' => $this->users[$msg['type'] . $msg['id']]\n+        );\n+    }\n+\n+    /**\n+     * Called when the user has left the page.\n+     *\n+     * @param ConnectionInterface $conn\n+     * @param MessageHandlerContext $context\n+     * @param $msg\n+     *\n+     * @return array\n+     */\n+    private function leave(ConnectionInterface $conn, MessageHandlerContext $context, $msg)\n+    {\n+        $this->removeUser($msg['type'] . $msg['id'], $msg['userId']);\n+    }\n+\n+    private function addUser($id, $userId)\n+    {\n+        if (!array_key_exists($id, $this->users)) {\n+            $this->users[$id] = array();\n+        }\n+\n+        if (!in_array(array('id' => $userId), $this->users[$id])) {\n+            $this->users[$id][] = array('id' => $userId);","path":"src/Sulu/Bundle/ContentBundle/Collaboration/CollaborationMessageHandler.php","position":136,"original_position":136,"commit_id":"d45ed2bb62b92bd6a9c088ad80ada882cc8d33e0","original_commit_id":"d45ed2bb62b92bd6a9c088ad80ada882cc8d33e0","user":{"login":"wachterjohannes","id":1464615,"avatar_url":"https://avatars.githubusercontent.com/u/1464615?v=3","gravatar_id":"","url":"https://api.github.com/users/wachterjohannes","html_url":"https://github.com/wachterjohannes","followers_url":"https://api.github.com/users/wachterjohannes/followers","following_url":"https://api.github.com/users/wachterjohannes/following{/other_user}","gists_url":"https://api.github.com/users/wachterjohannes/gists{/gist_id}","starred_url":"https://api.github.com/users/wachterjohannes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wachterjohannes/subscriptions","organizations_url":"https://api.github.com/users/wachterjohannes/orgs","repos_url":"https://api.github.com/users/wachterjohannes/repos","events_url":"https://api.github.com/users/wachterjohannes/events{/privacy}","received_events_url":"https://api.github.com/users/wachterjohannes/received_events","type":"User","site_admin":false},"body":"if you add here the `$conn` then you could send updates to all users with the same `uuid`","created_at":"2015-06-15T06:57:35Z","updated_at":"2015-06-15T06:57:35Z","html_url":"https://github.com/sulu-io/sulu/pull/1233#discussion_r32395184","pull_request_url":"https://api.github.com/repos/sulu-io/sulu/pulls/1233","_links":{"self":{"href":"https://api.github.com/repos/sulu-io/sulu/pulls/comments/32395184"},"html":{"href":"https://github.com/sulu-io/sulu/pull/1233#discussion_r32395184"},"pull_request":{"href":"https://api.github.com/repos/sulu-io/sulu/pulls/1233"}}},"pull_request":{"url":"https://api.github.com/repos/sulu-io/sulu/pulls/1233","id":37662121,"html_url":"https://github.com/sulu-io/sulu/pull/1233","diff_url":"https://github.com/sulu-io/sulu/pull/1233.diff","patch_url":"https://github.com/sulu-io/sulu/pull/1233.patch","issue_url":"https://api.github.com/repos/sulu-io/sulu/issues/1233","number":1233,"state":"open","locked":false,"title":"Showing current collaborators of pages","user":{"login":"danrot","id":405874,"avatar_url":"https://avatars.githubusercontent.com/u/405874?v=3","gravatar_id":"","url":"https://api.github.com/users/danrot","html_url":"https://github.com/danrot","followers_url":"https://api.github.com/users/danrot/followers","following_url":"https://api.github.com/users/danrot/following{/other_user}","gists_url":"https://api.github.com/users/danrot/gists{/gist_id}","starred_url":"https://api.github.com/users/danrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danrot/subscriptions","organizations_url":"https://api.github.com/users/danrot/orgs","repos_url":"https://api.github.com/users/danrot/repos","events_url":"https://api.github.com/users/danrot/events{/privacy}","received_events_url":"https://api.github.com/users/danrot/received_events","type":"User","site_admin":false},"body":"This PR uses websockets to show a list of editors, who are currently editing a page in the edit form.","created_at":"2015-06-15T06:41:44Z","updated_at":"2015-06-15T06:57:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fd1487be545538ac164ab3a762e6d9a0e998e308","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/sulu-io/sulu/pulls/1233/commits","review_comments_url":"https://api.github.com/repos/sulu-io/sulu/pulls/1233/comments","review_comment_url":"https://api.github.com/repos/sulu-io/sulu/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sulu-io/sulu/issues/1233/comments","statuses_url":"https://api.github.com/repos/sulu-io/sulu/statuses/d45ed2bb62b92bd6a9c088ad80ada882cc8d33e0","head":{"label":"sulu-io:feature/collaboration","ref":"feature/collaboration","sha":"d45ed2bb62b92bd6a9c088ad80ada882cc8d33e0","user":{"login":"sulu-io","id":5717006,"avatar_url":"https://avatars.githubusercontent.com/u/5717006?v=3","gravatar_id":"","url":"https://api.github.com/users/sulu-io","html_url":"https://github.com/sulu-io","followers_url":"https://api.github.com/users/sulu-io/followers","following_url":"https://api.github.com/users/sulu-io/following{/other_user}","gists_url":"https://api.github.com/users/sulu-io/gists{/gist_id}","starred_url":"https://api.github.com/users/sulu-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sulu-io/subscriptions","organizations_url":"https://api.github.com/users/sulu-io/orgs","repos_url":"https://api.github.com/users/sulu-io/repos","events_url":"https://api.github.com/users/sulu-io/events{/privacy}","received_events_url":"https://api.github.com/users/sulu-io/received_events","type":"Organization","site_admin":false},"repo":{"id":13678015,"name":"sulu","full_name":"sulu-io/sulu","owner":{"login":"sulu-io","id":5717006,"avatar_url":"https://avatars.githubusercontent.com/u/5717006?v=3","gravatar_id":"","url":"https://api.github.com/users/sulu-io","html_url":"https://github.com/sulu-io","followers_url":"https://api.github.com/users/sulu-io/followers","following_url":"https://api.github.com/users/sulu-io/following{/other_user}","gists_url":"https://api.github.com/users/sulu-io/gists{/gist_id}","starred_url":"https://api.github.com/users/sulu-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sulu-io/subscriptions","organizations_url":"https://api.github.com/users/sulu-io/orgs","repos_url":"https://api.github.com/users/sulu-io/repos","events_url":"https://api.github.com/users/sulu-io/events{/privacy}","received_events_url":"https://api.github.com/users/sulu-io/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/sulu-io/sulu","description":"Sulu 2.0 core bundle & components","fork":false,"url":"https://api.github.com/repos/sulu-io/sulu","forks_url":"https://api.github.com/repos/sulu-io/sulu/forks","keys_url":"https://api.github.com/repos/sulu-io/sulu/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sulu-io/sulu/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sulu-io/sulu/teams","hooks_url":"https://api.github.com/repos/sulu-io/sulu/hooks","issue_events_url":"https://api.github.com/repos/sulu-io/sulu/issues/events{/number}","events_url":"https://api.github.com/repos/sulu-io/sulu/events","assignees_url":"https://api.github.com/repos/sulu-io/sulu/assignees{/user}","branches_url":"https://api.github.com/repos/sulu-io/sulu/branches{/branch}","tags_url":"https://api.github.com/repos/sulu-io/sulu/tags","blobs_url":"https://api.github.com/repos/sulu-io/sulu/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sulu-io/sulu/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sulu-io/sulu/git/refs{/sha}","trees_url":"https://api.github.com/repos/sulu-io/sulu/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sulu-io/sulu/statuses/{sha}","languages_url":"https://api.github.com/repos/sulu-io/sulu/languages","stargazers_url":"https://api.github.com/repos/sulu-io/sulu/stargazers","contributors_url":"https://api.github.com/repos/sulu-io/sulu/contributors","subscribers_url":"https://api.github.com/repos/sulu-io/sulu/subscribers","subscription_url":"https://api.github.com/repos/sulu-io/sulu/subscription","commits_url":"https://api.github.com/repos/sulu-io/sulu/commits{/sha}","git_commits_url":"https://api.github.com/repos/sulu-io/sulu/git/commits{/sha}","comments_url":"https://api.github.com/repos/sulu-io/sulu/comments{/number}","issue_comment_url":"https://api.github.com/repos/sulu-io/sulu/issues/comments{/number}","contents_url":"https://api.github.com/repos/sulu-io/sulu/contents/{+path}","compare_url":"https://api.github.com/repos/sulu-io/sulu/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sulu-io/sulu/merges","archive_url":"https://api.github.com/repos/sulu-io/sulu/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sulu-io/sulu/downloads","issues_url":"https://api.github.com/repos/sulu-io/sulu/issues{/number}","pulls_url":"https://api.github.com/repos/sulu-io/sulu/pulls{/number}","milestones_url":"https://api.github.com/repos/sulu-io/sulu/milestones{/number}","notifications_url":"https://api.github.com/repos/sulu-io/sulu/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sulu-io/sulu/labels{/name}","releases_url":"https://api.github.com/repos/sulu-io/sulu/releases{/id}","created_at":"2013-10-18T12:57:02Z","updated_at":"2015-06-12T08:59:01Z","pushed_at":"2015-06-15T06:44:54Z","git_url":"git://github.com/sulu-io/sulu.git","ssh_url":"git@github.com:sulu-io/sulu.git","clone_url":"https://github.com/sulu-io/sulu.git","svn_url":"https://github.com/sulu-io/sulu","homepage":"www.sulu.io","size":58043,"stargazers_count":39,"watchers_count":39,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":15,"mirror_url":null,"open_issues_count":290,"forks":15,"open_issues":290,"watchers":39,"default_branch":"develop"}},"base":{"label":"sulu-io:develop","ref":"develop","sha":"6b67ac87047d26bb78da135ce6ec41cc5126b78c","user":{"login":"sulu-io","id":5717006,"avatar_url":"https://avatars.githubusercontent.com/u/5717006?v=3","gravatar_id":"","url":"https://api.github.com/users/sulu-io","html_url":"https://github.com/sulu-io","followers_url":"https://api.github.com/users/sulu-io/followers","following_url":"https://api.github.com/users/sulu-io/following{/other_user}","gists_url":"https://api.github.com/users/sulu-io/gists{/gist_id}","starred_url":"https://api.github.com/users/sulu-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sulu-io/subscriptions","organizations_url":"https://api.github.com/users/sulu-io/orgs","repos_url":"https://api.github.com/users/sulu-io/repos","events_url":"https://api.github.com/users/sulu-io/events{/privacy}","received_events_url":"https://api.github.com/users/sulu-io/received_events","type":"Organization","site_admin":false},"repo":{"id":13678015,"name":"sulu","full_name":"sulu-io/sulu","owner":{"login":"sulu-io","id":5717006,"avatar_url":"https://avatars.githubusercontent.com/u/5717006?v=3","gravatar_id":"","url":"https://api.github.com/users/sulu-io","html_url":"https://github.com/sulu-io","followers_url":"https://api.github.com/users/sulu-io/followers","following_url":"https://api.github.com/users/sulu-io/following{/other_user}","gists_url":"https://api.github.com/users/sulu-io/gists{/gist_id}","starred_url":"https://api.github.com/users/sulu-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sulu-io/subscriptions","organizations_url":"https://api.github.com/users/sulu-io/orgs","repos_url":"https://api.github.com/users/sulu-io/repos","events_url":"https://api.github.com/users/sulu-io/events{/privacy}","received_events_url":"https://api.github.com/users/sulu-io/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/sulu-io/sulu","description":"Sulu 2.0 core bundle & components","fork":false,"url":"https://api.github.com/repos/sulu-io/sulu","forks_url":"https://api.github.com/repos/sulu-io/sulu/forks","keys_url":"https://api.github.com/repos/sulu-io/sulu/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sulu-io/sulu/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sulu-io/sulu/teams","hooks_url":"https://api.github.com/repos/sulu-io/sulu/hooks","issue_events_url":"https://api.github.com/repos/sulu-io/sulu/issues/events{/number}","events_url":"https://api.github.com/repos/sulu-io/sulu/events","assignees_url":"https://api.github.com/repos/sulu-io/sulu/assignees{/user}","branches_url":"https://api.github.com/repos/sulu-io/sulu/branches{/branch}","tags_url":"https://api.github.com/repos/sulu-io/sulu/tags","blobs_url":"https://api.github.com/repos/sulu-io/sulu/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sulu-io/sulu/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sulu-io/sulu/git/refs{/sha}","trees_url":"https://api.github.com/repos/sulu-io/sulu/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sulu-io/sulu/statuses/{sha}","languages_url":"https://api.github.com/repos/sulu-io/sulu/languages","stargazers_url":"https://api.github.com/repos/sulu-io/sulu/stargazers","contributors_url":"https://api.github.com/repos/sulu-io/sulu/contributors","subscribers_url":"https://api.github.com/repos/sulu-io/sulu/subscribers","subscription_url":"https://api.github.com/repos/sulu-io/sulu/subscription","commits_url":"https://api.github.com/repos/sulu-io/sulu/commits{/sha}","git_commits_url":"https://api.github.com/repos/sulu-io/sulu/git/commits{/sha}","comments_url":"https://api.github.com/repos/sulu-io/sulu/comments{/number}","issue_comment_url":"https://api.github.com/repos/sulu-io/sulu/issues/comments{/number}","contents_url":"https://api.github.com/repos/sulu-io/sulu/contents/{+path}","compare_url":"https://api.github.com/repos/sulu-io/sulu/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sulu-io/sulu/merges","archive_url":"https://api.github.com/repos/sulu-io/sulu/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sulu-io/sulu/downloads","issues_url":"https://api.github.com/repos/sulu-io/sulu/issues{/number}","pulls_url":"https://api.github.com/repos/sulu-io/sulu/pulls{/number}","milestones_url":"https://api.github.com/repos/sulu-io/sulu/milestones{/number}","notifications_url":"https://api.github.com/repos/sulu-io/sulu/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sulu-io/sulu/labels{/name}","releases_url":"https://api.github.com/repos/sulu-io/sulu/releases{/id}","created_at":"2013-10-18T12:57:02Z","updated_at":"2015-06-12T08:59:01Z","pushed_at":"2015-06-15T06:44:54Z","git_url":"git://github.com/sulu-io/sulu.git","ssh_url":"git@github.com:sulu-io/sulu.git","clone_url":"https://github.com/sulu-io/sulu.git","svn_url":"https://github.com/sulu-io/sulu","homepage":"www.sulu.io","size":58043,"stargazers_count":39,"watchers_count":39,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":15,"mirror_url":null,"open_issues_count":290,"forks":15,"open_issues":290,"watchers":39,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/sulu-io/sulu/pulls/1233"},"html":{"href":"https://github.com/sulu-io/sulu/pull/1233"},"issue":{"href":"https://api.github.com/repos/sulu-io/sulu/issues/1233"},"comments":{"href":"https://api.github.com/repos/sulu-io/sulu/issues/1233/comments"},"review_comments":{"href":"https://api.github.com/repos/sulu-io/sulu/pulls/1233/comments"},"review_comment":{"href":"https://api.github.com/repos/sulu-io/sulu/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sulu-io/sulu/pulls/1233/commits"},"statuses":{"href":"https://api.github.com/repos/sulu-io/sulu/statuses/d45ed2bb62b92bd6a9c088ad80ada882cc8d33e0"}}}}