2021-11-26 23:31:53 +00:00
< ? php
declare ( strict_types = 1 );
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Entity ;
use App\Core\Cache ;
use App\Core\DB\DB ;
use App\Core\Entity ;
2021-12-01 01:33:12 +00:00
use App\Core\Event ;
2021-11-26 23:31:53 +00:00
use function App\Core\I18n\_m ;
2021-12-01 01:33:12 +00:00
use App\Core\Router\Router ;
2021-11-26 23:31:53 +00:00
use DateTimeInterface ;
/**
* Entity for feeds a user follows
*
* @ category DB
* @ package GNUsocial
*
* @ author Hugo Sales < hugo @ hsal . es >
* @ copyright 2021 Free Software Foundation , Inc http :// www . fsf . org
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
class Feed extends Entity
{
// {{{ Autocode
// @codeCoverageIgnoreStart
private int $actor_id ;
private string $url ;
private string $title ;
private string $route ;
private int $ordering ;
2021-12-26 15:12:06 +00:00
private \DateTimeInterface $created ;
private \DateTimeInterface $modified ;
2021-11-26 23:31:53 +00:00
public function setActorId ( int $actor_id ) : self
{
$this -> actor_id = $actor_id ;
return $this ;
}
public function getActorId () : int
{
return $this -> actor_id ;
}
public function setUrl ( string $url ) : self
{
$this -> url = $url ;
return $this ;
}
public function getUrl () : string
{
return $this -> url ;
}
public function setTitle ( string $title ) : self
{
$this -> title = $title ;
return $this ;
}
public function getTitle () : string
{
return $this -> title ;
}
public function setRoute ( string $route ) : self
{
$this -> route = $route ;
return $this ;
}
public function getRoute () : string
{
return $this -> route ;
}
public function setOrdering ( int $ordering ) : self
{
$this -> ordering = $ordering ;
return $this ;
}
public function getOrdering () : int
{
return $this -> ordering ;
}
2021-12-26 15:12:06 +00:00
public function setCreated ( \DateTimeInterface $created ) : self
2021-11-26 23:31:53 +00:00
{
$this -> created = $created ;
return $this ;
}
2021-12-26 15:12:06 +00:00
public function getCreated () : \DateTimeInterface
2021-11-26 23:31:53 +00:00
{
return $this -> created ;
}
2021-12-26 15:12:06 +00:00
public function setModified ( \DateTimeInterface $modified ) : self
2021-11-26 23:31:53 +00:00
{
$this -> modified = $modified ;
return $this ;
}
2021-12-26 15:12:06 +00:00
public function getModified () : \DateTimeInterface
2021-11-26 23:31:53 +00:00
{
return $this -> modified ;
}
// @codeCoverageIgnoreEnd
// }}} Autocode
public static function cacheKey ( LocalUser | Actor $user_actor ) : string
{
return 'feeds-' . $user_actor -> getId ();
}
/**
* @ return self []
*/
public static function getFeeds ( LocalUser | Actor $user_actor ) : array
{
return Cache :: getList ( self :: cacheKey ( $user_actor ), fn () => DB :: findBy ( 'feed' , [ 'actor_id' => $user_actor -> getId ()], order_by : [ 'ordering' => 'ASC' ]));
}
/**
* This is called in the register function , in `DB::persistWithSameId` ,
* so we don ' t have the $user with an id yet , hence the awkward
* arguments
*/
public static function createDefaultFeeds ( int $actor_id , LocalUser $user ) : void
{
2021-12-01 01:33:12 +00:00
$ordering = 1 ;
2021-12-29 13:26:11 +00:00
DB :: persist ( self :: create ([ 'actor_id' => $actor_id , 'url' => Router :: url ( $route = 'feed_home' ), 'route' => $route , 'title' => _m ( 'Home' ), 'ordering' => $ordering ++ ]));
2021-12-23 13:27:31 +00:00
DB :: persist ( self :: create ([ 'actor_id' => $actor_id , 'url' => Router :: url ( $route = 'feed_public' ), 'route' => $route , 'title' => _m ( 'Planet' ), 'ordering' => $ordering ++ ]));
2021-12-01 01:33:12 +00:00
Event :: handle ( 'CreateDefaultFeeds' , [ $actor_id , $user , & $ordering ]);
2021-11-26 23:31:53 +00:00
}
public static function schemaDef () : array
{
return [
'name' => 'feed' ,
'fields' => [
'actor_id' => [ 'type' => 'int' , 'foreign key' => true , 'target' => 'Actor.id' , 'multiplicity' => 'many to one' , 'not null' => true , 'description' => 'foreign key to actor table' ],
'url' => [ 'type' => 'text' , 'not null' => true ],
'title' => [ 'type' => 'text' , 'not null' => true ],
'route' => [ 'type' => 'text' , 'not null' => true ],
'ordering' => [ 'type' => 'int' , 'not null' => true ],
'created' => [ 'type' => 'datetime' , 'not null' => true , 'default' => 'CURRENT_TIMESTAMP' , 'description' => 'date this record was created' ],
'modified' => [ 'type' => 'timestamp' , 'not null' => true , 'default' => 'CURRENT_TIMESTAMP' , 'description' => 'date this record was modified' ],
],
'primary key' => [ 'actor_id' , 'url' ],
'indexes' => [
'feed_actor_id_idx' => [ 'actor_id' ],
],
];
}
}