]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Friendica/Activities.php
Merge pull request #12802 from nupplaphil/feat/system_url_handling
[friendica.git] / src / Factory / Api / Friendica / Activities.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Factory\Api\Friendica;
23
24 use Friendica\BaseFactory;
25 use Friendica\Database\DBA;
26 use Friendica\Model\Item;
27 use Friendica\Model\Post;
28 use Friendica\Network\HTTPException;
29 use Friendica\Protocol\Activity;
30 use Psr\Log\LoggerInterface;
31 use Friendica\Factory\Api\Twitter\User as TwitterUser;
32
33 class Activities extends BaseFactory
34 {
35         /** @var twitterUser entity */
36         private $twitterUser;
37
38         public function __construct(LoggerInterface $logger, TwitterUser $twitteruser)
39         {
40                 parent::__construct($logger);
41
42                 $this->twitterUser = $twitteruser;
43         }
44
45         /**
46          * Creates activities array from URI id, user id
47          *
48          * @param int $uriId Uri-ID of the item
49          * @param int $uid User id
50          * @param string $type Type of returned activities, can be 'json' or 'xml', default: json
51          *
52          * @return array Array of found activities
53          * @throws HTTPException\InternalServerErrorException
54          */
55         public function createFromUriId(int $uriId, int $uid, string $type = 'json'): array
56         {
57                 $activities = [
58                         'like'        => [],
59                         'dislike'     => [],
60                         'attendyes'   => [],
61                         'attendno'    => [],
62                         'attendmaybe' => [],
63                         'announce'    => [],
64                 ];
65
66                 $condition = ['uid' => $uid, 'thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_ACTIVITY];
67
68                 $ret = Post::selectForUser($uid, ['author-id', 'verb'], $condition);
69
70                 while ($parent_item = Post::fetch($ret)) {
71                         // get user data and add it to the array of the activity
72                         $user = $this->twitterUser->createFromContactId($parent_item['author-id'], $uid, true)->toArray();
73                         switch ($parent_item['verb']) {
74                                 case Activity::LIKE:
75                                         $activities['like'][] = $user;
76                                         break;
77
78                                 case Activity::DISLIKE:
79                                         $activities['dislike'][] = $user;
80                                         break;
81
82                                 case Activity::ATTEND:
83                                         $activities['attendyes'][] = $user;
84                                         break;
85
86                                 case Activity::ATTENDNO:
87                                         $activities['attendno'][] = $user;
88                                         break;
89
90                                 case Activity::ATTENDMAYBE:
91                                         $activities['attendmaybe'][] = $user;
92                                         break;
93
94                                 case Activity::ANNOUNCE:
95                                         $activities['announce'][] = $user;
96                                         break;
97
98                                 default:
99                                         $this->logger->warning('Unsupported verb in parent item:', ['parent_item' => $parent_item]);
100                                         break;
101                         }
102                 }
103
104                 DBA::close($ret);
105
106                 if ($type == 'xml') {
107                         $xml_activities = [];
108                         foreach ($activities as $k => $v) {
109                                 // change xml element from "like" to "friendica:like"
110                                 $xml_activities['friendica:' . $k] = $v;
111                                 // add user data into xml output
112                                 $k_user = 0;
113                                 foreach ($v as $user) {
114                                         $xml_activities['friendica:' . $k][$k_user++ . ':user'] = $user;
115                                 }
116                         }
117                         $activities = $xml_activities;
118                 }
119
120                 return $activities;
121         }
122 }