From bd023401624a485a57e9d3432cab4c702ec8ef3a Mon Sep 17 00:00:00 2001 From: pankraz Date: Fri, 22 Jul 2022 19:46:18 +0200 Subject: [PATCH 1/2] new event APIs --- doc/API-Friendica.md | 22 ++++ src/Module/Api/Friendica/Events/Create.php | 115 +++++++++++++++++++++ src/Module/Api/Friendica/Events/Delete.php | 64 ++++++++++++ static/routes.config.php | 2 + 4 files changed, 203 insertions(+) create mode 100644 src/Module/Api/Friendica/Events/Create.php create mode 100644 src/Module/Api/Friendica/Events/Delete.php diff --git a/doc/API-Friendica.md b/doc/API-Friendica.md index 726accf62a..a80884baeb 100644 --- a/doc/API-Friendica.md +++ b/doc/API-Friendica.md @@ -24,6 +24,28 @@ Returns a list of [Event](help/API-Entities#Event) entities for the current logg - `since_id`: (optional) minimum event id for pagination - `count`: maximum number of items returned, default 20 +### POST api/friendica/event_create + +Create a new event for the current logged in user. + +#### Parameters + +- `id` : (optional) id of event, event will be amended if supplied +- `name` : name of the event (required) +- `startTime` : start of the event (ISO), required +- `endTime` : (optional) end of the event, event is open end, if not supplied +- `desc` : (optional) description of the event +- `place` : (optional) location of the event +- `publish` : (optional) create message for event + +### POST api/friendica/event_delete + +Delete event from calendar (not the message) + +#### Parameters + +- `id` : id of event to be deleted + ### GET api/externalprofile/show Returns a [Contact](help/API-Entities#Contact) entity for the provided profile URL. diff --git a/src/Module/Api/Friendica/Events/Create.php b/src/Module/Api/Friendica/Events/Create.php new file mode 100644 index 0000000000..8a1ebbbf4a --- /dev/null +++ b/src/Module/Api/Friendica/Events/Create.php @@ -0,0 +1,115 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Events; + +use Friendica\Core\Protocol; +use Friendica\Core\Worker; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Event; +use Friendica\Model\Conversation; +use Friendica\Model\Item; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; +use Friendica\Util\DateTimeFormat; +use Friendica\Worker\Delivery; + +/** + * API endpoint: /api/friendica/event_create + */ +class Create extends BaseApi +{ + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // params + $request = $this->getRequest([ + 'id' => 0, //if provided, event will be amended + 'name' => '', //summary of the event + 'desc' => '', //description in BBCode + 'startTime' => '', //starttime, required + 'endTime' => '', //endtime, required if nofinish false + 'place' => '', //location of the event + 'publish' => 0, //publish message + 'allow_cid' => '', //array of allowed person, if access restricted + 'allow_gid' => '', //array of allowed groups, if access restricted + 'deny_cid' => '', //array of denied person, if access restricted + 'deny_gid' => '', //array of denied groups, if access restricted + ], $request); + + // error if no name specified + if (empty($request['name'])) { + throw new HTTPException\BadRequestException('event name not specified'); + } + + // error startDate is not specified + if (empty($request['startTime'])) { + throw new HTTPException\BadRequestException('startDate not specified'); + } + + // nofinish if endTime is not specified + if (empty($request['endTime'])) { + $finish = DBA::NULL_DATETIME; + $nofinish = true; + } else { + $finish = DateTimeFormat::convert($request['endTime'], 'UTC', DI::app()->getTimeZone()); + $nofinish = false; + } + + $start = DateTimeFormat::convert($request['startTime'], 'UTC', DI::app()->getTimeZone()); + + // create event + $event = []; + + $event['id'] = $request['id']; + $event['uid'] = $uid; + $event['type'] = 'event'; + $event['summary'] = $request['name']; + $event['desc'] = $request['desc']; + $event['location'] = $request['place']; + $event['start'] = $start; + $event['finish'] = $finish; + $event['nofinish'] = $nofinish; + + $event['allow_cid'] = $request['allow_cid']; + $event['allow_gid'] = $request['allow_gid']; + $event['deny_cid'] = $request['deny_cid']; + $event['deny_gid'] = $request['deny_gid']; + $event['publish'] = $request['publish']; + + $event_id = Event::store($event); + + if (!empty($request['publish'])) { + $item = ['network' => Protocol::DFRN, 'protocol' => Conversation::PARCEL_DIRECT, 'direction' => Conversation::PUSH]; + $item = Event::getItemArrayForId($event_id, $item); + if (Item::insert($item)) { + Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$item['uri-id'], $uid); + } + } + + $result = ['success' => true, 'event_id' => $event_id, 'event' => $event]; + + $this->response->exit('event_create', ['$result' => $result], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Friendica/Events/Delete.php b/src/Module/Api/Friendica/Events/Delete.php new file mode 100644 index 0000000000..cf23b91ce0 --- /dev/null +++ b/src/Module/Api/Friendica/Events/Delete.php @@ -0,0 +1,64 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Events; + +use Friendica\Database\DBA; +use Friendica\Model\Event; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * API endpoint: /api/friendica/event_delete + */ + + +class Delete extends BaseApi +{ + protected function post(array $request = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + $request = $this->getRequest([ + 'id' => 0 + ], $request); + + // params + + // error if no id specified + if ($request['id'] == 0) { + throw new HTTPException\BadRequestException('id not specified'); + } + + // error message if specified id is not in database + if (!DBA::exists('event', ['uid' => $uid, 'id' => $request['id']])) { + throw new HTTPException\BadRequestException('id not available'); + } + + // delete event + $eventid = $request['id']; + Event::delete($eventid); + + $success = ['id' => $eventid, 'status' => 'deleted']; + $this->response->exit('event_delete', ['$result' => $success], $this->parameters['extension'] ?? null); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 65beac70b2..111a989dfb 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -82,6 +82,8 @@ $apiRoutes = [ '/direct_messages_setseen[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\DirectMessages\Setseen::class, [ R::POST]], '/direct_messages_search[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\DirectMessages\Search ::class, [R::GET ]], '/events[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Events\Index::class, [R::GET ]], + '/event_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Events\Create::class, [ R::POST]], + '/event_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Events\Delete::class, [ R::POST]], '/group_show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Show::class, [R::GET ]], '/group_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Create::class, [ R::POST]], '/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [ R::POST]], From 946db2ab459bd1c8e5d3fff47a6e964cd7a9094e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 24 Jul 2022 05:49:18 -0400 Subject: [PATCH 2/2] Normalize start_time and end_time parameter names in Friendica API event endpoints --- doc/API-Entities.md | 4 +- doc/API-Friendica.md | 8 +++- src/Module/Api/Friendica/Events/Create.php | 50 +++++++++++----------- src/Module/Api/Friendica/Events/Index.php | 34 +++++++-------- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/doc/API-Entities.md b/doc/API-Entities.md index bd84cb7076..23fc2cec48 100644 --- a/doc/API-Entities.md +++ b/doc/API-Entities.md @@ -410,13 +410,13 @@ Ex: Wed May 23 06:01:13 +0000 2007 -startTime +start_time String (UTC YYYY-MM-DD HH:II:SS)) -endTime +end_time String (UTC YYYY-MM-DD HH:II:SS)) Optional (null date is 0001-01-01 00:00:00 diff --git a/doc/API-Friendica.md b/doc/API-Friendica.md index a80884baeb..8460fd4ab6 100644 --- a/doc/API-Friendica.md +++ b/doc/API-Friendica.md @@ -32,11 +32,15 @@ Create a new event for the current logged in user. - `id` : (optional) id of event, event will be amended if supplied - `name` : name of the event (required) -- `startTime` : start of the event (ISO), required -- `endTime` : (optional) end of the event, event is open end, if not supplied +- `start_time` : start of the event (ISO), required +- `end_time` : (optional) end of the event, event is open end, if not supplied - `desc` : (optional) description of the event - `place` : (optional) location of the event - `publish` : (optional) create message for event +- `allow_cid` : (optional) ACL-formatted list of allowed contact ids if private event +- `allow_gid` : (optional) ACL-formatted list of disallowed contact ids if private event +- `deny_cid` : (optional) ACL-formatted list of allowed group ids if private event +- `deny_gid` : (optional) ACL-formatted list of disallowed group ids if private event ### POST api/friendica/event_delete diff --git a/src/Module/Api/Friendica/Events/Create.php b/src/Module/Api/Friendica/Events/Create.php index 8a1ebbbf4a..eb45b4974c 100644 --- a/src/Module/Api/Friendica/Events/Create.php +++ b/src/Module/Api/Friendica/Events/Create.php @@ -45,17 +45,17 @@ class Create extends BaseApi // params $request = $this->getRequest([ - 'id' => 0, //if provided, event will be amended - 'name' => '', //summary of the event - 'desc' => '', //description in BBCode - 'startTime' => '', //starttime, required - 'endTime' => '', //endtime, required if nofinish false - 'place' => '', //location of the event - 'publish' => 0, //publish message - 'allow_cid' => '', //array of allowed person, if access restricted - 'allow_gid' => '', //array of allowed groups, if access restricted - 'deny_cid' => '', //array of denied person, if access restricted - 'deny_gid' => '', //array of denied groups, if access restricted + 'id' => 0, //if provided, event will be amended + 'name' => '', //summary of the event + 'desc' => '', //description in BBCode + 'start_time' => '', //start_time, required + 'end_time' => '', //endtime, required if nofinish false + 'place' => '', //location of the event + 'publish' => 0, //publish message + 'allow_cid' => '', //array of allowed person, if access restricted + 'allow_gid' => '', //array of allowed groups, if access restricted + 'deny_cid' => '', //array of denied person, if access restricted + 'deny_gid' => '', //array of denied groups, if access restricted ], $request); // error if no name specified @@ -64,33 +64,33 @@ class Create extends BaseApi } // error startDate is not specified - if (empty($request['startTime'])) { + if (empty($request['start_time'])) { throw new HTTPException\BadRequestException('startDate not specified'); } - // nofinish if endTime is not specified - if (empty($request['endTime'])) { + // nofinish if end_time is not specified + if (empty($request['end_time'])) { $finish = DBA::NULL_DATETIME; $nofinish = true; } else { - $finish = DateTimeFormat::convert($request['endTime'], 'UTC', DI::app()->getTimeZone()); + $finish = DateTimeFormat::convert($request['end_time'], 'UTC', DI::app()->getTimeZone()); $nofinish = false; } - $start = DateTimeFormat::convert($request['startTime'], 'UTC', DI::app()->getTimeZone()); + $start = DateTimeFormat::convert($request['start_time'], 'UTC', DI::app()->getTimeZone()); // create event $event = []; - $event['id'] = $request['id']; - $event['uid'] = $uid; - $event['type'] = 'event'; - $event['summary'] = $request['name']; - $event['desc'] = $request['desc']; - $event['location'] = $request['place']; - $event['start'] = $start; - $event['finish'] = $finish; - $event['nofinish'] = $nofinish; + $event['id'] = $request['id']; + $event['uid'] = $uid; + $event['type'] = 'event'; + $event['summary'] = $request['name']; + $event['desc'] = $request['desc']; + $event['location'] = $request['place']; + $event['start_time'] = $start; + $event['end_time'] = $finish; + $event['nofinish'] = $nofinish; $event['allow_cid'] = $request['allow_cid']; $event['allow_gid'] = $request['allow_gid']; diff --git a/src/Module/Api/Friendica/Events/Index.php b/src/Module/Api/Friendica/Events/Index.php index 257c9f06f4..3e930f614b 100644 --- a/src/Module/Api/Friendica/Events/Index.php +++ b/src/Module/Api/Friendica/Events/Index.php @@ -49,23 +49,23 @@ class Index extends BaseApi $items = []; foreach ($events as $event) { $items[] = [ - 'id' => intval($event['id']), - 'uid' => intval($event['uid']), - 'cid' => $event['cid'], - 'uri' => $event['uri'], - 'name' => $event['summary'], - 'desc' => BBCode::convertForUriId($event['uri-id'], $event['desc']), - 'startTime' => $event['start'], - 'endTime' => $event['finish'], - 'type' => $event['type'], - 'nofinish' => $event['nofinish'], - 'place' => $event['location'], - 'adjust' => 1, - 'ignore' => $event['ignore'], - 'allow_cid' => $event['allow_cid'], - 'allow_gid' => $event['allow_gid'], - 'deny_cid' => $event['deny_cid'], - 'deny_gid' => $event['deny_gid'] + 'id' => intval($event['id']), + 'uid' => intval($event['uid']), + 'cid' => $event['cid'], + 'uri' => $event['uri'], + 'name' => $event['summary'], + 'desc' => BBCode::convertForUriId($event['uri-id'], $event['desc']), + 'start_time' => $event['start'], + 'end_time' => $event['finish'], + 'type' => $event['type'], + 'nofinish' => $event['nofinish'], + 'place' => $event['location'], + 'adjust' => 1, + 'ignore' => $event['ignore'], + 'allow_cid' => $event['allow_cid'], + 'allow_gid' => $event['allow_gid'], + 'deny_cid' => $event['deny_cid'], + 'deny_gid' => $event['deny_gid'] ]; }