6 Commits
1.0.0 ... 1.1.1

Author SHA1 Message Date
a76ab77757 v1.1.1 2022-11-30 11:48:55 -03:00
5e4e52e620 No registered mailboxes 2022-11-30 11:46:57 -03:00
f6ac8eae7c Fix: Empty mailbox 2022-11-30 11:46:41 -03:00
e42c9f4d8f Fix: Empty mailbox 2022-11-30 11:32:00 -03:00
db95b12985 v1.1.0 2022-11-30 10:42:48 -03:00
a5d97729dc Changed way to connect to api 2022-11-30 10:40:36 -03:00
14 changed files with 149 additions and 48 deletions

View File

@ -83,16 +83,25 @@ class Mailboxes
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
throw new MissingArgument('mailboxes', 'array', 'mailboxes ids');
}
$output = [
'mailboxes' => $json->mailboxes,
'total' => count($json->mailboxes),
'unregistered' => 0
'unregistered' => [
'total' => 0,
'mailboxes' => []
]
];
foreach ($json->mailboxes as $mailbox_name) {
if ($service->unregister($mailbox_name)) {
$output['unregistered'] ++;
foreach ($json->mailboxes as $mailbox_id) {
$mailbox = $service->getRepository()->fetchById($mailbox_id);
if ($service->unregister($mailbox->getName())) {
$output['unregistered']['total'] ++;
$output['unregistered']['mailboxes'] []= [
'id' => $mailbox->getId(),
'name' => $mailbox->getName(),
'unregistered' => true
];
}
}
return $this->withJson($response, $output);

View File

@ -118,6 +118,7 @@ class Mailboxes extends Base
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
} catch (BlankResult $e) {
$this->getLogger()->error($e);
// It's already unregistered
return true;
}

View File

@ -146,7 +146,7 @@ class Message implements Model
public function doesHaveAttachments(): Message
{
if (!isset($this->getState()['has_attachments'])) {
if (!isset($this->getStates()['has_attachments'])) {
$this->newState('has_attachments');
}
$this->getState('has_attachments')->setValue(true);

View File

@ -0,0 +1,17 @@
<?php
namespace ProVM\Common\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Service\Api as Service;
class Api
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents(), JSON_OBJECT_AS_ARRAY);
return $service->sendRequest($json);
}
}

47
ui/common/Service/Api.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace ProVM\Common\Service;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
class Api
{
public function __construct(ClientInterface $client, RequestFactoryInterface $factory)
{
$this->setClient($client)
->setFactory($factory);
}
protected ClientInterface $client;
protected RequestFactoryInterface $factory;
public function getClient(): ClientInterface
{
return $this->client;
}
public function getFactory(): RequestFactoryInterface
{
return $this->factory;
}
public function setClient(ClientInterface $client): Api
{
$this->client = $client;
return $this;
}
public function setFactory(RequestFactoryInterface $factory): Api
{
$this->factory = $factory;
return $this;
}
public function sendRequest(array $request_data): ResponseInterface
{
$request = $this->getFactory()->createRequest(strtoupper($request_data['method']) ?? 'GET', $request_data['uri']);
if (strtolower($request_data['method']) !== 'get') {
$request->getBody()->write(\Safe\json_encode($request_data['data']));
$request->withHeader('Content-Type', 'application/json');
}
return $this->getClient()->sendRequest($request);
}
}

View File

@ -0,0 +1,4 @@
<?php
use ProVM\Common\Controller\Api;
$app->post('/api', Api::class);

View File

@ -1,4 +1,4 @@
<?php
use ProVM\Common\Controller\Home;
$app->get('[/]', Home::class);
$app->get('[/]', Home::class);

View File

