Skip to content
This repository has been archived by the owner on Apr 5, 2020. It is now read-only.

Commit

Permalink
Remove RE: and FW: from task title
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Feb 9, 2016
1 parent 7fa25e8 commit e18ff99
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
74 changes: 59 additions & 15 deletions EmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ public function sendEmail($email, $name, $subject, $html, $author)
* @return boolean
*/
public function receiveEmail(array $payload)
{
$result = $this->validate($payload);

if ($result === false) {
return false;
}

list($user, $project) = $result;

return (bool) $this->taskCreation->create(array(
'project_id' => $project['id'],
'title' => $this->getTitle($payload),
'description' => $this->getDescription($payload),
'creator_id' => $user['id'],
));
}

/**
* Validate incoming email
*
* @access public
* @param array $payload
* @return array|boolean
*/
public function validate(array $payload)
{
if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) {
return false;
Expand All @@ -60,41 +85,60 @@ public function receiveEmail(array $payload)
$user = $this->user->getByEmail($payload['sender']);

if (empty($user)) {
$this->logger->debug('Mailgun: ignored => user not found');
$this->logger->info('Mailgun: ignored => user not found');
return false;
}

// The project must have a short name
$project = $this->project->getByIdentifier(Tool::getMailboxHash($payload['recipient']));

if (empty($project)) {
$this->logger->debug('Mailgun: ignored => project not found');
$this->logger->info('Mailgun: ignored => project not found');
return false;
}

// The user must be member of the project
if (! $this->projectPermission->isMember($project['id'], $user['id'])) {
$this->logger->debug('Mailgun: ignored => user is not member of the project');
$this->logger->info('Mailgun: ignored => user is not member of the project');
return false;
}

// Get the Markdown contents
return array($user, $project);
}

/**
* Get task title
*
* @access public
* @param array $payload
* @return string
*/
public function getTitle(array $payload)
{
$title = $payload['subject'];
$title = str_replace('RE: ', '', $title);
$title = str_replace('FW: ', '', $title);

return $title;
}

/**
* Get Markdown content for the task
*
* @access public
* @param array $payload
* @return string
*/
public function getDescription(array $payload)
{
if (! empty($payload['stripped-html'])) {
$htmlConverter = new HtmlConverter(array('strip_tags' => true));
$description = $htmlConverter->convert($payload['stripped-html']);
return $htmlConverter->convert($payload['stripped-html']);
} elseif (! empty($payload['stripped-text'])) {
$description = $payload['stripped-text'];
} else {
$description = '';
return $payload['stripped-text'];
}

// Finally, we create the task
return (bool) $this->taskCreation->create(array(
'project_id' => $project['id'],
'title' => $payload['subject'],
'description' => $description,
'creator_id' => $user['id'],
));
return '';
}

/**
Expand Down
16 changes: 16 additions & 0 deletions Test/EmailHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,20 @@ public function testHandlePayload()
$this->assertEquals('**boo**', $task['description']);
$this->assertEquals(2, $task['creator_id']);
}

public function testGetSubject()
{
$handler = new EmailHandler($this->container);
$this->assertEquals('Test', $handler->getTitle(array('subject' => 'Test')));
$this->assertEquals('Test', $handler->getTitle(array('subject' => 'RE: Test')));
$this->assertEquals('Test', $handler->getTitle(array('subject' => 'FW: Test')));
}

public function testGetDescription()
{
$handler = new EmailHandler($this->container);
$this->assertEquals('**Test**', $handler->getDescription(array('stripped-html' => '<b>Test</b>')));
$this->assertEquals('foobar', $handler->getDescription(array('stripped-html' => '', 'stripped-text' => 'foobar')));
$this->assertEquals('', $handler->getDescription(array('stripped-html' => '', 'stripped-text' => '')));
}
}

0 comments on commit e18ff99

Please sign in to comment.