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 getenv('INTERCOM_APP_TOKEN');
});

// Roles config settings
Expand Down Expand Up @@ -690,18 +690,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
55 changes: 55 additions & 0 deletions application/classes/Ushahidi/Listener/IntercomAdminListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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)
{
if($user && $user->role === 'admin') {
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
$domain = service('site');
Copy link
Contributor

Choose a reason for hiding this comment

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

This should return quakemap.api.ushahidi.io or just an empty string if we're not on an ushahidi.io deployment ... was that what you expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this is intended to track .io I think that's ok, I'll test it with a blank string to make that it behaves as expected.

$company = [
"id" => $domain
];

if ($intercomAppToken) {
$client = new IntercomClient($intercomAppToken, null);

try {
$client->users->update([
Copy link
Contributor

Choose a reason for hiding this comment

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

indenting is weird here?

"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));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@

use GuzzleHttp\Exception\ClientException;

class Ushahidi_Listener_IntercomListener extends AbstractListener
class Ushahidi_Listener_IntercomCompanyListener extends AbstractListener
{
public function handle(EventInterface $event, $user_email = null, $data = null)
protected $config_repo;

public function setConfigRepo(ConfigRepository $config_repo)
{
$intercomAppToken = service('site.intercomAppToken');
$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?

}

if ($user_email && $intercomAppToken) {
public function handle(EventInterface $event, $data = null)
{
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
if ($intercomAppToken) {

$client = new IntercomClient($intercomAppToken, null);

$client = new IntercomClient($intercomAppToken, null);
try {
$company = [
"company_id" => service('site')
];

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

$client->users->update([
"email" => $user_email,
"custom_attributes" => $data
]);
} catch(ClientException $e) {
Kohana::$log->add(Log::ERROR, print_r($e,true));
}
Expand Down
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 @@ -79,8 +79,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;
}
}
$form = $entity->getChanged();
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,
);