Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating intercom functionality #1734

Merged
merged 17 commits into from
Jun 27, 2017
Merged
11 changes: 5 additions & 6 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static function init()
});

// Intercom config settings
$di->set('site.intercomAppToken', function() use ($di) {
return Kohana::$config->load('site.intercomAppToken');
$di->set('thirdparty.intercomAppToken', function() use ($di) {
return Kohana::$config->load('thirdparty.intercomAppToken');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest just calling getenv() here rather than using Kohana config

});

// Roles config settings
Expand Down Expand Up @@ -680,18 +680,17 @@ public static function init()
// Add Intercom Listener to Config
$di->setter['Ushahidi_Repository_Config']['setEvent'] = 'ConfigUpdateEvent';
$di->setter['Ushahidi_Repository_Config']['setListener'] =
$di->lazyNew('Ushahidi_Listener_IntercomListener');
$di->lazyNew('Ushahidi_Listener_IntercomCompanyListener');

// Add Intercom Listener to Form
$di->setter['Ushahidi_Repository_Form']['setEvent'] = 'FormUpdateEvent';
$di->setter['Ushahidi_Repository_Form']['setListener'] =
$di->lazyNew('Ushahidi_Listener_IntercomListener');
$di->lazyNew('Ushahidi_Listener_IntercomCompanyListener');

// Add Intercom Listener to User
$di->setter['Ushahidi_Repository_User']['setEvent'] = 'UserGetAllEvent';
$di->setter['Ushahidi_Repository_User']['setListener'] =
$di->lazyNew('Ushahidi_Listener_IntercomListener');

$di->lazyNew('Ushahidi_Listener_IntercomAdminListener');

/**
* 1. Load the plugins
Expand Down
74 changes: 74 additions & 0 deletions application/classes/Ushahidi/Listener/IntercomAdminListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php defined('SYSPATH') or die('No direct script access');

/**
* Ushahidi Intercom Listener
*
* Listens for new posts that are added to a set
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

use League\Event\AbstractListener;
use League\Event\EventInterface;

use Intercom\IntercomClient;

use GuzzleHttp\Exception\ClientException;

class Ushahidi_Listener_IntercomAdminListener extends AbstractListener
{

public function handle(EventInterface $event, $user = null)
{

$config = service('repository.config')->get('thirdparty');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to use the repo here, just service('thirdparty.intercomapptoken')
The repo is only really necessary for things that are use editable

$domain = Kohana::$config->load('site.client_url');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be able to use service('site') here. I think it'll give you domain.ushahidi.io or similar. But relies on HOST being set or an actual HTTP request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will that do in a non .io setup, it seems to be unset if not multisite


$intercomAppToken = $config->intercomAppToken;
$company = [
"id" => $config->intercomCompanyId
];

if ($user && $config->intercomAppToken) {
$client = new IntercomClient($config->intercomAppToken, null);

try {
// Get Company if it already exists, if not create it
if (!$config->intercomCompanyId) {

$site_name = Kohana::$config->load('site.name') ?: 'Ushahidi';
$created = date("Y-m-d H:i:s");

$company = $client->companies->create([
"company_id" => $domain,
"name" => $site_name,
"custom_attributes" => [
"created" => $created
]
]);

$config->intercomCompanyId = $company->id;
service('repository.config')->update($config);
}

$client->users->update([
"email" => $user->email,
"created_at" => $user->created,
"user_id" => $domain . '_' . $user->id,
"realname" => $user->realname,
"last_login" => $user->last_login,
"role" => $user->role,
"language" => $user->language,
"companies" => [
$company
]
]);
} catch(ClientException $e) {
Kohana::$log->add(Log::ERROR, print_r($e,true));
}
}
}
}
65 changes: 65 additions & 0 deletions application/classes/Ushahidi/Listener/IntercomCompanyListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php defined('SYSPATH') or die('No direct script access');

/**
* Ushahidi Intercom Listener
*
* Listens for new posts that are added to a set
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

use League\Event\AbstractListener;
use League\Event\EventInterface;

use Intercom\IntercomClient;

use GuzzleHttp\Exception\ClientException;

class Ushahidi_Listener_IntercomCompanyListener extends AbstractListener
{
protected $config_repo;

public function setConfigRepo(ConfigRepository $config_repo)
{
$this->config_repo = $config_repo;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is never used?

}

public function handle(EventInterface $event, $data = null)
{

$config = service('repository.config')->get('thirdparty');
$company = [
"company_id" => $config->intercomCompanyId
];

Kohana::$log->add(Log::ERROR, print_r($config));

if ($config->intercomAppToken) {

$client = new IntercomClient($config->intercomAppToken, null);

try {
// Create company with current date if it does not already exist
if (!$config->intercomCompanyId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this happen? ie. why wouldn't we have created the company already?

$company->name = Kohana::$config->load('site.name') ?: 'Ushahidi';
$config->intercomCompanyId = Kohana::$config->load('site.client_url');

service('repository.config')->update($config);
$company->company_id = $config->intercomCompanyId;

$data['created'] = date("Y-m-d H:i:s");
}

$company->custom_attributes = $data;
// Update company
$client->companies->create($company);

} catch(ClientException $e) {
Kohana::$log->add(Log::ERROR, print_r($e,true));
}
}
}
}
41 changes: 0 additions & 41 deletions application/classes/Ushahidi/Listener/IntercomListener.php

This file was deleted.

4 changes: 1 addition & 3 deletions application/classes/Ushahidi/Repository/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public function update(Entity $entity)
}

if ($intercom_data) {
$user = service('session.user');
$this->emit($this->event, $user->email, $intercom_data);
$this->emit($this->event, $intercom_data);
}
}

Expand Down Expand Up @@ -160,4 +159,3 @@ public function all(Array $groups = null)
return $result;
}
}

3 changes: 1 addition & 2 deletions application/classes/Ushahidi/Repository/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public function update(Entity $entity)
// If orignal Form update Intercom if Name changed
if ($entity->id === 1) {
foreach ($entity->getChanged() as $key => $val) {
$user = service('session.user');
$key === 'name' ? $this->emit($this->event, $user->email, ['primary_survey_name' => $val]) : null;
$key === 'name' ? $this->emit($this->event, ['primary_survey_name' => $val]) : null;
}
}

Expand Down
26 changes: 15 additions & 11 deletions application/classes/Ushahidi/Repository/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function create(Entity $entity)
'created' => time(),
'password' => $this->hasher->hash($entity->password),
];
return parent::create($entity->setState($state));
$entity->setState($state);
if ($entity->role === 'admin') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this check happens in the listener too. Then the event is reusable for future integrations that might want all users.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any change on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that's changed

$this->updateIntercomAdminUsers($entity);
}
return parent::create($entity);
}

// CreateRepository
Expand All @@ -73,8 +77,12 @@ public function createWithHash(Entity $entity)
$state = [
'created' => time()
];
$entity->setState($state);
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($state);
}

return parent::create($entity->setState($state));
return parent::create($entity);
}

// UpdateRepository
Expand Down Expand Up @@ -139,8 +147,6 @@ public function isUniqueEmail($email)
public function register(Entity $entity)
{

$this->updateIntercomUserCount(1);

return $this->executeInsert([
'realname' => $entity->realname,
'email' => $entity->email,
Expand Down Expand Up @@ -213,7 +219,9 @@ public function getTotalCount(Array $where = [])

// DeleteRepository
public function delete(Entity $entity) {
$this->updateIntercomUserCount(-1);
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($entity);
}
return parent::delete($entity);
}

Expand All @@ -223,12 +231,8 @@ public function delete(Entity $entity) {
* @param Integer $offset
* @return void
*/
protected function updateIntercomUserCount($offset)
protected function updateIntercomAdminUsers($user)
{
$data = [
'total_users' => $this->getTotalCount() + $offset
];
$user = service('session.user');
$this->emit($this->event, $user->email, $data);
$this->emit($this->event, $user);
}
}
2 changes: 0 additions & 2 deletions application/config/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* - string date_format Set format in which to return dates. See http://php.net/manual/en/datetime.createfromformat.php
*/

$intercomAppToken = getenv('INTERCOM_APP_TOKEN');

$clientUrl = getenv('CLIENT_URL');

Expand All @@ -46,5 +45,4 @@
'first_login' => true,
'tier' => 'free',
'private' => false,
'intercomAppToken' => $intercomAppToken,
);
30 changes: 30 additions & 0 deletions application/config/thirdparty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this file at all anymore?


/**
* Kohana Site Config
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application\Config
* @copyright 2013 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

/**
* Site settings
*
* The following options are available:
*
* - string name Display name of the site.
* - string description A brief description of the site.
* - string email Site contact email.
* - string timezone Default timezone for the site. See http://php.net/manual/en/timezones.php
* - string language Native language for the site in ISO 639-1 format. See http://en.wikipedia.org/wiki/ISO_639-1
* - string date_format Set format in which to return dates. See http://php.net/manual/en/datetime.createfromformat.php
*/

$intercomAppToken = getenv('INTERCOM_APP_TOKEN');

return array(
'intercomCompanyId' => null
'intercomAppToken' => $intercomAppToken,
);