Use the cached activity function
This commit is contained in:
parent
da658cbf1d
commit
a0b99f61ea
10 changed files with 6 additions and 173 deletions
|
@ -196,8 +196,6 @@ class Item
|
|||
$notify_items = [];
|
||||
|
||||
while ($item = DBA::fetch($items)) {
|
||||
ActivityPub\PageCache::deleteByUriId($item['uri-id']);
|
||||
|
||||
if (!empty($fields['body'])) {
|
||||
Post\Media::insertFromAttachmentData($item['uri-id'], $fields['body']);
|
||||
|
||||
|
@ -321,8 +319,6 @@ class Item
|
|||
// clean up categories and tags so they don't end up as orphans
|
||||
Post\Category::deleteByURIId($item['uri-id'], $item['uid']);
|
||||
|
||||
ActivityPub\PageCache::deleteByUriId($item['uri-id']);
|
||||
|
||||
/*
|
||||
* If item is a link to a photo resource, nuke all the associated photos
|
||||
* (visitors will not have photo resources)
|
||||
|
|
|
@ -31,7 +31,6 @@ use Friendica\Model\Item;
|
|||
use Friendica\Model\Post;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Protocol\ActivityPub;
|
||||
use Friendica\Protocol\ActivityPub\PageCache;
|
||||
use Friendica\Util\HTTPSignature;
|
||||
use Friendica\Util\Network;
|
||||
use Friendica\Util\Strings;
|
||||
|
@ -51,13 +50,6 @@ class Objects extends BaseModule
|
|||
DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
|
||||
}
|
||||
|
||||
$data = PageCache::fetch($_SERVER['REQUEST_URI']);
|
||||
if (!empty($data)) {
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
||||
System::jsonExit($data, 'application/activity+json');
|
||||
}
|
||||
|
||||
$itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $this->parameters['guid']]);
|
||||
|
||||
if (DBA::isResult($itemuri)) {
|
||||
|
@ -106,7 +98,7 @@ class Objects extends BaseModule
|
|||
Network::checkEtagModified($etag, $last_modified);
|
||||
|
||||
if (empty($this->parameters['activity']) && ($item['gravity'] != GRAVITY_ACTIVITY)) {
|
||||
$activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
|
||||
$activity = ActivityPub\Transmitter::createCachedActivityFromItem($item['id'], false, true);
|
||||
if (empty($activity['type'])) {
|
||||
throw new HTTPException\NotFoundException();
|
||||
}
|
||||
|
@ -123,7 +115,7 @@ class Objects extends BaseModule
|
|||
} elseif (empty($this->parameters['activity']) || in_array($this->parameters['activity'],
|
||||
['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject',
|
||||
'TentativeAccept', 'Follow', 'Add'])) {
|
||||
$data = ActivityPub\Transmitter::createActivityFromItem($item['id']);
|
||||
$data = ActivityPub\Transmitter::createCachedActivityFromItem($item['id']);
|
||||
if (empty($data)) {
|
||||
throw new HTTPException\NotFoundException();
|
||||
}
|
||||
|
@ -135,10 +127,6 @@ class Objects extends BaseModule
|
|||
throw new HTTPException\NotFoundException();
|
||||
}
|
||||
|
||||
if (in_array($item['private'], [Item::PUBLIC, Item::UNLISTED])) {
|
||||
PageCache::add($_SERVER['REQUEST_URI'], $item['uri-id'], $data);
|
||||
}
|
||||
|
||||
// Relaxed CORS header for public items
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2022, 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\Protocol\ActivityPub;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* This class handles the page cache
|
||||
*/
|
||||
class PageCache
|
||||
{
|
||||
/**
|
||||
* Add content to the page cache
|
||||
*
|
||||
* @param string $page
|
||||
* @param int $uriid
|
||||
* @param mixed $content
|
||||
* @return void
|
||||
*/
|
||||
public static function add(string $page, int $uriid, $content)
|
||||
{
|
||||
if (!DI::config()->get('system', 'pagecache')) {
|
||||
return;
|
||||
}
|
||||
|
||||
DBA::delete('pagecache', ["`fetched` < ?", DateTimeFormat::utc('now - 5 minutes')]);
|
||||
DBA::insert('pagecache', ['page' => $page, 'uri-id' => $uriid, 'content' => serialize($content), 'fetched' => DateTimeFormat::utcNow()], Database::INSERT_UPDATE);
|
||||
|
||||
Logger::debug('Page added', ['page' => $page]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data from the page cache
|
||||
*
|
||||
* @param string $page
|
||||
* @return mixed
|
||||
*/
|
||||
public static function fetch(string $page)
|
||||
{
|
||||
$pagecache = DBA::selectFirst('pagecache', [], ['page' => $page]);
|
||||
if (empty($pagecache['content'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
DBA::update('pagecache', ['fetched' => DateTimeFormat::utcNow()], ['page' => $page]);
|
||||
|
||||
Logger::debug('Page fetched', ['page' => $page]);
|
||||
|
||||
return unserialize($pagecache['content']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the pagecache via its connected uri-id
|
||||
*
|
||||
* @param integer $uriid
|
||||
* @return void
|
||||
*/
|
||||
public static function deleteByUriId(int $uriid)
|
||||
{
|
||||
DBA::delete('pagecache', ['uri-id' => $uriid]);
|
||||
}
|
||||
}
|
|
@ -1185,9 +1185,9 @@ class Transmitter
|
|||
* @return array|false activity or false on failure
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function createCachedActivityFromItem(int $item_id, bool $force = false)
|
||||
public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false)
|
||||
{
|
||||
$cachekey = 'APDelivery:createActivity:' . $item_id;
|
||||
$cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode;
|
||||
|
||||
if (!$force) {
|
||||
$data = DI::cache()->get($cachekey);
|
||||
|
@ -1196,7 +1196,7 @@ class Transmitter
|
|||
}
|
||||
}
|
||||
|
||||
$data = self::createActivityFromItem($item_id);
|
||||
$data = self::createActivityFromItem($item_id, $object_mode);
|
||||
|
||||
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
|
||||
return $data;
|
||||
|
|
|
@ -43,7 +43,6 @@ class OptimizeTables
|
|||
DBA::e("OPTIMIZE TABLE `cache`");
|
||||
DBA::e("OPTIMIZE TABLE `locks`");
|
||||
DBA::e("OPTIMIZE TABLE `oembed`");
|
||||
DBA::e("OPTIMIZE TABLE `pagecache`");
|
||||
DBA::e("OPTIMIZE TABLE `parsed_url`");
|
||||
DBA::e("OPTIMIZE TABLE `session`");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue