2020-03-29 20:56:35 +01:00
< ? php
2021-10-21 13:49:41 +01:00
declare ( strict_types = 1 );
2020-03-29 20:56:35 +01:00
// {{{ License
2020-06-30 17:26:40 +01:00
2020-05-20 17:53:53 +01:00
// This file is part of GNU social - https://www.gnu.org/software/social
2020-03-29 20:56:35 +01:00
//
// GNU social is free software: you can redistribute it and/or modify
2020-05-10 21:43:15 +01:00
// it under the terms of the GNU Affero General Public License as published by
2020-03-29 20:56:35 +01:00
// 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.
//
2020-05-10 21:43:15 +01:00
// You should have received a copy of the GNU Affero General Public License
2020-03-29 20:56:35 +01:00
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
2020-06-30 17:26:40 +01:00
2020-03-29 20:56:35 +01:00
// }}}
namespace App\Entity ;
2020-08-28 21:16:26 +01:00
use App\Core\Cache ;
2020-08-15 06:47:45 +01:00
use App\Core\DB\DB ;
use App\Core\Entity ;
2021-11-27 04:12:44 +00:00
use App\Core\Event ;
2021-04-15 23:28:28 +01:00
use App\Core\VisibilityScope ;
2021-09-18 04:54:35 +01:00
use Component\Avatar\Avatar ;
2021-11-27 04:12:44 +00:00
use Component\Notification\Entity\Notification ;
2020-05-10 21:43:15 +01:00
use DateTimeInterface ;
2020-03-29 20:56:35 +01:00
/**
* Entity for notices
*
* @ category DB
* @ package GNUsocial
*
2021-02-19 23:29:43 +00:00
* @ author Hugo Sales < hugo @ hsal . es >
* @ copyright 2020 - 2021 Free Software Foundation , Inc http :// www . fsf . org
2020-03-29 20:56:35 +01:00
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
2021-09-09 03:46:30 +01:00
class Note extends Entity
2020-03-29 20:56:35 +01:00
{
2020-03-30 15:00:13 +01:00
// {{{ Autocode
2021-05-05 17:03:03 +01:00
// @codeCoverageIgnoreStart
2020-03-30 16:13:51 +01:00
private int $id ;
2021-09-18 03:22:27 +01:00
private int $actor_id ;
2021-09-20 17:06:21 +01:00
private ? string $content_type = null ;
private ? string $content = null ;
private ? string $rendered = null ;
2021-11-16 23:24:06 +00:00
private bool $is_local ;
2020-03-30 16:13:51 +01:00
private ? string $source ;
2021-09-09 03:46:30 +01:00
private int $scope = VisibilityScope :: PUBLIC ;
2021-10-04 17:00:58 +01:00
private string $url ;
2021-11-27 15:06:46 +00:00
private ? int $language_id = null ;
2021-10-21 13:49:41 +01:00
private DateTimeInterface $created ;
private DateTimeInterface $modified ;
2020-03-30 16:13:51 +01:00
public function setId ( int $id ) : self
{
$this -> id = $id ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2020-03-30 16:13:51 +01:00
public function getId () : int
{
return $this -> id ;
}
2021-09-18 03:22:27 +01:00
public function setActorId ( int $actor_id ) : self
2020-03-30 16:13:51 +01:00
{
2021-09-18 03:22:27 +01:00
$this -> actor_id = $actor_id ;
2020-03-30 16:13:51 +01:00
return $this ;
}
2020-08-08 17:11:18 +01:00
2021-09-18 03:22:27 +01:00
public function getActorId () : int
2020-03-30 16:13:51 +01:00
{
2021-09-18 03:22:27 +01:00
return $this -> actor_id ;
2020-03-30 16:13:51 +01:00
}
2021-09-09 03:46:30 +01:00
public function getContentType () : string
{
return $this -> content_type ;
}
/**
2021-09-18 03:22:27 +01:00
* @ return Note
2021-09-09 03:46:30 +01:00
*/
public function setContentType ( string $content_type ) : self
{
$this -> content_type = $content_type ;
return $this ;
}
2020-03-30 16:13:51 +01:00
public function setContent ( ? string $content ) : self
{
$this -> content = $content ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2020-03-30 16:13:51 +01:00
public function getContent () : ? string
{
return $this -> content ;
}
2020-09-05 21:46:37 +01:00
public function setRendered ( ? string $rendered ) : self
{
$this -> rendered = $rendered ;
return $this ;
}
public function getRendered () : ? string
{
return $this -> rendered ;
}
2021-11-16 23:24:06 +00:00
public function setIsLocal ( bool $is_local ) : self
2020-03-30 16:13:51 +01:00
{
$this -> is_local = $is_local ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2021-11-16 23:24:06 +00:00
public function getIsLocal () : bool
2020-03-30 16:13:51 +01:00
{
return $this -> is_local ;
}
public function setSource ( ? string $source ) : self
{
$this -> source = $source ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2020-03-30 16:13:51 +01:00
public function getSource () : ? string
{
return $this -> source ;
}
2020-09-05 03:33:29 +01:00
public function setScope ( int $scope ) : self
2020-03-30 16:13:51 +01:00
{
$this -> scope = $scope ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2020-09-05 03:33:29 +01:00
public function getScope () : int
2020-03-30 16:13:51 +01:00
{
return $this -> scope ;
}
2021-10-04 17:00:58 +01:00
public function getUrl () : string
{
return $this -> url ;
}
public function setUrl ( string $url ) : self
{
$this -> url = $url ;
return $this ;
}
2021-11-24 15:51:01 +00:00
public function getLanguageId () : int
2021-10-21 13:49:41 +01:00
{
2021-11-24 15:51:01 +00:00
return $this -> language_id ;
2021-10-21 13:49:41 +01:00
}
2021-11-27 15:06:46 +00:00
public function setLanguageId ( ? int $language_id ) : self
2021-10-21 13:49:41 +01:00
{
2021-11-24 15:51:01 +00:00
$this -> language_id = $language_id ;
2021-10-21 13:49:41 +01:00
return $this ;
}
2021-05-05 17:03:03 +01:00
public function setCreated ( DateTimeInterface $created ) : self
2020-06-30 19:20:50 +01:00
{
$this -> created = $created ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2021-05-05 17:03:03 +01:00
public function getCreated () : DateTimeInterface
2020-06-30 19:20:50 +01:00
{
return $this -> created ;
}
2021-05-05 17:03:03 +01:00
public function setModified ( DateTimeInterface $modified ) : self
2020-06-30 19:20:50 +01:00
{
$this -> modified = $modified ;
return $this ;
}
2020-08-08 17:11:18 +01:00
2021-05-05 17:03:03 +01:00
public function getModified () : DateTimeInterface
2020-06-30 19:20:50 +01:00
{
return $this -> modified ;
}
2021-05-05 17:03:03 +01:00
// @codeCoverageIgnoreEnd
2020-03-30 15:00:13 +01:00
// }}} Autocode
2020-03-29 20:56:35 +01:00
2021-09-18 03:22:27 +01:00
public function getActor () : Actor
2021-09-14 13:40:50 +01:00
{
2021-10-04 17:00:58 +01:00
return Actor :: getById ( $this -> actor_id );
2021-09-14 13:40:50 +01:00
}
public function getActorNickname () : string
2020-08-15 06:47:45 +01:00
{
2021-10-04 17:00:58 +01:00
return Actor :: getNicknameById ( $this -> actor_id );
2020-08-19 16:31:52 +01:00
}
2021-10-29 22:05:10 +01:00
public function getActorFullname () : ? string
2021-10-26 14:47:28 +01:00
{
return Actor :: getFullnameById ( $this -> actor_id );
}
2021-09-18 04:54:35 +01:00
public function getActorAvatarUrl ( string $size = 'full' ) : string
2020-08-19 16:31:52 +01:00
{
2021-09-18 04:54:35 +01:00
return Avatar :: getAvatarUrl ( $this -> getActorId (), $size );
2020-08-15 06:47:45 +01:00
}
2021-09-14 13:40:50 +01:00
2021-11-08 20:21:41 +00:00
public static function getAllNotes ( int $note_scope ) : array
2020-12-02 22:57:32 +00:00
{
2021-10-21 13:49:41 +01:00
return DB :: sql (
2021-11-08 20:21:41 +00:00
<<< 'EOF'
2021-11-09 23:37:46 +00:00
select { select } from note n
where ( n . scope & : scope ) <> 0
2021-11-08 20:21:41 +00:00
order by n . created DESC
EOF ,
2021-11-09 23:37:46 +00:00
[ 'scope' => $note_scope ],
2020-12-02 22:57:32 +00:00
);
}
2020-08-15 06:47:45 +01:00
2020-08-28 21:16:26 +01:00
public function getAttachments () : array
{
return Cache :: get ( 'note-attachments-' . $this -> id , function () {
return DB :: dql (
2021-11-09 23:37:46 +00:00
<<< 'EOF'
select att from attachment att
join attachment_to_note atn with atn . attachment_id = att . id
where atn . note_id = : note_id
EOF ,
2021-10-21 13:49:41 +01:00
[ 'note_id' => $this -> id ],
);
2020-08-28 21:16:26 +01:00
});
}
2021-11-17 17:14:15 +00:00
public function getAttachmentsWithTitle () : array
{
return Cache :: get ( 'note-attachments-with-title-' . $this -> id , function () {
$from_db = DB :: dql (
<<< 'EOF'
select att , atn . title
from attachment att
join attachment_to_note atn with atn . attachment_id = att . id
where atn . note_id = : note_id
EOF ,
[ 'note_id' => $this -> id ],
);
$results = [];
foreach ( $from_db as $fd ) {
$results [] = [ $fd [ 0 ], $fd [ 'title' ]];
}
return $results ;
});
}
2021-08-14 15:07:51 +01:00
public function getLinks () : array
{
return Cache :: get ( 'note-links-' . $this -> id , function () {
return DB :: dql (
2021-11-09 23:37:46 +00:00
<<< 'EOF'
select l from link l
join note_to_link ntl with ntl . link_id = l . id
where ntl . note_id = : note_id
EOF ,
2021-10-21 13:49:41 +01:00
[ 'note_id' => $this -> id ],
2021-08-14 15:07:51 +01:00
);
});
}
2020-11-06 19:47:15 +00:00
/**
* Whether this note is visible to the given actor
*/
2021-09-21 16:38:50 +01:00
public function isVisibleTo ( null | Actor | LocalUser $a ) : bool
2020-09-10 23:27:23 +01:00
{
2021-09-21 16:38:50 +01:00
// TODO cache this
2021-04-15 23:28:28 +01:00
$scope = VisibilityScope :: create ( $this -> scope );
2020-09-10 23:27:23 +01:00
return $scope -> public
2021-10-21 13:49:41 +01:00
|| ( ! \is_null ( $a ) && (
2021-11-24 15:51:01 +00:00
( $scope -> subscriber && 0 != DB :: count ( 'subscription' , [ 'subscriber' => $a -> getId (), 'subscribed' => $this -> actor_id ]))
2021-11-07 01:32:06 +00:00
|| ( $scope -> addressee && 0 != DB :: count ( 'notification' , [ 'activity_id' => $this -> id , 'actor_id' => $a -> getId ()]))
|| ( $scope -> group && [] != DB :: dql (
2021-11-24 15:51:01 +00:00
<<< 'EOF'
select m from group_member m
join group_inbox i with m . group_id = i . group_id
join note n with i . activity_id = n . id
where n . id = : note_id and m . actor_id = : actor_id
EOF ,
[ 'note_id' => $this -> id , 'actor_id' => $a -> getId ()],
))
));
2020-09-10 23:27:23 +01:00
}
2021-11-27 04:12:44 +00:00
public function getNotificationTargets ( array $ids_already_known = []) : array
{
$mentions = [];
Event :: handle ( 'RenderNoteContent' , [ $this -> getContent (), $this -> getContentType (), & $rendered , & $mentions , $this -> getActor (), Language :: getFromId ( $this -> getLanguageId ()) -> getLocale ()]);
$mentioned = [];
foreach ( $mentions as $mention ) {
foreach ( $mention [ 'mentioned' ] as $m ) {
$mentioned [] = $m ;
}
}
return $mentioned ;
}
2020-03-29 20:56:35 +01:00
public static function schemaDef () : array
{
2020-08-15 06:47:45 +01:00
return [
2020-08-13 00:57:22 +01:00
'name' => 'note' ,
2020-03-29 20:56:35 +01:00
'fields' => [
2021-02-22 21:34:59 +00:00
'id' => [ 'type' => 'serial' , 'not null' => true ],
2021-09-18 03:22:27 +01:00
'actor_id' => [ 'type' => 'int' , 'foreign key' => true , 'target' => 'Actor.id' , 'multiplicity' => 'one to one' , 'not null' => true , 'description' => 'who made the note' ],
2021-09-20 17:06:21 +01:00
'content' => [ 'type' => 'text' , 'description' => 'note content' ],
2021-10-28 17:34:01 +01:00
'content_type' => [ 'type' => 'varchar' , 'not null' => true , 'default' => 'text/plain' , 'length' => 129 , 'description' => 'A note can be written in a multitude of formats such as text/plain, text/markdown, application/x-latex, and text/html' ],
2021-02-22 21:34:59 +00:00
'rendered' => [ 'type' => 'text' , 'description' => 'rendered note content, so we can keep the microtags (if not local)' ],
2021-11-16 23:24:06 +00:00
'is_local' => [ 'type' => 'bool' , 'not null' => true , 'description' => 'was this note generated by a local actor' ],
2021-03-22 12:14:09 +00:00
'source' => [ 'type' => 'varchar' , 'foreign key' => true , 'length' => 32 , 'target' => 'NoteSource.code' , 'multiplicity' => 'many to one' , 'description' => 'fkey to source of note, like "web", "im", or "clientname"' ],
2021-11-24 15:51:01 +00:00
'scope' => [ 'type' => 'int' , 'not null' => true , 'default' => VisibilityScope :: PUBLIC , 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = groups; 8 = subscribers; 16 = messages; null = default' ],
'url' => [ 'type' => 'text' , 'description' => 'Permalink to Note' ],
'language_id' => [ 'type' => 'int' , 'foreign key' => true , 'target' => 'Language.id' , 'multiplicity' => 'one to many' , 'description' => 'The language for this note' ],
'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' ],
2020-03-29 20:56:35 +01:00
],
2021-02-22 21:34:59 +00:00
'primary key' => [ 'id' ],
'indexes' => [
2021-09-18 03:22:27 +01:00
'note_created_id_is_local_idx' => [ 'created' , 'is_local' ],
'note_actor_created_idx' => [ 'actor_id' , 'created' ],
'note_is_local_created_actor_idx' => [ 'is_local' , 'created' , 'actor_id' ],
2020-03-29 20:56:35 +01:00
],
2021-10-28 01:26:16 +01:00
'fulltext indexes' => [ 'notice_fulltext_idx' => [ 'content' ]], // TODO make this configurable
2020-03-29 20:56:35 +01:00
];
}
}