3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Factory\Api\Friendica;
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;
33 class Activities extends BaseFactory
35 /** @var twitterUser entity */
38 public function __construct(LoggerInterface $logger, TwitterUser $twitteruser)
40 parent::__construct($logger);
42 $this->twitterUser = $twitteruser;
46 * Creates activities array from URI id, user id
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
52 * @return array Array of found activities
53 * @throws HTTPException\InternalServerErrorException
55 public function createFromUriId(int $uriId, int $uid, string $type = 'json'): array
66 $condition = ['uid' => $uid, 'thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_ACTIVITY];
68 $ret = Post::selectForUser($uid, ['author-id', 'verb'], $condition);
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']) {
75 $activities['like'][] = $user;
78 case Activity::DISLIKE:
79 $activities['dislike'][] = $user;
82 case Activity::ATTEND:
83 $activities['attendyes'][] = $user;
86 case Activity::ATTENDNO:
87 $activities['attendno'][] = $user;
90 case Activity::ATTENDMAYBE:
91 $activities['attendmaybe'][] = $user;
94 case Activity::ANNOUNCE:
95 $activities['announce'][] = $user;
99 $this->logger->warning('Unsupported verb in parent item:', ['parent_item' => $parent_item]);
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
113 foreach ($v as $user) {
114 $xml_activities['friendica:' . $k][$k_user++ . ':user'] = $user;
117 $activities = $xml_activities;