Skip to content

Commit

Permalink
Add Video Picker (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrilyew authored Sep 17, 2023
1 parent 468eba8 commit 43de40a
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 36 deletions.
44 changes: 43 additions & 1 deletion ServiceAPI/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\{Posts, Notes};
use openvk\Web\Models\Repositories\{Posts, Notes, Videos};

class Wall implements Handler
{
Expand All @@ -15,6 +15,7 @@ function __construct(?User $user)
$this->user = $user;
$this->posts = new Posts;
$this->notes = new Notes;
$this->videos = new Videos;
}

function getPost(int $id, callable $resolve, callable $reject): void
Expand Down Expand Up @@ -95,4 +96,45 @@ function getMyNotes(callable $resolve, callable $reject)

$resolve($arr);
}

function getVideos(int $page = 1, callable $resolve, callable $reject)
{
$videos = $this->videos->getByUser($this->user, $page, 8);
$count = $this->videos->getUserVideosCount($this->user);

$arr = [
"count" => $count,
"items" => [],
];

foreach($videos as $video) {
$res = json_decode(json_encode($video->toVkApiStruct()), true);
$res["video"]["author_name"] = $video->getOwner()->getCanonicalName();

$arr["items"][] = $res;
}

$resolve($arr);
}

function searchVideos(int $page = 1, string $query, callable $resolve, callable $reject)
{
$dbc = $this->videos->find($query);
$videos = $dbc->page($page, 8);
$count = $dbc->size();

$arr = [
"count" => $count,
"items" => [],
];

foreach($videos as $video) {
$res = json_decode(json_encode($video->toVkApiStruct()), true);
$res["video"]["author_name"] = $video->getOwner()->getCanonicalName();

$arr["items"][] = $res;
}

$resolve($arr);
}
}
27 changes: 12 additions & 15 deletions VKAPI/Handlers/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,28 +463,25 @@ function post(string $owner_id, string $message = "", int $from_group = 0, int $
if($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this photo");
$this->fail(100, "Invalid photo");
if(!$attacc->getOwner()->getPrivacyPermission('photos.read', $this->getUser()))
$this->fail(43, "Access to photo denied");

$post->attach($attacc);
} elseif($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this video");
if(!$attacc->getOwner()->getPrivacyPermission('videos.read', $this->getUser()))
$this->fail(43, "Access to video denied");

$post->attach($attacc);
} elseif($attachmentType == "note") {
$attacc = (new NotesRepo)->getNoteById($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Note does not exist");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this note");

if($attacc->getOwner()->getPrivacySetting("notes.read") < 1)
$this->fail(11, "You can't attach note to post, because your notes list is closed. Change it in privacy settings in web-version.");
if(!$attacc->getOwner()->getPrivacyPermission('notes.read', $this->getUser()))
$this->fail(11, "Access to note denied");

$post->attach($attacc);
}
Expand Down Expand Up @@ -678,7 +675,7 @@ function getComment(int $owner_id, int $comment_id, bool $extended = false, stri
return $response;
}

function createComment(int $owner_id, int $post_id, string $message, int $from_group = 0, string $attachments = "") {
function createComment(int $owner_id, int $post_id, string $message = "", int $from_group = 0, string $attachments = "") {
$this->requireUser();
$this->willExecuteWriteAction();

Expand Down Expand Up @@ -736,16 +733,16 @@ function createComment(int $owner_id, int $post_id, string $message, int $from_g
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this photo");
if(!$attacc->getOwner()->getPrivacyPermission('photos.read', $this->getUser()))
$this->fail(11, "Access to photo denied");

$comment->attach($attacc);
} elseif($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this video");
if(!$attacc->getOwner()->getPrivacyPermission('videos.read', $this->getUser()))
$this->fail(11, "Access to video denied");

$comment->attach($attacc);
}
Expand Down
31 changes: 23 additions & 8 deletions Web/Presenters/CommentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Comment, Notifications\MentionNotification, Photo, Video, User, Topic, Post};
use openvk\Web\Models\Entities\Notifications\CommentNotification;
use openvk\Web\Models\Repositories\{Comments, Clubs};
use openvk\Web\Models\Repositories\{Comments, Clubs, Videos};

final class CommentPresenter extends OpenVKPresenter
{
Expand Down Expand Up @@ -73,21 +73,35 @@ function renderMakeComment(string $repo, int $eId): void
# TODO move to trait
try {
$photo = NULL;
$video = NULL;
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
$album = NULL;
if($wall > 0 && $wall === $this->user->id)
$album = (new Albums)->getUserWallAlbum($wallOwner);

$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album);
}

if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK) {
$video = Video::fastMake($this->user->id, $_FILES["_vid_attachment"]["name"], $this->postParam("text"), $_FILES["_vid_attachment"]);
}
} catch(ISE $ex) {
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_file_too_big"));
}

$videos = [];

if(!empty($this->postParam("videos"))) {
$un = rtrim($this->postParam("videos"), ",");
$arr = explode(",", $un);

if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$video = (new Videos)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);

if(!$video || $video->isDeleted())
continue;

$videos[] = $video;
}
}
}

if(empty($this->postParam("text")) && !$photo && !$video)
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_empty"));
Expand All @@ -108,8 +122,9 @@ function renderMakeComment(string $repo, int $eId): void
if(!is_null($photo))
$comment->attach($photo);

if(!is_null($video))
$comment->attach($video);
if(sizeof($videos) > 0)
foreach($videos as $vid)
$comment->attach($vid);

