Wall [Experimental]: Allow to embed videos in posts

This commit is contained in:
Celestora 2021-10-13 22:51:28 +03:00
parent a53826f46d
commit 1c341874a0
9 changed files with 62 additions and 39 deletions

View file

@ -93,10 +93,10 @@ class Video extends Media
function isDeleted(): bool function isDeleted(): bool
{ {
if ($this->getRecord()->deleted == 1) if ($this->getRecord()->deleted == 1)
return TRUE; return TRUE;
else else
return FALSE; return FALSE;
} }
function deleteVideo(): void function deleteVideo(): void
@ -105,4 +105,18 @@ class Video extends Media
$this->unwire(); $this->unwire();
$this->save(); $this->save();
} }
static function fastMake(int $owner, string $description = "", array $file, bool $unlisted = true): Video
{
$video = new Video;
$video->setOwner($owner);
$video->setName("Unnamed Video.ogv");
$video->setDescription(ovk_proc_strtr($description, 300));
$video->setCreated(time());
$video->setFile($file);
$video->setUnlisted($unlisted);
$video->save();
return $video;
}
} }

View file

@ -37,7 +37,7 @@ class Videos
function getByUser(User $user, int $page = 1, ?int $perPage = NULL): \Traversable function getByUser(User $user, int $page = 1, ?int $perPage = NULL): \Traversable
{ {
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; $perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
foreach($this->videos->where("owner", $user->getId())->where("deleted", 0)->page($page, $perPage) as $video) foreach($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])->page($page, $perPage) as $video)
yield new Video($video); yield new Video($video);
} }

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\Web\Presenters; namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Post, Photo, Club, User}; use openvk\Web\Models\Entities\{Post, Photo, Video, Club, User};
use openvk\Web\Models\Entities\Notifications\{LikeNotification, RepostNotification, WallPostNotification}; use openvk\Web\Models\Entities\Notifications\{LikeNotification, RepostNotification, WallPostNotification};
use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums}; use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums};
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
@ -186,44 +186,42 @@ final class WallPresenter extends OpenVKPresenter
if($this->postParam("force_sign") === "on") if($this->postParam("force_sign") === "on")
$flags |= 0b01000000; $flags |= 0b01000000;
try {
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) { $photo = NULL;
try { $video = NULL;
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
$album = NULL; $album = NULL;
if($wall > 0 && $wall === $this->user->id) if($wall > 0 && $wall === $this->user->id)
$album = (new Albums)->getUserWallAlbum($wallOwner); $album = (new Albums)->getUserWallAlbum($wallOwner);
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album); $photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album);
} catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
} }
$post = new Post; if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK) {
$post->setOwner($this->user->id); $video = Video::fastMake($this->user->id, $this->postParam("text"), $_FILES["_vid_attachment"]);
$post->setWall($wall);
$post->setCreated(time());
$post->setContent($this->postParam("text"));
$post->setFlags($flags);
$post->setNsfw($this->postParam("nsfw") === "on");
$post->save();
$post->attach($photo);
} elseif($this->postParam("text")) {
try {
$post = new Post;
$post->setOwner($this->user->id);
$post->setWall($wall);
$post->setCreated(time());
$post->setContent($this->postParam("text"));
$post->setFlags($flags);
$post->setNsfw($this->postParam("nsfw") === "on");
$post->save();
} catch(\LogicException $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой.");
} }
} else { } catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой."); $this->flashFail("err", "Не удалось опубликовать пост", "Файл медиаконтента повреждён или слишком велик.");
} }
if(empty($this->postParam("text")) && !$photo && !$video)
$this->flashFail("err", "Не удалось опубликовать пост", "Пост пустой или слишком большой.");
$post = new Post;
$post->setOwner($this->user->id);
$post->setWall($wall);
$post->setCreated(time());
$post->setContent($this->postParam("text"));
$post->setFlags($flags);
$post->setNsfw($this->postParam("nsfw") === "on");
$post->save();
if(!is_null($photo))
$post->attach($photo);
if(!is_null($video))
$post->attach($video);
if($wall > 0 && $wall !== $this->user->identity->getId()) if($wall > 0 && $wall !== $this->user->identity->getId())
(new WallPostNotification($wallOwner, $post, $this->user->identity))->emit(); (new WallPostNotification($wallOwner, $post, $this->user->identity))->emit();

