2022-03-19 03:31:27 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\VKAPI\Handlers ;
use Chandler\Database\DatabaseConnection ;
use openvk\Web\Models\Repositories\Posts as PostsRepo ;
2022-11-06 14:30:54 +03:00
use openvk\Web\Models\Entities\User ;
2022-03-19 03:31:27 +03:00
use openvk\VKAPI\Handlers\Wall ;
final class Newsfeed extends VKAPIRequestHandler
{
2023-06-01 13:04:18 +03:00
function get ( string $fields = " " , int $start_from = 0 , int $start_time = 0 , int $end_time = 0 , int $offset = 0 , int $count = 30 , int $extended = 0 , int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0 )
2022-03-19 03:31:27 +03:00
{
$this -> requireUser ();
2023-03-13 17:45:45 +03:00
if ( $forGodSakePleaseDoNotReportAboutMyOnlineActivity == 0 )
{
$this -> getUser () -> updOnline ( $this -> getPlatform ());
}
2022-03-19 03:31:27 +03:00
$id = $this -> getUser () -> getId ();
$subs = DatabaseConnection :: i ()
-> getContext ()
-> table ( " subscriptions " )
-> where ( " follower " , $id );
$ids = array_map ( function ( $rel ) {
return $rel -> target * ( $rel -> model === " openvk \ Web \ Models \ Entities \ User " ? 1 : - 1 );
}, iterator_to_array ( $subs ));
$ids [] = $this -> getUser () -> getId ();
$posts = DatabaseConnection :: i ()
2022-07-21 22:13:09 +03:00
-> getContext ()
-> table ( " posts " )
-> select ( " id " )
-> where ( " wall IN (?) " , $ids )
-> where ( " deleted " , 0 )
2022-11-06 14:30:54 +03:00
-> where ( " id < (?) " , empty ( $start_from ) ? PHP_INT_MAX : $start_from )
2023-06-01 13:04:18 +03:00
-> where ( " ? <= created " , empty ( $start_time ) ? 0 : $start_time )
-> where ( " ? >= created " , empty ( $end_time ) ? PHP_INT_MAX : $end_time )
2022-07-21 22:13:09 +03:00
-> order ( " created DESC " );
2022-03-19 03:31:27 +03:00
$rposts = [];
foreach ( $posts -> page (( int ) ( $offset + 1 ), $count ) as $post )
$rposts [] = ( new PostsRepo ) -> get ( $post -> id ) -> getPrettyId ();
2022-07-25 13:25:06 +03:00
$response = ( new Wall ) -> getById ( implode ( ',' , $rposts ), $extended , $fields , $this -> getUser ());
2022-09-04 20:08:22 +03:00
$response -> next_from = end ( end ( $posts -> page (( int ) ( $offset + 1 ), $count ))); // ну и костыли пиздец конечно)
2022-11-06 14:30:54 +03:00
return $response ;
}
2023-06-01 13:04:18 +03:00
function getGlobal ( string $fields = " " , int $start_from = 0 , int $start_time = 0 , int $end_time = 0 , int $offset = 0 , int $count = 30 , int $extended = 0 )
2022-11-06 14:30:54 +03:00
{
$this -> requireUser ();
2023-11-16 19:44:12 +03:00
$queryBase = " FROM `posts` LEFT JOIN `groups` ON GREATEST(`posts`.`wall`, 0) = 0 AND `groups`.`id` = ABS(`posts`.`wall`) WHERE (`groups`.`hide_from_global_feed` = 0 OR `groups`.`name` IS NULL) AND `posts`.`deleted` = 0 AND `posts`.`suggested` = 0 " ;
2022-11-06 14:30:54 +03:00
if ( $this -> getUser () -> getNsfwTolerance () === User :: NSFW_INTOLERANT )
$queryBase .= " AND `nsfw` = 0 " ;
$start_from = empty ( $start_from ) ? PHP_INT_MAX : $start_from ;
2023-06-01 13:04:18 +03:00
$start_time = empty ( $start_time ) ? 0 : $start_time ;
$end_time = empty ( $end_time ) ? PHP_INT_MAX : $end_time ;
$posts = DatabaseConnection :: i () -> getConnection () -> query ( " SELECT `posts`.`id` " . $queryBase . " AND `posts`.`id` <= " . $start_from . " AND " . $start_time . " <= `posts`.`created` AND `posts`.`created` <= " . $end_time . " ORDER BY `created` DESC LIMIT " . $count . " OFFSET " . $offset );
2022-11-06 14:30:54 +03:00
$rposts = [];
$ids = [];
foreach ( $posts as $post ) {
$rposts [] = ( new PostsRepo ) -> get ( $post -> id ) -> getPrettyId ();
$ids [] = $post -> id ;
}
$response = ( new Wall ) -> getById ( implode ( ',' , $rposts ), $extended , $fields , $this -> getUser ());
$response -> next_from = end ( $ids );
2022-07-25 13:25:06 +03:00
return $response ;
2022-03-19 03:31:27 +03:00
}
}