2020-08-12 14:36:18 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\VKAPI\Handlers ;
use openvk\Web\Models\Entities\User ;
2020-11-01 01:36:35 +03:00
use openvk\Web\Models\Entities\Notifications\ { WallPostNotification };
2020-08-12 14:36:18 +03:00
use openvk\Web\Models\Repositories\Users as UsersRepo ;
2020-11-01 01:36:35 +03:00
use openvk\Web\Models\Entities\Club ;
use openvk\Web\Models\Repositories\Clubs as ClubsRepo ;
2020-08-12 14:36:18 +03:00
use openvk\Web\Models\Entities\Post ;
use openvk\Web\Models\Entities\Postable ;
use openvk\Web\Models\Repositories\Posts as PostsRepo ;
final class Wall extends VKAPIRequestHandler
{
function get ( string $owner_id , string $domain = " " , int $offset = 0 , int $count = 30 , int $extended = 0 ) : object
{
$posts = new PostsRepo ;
$items = [];
2021-11-24 22:24:44 +03:00
$profiles = [];
$groups = [];
$count = $posts -> getPostCountOnUserWall (( int ) $owner_id );
2020-08-12 14:36:18 +03:00
2021-11-24 22:24:44 +03:00
foreach ( $posts -> getPostsFromUsersWall (( int ) $owner_id , 1 , $count , $offset ) as $post ) {
$from_id = get_class ( $post -> getOwner ()) == " openvk \ Web \ Models \ Entities \ Club " ? $post -> getOwner () -> getId () * ( - 1 ) : $post -> getOwner () -> getId ();
2022-03-25 14:05:44 +03:00
2022-05-01 17:04:59 +03:00
$attachments = [];
foreach ( $post -> getChildren () as $attachment ) {
if ( $attachment instanceof \openvk\Web\Models\Entities\Photo ) {
if ( $attachment -> isDeleted ())
continue ;
2022-03-25 14:05:44 +03:00
$attachments [] = [
" type " => " photo " ,
" photo " => [
2022-05-08 13:06:26 +03:00
" album_id " => $attachment -> getAlbum () ? $attachment -> getAlbum () -> getId () : NULL ,
2022-04-24 15:38:53 +03:00
" date " => $attachment -> getPublicationTime () -> timestamp (),
" id " => $attachment -> getVirtualId (),
2022-03-25 14:05:44 +03:00
" owner_id " => $attachment -> getOwner () -> getId (),
2022-04-24 15:38:53 +03:00
" sizes " => array_values ( $attachment -> getVkApiSizes ()),
" text " => " " ,
2022-03-25 14:05:44 +03:00
" has_tags " => false
]
];
}
}
2020-08-12 14:36:18 +03:00
$items [] = ( object )[
" id " => $post -> getVirtualId (),
2021-11-24 22:24:44 +03:00
" from_id " => $from_id ,
2020-08-12 14:36:18 +03:00
" owner_id " => $post -> getTargetWall (),
" date " => $post -> getPublicationTime () -> timestamp (),
" post_type " => " post " ,
2022-07-21 09:40:42 +03:00
" text " => $post -> getText ( false ),
2022-05-08 13:06:26 +03:00
" can_edit " => 0 , # TODO
2020-08-12 14:36:18 +03:00
" can_delete " => $post -> canBeDeletedBy ( $this -> getUser ()),
2021-11-24 22:24:44 +03:00
" can_pin " => $post -> canBePinnedBy ( $this -> getUser ()),
2022-05-08 13:06:26 +03:00
" can_archive " => false , # TODO MAYBE
2020-08-12 14:36:18 +03:00
" is_archived " => false ,
2021-11-24 22:24:44 +03:00
" is_pinned " => $post -> isPinned (),
2022-03-25 14:05:44 +03:00
" attachments " => $attachments ,
2020-08-12 14:36:18 +03:00
" post_source " => ( object )[ " type " => " vk " ],
" comments " => ( object )[
" count " => $post -> getCommentsCount (),
" can_post " => 1
],
" likes " => ( object )[
" count " => $post -> getLikesCount (),
" user_likes " => ( int ) $post -> hasLikeFrom ( $this -> getUser ()),
" can_like " => 1 ,
" can_publish " => 1 ,
],
" reposts " => ( object )[
" count " => $post -> getRepostCount (),
" user_reposted " => 0
]
];
2021-11-24 22:24:44 +03:00
if ( $from_id > 0 )
$profiles [] = $from_id ;
else
$groups [] = $from_id * - 1 ;
2022-03-25 22:29:29 +03:00
2022-05-08 13:06:26 +03:00
$attachments = NULL ; # free attachments so it will not clone everythingg
2020-08-12 14:36:18 +03:00
}
2021-11-24 22:24:44 +03:00
if ( $extended == 1 )
{
$profiles = array_unique ( $profiles );
$groups = array_unique ( $groups );
$profilesFormatted = [];
$groupsFormatted = [];
2020-08-12 14:36:18 +03:00
2021-11-24 22:24:44 +03:00
foreach ( $profiles as $prof ) {
$user = ( new UsersRepo ) -> get ( $prof );
$profilesFormatted [] = ( object )[
" first_name " => $user -> getFirstName (),
" id " => $user -> getId (),
" last_name " => $user -> getLastName (),
" can_access_closed " => false ,
" is_closed " => false ,
" sex " => $user -> isFemale () ? 1 : 2 ,
" screen_name " => $user -> getShortCode (),
" photo_50 " => $user -> getAvatarUrl (),
" photo_100 " => $user -> getAvatarUrl (),
" online " => $user -> isOnline ()
];
}
2020-08-12 14:36:18 +03:00
2021-11-24 22:24:44 +03:00
foreach ( $groups as $g ) {
$group = ( new ClubsRepo ) -> get ( $g );
$groupsFormatted [] = ( object )[
" id " => $group -> getId (),
" name " => $group -> getName (),
" screen_name " => $group -> getShortCode (),
" is_closed " => 0 ,
" type " => " group " ,
" photo_50 " => $group -> getAvatarUrl (),
" photo_100 " => $group -> getAvatarUrl (),
" photo_200 " => $group -> getAvatarUrl (),
];
}
return ( object )[
" count " => $count ,
" items " => ( array ) $items ,
" profiles " => ( array ) $profilesFormatted ,
" groups " => ( array ) $groupsFormatted
];
}
2020-08-12 14:36:18 +03:00
else
2021-11-24 22:24:44 +03:00
return ( object )[
" count " => $count ,
" items " => ( array ) $items
];
2020-08-12 14:36:18 +03:00
}
2020-11-01 01:36:35 +03:00
2022-05-08 13:06:26 +03:00
function getById ( string $posts , int $extended = 0 , string $fields = " " , User $user = NULL )
2022-03-19 03:31:27 +03:00
{
2022-05-08 13:06:26 +03:00
if ( $user == NULL ) $user = $this -> getUser (); # костыли костыли крылышки
2022-03-19 03:31:27 +03:00
$items = [];
$profiles = [];
$groups = [];
# $count = $posts->getPostCountOnUserWall((int) $owner_id);
$psts = explode ( " , " , $posts );
foreach ( $psts as $pst )
{
$id = explode ( " _ " , $pst );
$post = ( new PostsRepo ) -> getPostById ( intval ( $id [ 0 ]), intval ( $id [ 1 ]));
if ( $post ) {
$from_id = get_class ( $post -> getOwner ()) == " openvk \ Web \ Models \ Entities \ Club " ? $post -> getOwner () -> getId () * ( - 1 ) : $post -> getOwner () -> getId ();
2022-03-25 14:05:44 +03:00
$attachments ;
foreach ( $post -> getChildren () as $attachment )
{
if ( $attachment instanceof \openvk\Web\Models\Entities\Photo )
{
$attachments [] = [
" type " => " photo " ,
" photo " => [
2022-05-08 13:06:26 +03:00
" album_id " => $attachment -> getAlbum () ? $attachment -> getAlbum () -> getId () : NULL ,
2022-03-25 14:05:44 +03:00
" date " => $attachment -> getPublicationTime () -> timestamp (),
" id " => $attachment -> getVirtualId (),
" owner_id " => $attachment -> getOwner () -> getId (),
2022-04-23 21:04:54 +03:00
" sizes " => array (
[
" height " => 2560 ,
" url " => $attachment -> getURLBySizeId ( " normal " ),
2022-03-25 14:05:44 +03:00
" type " => " m " ,
2022-04-23 21:04:54 +03:00
" width " => 2560 ,
],
[
" height " => 130 ,
" url " => $attachment -> getURLBySizeId ( " tiny " ),
" type " => " o " ,
" width " => 130 ,
],
[
" height " => 604 ,
" url " => $attachment -> getURLBySizeId ( " normal " ),
" type " => " p " ,
" width " => 604 ,
],
[
" height " => 807 ,
" url " => $attachment -> getURLBySizeId ( " large " ),
" type " => " q " ,
" width " => 807 ,
],
[
" height " => 1280 ,
" url " => $attachment -> getURLBySizeId ( " larger " ),
" type " => " r " ,
" width " => 1280 ,
],
[
2022-05-08 13:06:26 +03:00
" height " => 75 , # Для временного компросима оставляю статическое число. Если каждый раз обращаться к файлу за количеством пикселов, то наступает пuпuс ька полная с производительностью, так что пока так
2022-04-23 21:04:54 +03:00
" url " => $attachment -> getURLBySizeId ( " miniscule " ),
" type " => " s " ,
" width " => 75 ,
2022-03-25 14:05:44 +03:00
]),
" text " => " " ,
" has_tags " => false
]
];
}
}
2022-03-19 03:31:27 +03:00
$items [] = ( object )[
" id " => $post -> getVirtualId (),
" from_id " => $from_id ,
" owner_id " => $post -> getTargetWall (),
" date " => $post -> getPublicationTime () -> timestamp (),
" post_type " => " post " ,
2022-07-21 09:40:42 +03:00
" text " => $post -> getText ( false ),
2022-05-08 13:06:26 +03:00
" can_edit " => 0 , # TODO
2022-03-19 03:31:27 +03:00
" can_delete " => $post -> canBeDeletedBy ( $user ),
" can_pin " => $post -> canBePinnedBy ( $user ),
2022-05-08 13:06:26 +03:00
" can_archive " => false , # TODO MAYBE
2022-03-19 03:31:27 +03:00
" is_archived " => false ,
" is_pinned " => $post -> isPinned (),
" post_source " => ( object )[ " type " => " vk " ],
2022-03-25 14:05:44 +03:00
" attachments " => $attachments ,
2022-03-19 03:31:27 +03:00
" comments " => ( object )[
" count " => $post -> getCommentsCount (),
" can_post " => 1
],
" likes " => ( object )[
" count " => $post -> getLikesCount (),
" user_likes " => ( int ) $post -> hasLikeFrom ( $user ),
" can_like " => 1 ,
" can_publish " => 1 ,
],
" reposts " => ( object )[
" count " => $post -> getRepostCount (),
" user_reposted " => 0
]
];
if ( $from_id > 0 )
$profiles [] = $from_id ;
else
$groups [] = $from_id * - 1 ;
2022-03-25 22:29:29 +03:00
2022-05-08 13:06:26 +03:00
$attachments = NULL ; # free attachments so it will not clone everythingg
2022-03-19 03:31:27 +03:00
}
}
if ( $extended == 1 )
{
$profiles = array_unique ( $profiles );
$groups = array_unique ( $groups );
$profilesFormatted = [];
$groupsFormatted = [];
foreach ( $profiles as $prof ) {
$user = ( new UsersRepo ) -> get ( $prof );
$profilesFormatted [] = ( object )[
" first_name " => $user -> getFirstName (),
" id " => $user -> getId (),
" last_name " => $user -> getLastName (),
" can_access_closed " => false ,
" is_closed " => false ,
" sex " => $user -> isFemale () ? 1 : 2 ,
" screen_name " => $user -> getShortCode (),
" photo_50 " => $user -> getAvatarUrl (),
" photo_100 " => $user -> getAvatarUrl (),
" online " => $user -> isOnline ()
];
}
foreach ( $groups as $g ) {
$group = ( new ClubsRepo ) -> get ( $g );
$groupsFormatted [] = ( object )[
" id " => $group -> getId (),
" name " => $group -> getName (),
" screen_name " => $group -> getShortCode (),
" is_closed " => 0 ,
" type " => " group " ,
" photo_50 " => $group -> getAvatarUrl (),
" photo_100 " => $group -> getAvatarUrl (),
" photo_200 " => $group -> getAvatarUrl (),
];
}
return ( object )[
" items " => ( array ) $items ,
" profiles " => ( array ) $profilesFormatted ,
" groups " => ( array ) $groupsFormatted
];
}
else
return ( object )[
" items " => ( array ) $items
];
}
2022-01-15 19:50:03 +03:00
function post ( string $owner_id , string $message = " " , int $from_group = 0 , int $signed = 0 ) : object
2020-11-01 01:36:35 +03:00
{
$this -> requireUser ();
$owner_id = intval ( $owner_id );
$wallOwner = ( $owner_id > 0 ? ( new UsersRepo ) -> get ( $owner_id ) : ( new ClubsRepo ) -> get ( $owner_id * - 1 ))
? ? $this -> fail ( 18 , " User was deleted or banned " );
if ( $owner_id > 0 )
$canPost = $wallOwner -> getPrivacyPermission ( " wall.write " , $this -> getUser ());
else if ( $owner_id < 0 )
if ( $wallOwner -> canBeModifiedBy ( $this -> getUser ()))
$canPost = true ;
else
$canPost = $wallOwner -> canPost ();
else
$canPost = false ;
if ( $canPost == false ) $this -> fail ( 15 , " Access denied " );
2022-01-15 18:15:37 +03:00
$anon = OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " wall " ][ " anonymousPosting " ][ " enable " ];
if ( $wallOwner instanceof Club && $from_group == 1 && $signed != 1 && $anon ) {
$manager = $wallOwner -> getManager ( $this -> getUser ());
if ( $manager )
$anon = $manager -> isHidden ();
elseif ( $this -> getUser () -> getId () === $wallOwner -> getOwner () -> getId ())
$anon = $wallOwner -> isOwnerHidden ();
} else {
$anon = false ;
}
2020-11-01 01:36:35 +03:00
$flags = 0 ;
2022-01-15 18:51:04 +03:00
if ( $from_group == 1 && $wallOwner instanceof Club && $wallOwner -> canBeModifiedBy ( $this -> getUser ()))
2020-11-01 01:36:35 +03:00
$flags |= 0 b10000000 ;
2021-11-15 11:43:15 +03:00
if ( $signed == 1 )
$flags |= 0 b01000000 ;
2020-11-01 01:36:35 +03:00
2022-05-08 13:06:26 +03:00
# TODO: Compatible implementation of this
2022-01-15 18:15:37 +03:00
try {
2022-05-08 13:06:26 +03:00
$photo = NULL ;
$video = NULL ;
2022-01-15 18:15:37 +03:00
if ( $_FILES [ " photo " ][ " error " ] === UPLOAD_ERR_OK ) {
2022-05-08 13:06:26 +03:00
$album = NULL ;
2022-01-15 18:15:37 +03:00
if ( ! $anon && $owner_id > 0 && $owner_id === $this -> getUser () -> getId ())
$album = ( new AlbumsRepo ) -> getUserWallAlbum ( $wallOwner );
$photo = Photo :: fastMake ( $this -> getUser () -> getId (), $message , $_FILES [ " photo " ], $album , $anon );
}
if ( $_FILES [ " video " ][ " error " ] === UPLOAD_ERR_OK )
$video = Video :: fastMake ( $this -> getUser () -> getId (), $message , $_FILES [ " video " ], $anon );
} catch ( \DomainException $ex ) {
$this -> fail ( - 156 , " The media file is corrupted " );
} catch ( ISE $ex ) {
$this -> fail ( - 156 , " The media file is corrupted or too large " );
}
2022-01-15 19:50:03 +03:00
if ( empty ( $message ) && ! $photo && ! $video )
$this -> fail ( 100 , " Required parameter 'message' missing. " );
2020-11-01 01:36:35 +03:00
try {
$post = new Post ;
$post -> setOwner ( $this -> getUser () -> getId ());
$post -> setWall ( $owner_id );
$post -> setCreated ( time ());
$post -> setContent ( $message );
$post -> setFlags ( $flags );
$post -> save ();
} catch ( \LogicException $ex ) {
$this -> fail ( 100 , " One of the parameters specified was missing or invalid " );
}
2022-01-15 18:15:37 +03:00
if ( ! is_null ( $photo ))
$post -> attach ( $photo );
if ( ! is_null ( $video ))
$post -> attach ( $video );
2020-11-01 01:36:35 +03:00
if ( $wall > 0 && $wall !== $this -> user -> identity -> getId ())
( new WallPostNotification ( $wallOwner , $post , $this -> user -> identity )) -> emit ();
return ( object )[ " post_id " => $post -> getVirtualId ()];
}
2020-08-12 14:36:18 +03:00
}