if($entity->getOwner()->getId() !== $this->user->identity->getId())
if(($owner = $entity->getOwner()) instanceof User)
Expand Down
37 changes: 27 additions & 10 deletions Web/Presenters/WallPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use openvk\Web\Models\Exceptions\TooMuchOptionsException;
use openvk\Web\Models\Entities\{Poll, Post, Photo, Video, Club, User};
use openvk\Web\Models\Entities\Notifications\{MentionNotification, RepostNotification, WallPostNotification};
use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums, Notes, Comments};
use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums, Notes, Videos, Comments};
use Chandler\Database\DatabaseConnection;
use Nette\InvalidStateException as ISE;
use Bhaktaraz\RSSGenerator\Item;
Expand Down Expand Up @@ -231,10 +231,7 @@ function renderMakePost(int $wall): void

if(!$canPost)
$this->flashFail("err", tr("not_enough_permissions"), tr("not_enough_permissions_comment"));

if($_FILES["_vid_attachment"] && OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading'])
$this->flashFail("err", tr("error"), tr("video_uploads_disabled"));


$anon = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["enable"];
if($wallOwner instanceof Club && $this->postParam("as_group") === "on" && $this->postParam("force_sign") !== "on" && $anon) {
$manager = $wallOwner->getManager($this->user->identity);
Expand Down Expand Up @@ -263,8 +260,8 @@ function renderMakePost(int $wall): void
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album, $anon);
}

if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK)
$video = Video::fastMake($this->user->id, $_FILES["_vid_attachment"]["name"], $this->postParam("text"), $_FILES["_vid_attachment"], $anon);
/*if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK)
$video = Video::fastMake($this->user->id, $_FILES["_vid_attachment"]["name"], $this->postParam("text"), $_FILES["_vid_attachment"], $anon);*/
} catch(\DomainException $ex) {
$this->flashFail("err", tr("failed_to_publish_post"), tr("media_file_corrupted"));
} catch(ISE $ex) {
Expand Down Expand Up @@ -295,8 +292,27 @@ function renderMakePost(int $wall): void
$this->flashFail("err", " ");
}
}

$videos = [];

if(!empty($this->postParam("videos"))) {
$un = rtrim($this->postParam("videos"), ",");
$arr = explode(",", $un);

if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$video = (new Videos)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);

if(!$video || $video->isDeleted())
continue;

$videos[] = $video;
}
}
}

if(empty($this->postParam("text")) && !$photo && !$video && !$poll && !$note)
if(empty($this->postParam("text")) && !$photo && sizeof($videos) < 1 && !$poll && !$note)
$this->flashFail("err", tr("failed_to_publish_post"), tr("post_is_empty_or_too_big"));

try {
Expand All @@ -316,8 +332,9 @@ function renderMakePost(int $wall): void
if(!is_null($photo))
$post->attach($photo);

if(!is_null($video))
$post->attach($video);
if(sizeof($videos) > 0)
foreach($videos as $vid)
$post->attach($vid);

if(!is_null($poll))
$post->attach($poll);
Expand Down
8 changes: 6 additions & 2 deletions Web/Presenters/templates/components/textArea.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<div class="post-has-note">

</div>
<div class="post-has-videos"></div>

<div n:if="$postOpts ?? true" class="post-opts">
{var $anonEnabled = OPENVK_ROOT_CONF['openvk']['preferences']['wall']['anonymousPosting']['enable']}
{if !is_null($thisUser) && !is_null($club ?? NULL) && $owner < 0}
Expand Down Expand Up @@ -55,7 +57,7 @@
</label>
</div>
<input type="file" class="postFileSel" id="postFilePic" name="_pic_attachment" accept="image/*" style="display:none;" />
<input n:if="!OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading']" type="file" class="postFileSel" id="postFileVid" name="_vid_attachment" accept="video/*" style="display:none;" />
<input type="hidden" name="videos" value="" />
<input type="hidden" name="poll" value="none" />
<input type="hidden" id="note" name="note" value="none" />
<input type="hidden" name="type" value="1" />
Expand All @@ -75,7 +77,7 @@
<img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-x-egon.png" />
{_photo}
</a>
<a n:if="!OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading']" href="javascript:void(document.querySelector('#post-buttons{$textAreaId} input[name=_vid_attachment]').click());">
<a id="videoAttachment">
<img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-vnd.rn-realmedia.png" />
{_video}
</a>
Expand Down Expand Up @@ -105,6 +107,8 @@

setupWallPostInputHandlers({$textAreaId});
});

u("#post-buttons{$textAreaId} input[name='videos']")["nodes"].at(0).value = ""
</script>

{if $graffiti}
Expand Down
34 changes: 34 additions & 0 deletions Web/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,12 @@ body.scrolled .toTop:hover {
display: none;
}

.post-has-videos {
margin-top: 11px;
margin-left: 3px;
color: #3c3c3c;
}

.post-upload::before, .post-has-poll::before, .post-has-note::before {
content: " ";
width: 8px;
Expand All @@ -1477,6 +1483,28 @@ body.scrolled .toTop:hover {
margin-left: 2px;
}

.post-has-video {
padding-bottom: 4px;
cursor: pointer;
}

.post-has-video:hover span {
text-decoration: underline;
}

.post-has-video::before {
content: " ";
width: 14px;
height: 15px;
display: inline-block;
vertical-align: bottom;
background-image: url("/assets/packages/static/openvk/img/video.png");
background-repeat: no-repeat;
margin: 3px;
margin-left: 2px;
margin-bottom: -1px;
}

.post-opts {
margin-top: 10px;
}
Expand Down Expand Up @@ -2702,6 +2730,12 @@ body.article .floating_sidebar, body.article .page_content {
font-size: 12px;
}

.topGrayBlock {
background: #F0F0F0;
height: 37px;
border-bottom: 1px solid #C7C7C7;
}

.edited {
color: #9b9b9b;
}
Expand Down
Binary file added Web/static/img/video.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 43de40a

Please sign in to comment.