forked from grumpydevelop/federator
		
	- In-&Outbox now use factories to convert data - initial creation of followers-data (wip, not working) - fixed some issue where connector would always fail (as we deliberately always pass username@domain and the regex there always fails) - deleted all mastodon-files (mastodon.ini and mastodon plugin) - minor fix where toObject of create would override id with an empty string if no url is set
		
			
				
	
	
		
			249 lines
		
	
	
	
		
			6.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			249 lines
		
	
	
	
		
			6.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
 | 
						|
 * SPDX-License-Identifier: GPL-3.0-or-later
 | 
						|
 *
 | 
						|
 * @author Sascha Nitsch <grumpydevelop@contentnation.net>
 | 
						|
 **/
 | 
						|
 | 
						|
namespace Federator\Cache;
 | 
						|
 | 
						|
/**
 | 
						|
 * Caching class using redis
 | 
						|
 */
 | 
						|
class RedisCache implements Cache
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * config data
 | 
						|
     *
 | 
						|
     * @var array<string, mixed> $config
 | 
						|
     */
 | 
						|
    private $config;
 | 
						|
 | 
						|
    /**
 | 
						|
     * connection to redis open flag
 | 
						|
     *
 | 
						|
     * @var bool $connected
 | 
						|
     */
 | 
						|
    private $connected = false;
 | 
						|
 | 
						|
    /**
 | 
						|
     * connection handle
 | 
						|
     *
 | 
						|
     * @var \Redis $redis
 | 
						|
     */
 | 
						|
    private $redis;
 | 
						|
 | 
						|
    /**
 | 
						|
     * user cache time to live in secods
 | 
						|
     *
 | 
						|
     * @var int $userTTL
 | 
						|
     */
 | 
						|
    private $userTTL;
 | 
						|
 | 
						|
    /**
 | 
						|
     * constructor
 | 
						|
     */
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        $config = parse_ini_file('../rediscache.ini');
 | 
						|
        if ($config !== false) {
 | 
						|
            $this->config = $config;
 | 
						|
            $this->userTTL = array_key_exists('userttl', $config) ? intval($config['userttl'], 10) : 60;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * connect to redis
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    private function connect()
 | 
						|
    {
 | 
						|
         $this->redis = new \Redis();
 | 
						|
         $this->redis->pconnect($this->config['host'], intval($this->config['port'], 10));
 | 
						|
         // @phan-suppress-next-line PhanTypeMismatchArgumentInternalProbablyReal
 | 
						|
         $this->redis->auth([$this->config['username'], $this->config['password']]);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * create key from session and user
 | 
						|
     *
 | 
						|
     * @param string $prefix prefix to create name spaces
 | 
						|
     * @param string $input key name
 | 
						|
     * @return string key
 | 
						|
     */
 | 
						|
    private static function createKey($prefix, $input)
 | 
						|
    {
 | 
						|
        return $prefix . '_' . md5($input);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * get followers of given user
 | 
						|
     *
 | 
						|
     * @param string $id user id @unused-param
 | 
						|
 | 
						|
     * @return \Federator\Data\ActivityPub\Common\APObject[]|false
 | 
						|
     */
 | 
						|
    public function getRemoteFollowersOfUser($id)
 | 
						|
    {
 | 
						|
        error_log("rediscache::getRemoteFollowersOfUser not implemented");
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * get posts by given user
 | 
						|
     *
 | 
						|
     * @param string $id user id @unused-param
 | 
						|
     * @param string $min min date @unused-param
 | 
						|
     * @param string $max max date @unused-param
 | 
						|
 | 
						|
     * @return \Federator\Data\ActivityPub\Common\APObject[]|false
 | 
						|
     */
 | 
						|
    public function getRemotePostsByUser($id, $min, $max)
 | 
						|
    {
 | 
						|
        error_log("rediscache::getRemotePostsByUser not implemented");
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * get statistics from remote system
 | 
						|
     *
 | 
						|
     * @return \Federator\Data\Stats|false
 | 
						|
     */
 | 
						|
    public function getRemoteStats()
 | 
						|
    {
 | 
						|
        if (!$this->connected) {
 | 
						|
            $this->connect();
 | 
						|
        }
 | 
						|
        $key = 'm_stats';
 | 
						|
        $data = $this->redis->get($key);
 | 
						|
        if ($data === false) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        $stats = \Federator\Data\Stats::createFromJson($data);
 | 
						|
        return $stats;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * get remote user by given name
 | 
						|
     *
 | 
						|
     * @param string $_name user/profile name
 | 
						|
     * @return \Federator\Data\User | false
 | 
						|
     */
 | 
						|
    public function getRemoteUserByName(string $_name)
 | 
						|
    {
 | 
						|
        if (!$this->connected) {
 | 
						|
            $this->connect();
 | 
						|
        }
 | 
						|
        $key = self::createKey('u', $_name);
 | 
						|
        $data = $this->redis->get($key);
 | 
						|
        if ($data === false) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        $user = \Federator\Data\User::createFromJson($data);
 | 
						|
        return $user;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * get remote user by given session
 | 
						|
     *
 | 
						|
     * @param string $_session session id
 | 
						|
     * @param string $_user user/profile name
 | 
						|
     * @return \Federator\Data\User | false
 | 
						|
     */
 | 
						|
    public function getRemoteUserBySession($_session, $_user)
 | 
						|
    {
 | 
						|
        if (!$this->connected) {
 | 
						|
            $this->connect();
 | 
						|
        }
 | 
						|
        $key = self::createKey('s', $_session . $_user);
 | 
						|
        $data = $this->redis->get($key);
 | 
						|
        if ($data === false) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        $user = \Federator\Data\User::createFromJson($data);
 | 
						|
        return $user;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * save remote followers by user
 | 
						|
     *
 | 
						|
     * @param string $user user name @unused-param
 | 
						|
     * @param \Federator\Data\ActivityPub\Common\APObject[]|false $followers user followers @unused-param
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    public function saveRemoteFollowersOfUser($user, $followers)
 | 
						|
    {
 | 
						|
        error_log("rediscache::saveRemoteFollowersOfUser not implemented");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * save remote posts by user
 | 
						|
     *
 | 
						|
     * @param string $user user name @unused-param
 | 
						|
     * @param \Federator\Data\ActivityPub\Common\APObject[]|false $posts user posts @unused-param
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    public function saveRemotePostsByUser($user, $posts)
 | 
						|
    {
 | 
						|
        error_log("rediscache::saveRemotePostsByUser not implemented");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * save remote stats
 | 
						|
     *
 | 
						|
     * @param \Federator\Data\Stats $stats stats to save
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    public function saveRemoteStats($stats)
 | 
						|
    {
 | 
						|
        if (!$this->connected) {
 | 
						|
            $this->connect();
 | 
						|
        }
 | 
						|
        $key = 'm_stats';
 | 
						|
        $serialized = $stats->toJson();
 | 
						|
        $this->redis->setEx($key, $this->config['statsttl'], $serialized);
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * save remote user by name
 | 
						|
     *
 | 
						|
     * @param string $_name user/profile name
 | 
						|
     * @param \Federator\Data\User $user user data
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    public function saveRemoteUserByName($_name, $user)
 | 
						|
    {
 | 
						|
        $key = self::createKey('u', $_name);
 | 
						|
        $serialized = $user->toJson();
 | 
						|
        $this->redis->setEx($key, $this->userTTL, $serialized);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * save remote user by given session
 | 
						|
     *
 | 
						|
     * @param string $_session session id
 | 
						|
     * @param string $_user user/profile name
 | 
						|
     * @param \Federator\Data\User $user user data
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    public function saveRemoteUserBySession($_session, $_user, $user)
 | 
						|
    {
 | 
						|
        $key = self::createKey('s', $_session . $_user);
 | 
						|
        $serialized = $user->toJson();
 | 
						|
        $this->redis->setEx($key, $this->userTTL, $serialized,);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
namespace Federator;
 | 
						|
 | 
						|
/**
 | 
						|
 * Function to initialize plugin
 | 
						|
 *
 | 
						|
 * @param  \Federator\Main $main main instance
 | 
						|
 * @return void
 | 
						|
 */
 | 
						|
function rediscache_load($main)
 | 
						|
{
 | 
						|
    $rc = new Cache\RedisCache();
 | 
						|
    $main->setCache($rc);
 | 
						|
}
 |