More objects added

This commit is contained in:
Michael 2021-11-23 10:12:11 +00:00
parent 8211cef49d
commit b56ccbcf2b
14 changed files with 722 additions and 29 deletions

View file

@ -0,0 +1,98 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Friendica;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Database\DBA;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Psr\Log\LoggerInterface;
use Friendica\Factory\Api\Twitter\User as TwitterUser;
class Activities extends BaseFactory
{
/** @var BaseURL */
private $baseUrl;
/** @var twitterUser entity */
private $twitterUser;
public function __construct(LoggerInterface $logger, BaseURL $baseURL, TwitterUser $twitteruser)
{
parent::__construct($logger);
$this->twitterUser = $twitteruser;
$this->baseUrl = $baseURL;
}
/**
* @param int $uriId Uri-ID of the item
* @return Array
* @throws HTTPException\InternalServerErrorException
*/
public function createFromUriId(int $uriId, int $uid): Array
{
$activities = [
'like' => [],
'dislike' => [],
'attendyes' => [],
'attendno' => [],
'attendmaybe' => [],
'announce' => [],
];
$condition = ['uid' => $uid, 'thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY];
$ret = Post::selectForUser($uid, ['author-id', 'verb'], $condition);
while ($parent_item = Post::fetch($ret)) {
// get user data and add it to the array of the activity
$user = $this->twitterUser->createFromContactId($parent_item['author-id'], $uid)->toArray();
switch ($parent_item['verb']) {
case Activity::LIKE:
$activities['like'][] = $user;
break;
case Activity::DISLIKE:
$activities['dislike'][] = $user;
break;
case Activity::ATTEND:
$activities['attendyes'][] = $user;
break;
case Activity::ATTENDNO:
$activities['attendno'][] = $user;
break;
case Activity::ATTENDMAYBE:
$activities['attendmaybe'][] = $user;
break;
case Activity::ANNOUNCE:
$activities['announce'][] = $user;
break;
default:
break;
}
}
DBA::close($ret);
return $activities;
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Twitter;
use Friendica\BaseFactory;
use Friendica\Network\HTTPException;
use Friendica\Model\Tag;
use Psr\Log\LoggerInterface;
class Hashtag extends BaseFactory
{
public function __construct(LoggerInterface $logger)
{
parent::__construct($logger);
}
/**
* @param int $uriId Uri-ID of the attachments
* @return array
* @throws HTTPException\InternalServerErrorException
*/
public function createFromUriId(int $uriId, string $text): array
{
$hashtags = [];
foreach (Tag::getByURIId($uriId, [Tag::HASHTAG]) as $tag) {
$indices = [];
$object = new \Friendica\Object\Api\Twitter\Hashtag($tag['name'], $indices);
$hashtags[] = $object->toArray();
}
return $hashtags;
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Twitter;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Network\HTTPException;
use Friendica\Model\Post;
use Psr\Log\LoggerInterface;
class Media extends BaseFactory
{
/** @var BaseURL */
private $baseUrl;
public function __construct(LoggerInterface $logger, BaseURL $baseURL)
{
parent::__construct($logger);
$this->baseUrl = $baseURL;
}
/**
* @param int $uriId Uri-ID of the attachments
* @return array
* @throws HTTPException\InternalServerErrorException
*/
public function createFromUriId(int $uriId, string $text): array
{
$attachments = [];
foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
if ($attachment['type'] == Post\Media::IMAGE) {
$url = Post\Media::getUrlForId($attachment['id']);
} elseif (!empty($attachment['preview'])) {
$url = Post\Media::getPreviewUrlForId($attachment['id']);
} else {
$url = $attachment['url'];
}
$indices = [];
$object = new \Friendica\Object\Api\Twitter\Media($attachment, $url, $indices);
$attachments[] = $object->toArray();
}
return $attachments;
}
}

View file

@ -0,0 +1,61 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Twitter;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Mentions;
use Friendica\Model\Contact;
use Friendica\Model\Tag;
use Friendica\Network\HTTPException;
use Psr\Log\LoggerInterface;
class Mention extends BaseFactory
{
/** @var BaseURL */
private $baseUrl;
public function __construct(LoggerInterface $logger, BaseURL $baseURL)
{
parent::__construct($logger);
$this->baseUrl = $baseURL;
}
/**
* @param int $uriId Uri-ID of the item
* @return Array
* @throws HTTPException\InternalServerErrorException
*/
public function createFromUriId(int $uriId): Array
{
$mentions = [];
$tags = Tag::getByURIId($uriId, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]);
foreach ($tags as $tag) {
$indices = [];
$contact = Contact::getByURL($tag['url'], false);
$object = new \Friendica\Object\Api\Twitter\Mention($tag, $contact, $indices);
$mentions[] = $object->toArray();
}
return $mentions;
}
}

View file

@ -23,8 +23,13 @@ namespace Friendica\Factory\Api\Twitter;
use Friendica\BaseFactory;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Database\Database;
use Friendica\Factory\Api\Friendica\Activities;
use Friendica\Factory\Api\Twitter\User as TwitterUser;
use Friendica\Factory\Api\Twitter\Hashtag;
use Friendica\Factory\Api\Twitter\Mention;
use Friendica\Factory\Api\Twitter\Url;
use Friendica\Model\Post;
use Friendica\Model\Verb;
use Friendica\Network\HTTPException;
@ -36,14 +41,29 @@ class Status extends BaseFactory
{
/** @var Database */
private $dba;
/** @var TwitterUser */
/** @var twitterUser entity */
private $twitterUser;
/** @var Hashtag entity */
private $hashtag;
/** @var Media entity */
private $media;
/** @var Url entity */
private $url;
/** @var Mention entity */
private $mention;
/** @var Activities entity */
private $activities;
public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser)
public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities)
{
parent::__construct($logger);
$this->dba = $dba;
$this->twitterUser = $twitteruser;
$this->hashtag = $hashtag;
$this->media = $media;
$this->url = $url;
$this->mention = $mention;
$this->activities = $activities;
}
/**
@ -57,7 +77,7 @@ class Status extends BaseFactory
public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Twitter\Status
{
$fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord'];
$item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
if (!$item) {
throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
@ -68,26 +88,38 @@ class Status extends BaseFactory
$friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]);
$text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0));
$geo = [];
//$mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
//$tags = $this->mstdnTagFactory->createFromUriId($uriId);
//$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
$entities = [];
$attachments = [];
$friendica_activities = [];
if ($item['coord'] != '') {
$coords = explode(' ', $item["coord"]);
if (count($coords) == 2) {
$geo = [
'type' => 'Point',
'coordinates' => [(float) $coords[0], (float) $coords[1]]
];
}
}
$hashtags = $this->hashtag->createFromUriId($uriId, $text);
$medias = $this->media->createFromUriId($uriId, $text);
$urls = $this->url->createFromUriId($uriId, $text);
$mentions = $this->mention->createFromUriId($uriId, $text);
$friendica_activities = $this->activities->createFromUriId($uriId, $uid);
$shared = BBCode::fetchShareAttributes($item['body']);
if (!empty($shared['guid'])) {
//$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
//$shared_uri_id = $shared_item['uri-id'] ?? 0;
$shared_uri_id = $shared_item['uri-id'] ?? 0;
$hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
$medias = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text));
$urls = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text));
$mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text));
//$mentions = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
//$tags = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
//$attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
$entities = [];
$attachments = [];
$friendica_activities = [];
}
@ -101,8 +133,10 @@ class Status extends BaseFactory
$retweeted = [];
}
$quoted = [];
$quoted = []; // @todo
return new \Friendica\Object\Api\Twitter\Status($item, $author, $owner, $retweeted, $quoted, $attachments, $geo, $friendica_activities, $entities, $friendica_comments);
$entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions];
return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $friendica_comments);
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Twitter;
use Friendica\BaseFactory;
use Friendica\Network\HTTPException;
use Friendica\Model\Post;
use Psr\Log\LoggerInterface;
class Url extends BaseFactory
{
public function __construct(LoggerInterface $logger)
{
parent::__construct($logger);
}
/**
* @param int $uriId Uri-ID of the attachments
* @return array
* @throws HTTPException\InternalServerErrorException
*/
public function createFromUriId(int $uriId): array
{
$attachments = [];
foreach (Post\Media::getByURIId($uriId, [Post\Media::HTML, Post\Media::PLAIN, Post\Media::TEXT]) as $attachment) {
$indices = [];
$object = new \Friendica\Object\Api\Twitter\Url($attachment, $indices);
$attachments[] = $object->toArray();
}
return $attachments;
}
}

View file

@ -50,7 +50,9 @@ class User extends BaseFactory
$apcontact = APContact::getByURL($publicContact['url'], false);
return new \Friendica\Object\Api\Twitter\User($publicContact, $apcontact, $userContact, $skip_status, $include_user_entities);
$status = null; // @todo fetch last status
return new \Friendica\Object\Api\Twitter\User($publicContact, $apcontact, $userContact, $status, $include_user_entities);
}
public function createFromUserId(int $uid, $skip_status = false, $include_user_entities = true)