2021-09-18 05:37:41 +02:00
< ? php
2022-01-02 10:49:50 +01:00
/**
* @ 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 />.
*
*/
2021-09-18 05:37:41 +02:00
2021-10-21 23:18:08 +02:00
namespace Friendica\Navigation\Notifications\Repository ;
2021-09-18 05:37:41 +02:00
use Exception ;
use Friendica\BaseCollection ;
2021-10-21 23:18:08 +02:00
use Friendica\BaseRepository ;
2022-03-17 02:56:22 +01:00
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues ;
2021-09-18 05:37:41 +02:00
use Friendica\Database\Database ;
use Friendica\Database\DBA ;
2022-03-19 16:25:00 +01:00
use Friendica\Model\Post\UserNotification ;
2021-09-18 05:37:41 +02:00
use Friendica\Model\Verb ;
use Friendica\Navigation\Notifications\Collection ;
use Friendica\Navigation\Notifications\Entity ;
use Friendica\Navigation\Notifications\Factory ;
use Friendica\Network\HTTPException\NotFoundException ;
use Friendica\Util\DateTimeFormat ;
use Psr\Log\LoggerInterface ;
2021-10-21 23:18:08 +02:00
class Notification extends BaseRepository
2021-09-18 05:37:41 +02:00
{
/** @var Factory\Notification */
protected $factory ;
protected static $table_name = 'notification' ;
2022-03-17 02:56:22 +01:00
/** @var IManagePersonalConfigValues */
private $pconfig ;
public function __construct ( IManagePersonalConfigValues $pconfig , Database $database , LoggerInterface $logger , Factory\Notification $factory )
2021-09-18 05:37:41 +02:00
{
2022-03-03 14:49:07 +01:00
parent :: __construct ( $database , $logger , $factory );
2022-03-17 02:56:22 +01:00
$this -> pconfig = $pconfig ;
2021-09-18 05:37:41 +02:00
}
/**
* @ param array $condition
* @ param array $params
* @ return Entity\Notification
* @ throws NotFoundException
*/
private function selectOne ( array $condition , array $params = []) : Entity\Notification
{
return parent :: _selectOne ( $condition , $params );
}
private function select ( array $condition , array $params = []) : Collection\Notifications
{
return new Collection\Notifications ( parent :: _select ( $condition , $params ) -> getArrayCopy ());
}
public function countForUser ( $uid , array $condition , array $params = []) : int
{
$condition = DBA :: mergeConditions ( $condition , [ 'uid' => $uid ]);
return $this -> count ( $condition , $params );
}
public function existsForUser ( $uid , array $condition ) : bool
{
$condition = DBA :: mergeConditions ( $condition , [ 'uid' => $uid ]);
return $this -> exists ( $condition );
}
/**
* @ param int $id
* @ return Entity\Notification
* @ throws NotFoundException
*/
public function selectOneById ( int $id ) : Entity\Notification
{
return $this -> selectOne ([ 'id' => $id ]);
}
public function selectOneForUser ( int $uid , array $condition , array $params = []) : Entity\Notification
{
$condition = DBA :: mergeConditions ( $condition , [ 'uid' => $uid ]);
return $this -> selectOne ( $condition , $params );
}
public function selectForUser ( int $uid , array $condition = [], array $params = []) : Collection\Notifications
{
$condition = DBA :: mergeConditions ( $condition , [ 'uid' => $uid ]);
return $this -> select ( $condition , $params );
}
2022-03-17 02:56:22 +01:00
/**
* Returns only the most recent notifications for the same conversation or contact
*
* @ param int $uid
* @ return Collection\Notifications
* @ throws Exception
*/
public function selectDetailedForUser ( int $uid ) : Collection\Notifications
{
2022-06-05 17:02:03 +02:00
$condition = [ " `type` & ? != 0 " , $this -> pconfig -> get ( $uid , 'system' , 'notify_type' , 3 | 72 | 4 | 16 | 32 ) | 128 | 256 ];
2022-03-17 02:56:22 +01:00
if ( ! $this -> pconfig -> get ( $uid , 'system' , 'notify_like' )) {
2022-06-05 18:54:29 +02:00
$condition = DBA :: mergeConditions ( $condition , [ 'NOT `vid` IN (?, ?)' , Verb :: getID ( \Friendica\Protocol\Activity :: LIKE ), Verb :: getID ( \Friendica\Protocol\Activity :: DISLIKE )]);
2022-03-17 02:56:22 +01:00
}
if ( ! $this -> pconfig -> get ( $uid , 'system' , 'notify_announce' )) {
$condition = DBA :: mergeConditions ( $condition , [ '`vid` != ?' , Verb :: getID ( \Friendica\Protocol\Activity :: ANNOUNCE )]);
}
2022-03-19 10:27:49 +01:00
return $this -> selectForUser ( $uid , $condition , [ 'limit' => 50 , 'order' => [ 'id' => true ]]);
2022-03-17 02:56:22 +01:00
}
2022-03-05 21:34:09 +01:00
/**
* Returns only the most recent notifications for the same conversation or contact
*
* @ param int $uid
* @ return Collection\Notifications
* @ throws Exception
*/
public function selectDigestForUser ( int $uid ) : Collection\Notifications
{
2022-06-05 17:02:03 +02:00
$values = [ $uid , $this -> pconfig -> get ( $uid , 'system' , 'notify_type' , 3 | 72 | 4 | 16 | 32 ) | 128 | 256 ];
2022-03-17 02:56:22 +01:00
$like_condition = '' ;
if ( ! $this -> pconfig -> get ( $uid , 'system' , 'notify_like' )) {
2022-06-05 18:54:29 +02:00
$like_condition = 'AND NOT `vid` IN (?, ?)' ;
2022-03-17 02:56:22 +01:00
$values [] = Verb :: getID ( \Friendica\Protocol\Activity :: LIKE );
2022-06-05 18:54:29 +02:00
$values [] = Verb :: getID ( \Friendica\Protocol\Activity :: DISLIKE );
2022-03-17 02:56:22 +01:00
}
$announce_condition = '' ;
if ( ! $this -> pconfig -> get ( $uid , 'system' , 'notify_announce' )) {
$announce_condition = 'AND vid != ?' ;
$values [] = Verb :: getID ( \Friendica\Protocol\Activity :: ANNOUNCE );
}
2022-03-05 21:34:09 +01:00
$rows = $this -> db -> p ( "
SELECT notification .*
FROM notification
2022-03-19 16:25:00 +01:00
WHERE `id` IN (
2022-03-05 21:34:09 +01:00
SELECT MAX ( `id` )
2022-03-19 16:25:00 +01:00
FROM `notification`
2022-06-05 17:02:03 +02:00
WHERE `uid` = ? AND `type` & ? != 0
2022-03-17 02:56:22 +01:00
$like_condition
$announce_condition
2022-03-05 21:34:09 +01:00
GROUP BY IFNULL ( `parent-uri-id` , `actor-id` )
)
ORDER BY `seen` , `id` DESC
LIMIT 50
2022-03-17 02:56:22 +01:00
" , ... $values );
2022-03-05 21:34:09 +01:00
$Entities = new Collection\Notifications ();
foreach ( $rows as $fields ) {
$Entities [] = $this -> factory -> createFromTableRow ( $fields );
}
return $Entities ;
}
2021-09-18 05:37:41 +02:00
public function selectAllForUser ( int $uid ) : Collection\Notifications
{
return $this -> selectForUser ( $uid );
}
/**
* @ param array $condition
* @ param array $params
* @ param int | null $min_id Retrieve models with an id no fewer than this , as close to it as possible
* @ param int | null $max_id Retrieve models with an id no greater than this , as close to it as possible
* @ param int $limit
* @ return BaseCollection
* @ throws Exception
* @ see _selectByBoundaries
*/
public function selectByBoundaries ( array $condition = [], array $params = [], int $min_id = null , int $max_id = null , int $limit = self :: LIMIT )
{
$BaseCollection = parent :: _selectByBoundaries ( $condition , $params , $min_id , $max_id , $limit );
return new Collection\Notifications ( $BaseCollection -> getArrayCopy (), $BaseCollection -> getTotalCount ());
}
public function setAllSeenForUser ( int $uid , array $condition = []) : bool
{
$condition = DBA :: mergeConditions ( $condition , [ 'uid' => $uid ]);
return $this -> db -> update ( self :: $table_name , [ 'seen' => true ], $condition );
}
2021-12-28 21:38:18 +01:00
public function setAllDismissedForUser ( int $uid , array $condition = []) : bool
{
$condition = DBA :: mergeConditions ( $condition , [ 'uid' => $uid ]);
return $this -> db -> update ( self :: $table_name , [ 'dismissed' => true ], $condition );
}
2021-09-18 05:37:41 +02:00
/**
* @ param Entity\Notification $Notification
* @ return Entity\Notification
* @ throws Exception
*/
public function save ( Entity\Notification $Notification ) : Entity\Notification
{
$fields = [
'uid' => $Notification -> uid ,
'vid' => Verb :: getID ( $Notification -> verb ),
'type' => $Notification -> type ,
'actor-id' => $Notification -> actorId ,
'target-uri-id' => $Notification -> targetUriId ,
'parent-uri-id' => $Notification -> parentUriId ,
'seen' => $Notification -> seen ,
2021-12-28 21:38:18 +01:00
'dismissed' => $Notification -> dismissed ,
2021-09-18 05:37:41 +02:00
];
if ( $Notification -> id ) {
$this -> db -> update ( self :: $table_name , $fields , [ 'id' => $Notification -> id ]);
} else {
$fields [ 'created' ] = DateTimeFormat :: utcNow ();
$this -> db -> insert ( self :: $table_name , $fields );
$Notification = $this -> selectOneById ( $this -> db -> lastInsertId ());
}
return $Notification ;
}
2022-03-06 12:36:39 +01:00
public function deleteForUserByVerb ( int $uid , string $verb , array $condition = []) : bool
{
$condition [ 'uid' ] = $uid ;
$condition [ 'vid' ] = Verb :: getID ( $verb );
$this -> logger -> notice ( 'deleteForUserByVerb' , [ 'condition' => $condition ]);
return $this -> db -> delete ( self :: $table_name , $condition );
}
2021-09-18 05:37:41 +02:00
}