use Friendica\Core\Cache\Duration;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
+use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
+use Friendica\Model\Conversation;
use Friendica\Model\Item;
+use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Model\User;
use Friendica\Util\DateTimeFormat;
*/
class Feed
{
- /**
- * consume - process atom feed and update anything/everything we might need to update
- *
- * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
- *
- * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
- * It is this person's stuff that is going to be updated.
- * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
- * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
- * have a contact record.
- * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
- * might not) try and subscribe to it.
- * $datedir sorts in reverse order
- * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
- * imported prior to its children being seen in the stream unless we are certain
- * of how the feed is arranged/ordered.
- * With $pass = 1, we only pull parent items out of the stream.
- * With $pass = 2, we only pull children (comments/likes).
- *
- * So running this twice, first with pass 1 and then with pass 2 will do the right
- * thing regardless of feed ordering. This won't be adequate in a fully-threaded
- * model where comments can have sub-threads. That would require some massive sorting
- * to get all the feed items into a mostly linear ordering, and might still require
- * recursion.
- *
- * @param $xml
- * @param array $importer
- * @param array $contact
- * @param $hub
- * @throws ImagickException
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- public static function consume($xml, array $importer, array $contact, &$hub)
- {
- if ($contact['network'] === Protocol::OSTATUS) {
- Logger::info('Consume OStatus messages');
- OStatus::import($xml, $importer, $contact, $hub);
-
- return;
- }
-
- if ($contact['network'] === Protocol::FEED) {
- Logger::info('Consume feeds');
- self::import($xml, $importer, $contact);
-
- return;
- }
-
- if ($contact['network'] === Protocol::DFRN) {
- Logger::info('Consume DFRN messages');
- $dfrn_importer = DFRN::getImporter($contact['id'], $importer['uid']);
- if (!empty($dfrn_importer)) {
- Logger::info('Now import the DFRN feed');
- DFRN::import($xml, $dfrn_importer, true);
- return;
- }
- }
- }
-
/**
* Read a RSS/RDF/Atom feed and create an item entry for it
*
$total_items = $max_items;
}
+ $postings = [];
+
// Importing older entries first
for ($i = $total_items; $i >= 0; --$i) {
$entry = $entries->item($i);
$item["plink"] = DI::httpRequest()->finalUrl($item["plink"]);
- $item["parent-uri"] = $item["uri"];
-
$item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
if (empty($item["title"])) {
$enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
foreach ($enclosures AS $enclosure) {
$href = "";
- $length = "";
- $type = "";
+ $length = null;
+ $type = null;
foreach ($enclosure->attributes AS $attribute) {
if (in_array($attribute->name, ["url", "href"])) {
$href = $attribute->textContent;
} elseif ($attribute->name == "length") {
- $length = $attribute->textContent;
+ $length = (int)$attribute->textContent;
} elseif ($attribute->name == "type") {
$type = $attribute->textContent;
}
}
- if (!empty($item["attach"])) {
- $item["attach"] .= ',';
- } else {
- $item["attach"] = '';
+ if (!empty($href)) {
+ $attachments[] = ['type' => Post\Media::DOCUMENT, 'url' => $href, 'mimetype' => $type, 'size' => $length];
}
-
- $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
-
- $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '"[/attach]';
}
$taglist = [];
}
$item["body"] = HTML::toBBCode($body, $basepath);
+ // Remove tracking pixels
+ $item["body"] = preg_replace("/\[img=1x1\]([^\[\]]*)\[\/img\]/Usi", '', $item["body"]);
+
if (($item["body"] == '') && ($item["title"] != '')) {
$item["body"] = $item["title"];
$item["title"] = '';
$items[] = $item;
break;
} elseif (!Item::isValid($item)) {
- Logger::info('Feed is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
+ Logger::info('Feed item is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
+ continue;
+ } elseif (Item::isTooOld($item)) {
+ Logger::info('Feed is too old', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
continue;
}
if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
// Handle enclosures and treat them as preview picture
foreach ($attachments AS $attachment) {
- if ($attachment["type"] == "image/jpeg") {
- $preview = $attachment["link"];
+ if ($attachment["mimetype"] == "image/jpeg") {
+ $preview = $attachment["url"];
}
}
$data = PageInfo::queryUrl($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
- // Take the data that was provided by the feed if the query is empty
- if (($data['type'] == 'link') && empty($data['title']) && empty($data['text'])) {
- $data['title'] = $saved_title;
- $item["body"] = $saved_body;
- }
+ if (!empty($data)) {
+ // Take the data that was provided by the feed if the query is empty
+ if (($data['type'] == 'link') && empty($data['title']) && empty($data['text'])) {
+ $data['title'] = $saved_title;
+ $item["body"] = $saved_body;
+ }
- $data_text = strip_tags(trim($data['text'] ?? ''));
- $item_body = strip_tags(trim($item['body'] ?? ''));
+ $data_text = strip_tags(trim($data['text'] ?? ''));
+ $item_body = strip_tags(trim($item['body'] ?? ''));
- if (!empty($data['text']) && (($data_text == $item_body) || strstr($item_body, $data_text))) {
- $data['text'] = '';
- }
+ if (!empty($data_text) && (($data_text == $item_body) || strstr($item_body, $data_text))) {
+ $data['text'] = '';
+ }
- // We always strip the title since it will be added in the page information
- $item["title"] = "";
- $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromData($data, false);
- $taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
- $item["object-type"] = Activity\ObjectType::BOOKMARK;
- unset($item["attach"]);
+ // We always strip the title since it will be added in the page information
+ $item["title"] = "";
+ $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromData($data, false);
+ $taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
+ $item["object-type"] = Activity\ObjectType::BOOKMARK;
+ $attachments = [];
+ }
} else {
if (!empty($summary)) {
$item["body"] = '[abstract]' . HTML::toBBCode($summary, $basepath) . "[/abstract]\n" . $item["body"];
// Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
if ($notify) {
$item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname());
- unset($item['uri']);
+ $item['uri'] = Item::newURI($item['uid'], $item['guid']);
+ unset($item['thr-parent']);
unset($item['parent-uri']);
// Set the delivery priority for "remote self" to "medium"
$notify = PRIORITY_MEDIUM;
}
- $id = Item::insert($item, $notify);
+ $condition = ['uid' => $item['uid'], 'uri' => $item['uri']];
+ if (!Item::exists($condition) && !Post\Delayed::exists($item["uri"], $item['uid'])) {
+ if (!$notify) {
+ Post\Delayed::publish($item, $notify, $taglist, $attachments);
+ } else {
+ $postings[] = ['item' => $item, 'notify' => $notify,
+ 'taglist' => $taglist, 'attachments' => $attachments];
+ }
+ } else {
+ Logger::info('Post already created or exists in the delayed posts queue', ['uid' => $item['uid'], 'uri' => $item["uri"]]);
+ }
+ }
- Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
+ if (!empty($postings)) {
+ $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
+ $total = count($postings);
+ if ($total > 1) {
+ // Posts shouldn't be delayed more than a day
+ $interval = min(1440, self::getPollInterval($contact));
+ $delay = max(round(($interval * 60) / $total), 60 * $min_posting);
+ Logger::info('Got posting delay', ['delay' => $delay, 'interval' => $interval, 'items' => $total, 'cid' => $contact['id'], 'url' => $contact['url']]);
+ } else {
+ $delay = 0;
+ }
+
+ $post_delay = 0;
- if (!empty($id) && !empty($taglist)) {
- $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
- foreach ($taglist as $tag) {
- Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
+ foreach ($postings as $posting) {
+ if ($delay > 0) {
+ $publish_time = time() + $post_delay;
+ $post_delay += $delay;
+ } else {
+ $publish_time = time();
}
+
+ $last_publish = DI::pConfig()->get($posting['item']['uid'], 'system', 'last_publish', 0, true);
+ $next_publish = max($last_publish + (60 * $min_posting), time());
+ if ($publish_time < $next_publish) {
+ $publish_time = $next_publish;
+ }
+ $publish_at = date(DateTimeFormat::MYSQL, $publish_time);
+
+ Post\Delayed::add($posting['item']['uri'], $posting['item'], $posting['notify'], false, $publish_at, $posting['taglist'], $posting['attachments']);
}
}
$oldest = $day;
$oldest_date = $date;
}
-
+
if ($newest < $day) {
$newest = $day;
$newest_date = $date;
}
}
+ /**
+ * Get the poll interval for the given contact array
+ *
+ * @param array $contact
+ * @return int Poll interval in minutes
+ */
+ public static function getPollInterval(array $contact)
+ {
+ if (in_array($contact['network'], [Protocol::MAIL, Protocol::FEED])) {
+ $ratings = [0, 3, 7, 8, 9, 10];
+ if (DI::config()->get('system', 'adjust_poll_frequency') && ($contact['network'] == Protocol::FEED)) {
+ $rating = $contact['rating'];
+ } elseif (array_key_exists($contact['priority'], $ratings)) {
+ $rating = $ratings[$contact['priority']];
+ } else {
+ $rating = -1;
+ }
+ } else {
+ // Check once a week per default for all other networks
+ $rating = 9;
+ }
+
+ // Friendica and OStatus are checked once a day
+ if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
+ $rating = 8;
+ }
+
+ // Check archived contacts or contacts with unsupported protocols once a month
+ if ($contact['archive'] || in_array($contact['network'], [Protocol::ZOT, Protocol::PHANTOM])) {
+ $rating = 10;
+ }
+
+ if ($rating < 0) {
+ return 0;
+ }
+ /*
+ * Based on $contact['priority'], should we poll this site now? Or later?
+ */
+
+ $min_poll_interval = max(1, DI::config()->get('system', 'min_poll_interval'));
+
+ $poll_intervals = [$min_poll_interval, 15, 30, 60, 120, 180, 360, 720 ,1440, 10080, 43200];
+
+ //$poll_intervals = [$min_poll_interval . ' minute', '15 minute', '30 minute',
+ // '1 hour', '2 hour', '3 hour', '6 hour', '12 hour' ,'1 day', '1 week', '1 month'];
+
+ return $poll_intervals[$rating];
+ }
+
/**
* Convert a tag array to a tag string
*
if ($tagstr != "") {
$tagstr .= ", ";
}
-
+
$tagstr .= "#[url=" . DI::baseUrl() . "/search?tag=" . urlencode($tag) . "]" . $tag . "[/url]";
}
if ($item['gravity'] != GRAVITY_PARENT) {
$parent = Item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
- $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
- $thrparent = Item::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $parent_item]);
+ $thrparent = Item::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $item['thr-parent']]);
if (DBA::isResult($thrparent)) {
$mentioned[$thrparent["author-link"]] = $thrparent["author-link"];
}
$attributes = [
- "ref" => $parent_item,
+ "ref" => $item['thr-parent'],
"href" => $parent_plink];
XML::addElement($doc, $entry, "thr:in-reply-to", "", $attributes);