*
* @param integer $UriId
* @param integer $uid
+ * @param bool $add_media
* @return string
*/
- public function createSharedPostByUriId(int $UriId, int $uid = 0): string
+ public function createSharedPostByUriId(int $UriId, int $uid = 0, bool $add_media = false): string
{
$fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
$shared_item = Post::selectFirst($fields, ['uri-id' => $UriId, 'uid' => [$uid, 0], 'private' => [ModelItem::PUBLIC, ModelItem::UNLISTED]]);
return '';
}
- return $this->createSharedBlockByArray($shared_item);
+ return $this->createSharedBlockByArray($shared_item, $add_media);
}
/**
*
* @param string $guid
* @param integer $uid
+ * @param bool $add_media
* @return string
*/
- public function createSharedPostByGuid(string $guid, int $uid = 0, string $host = ''): string
+ public function createSharedPostByGuid(string $guid, int $uid = 0, string $host = '', bool $add_media = false): string
{
$fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
$shared_item = Post::selectFirst($fields, ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [ModelItem::PUBLIC, ModelItem::UNLISTED]]);
return '';
}
- return $this->createSharedBlockByArray($shared_item);
+ return $this->createSharedBlockByArray($shared_item, $add_media);
}
/**
// If it is a reshared post then reformat it to avoid display problems with two share elements
if (Diaspora::isReshare($item['body'], false)) {
- if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid']))) {
+ if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid'], 0, '', $add_media))) {
$item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
}
$body = $item['body'] ?? '';
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
- $shared_item = Post::selectFirst(['uri-id', 'plink', 'has-media'], ['guid' => $shared['guid']]);
- $shared_uri_id = $shared_item['uri-id'] ?? 0;
- $shared_links = [strtolower($shared_item['plink'] ?? '')];
- $shared_attachments = Post\Media::splitAttachments($shared_uri_id, $shared['guid'], [], $shared_item['has-media'] ?? false);
+ $shared_item = Post::selectFirst(['uri-id', 'guid', 'plink', 'has-media'], ['guid' => $shared['guid'], 'uid' => [$item['uid'], 0]]);
+ }
+
+ $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media'];
+
+ $shared_uri_id = 0;
+ $shared_links = [];
+
+ if (empty($shared_item['uri-id']) && !empty($item['quote-uri-id'])) {
+ $shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]);
+ $quote_uri_id = $item['quote-uri-id'] ?? 0;
+ $shared_links[] = strtolower($item['quote-uri']);
+ } elseif (empty($shared_item['uri-id']) && empty($item['quote-uri-id'])) {
+ $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::ACTIVITY]);
+ if (!empty($media)) {
+ $shared_item = Post::selectFirst($fields, ['plink' => $media[0]['url'], 'uid' => [$item['uid'], 0]]);
+
+ if (empty($shared_item['uri-id'])) {
+ $shared_item = Post::selectFirst($fields, ['uri' => $media[0]['url'], 'uid' => [$item['uid'], 0]]);
+ $shared_links[] = strtolower($media[0]['url']);
+ }
+
+ $quote_uri_id = $shared_item['uri-id'] ?? 0;
+ }
+ }
+
+ if (!empty($quote_uri_id)) {
+ $item['body'] .= "\n" . DI::contentItem()->createSharedBlockByArray($shared_item);
+ }
+
+ if (!empty($shared_item['uri-id'])) {
+ $shared_uri_id = $shared_item['uri-id'];
+ $shared_links[] = strtolower($shared_item['plink']);
+ $shared_attachments = Post\Media::splitAttachments($shared_uri_id, $shared_item['guid'], [], $shared_item['has-media']);
$shared_links = array_merge($shared_links, array_column($shared_attachments['visual'], 'url'));
$shared_links = array_merge($shared_links, array_column($shared_attachments['link'], 'url'));
$shared_links = array_merge($shared_links, array_column($shared_attachments['additional'], 'url'));
$item['body'] = self::replaceVisualAttachments($shared_attachments, $item['body']);
- } else {
- $shared_uri_id = 0;
- $shared_links = [];
}
$attachments = Post\Media::splitAttachments($item['uri-id'], $item['guid'] ?? '', $shared_links, $item['has-media'] ?? false);
}
DI::profiler()->stopRecording();
- if (isset($data['url']) && !in_array($data['url'], $ignore_links)) {
+ if (isset($data['url']) && !in_array(strtolower($data['url']), $ignore_links)) {
if (!empty($data['description']) || !empty($data['image']) || !empty($data['preview'])) {
$parts = parse_url($data['url']);
if (!empty($parts['scheme']) && !empty($parts['host'])) {
use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger;
+use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
+use Friendica\Model\Contact;
use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Model\Post;
const HTML = 17;
const XML = 18;
const PLAIN = 19;
+ const ACTIVITY = 20;
const DOCUMENT = 128;
/**
$media = self::addType($media);
}
+ if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
+ $media = self::addActivity($media);
+ }
+
if ($media['type'] == self::HTML) {
$data = ParseUrl::getSiteinfoCached($media['url'], false);
$media['preview'] = $data['images'][0]['src'] ?? null;
return $media;
}
+ /**
+ * Adds the activity type if the media entry is linked to an activity
+ *
+ * @param array $media
+ * @return array
+ */
+ private static function addActivity(array $media): array
+ {
+ $id = Item::fetchByLink($media['url']);
+ if (empty($id)) {
+ return $media;
+ }
+
+ $item = Post::selectFirst([], ['id' => $id, 'network' => Protocol::FEDERATED]);
+ if (empty($item['id'])) {
+ Logger::debug('Not a federated activity', ['id' => $id, 'uri-id' => $media['uri-id'], 'url' => $media['url']]);
+ return $media;
+ }
+
+ if (!empty($item['plink']) && Strings::compareLink($item['plink'], $media['url']) &&
+ parse_url($item['plink'], PHP_URL_HOST) != parse_url($item['uri'], PHP_URL_HOST)) {
+ Logger::debug('Not a link to an activity', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
+ return $media;
+ }
+
+ if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+ $media['mimetype'] = 'application/activity+json';
+ } elseif ($item['network'] == Protocol::DIASPORA) {
+ $media['mimetype'] = 'application/xml';
+ } else {
+ $media['mimetype'] = '';
+ }
+
+ $contact = Contact::getById($item['author-id'], ['avatar', 'gsid']);
+ if (!empty($contact['gsid'])) {
+ $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
+ }
+
+ $media['type'] = self::ACTIVITY;
+ $media['media-uri-id'] = $item['uri-id'];
+ $media['height'] = null;
+ $media['width'] = null;
+ $media['size'] = null;
+ $media['preview'] = null;
+ $media['preview-height'] = null;
+ $media['preview-width'] = null;
+ $media['description'] = $item['body'];
+ $media['name'] = $item['title'];
+ $media['author-url'] = $item['author-link'];
+ $media['author-name'] = $item['author-name'];
+ $media['author-image'] = $contact['avatar'] ?? $item['author-avatar'];
+ $media['publisher-url'] = $gserver['url'] ?? null;
+ $media['publisher-name'] = $gserver['site_name'] ?? null;
+ $media['publisher-image'] = null;
+
+ Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
+ return $media;
+ }
+
/**
* Fetch media data from local resources
* @param array $media
}
/**
- * Stire attachment data
+ * Store attachment data
*
* @param array $activity
* @param array $item
}
/**
- * Store attachment data
+ * Store question data
*
* @param array $activity
* @param array $item
}
$data['quoteUrl'] = $item['quote-uri'];
} elseif (!empty($item['quote-uri']) && !Diaspora::isReshare($body, false)) {
- $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
- $shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]);
- if (!empty($shared_item['uri-id'])) {
- $shared_item['body'] = Post\Media::addAttachmentsToBody($shared_item['uri-id'], $shared_item['body']);
- $body .= "\n" . DI::contentItem()->createSharedBlockByArray($shared_item);
- $item['body'] = Item::improveSharedDataInBody($item, true);
- }
+ $body .= "\n" . DI::contentItem()->createSharedPostByUriId($item['quote-uri-id'], $item['uid'], true);
+ $item['body'] = Item::improveSharedDataInBody($item, true);
}
$data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
- define('DB_UPDATE_VERSION', 1485);
+ define('DB_UPDATE_VERSION', 1486);
}
return [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"uri-id" => ["type" => "int unsigned", "not null" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"],
"url" => ["type" => "varbinary(1024)", "not null" => "1", "comment" => "Media URL"],
+ "media-uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the activities uri-id"],
"type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Media type"],
"mimetype" => ["type" => "varchar(60)", "comment" => ""],
"height" => ["type" => "smallint unsigned", "comment" => "Height of the media"],
"PRIMARY" => ["id"],
"uri-id-url" => ["UNIQUE", "uri-id", "url(512)"],
"uri-id-id" => ["uri-id", "id"],
+ "media-uri-id" => ["media-uri-id"],
]
],
"post-question" => [