2021-11-01 21:19:20 +00:00
< ? php
2021-12-24 00:39:54 +00:00
declare ( strict_types = 1 );
2021-11-01 21:19:20 +00:00
// {{{ 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/>.
// }}}
2021-12-10 02:45:31 +00:00
namespace Plugin\RepeatNote\Entity ;
2021-11-01 21:19:20 +00:00
2022-01-08 17:15:00 +00:00
use App\Core\Cache ;
2022-03-27 15:19:09 +01:00
use App\Core\DB ;
2021-11-01 21:19:20 +00:00
use App\Core\Entity ;
2022-01-17 20:56:14 +00:00
use App\Entity\Actor ;
use App\Entity\LocalUser ;
2021-11-15 17:11:27 +00:00
use App\Entity\Note ;
2021-11-01 21:19:20 +00:00
/**
* Entity for notices
*
* @ category DB
* @ package GNUsocial
*
* @ author Eliseu Amaro < mail @ eliseuama . ro >
* @ copyright 2020 - 2021 Free Software Foundation , Inc http :// www . fsf . org
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
class NoteRepeat extends Entity
{
2022-01-08 17:15:00 +00:00
// {{{ Autocode
// @codeCoverageIgnoreStart
2021-11-15 17:11:27 +00:00
private int $note_id ;
2021-11-01 21:19:20 +00:00
private int $actor_id ;
private int $repeat_of ;
2021-11-15 17:11:27 +00:00
public function setNoteId ( int $note_id ) : self
2021-11-01 21:19:20 +00:00
{
2021-11-15 17:11:27 +00:00
$this -> note_id = $note_id ;
2021-11-01 21:19:20 +00:00
return $this ;
}
2021-11-15 17:11:27 +00:00
public function getNoteId () : int
2021-11-01 21:19:20 +00:00
{
2021-11-15 17:11:27 +00:00
return $this -> note_id ;
2021-11-01 21:19:20 +00:00
}
public function setActorId ( int $actor_id ) : self
{
$this -> actor_id = $actor_id ;
return $this ;
}
2022-02-26 14:45:38 +00:00
public function getActorId () : int
2021-11-01 21:19:20 +00:00
{
return $this -> actor_id ;
}
public function setRepeatOf ( int $repeat_of ) : self
{
$this -> repeat_of = $repeat_of ;
return $this ;
}
public function getRepeatOf () : int
{
return $this -> repeat_of ;
}
2022-02-26 14:45:38 +00:00
2022-01-08 17:15:00 +00:00
// @codeCoverageIgnoreEnd
// }}} Autocode
2022-01-17 20:56:14 +00:00
public static function cacheKeys ( int | Note $note_id , null | int | Actor | LocalUser $actor_id = null ) : array
2022-01-08 17:15:00 +00:00
{
2022-01-17 20:56:14 +00:00
$note_id = \is_int ( $note_id ) ? $note_id : $note_id -> getId ();
$actor_id = \is_null ( $actor_id ) ? null : ( \is_int ( $actor_id ) ? $actor_id : $actor_id -> getId ());
2022-01-08 17:15:00 +00:00
return [
2022-01-17 20:56:14 +00:00
'is_repeat' => " note-repeat-is- { $note_id } " ,
'repeats' => " note-repeats- { $note_id } " ,
'actor_repeat' => " note-repeat- { $note_id } - { $actor_id } " ,
2022-01-08 17:15:00 +00:00
];
}
2021-11-01 21:19:20 +00:00
2022-01-17 20:56:14 +00:00
public static function getNoteActorRepeat ( int | Note $note_id , int $actor_id ) : ? self
{
$note_id = \is_int ( $note_id ) ? $note_id : $note_id -> getId ();
return Cache :: get (
self :: cacheKeys ( $note_id , $actor_id )[ 'actor_repeat' ],
fn () => DB :: findOneBy ( 'note_repeat' , [
'actor_id' => $actor_id ,
'repeat_of' => $note_id ,
], return_null : true ),
);
}
2021-12-28 17:28:44 +00:00
/**
* @ return bool Returns true if Note provided is a repeat of another Note
*/
public static function isNoteRepeat ( Note $note ) : bool
{
2022-01-08 17:15:00 +00:00
return Cache :: get ( self :: cacheKeys ( $note )[ 'is_repeat' ], fn () => DB :: count ( self :: class , [ 'note_id' => $note -> getId ()]) > 0 );
2021-12-28 17:28:44 +00:00
}
2021-11-15 17:11:27 +00:00
public static function getNoteRepeats ( Note $note ) : array
{
2022-01-08 17:15:00 +00:00
return Cache :: getList (
self :: cacheKeys ( $note )[ 'repeats' ],
fn () => DB :: dql (
<<< 'EOF'
select n from note as n
inner join note_repeat as nr
with nr . note_id = n . id
where nr . repeat_of = : note_id
order by n . created DESC , n . id DESC
EOF ,
[ 'note_id' => $note -> getId ()],
),
2021-11-15 17:11:27 +00:00
);
}
2021-11-01 21:19:20 +00:00
public static function schemaDef () : array
{
return [
2021-12-24 00:39:54 +00:00
'name' => 'note_repeat' ,
2021-11-01 21:19:20 +00:00
'fields' => [
2021-12-24 00:39:54 +00:00
'note_id' => [ 'type' => 'int' , 'not null' => true , 'foreign key' => true , 'target' => 'Note.id' , 'multiplicity' => 'one to one' , 'description' => 'The id of the repeat itself' ],
'actor_id' => [ 'type' => 'int' , 'not null' => true , 'foreign key' => true , 'target' => 'Actor.id' , 'multiplicity' => 'one to one' , 'description' => 'Who made this repeat' ],
2021-11-01 21:19:20 +00:00
'repeat_of' => [ 'type' => 'int' , 'not null' => true , 'foreign key' => true , 'target' => 'Note.id' , 'multiplicity' => 'one to one' , 'description' => 'Note this is a repeat of' ],
],
2021-12-24 00:39:54 +00:00
'primary key' => [ 'note_id' ],
2021-11-01 21:19:20 +00:00
'foreign keys' => [
2021-12-24 00:39:54 +00:00
'note_id_to_id_fkey' => [ 'note' , [ 'note_id' => 'id' ]],
2021-11-01 21:19:20 +00:00
'note_repeat_of_id_fkey' => [ 'note' , [ 'repeat_of' => 'id' ]],
2021-11-15 17:11:27 +00:00
'actor_reply_to_id_fkey' => [ 'actor' , [ 'actor_id' => 'id' ]],
],
2021-12-10 02:45:31 +00:00
'indexes' => [
2021-11-15 17:11:27 +00:00
'note_repeat_of_idx' => [ 'repeat_of' ],
2021-11-01 21:19:20 +00:00
],
];
}
}