]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Events/Create.php
Issue 12191: We can now follow and unfollow tags via API
[friendica.git] / src / Module / Api / Friendica / Events / Create.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\Events;
23
24 use Friendica\Core\Protocol;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Event;
29 use Friendica\Model\Conversation;
30 use Friendica\Model\Item;
31 use Friendica\Module\BaseApi;
32 use Friendica\Network\HTTPException;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Worker\Delivery;
35
36 /**
37  * API endpoint: /api/friendica/event_create
38  */
39 class Create extends BaseApi
40 {
41         protected function post(array $request = [])
42         {
43                 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
44                 $uid = BaseApi::getCurrentUserID();
45
46                 // params
47                 $request = $this->getRequest([
48                         'id'         => 0, //if provided, event will be amended
49                         'name'       => '', //summary of the event
50                         'desc'       => '', //description in BBCode
51                         'start_time' => '', //start_time, required
52                         'end_time'   => '', //endtime, required if nofinish false
53                         'place'      => '', //location of the event
54                         'publish'    => 0,  //publish message
55                         'allow_cid'  => '', //array of allowed person, if access restricted
56                         'allow_gid'  => '', //array of allowed groups, if access restricted
57                         'deny_cid'   => '', //array of denied person, if access restricted
58                         'deny_gid'   => '', //array of denied groups, if access restricted
59                 ], $request);
60
61                 // error if no name specified
62                 if (empty($request['name'])) {
63                         throw new HTTPException\BadRequestException('event name not specified');
64                 }
65
66                 // error startDate is not specified
67                 if (empty($request['start_time'])) {
68                         throw new HTTPException\BadRequestException('startDate not specified');
69                 }
70
71                 // nofinish if end_time is not specified
72                 if (empty($request['end_time'])) {
73                         $finish   = DBA::NULL_DATETIME;
74                         $nofinish = true;
75                 } else {
76                         $finish   = DateTimeFormat::convert($request['end_time'], 'UTC', DI::app()->getTimeZone());
77                         $nofinish = false;
78                 }
79
80                 $start = DateTimeFormat::convert($request['start_time'], 'UTC', DI::app()->getTimeZone());
81
82                 // create event
83                 $event = [];
84
85                 $event['id']         = $request['id'];
86                 $event['uid']        = $uid;
87                 $event['type']       = 'event';
88                 $event['summary']    = $request['name'];
89                 $event['desc']       = $request['desc'];
90                 $event['location']   = $request['place'];
91                 $event['start']      = $start;
92                 $event['finish']     = $finish;
93                 $event['nofinish']   = $nofinish;
94
95                 $event['allow_cid'] = $request['allow_cid'];
96                 $event['allow_gid'] = $request['allow_gid'];
97                 $event['deny_cid']  = $request['deny_cid'];
98                 $event['deny_gid']  = $request['deny_gid'];
99                 $event['publish']   = $request['publish'];
100
101                 $event_id = Event::store($event);
102
103                 if (!empty($request['publish'])) {
104                         $item = ['network' => Protocol::DFRN, 'protocol' => Conversation::PARCEL_DIRECT, 'direction' => Conversation::PUSH];
105                         $item = Event::getItemArrayForId($event_id, $item);
106                         if (Item::insert($item)) {
107                                 Worker::add(Worker::PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$item['uri-id'], $uid);
108                         }
109                 }
110
111                 $result = ['success' => true, 'event_id' => $event_id, 'event' => $event];
112
113                 $this->response->exit('event_create', ['$result' => $result], $this->parameters['extension'] ?? null);
114         }
115 }