diff --git a/plugins/kolab_chat/config.inc.php.dist b/plugins/kolab_chat/config.inc.php.dist index cc5fe534..d355d83b 100644 --- a/plugins/kolab_chat/config.inc.php.dist +++ b/plugins/kolab_chat/config.inc.php.dist @@ -8,6 +8,7 @@ as in the Kolab server. Thanks to this we can auto-login users. 1. It has to use the same domain, if it's using different we have to use a proxy: Following Apache config worked for me with kolab_chat_url=https://kolab.example.com/mattermost + Note: This should be simpler with Mattermost 5.1 (to be released soon). ProxyPreserveHost Off RewriteEngine On @@ -31,3 +32,7 @@ $config['kolab_chat_url'] = 'https://mattermost.example.com'; // Enables opening chat in a new window (or tab) $config['kolab_chat_extwin'] = false; + +// Default channel to select when opening the chat app. +// Note: This has to be a channel ID. +$config['kolab_chat_channel'] = null; diff --git a/plugins/kolab_chat/drivers/mattermost.php b/plugins/kolab_chat/drivers/mattermost.php index f9bb8b29..a5b572d8 100644 --- a/plugins/kolab_chat/drivers/mattermost.php +++ b/plugins/kolab_chat/drivers/mattermost.php @@ -55,9 +55,13 @@ class kolab_chat_mattermost $url .= '/api/v4/websocket'; } else if ($this->rc->action == 'index' && $this->rc->task == 'kolab-chat') { - if ($channel = rcube_utils::get_input_value('_channel', rcube_utils::INPUT_GET)) { - $team = rcube_utils::get_input_value('_team', rcube_utils::INPUT_GET); + $channel = rcube_utils::get_input_value('_channel', rcube_utils::INPUT_GET); + if (empty($channel)) { + $channel = $this->rc->config->get('kolab_chat_channel'); + } + if ($channel) { + $team = rcube_utils::get_input_value('_team', rcube_utils::INPUT_GET); if (empty($team) && ($_channel = $this->get_channel($channel, true))) { $team = $_channel['team_name']; } @@ -101,6 +105,49 @@ class kolab_chat_mattermost exit; } + /** + * Handler for preferences_list hook. + * Adds options blocks into Chat settings sections in Preferences. + * + * @param array &$p Hook parameters + */ + public function preferences_list(&$p) + { + $no_override = array_flip((array) $this->rc->config->get('dont_override')); + + if (!isset($no_override['kolab_chat_channel'])) { + $field_id = 'rcmfd_kolab_chat_channel'; + $select = new html_select(array('name' => '_kolab_chat_channel', 'id' => $field_id)); + + $select->add('', ''); + if ($channels = $this->get_channels_list()) { + foreach ($channels as $channel) { + if ($channel['name'] && $channel['display_name']) { + $select->add($channel['display_name'], $channel['id']); + } + } + } + + $p['blocks']['main']['options']['kolab_chat_channel'] = array( + 'title' => html::label($field_id, rcube::Q($this->plugin->gettext('defaultchannel'))), + 'content' => $select->show($this->rc->config->get('kolab_chat_channel')), + ); + } + } + + /** + * Handler for preferences_save hook. + * Executed on Chat settings form submit. + * + * @param array &$p Hook parameters + */ + public function preferences_save(&$p) + { + if (isset($_POST['_kolab_chat_channel'])) { + $p['prefs']['kolab_chat_channel'] = rcube_utils::get_input_value('_kolab_chat_channel', rcube_utils::INPUT_POST); + } + } + /** * Returns the Mattermost session token * Note: This works only if the user/pass is the same in Kolab and Mattermost @@ -122,7 +169,7 @@ class kolab_chat_mattermost } try { - $request = $this->get_api_request('GET', '/api/v4/users/me'); + $request = $this->get_api_request('GET', '/users/me'); $request->setHeader('Authorization', "Bearer $token"); $response = $request->send(); @@ -140,7 +187,7 @@ class kolab_chat_mattermost // Request a new session token if (empty($token)) { - $request = $this->get_api_request('POST', '/api/v4/users/login'); + $request = $this->get_api_request('POST', '/users/login'); $request->setBody(json_encode(array( 'login_id' => $user, 'password' => $pass, @@ -198,11 +245,11 @@ class kolab_chat_mattermost */ protected function get_channel($channel_id, $extended = false) { - $channel = $this->api_get('/api/v4/channels/' . urlencode($channel_id)); + $channel = $this->api_get('/channels/' . urlencode($channel_id)); if ($extended && is_array($channel)) { if (!empty($channel['team_id'])) { - $team = $this->api_get('/api/v4/teams/' . urlencode($channel['team_id'])); + $team = $this->api_get('/teams/' . urlencode($channel['team_id'])); } else if ($teams = $this->get_user_teams()) { // if there's no team assigned to the channel, we'll get the user's team @@ -219,6 +266,27 @@ class kolab_chat_mattermost return $channel; } + /** + * Returns the Mattermost channels list for the user + * + * @return array Channels list (id, name, display_name, etc.) + */ + protected function get_channels_list() + { + // FIXME: Should we list only public channels here? + // FIXME: SHould we check all user teams? + + $user_id = $this->get_user_id(); + $teams = $this->get_user_teams(); + + if (!empty($teams) && ($team = $teams[0])) { + // FIXME: Why it does return channels not assigned to the specified team? + return $this->api_get(sprintf('/users/%s/teams/%s/channels', urlencode($user_id), urlencode($team['id']))); + } + + return false; + } + /** * Returns the Mattermost teams of the user * @@ -226,7 +294,7 @@ class kolab_chat_mattermost */ protected function get_user_teams() { - return $this->api_get('/api/v4/users/' . urlencode($this->get_user_id()) . '/teams'); + return $this->api_get('/users/' . urlencode($this->get_user_id()) . '/teams'); } /** @@ -242,7 +310,7 @@ class kolab_chat_mattermost $config = array_merge($defaults, (array) $this->rc->config->get('kolab_chat_http_request')); - return libkolab::http_request($url . $path, $type, $config); + return libkolab::http_request($url . '/api/v4' . $path, $type, $config); } /** diff --git a/plugins/kolab_chat/kolab_chat.php b/plugins/kolab_chat/kolab_chat.php index 03f5e35d..830eaa4e 100644 --- a/plugins/kolab_chat/kolab_chat.php +++ b/plugins/kolab_chat/kolab_chat.php @@ -46,7 +46,6 @@ class kolab_chat extends rcube_plugin return; } - $this->add_texts('localization/'); $this->load_config(); $extwin = $this->rc->config->get('kolab_chat_extwin'); @@ -62,6 +61,8 @@ class kolab_chat extends rcube_plugin $class_name = "kolab_chat_$driver"; $this->driver = new $class_name($this); + $this->add_texts('localization/'); + // Register UI end-points $this->register_task('kolab-chat'); $this->register_action('index', array($this, 'ui')); @@ -106,27 +107,25 @@ class kolab_chat extends rcube_plugin */ function ui() { - if ($this->driver) { - $this->driver->ui(); + $this->driver->ui(); - $url = rcube::JQ($this->driver->url()); + $url = rcube::JQ($this->driver->url()); - if (!empty($_GET['redirect'])) { - echo '' - . '' - . ''; - exit; - } - else { - $this->rc->output->add_script( - "rcmail.addEventListener('init', function() {" - . "rcmail.location_href('$url', rcmail.get_frame_window(rcmail.env.contentframe));" - . "});", - 'foot' - ); + if (!empty($_GET['redirect'])) { + echo '' + . '' + . ''; + exit; + } + else { + $this->rc->output->add_script( + "rcmail.addEventListener('init', function() {" + . "rcmail.location_href('$url', rcmail.get_frame_window(rcmail.env.contentframe));" + . "});", + 'foot' + ); - $this->rc->output->send('kolab_chat.chat'); - } + $this->rc->output->send('kolab_chat.chat'); } } @@ -135,9 +134,7 @@ class kolab_chat extends rcube_plugin */ function action() { - if ($this->driver) { - $this->driver->action(); - } + $this->driver->action(); } /** @@ -193,6 +190,9 @@ class kolab_chat extends rcube_plugin if ($p['current']) { // update env flag in the parent window $this->rc->output->command('parent.set_env', array('kolab_chat_extwin' => (bool) $this->rc->config->get('kolab_chat_extwin'))); + + // Add driver-specific options + $this->driver->preferences_list($p); } return $p; @@ -212,6 +212,9 @@ class kolab_chat extends rcube_plugin $p['prefs'] = array( 'kolab_chat_extwin' => isset($_POST['_kolab_chat_extwin']), ); + + // Add driver-specific options + $this->driver->preferences_save($p); } return $p; diff --git a/plugins/kolab_chat/localization/en_US.inc b/plugins/kolab_chat/localization/en_US.inc index 7447e557..7543f6e0 100644 --- a/plugins/kolab_chat/localization/en_US.inc +++ b/plugins/kolab_chat/localization/en_US.inc @@ -10,6 +10,7 @@ $labels['chat'] = 'Chat'; $labels['showinextwin'] = 'Open chat application in a new window'; +$labels['defaultchannel'] = 'Open chat application on channel'; $labels['directmessage'] = 'A direct message from $u.'; $labels['mentionmessage'] = 'You have been mentioned by $u in $c.'; $labels['openchat'] = 'Open chat';