-
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 13 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,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'); | ||
$company = [ | ||
"id" => $domain | ||
]; | ||
|
||
if ($intercomAppToken) { | ||
$client = new IntercomClient($intercomAppToken, null); | ||
|
||
try { | ||
$client->users->update([ | ||
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. 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 |
---|---|---|
|
@@ -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); | ||
} | ||
} |
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.
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?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.
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.