-
Notifications
You must be signed in to change notification settings - Fork 506
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
Changes from 9 commits
085ab4b
a625942
38e52dd
9dd9228
38874f8
d8c0541
3e9de89
ed17b75
9f685f4
56a9f0b
e84bacb
ba99e68
fb4b073
ae741bb
77f74e4
b1749bc
822bdf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to use the repo here, just |
||
$domain = Kohana::$config->load('site.client_url'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might be able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any change on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep that's changed |
||
$this->updateIntercomAdminUsers($entity); | ||
} | ||
return parent::create($entity); | ||
} | ||
|
||
// CreateRepository | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php defined('SYSPATH') OR die('No direct access allowed.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
); |
There was a problem hiding this comment.
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