View file

@ -8,6 +8,8 @@
<img class="media" src="/assets/packages/static/openvk/img/camera_200.png" alt="{_"attach_no_longer_available"}" /> <img class="media" src="/assets/packages/static/openvk/img/camera_200.png" alt="{_"attach_no_longer_available"}" />
</a> </a>
{/if} {/if}
{elseif $attachment instanceof \openvk\Web\Models\Entities\Video}
<video class="media" src="{$attachment->getURL()}" controls="controls"></video>
{elseif $attachment instanceof \openvk\Web\Models\Entities\Post} {elseif $attachment instanceof \openvk\Web\Models\Entities\Post}
{php $GLOBALS["_nesAttGloCou"] = (isset($GLOBALS["_nesAttGloCou"]) ? $GLOBALS["_nesAttGloCou"] : 0) + 1} {php $GLOBALS["_nesAttGloCou"] = (isset($GLOBALS["_nesAttGloCou"]) ? $GLOBALS["_nesAttGloCou"] : 0) + 1}
{if $GLOBALS["_nesAttGloCou"] > 2} {if $GLOBALS["_nesAttGloCou"] > 2}

View file

@ -31,7 +31,8 @@
<input type="checkbox" name="nsfw" /> {_"contains_nsfw"} <input type="checkbox" name="nsfw" /> {_"contains_nsfw"}
</label> </label>
</div> </div>
<input type="file" name="_pic_attachment" accept="image/*" style="display:none;" /> <input type="file" class="postFileSel" id="postFilePic" name="_pic_attachment" accept="image/*" style="display:none;" />
<input type="file" class="postFileSel" id="postFileVid" name="_vid_attachment" accept="video/*" style="display:none;" />
<input type="hidden" name="type" value="1" /> <input type="hidden" name="type" value="1" />
<input type="hidden" name="hash" value="{$csrfToken}" /> <input type="hidden" name="hash" value="{$csrfToken}" />
<br/> <br/>
@ -46,6 +47,10 @@
<img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-x-egon.png" /> <img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-x-egon.png" />
{_attach_photo} {_attach_photo}
</a> </a>
<a href="javascript:void(document.querySelector('input[name=_vid_attachment]').click());">
<img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-vnd.rn-realmedia.png" />
Прикрепить видео
</a>
<a n:if="$graffiti ?? false" href="javascript:initGraffiti();"> <a n:if="$graffiti ?? false" href="javascript:initGraffiti();">
<img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/actions/draw-brush.png" /> <img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/actions/draw-brush.png" />
Нарисовать граффити Нарисовать граффити

View file

@ -1,4 +1,4 @@
.post-nsfw .post-content img.media { .post-nsfw .post-content .media {
filter: saturate(0.8) blur(15px); filter: saturate(0.8) blur(15px);
} }
@ -7,7 +7,7 @@
position: relative; position: relative;
} }
.post-nsfw .post-content .attachment:active img.media { .post-nsfw .post-content .attachment:active .media {
filter: none; filter: none;
} }

View file

@ -513,7 +513,7 @@ h4 {
width: 102%; width: 102%;
} }
.post-content img.media { .post-content .media {
max-width: 100%; max-width: 100%;
image-rendering: -webkit-optimize-contrast; image-rendering: -webkit-optimize-contrast;
} }

View file

@ -24,6 +24,9 @@ function trim(string) {
function handleUpload() { function handleUpload() {
console.warn("блять..."); console.warn("блять...");
u(".postFileSel").not("#" + this.id).each(input => input.value = null);
var indicator = u(".post-upload"); var indicator = u(".post-upload");
var file = this.files[0]; var file = this.files[0];
if(typeof file === "undefined") { if(typeof file === "undefined") {
@ -108,4 +111,4 @@ u("#wall-post-input").on("input", function(e) {
// textArea.style.height = (newHeight > originalHeight ? (newHeight + boost) : originalHeight) + "px"; // textArea.style.height = (newHeight > originalHeight ? (newHeight + boost) : originalHeight) + "px";
}); });
u("input[name=_pic_attachment]").on("change", handleUpload); u(".postFileSel").on("change", handleUpload);

View file

@ -0,0 +1 @@
ALTER TABLE `videos` ADD `unlisted` BOOLEAN NOT NULL DEFAULT FALSE AFTER `link`, ADD INDEX (`unlisted`);