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
63 changes: 63 additions & 0 deletions application/classes/Ushahidi/Listener/IntercomAdminListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 = [
"company_id" => $domain
];

if ($intercomAppToken && !empty($domain)) {

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

try {
$intercom_user = [
"email" => $user->email,
"created_at" => $user->created,
"user_id" => $domain . '_' . $user->id,
"name" => $user->realname,
"companies" => [
$company
],
"custom_attributes" => [
"last_login" => $user->last_login,
"logins" => $user->logins,
"role" => $user->role,
"language" => $user->language,
]
];

$client->users->update($intercom_user);

} catch(ClientException $e) {
$message = $e->getMessage();
Kohana::$log->add(Log::ERROR, print_r($message,true));
}
}
}
}
}
47 changes: 47 additions & 0 deletions application/classes/Ushahidi/Listener/IntercomCompanyListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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
{

public function handle(EventInterface $event, $data = null)
{
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
$domain = service('site');

if ($intercomAppToken && !empty($domain)) {

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


$company = [
"company_id" => $domain,
"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 @@ -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
33 changes: 21 additions & 12 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($entity);
}

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

// UpdateRepository
Expand All @@ -88,7 +96,12 @@ public function update(Entity $entity)
$state['password'] = $this->hasher->hash($entity->password);
}

return parent::update($entity->setState($state));
$entity->setState($state);
if ($entity->role === 'admin') {
$this->updateIntercomAdminUsers($entity);
}

return parent::update($entity);
}

// SearchRepository
Expand Down Expand Up @@ -139,8 +152,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 +224,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 +236,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,
);