@ -33,8 +33,8 @@
).append(
$('<button></button>').addClass('ui mini circular red icon button').append(
$('<i></i>').addClass('remove icon')
).click(() => {
this.unregister()
).click(e => {
this.unregister($(e.currentTarget))
})
)
} else {
@ -42,7 +42,7 @@
register.append(
$('<button></button>').addClass('ui mini circular icon button').append(
$('<i></i>').addClass('save icon')
).click((e) => {
).click(e => {
this.register($(e.currentTarget))
})
)
@ -72,20 +72,25 @@
})
})
}
unregister() {
unregister(button) {
const uri = '/mailboxes/unregister'
const data = {
mailboxes: [this.id]
}
button.html('').append(
$('<i></i>').addClass('redo loading icon')
)
return Send.delete({
uri,
data
}).then(response => {
response.mailboxes.forEach(mb => {
if (mb.id === this.id && mb.removed) {
response.unregistered.mailboxes.forEach(mb => {
if (mb.id === this.id && mb.unregistered) {
this.id = null
this.registered = false
mailboxes.draw().table()
const tr = button.parent().parent()
tr.html('')
this.draw(tr)
}
})
})

View File

@ -221,7 +221,13 @@
},
messages: () => {
const uri = '/mailbox/{{$mailbox_id}}/messages/valid'
return Send.get(uri).then(response => {
return Send.get(uri).then((response, status, jqXHR) => {
if (parseInt(jqXHR.status/100) !== 2 || jqXHR.status === 204) {
$(this.id.results).html('').append(
this.draw().empty()
)
return
}
if (this.total === null) {
$(this.id.count).html(' (' + response.total + ')')
this.total = response.total
@ -384,6 +390,7 @@
)
},
empty: () => {
$(this.id.count).html(' (0)')
return $('<div></div>').addClass('ui message').html('No messages found.')
}
}
@ -448,4 +455,4 @@
messages.setup()
})
</script>
@endpush
@endpush

View File

@ -14,7 +14,8 @@
const uri = '/mailboxes/registered'
this.draw().loading()
return Send.get(uri).then((response, status, jqXHR) => {
if (parseInt(jqXHR.status / 100) !== 2) {
if (parseInt(jqXHR.status / 100) !== 2 || jqXHR.status === 204) {
this.draw().empty()
return
}
if (jqXHR.status === 200) {
@ -44,7 +45,7 @@
const parent = $(this.div_id)
parent.html('')
if (this.mailboxes.length === 0) {
parent.html('No mailboxes registered.')
this.draw().empty()
return
}
const list = $('<div></div>').addClass('ui list')
@ -54,6 +55,13 @@
)
})
parent.append(list)
},
empty: () => {
const parent = $(this.div_id)
parent.html('')
parent.append(
$('<div></div>').addClass('ui message').html('No mailboxes registered.')
)
}
}
}
@ -63,4 +71,4 @@
mailboxes.get()
})
</script>
@endpush
@endpush

View File

@ -1,38 +1,33 @@
<script type="text/javascript">
const Send = {
base_url: '{{$urls->api}}',
base: function({method, uri, data = null, dataType = 'json', contentType = 'application/json'}) {
const url = this.base_url + '/' + uri.replace(/^\//g, '')
base: function({method, uri, data = null}) {
const request = {
uri: uri.replace(/^\//g, ''),
method
}
const options = {
method,
url,
crossDomain: true,
headers: {
'Authorization': [
'Bearer {{$api_key}}'
]
},
dataType
url: this.base_url,
method: 'post',
contentType: 'application/json'
}
if (method.toLowerCase() !== 'get' && data !== null) {
if (!(typeof data === 'string' || data instanceof String)) {
options['data'] = JSON.stringify(data)
}
options['contentType'] = contentType
request['data'] = data
}
options['data'] = JSON.stringify(request)
return $.ajax(options)
},
get: function(uri, dataType = 'json') {
return this.base({method: 'get', uri, dataType})
get: function(uri) {
return this.base({method: 'get', uri})
},
post: function({uri, data, dataType = 'json', contentType = 'application/json'}) {
return this.base({method: 'post', uri, data, dataType, contentType})
post: function({uri, data}) {
return this.base({method: 'post', uri, data})
},
put: function({uri, data, dataType = 'json', contentType = 'application/json'}) {
return this.base({method: 'put', uri, data, dataType, contentType})
put: function({uri, data}) {
return this.base({method: 'put', uri, data})
},
delete: function({uri, data, dataType = 'json', contentType = 'application/json'}) {
return this.base({method: 'delete', uri, data, dataType, contentType})
delete: function({uri, data}) {
return this.base({method: 'delete', uri, data})
}
}
const _urls = JSON.parse('{!! Safe\json_encode($urls) !!}')

View File

@ -2,7 +2,12 @@
return [
'urls' => function() {
$arr = ['base' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']];
$arr['api'] = 'http://localhost:8080';
$arr['api'] = $_ENV['API_URI'];
return (object) $arr;
}
},
'view_urls' => function() {
$arr = ['base' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']];
$arr['api'] = implode('/', [$arr['base'], 'api']);
return (object) $arr;
},
];

View File

@ -11,5 +11,8 @@ return [
]
]
]);
}
},
Psr\Http\Message\RequestFactoryInterface::class => function(ContainerInterface $container) {
return $container->get(Nyholm\Psr7\Factory\Psr17Factory::class);
},
];

View File

@ -2,13 +2,13 @@
use Psr\Container\ContainerInterface;
return [
\Slim\Views\Blade::class => function(ContainerInterface $container) {
return new \Slim\Views\Blade(
Slim\Views\Blade::class => function(ContainerInterface $container) {
return new Slim\Views\Blade(
$container->get('folders')->views,
$container->get('folders')->cache,
null,
[
'urls' => $container->get('urls'),
'urls' => $container->get('view_urls'),
'api_key' => $container->get('api_key')
]
);