Replace args call with parameter from router in Module\Group

This commit is contained in:
Hypolite Petovan 2021-11-26 09:48:05 -05:00
parent 3d77fd5e29
commit a26322b4c2
2 changed files with 7 additions and 17 deletions

View file

@ -34,10 +34,8 @@ class Group extends BaseModule
{
public function post()
{
$a = DI::app();
if (DI::mode()->isAjax()) {
self::ajaxPost();
$this->ajaxPost();
}
if (!local_user()) {
@ -80,20 +78,16 @@ class Group extends BaseModule
}
}
public static function ajaxPost()
public function ajaxPost()
{
try {
$a = DI::app();
if (!local_user()) {
throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
}
// POST /group/123/add/123
// POST /group/123/remove/123
// @TODO: Replace with parameter from router
if (DI::args()->getArgc() == 4) {
list($group_id, $command, $contact_id) = array_slice(DI::args()->getArgv(), 1);
if (isset($this->parameters['command'])) {
$group_id = $this->parameters['group'];
$contact_id = $this->parameters['contact'];
if (!Model\Group::exists($group_id, local_user())) {
throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
@ -108,7 +102,7 @@ class Group extends BaseModule
throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
}
switch($command) {
switch($this->parameters['command']) {
case 'add':
if (!Model\Group::addMember($group_id, $contact_id)) {
throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
@ -123,8 +117,6 @@ class Group extends BaseModule
$message = DI::l10n()->t('Contact successfully removed from group.');
break;
default:
throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
}
} else {
throw new \Exception(DI::l10n()->t('Bad request.'), 400);

View file

@ -389,9 +389,7 @@ return [
'/new' => [Module\Group::class, [R::GET, R::POST]],
'/drop/{group:\d+}' => [Module\Group::class, [R::GET, R::POST]],
'/{group:\d+}/{contact:\d+}' => [Module\Group::class, [R::GET, R::POST]],
'/{group:\d+}/add/{contact:\d+}' => [Module\Group::class, [R::GET, R::POST]],
'/{group:\d+}/remove/{contact:\d+}' => [Module\Group::class, [R::GET, R::POST]],
'/{group:\d+}/{command:add|remove}/{contact:\d+}' => [Module\Group::class, [R::GET, R::POST]],
],
'/hashtag' => [Module\Hashtag::class, [R::GET]],
'/help[/{doc:.+}]' => [Module\Help::class, [R::GET]],