refactor parseAttachmetns in bootstrpa

This commit is contained in:
mrilyew 2024-11-03 13:42:31 +03:00
parent 341226cac5
commit 48c7751d6a
6 changed files with 100 additions and 198 deletions

View file

@ -70,64 +70,24 @@ final class CommentPresenter extends OpenVKPresenter
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_when_publishing_comment_description"));
}
}
$photos = [];
if(!empty($this->postParam("photos"))) {
$un = rtrim($this->postParam("photos"), ",");
$arr = explode(",", $un);
if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$photo = (new Photos)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);
if(!$photo || $photo->isDeleted())
continue;
$photos[] = $photo;
}
$horizontal_attachments = [];
$vertical_attachments = [];
if(!empty($this->postParam("horizontal_attachments"))) {
$horizontal_attachments_array = explode(",", $this->postParam("horizontal_attachments"));
if(sizeof($horizontal_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) {
$horizontal_attachments = parseAttachments($horizontal_attachments_array, ['photo', 'video']);
}
}
$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;
}
}
}
$audios = [];
if(!empty($this->postParam("audios"))) {
$un = rtrim($this->postParam("audios"), ",");
$arr = explode(",", $un);
if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$audio = (new Audios)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);
if(!$audio || $audio->isDeleted())
continue;
$audios[] = $audio;
}
if(!empty($this->postParam("vertical_attachments"))) {
$vertical_attachments_array = explode(",", $this->postParam("vertical_attachments"));
if(sizeof($vertical_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) {
$vertical_attachments = parseAttachments($vertical_attachments_array, ['audio', 'note']);
}
}
if(empty($this->postParam("text")) && sizeof($photos) < 1 && sizeof($videos) < 1 && sizeof($audios) < 1)
if(empty($this->postParam("text")) && sizeof($horizontal_attachments) < 1 && sizeof($vertical_attachments) < 1)
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_empty"));
try {
@ -143,15 +103,21 @@ final class CommentPresenter extends OpenVKPresenter
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_too_big"));
}
foreach($photos as $photo)
$comment->attach($photo);
if(sizeof($videos) > 0)
foreach($videos as $vid)
$comment->attach($vid);
foreach($horizontal_attachments as $horizontal_attachment) {
if(!$horizontal_attachment || $horizontal_attachment->isDeleted() || !$horizontal_attachment->canBeViewedBy($this->user->identity)) {
continue;
}
foreach($audios as $audio)
$comment->attach($audio);
$post->attach($horizontal_attachment);
}
foreach($vertical_attachments as $vertical_attachment) {
if(!$vertical_attachment || $vertical_attachment->isDeleted() || !$vertical_attachment->canBeViewedBy($this->user->identity)) {
continue;
}
$post->attach($vertical_attachment);
}
if($entity->getOwner()->getId() !== $this->user->identity->getId())
if(($owner = $entity->getOwner()) instanceof User)

View file

@ -272,25 +272,22 @@ final class WallPresenter extends OpenVKPresenter
if($this->postParam("force_sign") === "on")
$flags |= 0b01000000;
$photos = [];
if(!empty($this->postParam("photos"))) {
$un = rtrim($this->postParam("photos"), ",");
$arr = explode(",", $un);
if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$photo = (new Photos)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);
if(!$photo || $photo->isDeleted())
continue;
$photos[] = $photo;
}
$horizontal_attachments = [];
$vertical_attachments = [];
if(!empty($this->postParam("horizontal_attachments"))) {
$horizontal_attachments_array = explode(",", $this->postParam("horizontal_attachments"));
if(sizeof($horizontal_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) {
$horizontal_attachments = parseAttachments($horizontal_attachments_array, ['photo', 'video']);
}
}
if(!empty($this->postParam("vertical_attachments"))) {
$vertical_attachments_array = explode(",", $this->postParam("vertical_attachments"));
if(sizeof($vertical_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) {
$vertical_attachments = parseAttachments($vertical_attachments_array, ['audio', 'note']);
}
}
try {
$poll = NULL;
$xml = $this->postParam("poll");
@ -302,61 +299,10 @@ final class WallPresenter extends OpenVKPresenter
$this->flashFail("err", tr("failed_to_publish_post"), "Poll format invalid");
}
$note = NULL;
if(!is_null($this->postParam("note")) && $this->postParam("note") != "none") {
$note = (new Notes)->get((int)$this->postParam("note"));
if(!$note || $note->isDeleted() || $note->getOwner()->getId() != $this->user->id) {
$this->flashFail("err", tr("error"), tr("error_attaching_note"));
}
if($note->getOwner()->getPrivacySetting("notes.read") < 1) {
$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;
}
}
}
$audios = [];
if(!empty($this->postParam("audios"))) {
$un = rtrim($this->postParam("audios"), ",");
$arr = explode(",", $un);
if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$audio = (new Audios)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);
if(!$audio || $audio->isDeleted())
continue;
$audios[] = $audio;
}
}
}
if(empty($this->postParam("text")) && sizeof($photos) < 1 && sizeof($videos) < 1 && sizeof($audios) < 1 && !$poll && !$note)
if(empty($this->postParam("text")) && sizeof($horizontal_attachments) < 1 && sizeof($vertical_attachments) < 1 && !$poll)
$this->flashFail("err", tr("failed_to_publish_post"), tr("post_is_empty_or_too_big"));
$should_be_suggested = $wall < 0 && !$wallOwner->canBeModifiedBy($this->user->identity) && $wallOwner->getWallType() == 2;
try {
$post = new Post;
$post->setOwner($this->user->id);
@ -373,7 +319,7 @@ final class WallPresenter extends OpenVKPresenter
} catch(\Throwable) {}
}
if($wall < 0 && !$wallOwner->canBeModifiedBy($this->user->identity) && $wallOwner->getWallType() == 2)
if($should_be_suggested)
$post->setSuggested(1);
$post->save();
@ -381,21 +327,24 @@ final class WallPresenter extends OpenVKPresenter
$this->flashFail("err", tr("failed_to_publish_post"), tr("post_is_too_big"));
}
foreach($photos as $photo)
$post->attach($photo);
if(sizeof($videos) > 0)
foreach($videos as $vid)
$post->attach($vid);
foreach($horizontal_attachments as $horizontal_attachment) {
if(!$horizontal_attachment || $horizontal_attachment->isDeleted() || !$horizontal_attachment->canBeViewedBy($this->user->identity)) {
continue;
}
$post->attach($horizontal_attachment);
}
foreach($vertical_attachments as $vertical_attachment) {
if(!$vertical_attachment || $vertical_attachment->isDeleted() || !$vertical_attachment->canBeViewedBy($this->user->identity)) {
continue;
}
$post->attach($vertical_attachment);
}
if(!is_null($poll))
$post->attach($poll);
if(!is_null($note))
$post->attach($note);
foreach($audios as $audio)
$post->attach($audio);
if($wall > 0 && $wall !== $this->user->identity->getId())
(new WallPostNotification($wallOwner, $post, $this->user->identity))->emit();
@ -404,9 +353,7 @@ final class WallPresenter extends OpenVKPresenter
if($wall > 0)
$excludeMentions[] = $wall;
if($wall < 0 && !$wallOwner->canBeModifiedBy($this->user->identity) && $wallOwner->getWallType() == 2) {
# Чтобы не было упоминаний из предложки
} else {
if(!$should_be_suggested) {
$mentions = iterator_to_array($post->resolveMentions($excludeMentions));
foreach($mentions as $mentionee)
@ -414,18 +361,7 @@ final class WallPresenter extends OpenVKPresenter
(new MentionNotification($mentionee, $post, $post->getOwner(), strip_tags($post->getText())))->emit();
}
if($wall < 0 && !$wallOwner->canBeModifiedBy($this->user->identity) && $wallOwner->getWallType() == 2) {
$suggsCount = $this->posts->getSuggestedPostsCount($wallOwner->getId());
if($suggsCount % 10 == 0) {
$managers = $wallOwner->getManagers();
$owner = $wallOwner->getOwner();
(new NewSuggestedPostsNotification($owner, $wallOwner))->emit();
foreach($managers as $manager)
(new NewSuggestedPostsNotification($manager->getUser(), $wallOwner))->emit();
}
if($should_be_suggested) {
$this->redirect("/club".$wallOwner->getId()."/suggested");
} else {
$this->redirect($wallOwner->getURL());

View file

@ -62,11 +62,9 @@
<input type="checkbox" name="as_group" /> {_comment_as_group}
</label>
</div>
<input type="hidden" name="photos" value="" />
<input type="hidden" name="videos" value="" />
<input type="hidden" name="audios" value="" />
<input type="text" name="horizontal_attachments" value="" />
<input type="text" name="vertical_attachments" value="" />
<input type="hidden" name="poll" value="none" />
<input type="hidden" id="note" name="note" value="none" />
<input type="hidden" id="source" name="source" value="none" />
<input type="hidden" name="type" value="1" />
<input type="hidden" name="hash" value="{$csrfToken}" />

View file

@ -1,4 +1,7 @@

if(typeof u == 'undefined') {
console.error('!!! You forgot to install NPM packages !!!')
}
function expand_wall_textarea(id) {
var el = document.getElementById('post-buttons'+id);
var wi = document.getElementById('wall-post-input'+id);

View file

@ -232,49 +232,47 @@ function ovk_is_ssl(): bool
return $GLOBALS["requestIsSSL"];
}
function parseAttachments(string $attachments): array
function parseAttachments($attachments, array $allow_types = ['photo', 'video', 'note', 'audio']): array
{
$attachmentsArr = explode(",", $attachments);
$returnArr = [];
$exploded_attachments = is_array($attachments) ? $attachments : explode(",", $attachments);
$imploded_types = implode('|', $allow_types);
$output_attachments = [];
$repositories = [
'photo' => [
'repo' => 'openvk\Web\Models\Repositories\Photos',
'method' => 'getByOwnerAndVID',
],
'video' => [
'repo' => 'openvk\Web\Models\Repositories\Videos',
'method' => 'getByOwnerAndVID',
],
'audio' => [
'repo' => 'openvk\Web\Models\Repositories\Audios',
'method' => 'getByOwnerAndVID',
],
'note' => [
'repo' => 'openvk\Web\Models\Repositories\Notes',
'method' => 'getNoteById',
],
];
foreach($attachmentsArr as $attachment) {
$attachmentType = NULL;
foreach($exploded_attachments as $attachment_string) {
if(preg_match("/$imploded_types/", $attachment_string, $matches) == 1) {
$attachment_type = $matches[0];
if(!$repositories[$attachment_type])
continue;
if(str_contains($attachment, "photo"))
$attachmentType = "photo";
elseif(str_contains($attachment, "video"))
$attachmentType = "video";
elseif(str_contains($attachment, "note"))
$attachmentType = "note";
elseif(str_contains($attachment, "audio"))
$attachmentType = "audio";
$attachment_ids = str_replace($attachment_type, '', $attachment_string);
[$attachment_owner, $attachment_id] = array_map('intval', explode('_', $attachment_ids));
$attachmentIds = str_replace($attachmentType, "", $attachment);
$attachmentOwner = (int) explode("_", $attachmentIds)[0];
$gatoExplotano = explode("_", $attachmentIds);
$attachmentId = (int) end($gatoExplotano);
switch($attachmentType) {
case "photo":
$attachmentObj = (new openvk\Web\Models\Repositories\Photos)->getByOwnerAndVID($attachmentOwner, $attachmentId);
$returnArr[] = $attachmentObj;
break;
case "video":
$attachmentObj = (new openvk\Web\Models\Repositories\Videos)->getByOwnerAndVID($attachmentOwner, $attachmentId);
$returnArr[] = $attachmentObj;
break;
case "note":
$attachmentObj = (new openvk\Web\Models\Repositories\Notes)->getNoteById($attachmentOwner, $attachmentId);
$returnArr[] = $attachmentObj;
break;
case "audio":
$attachmentObj = (new openvk\Web\Models\Repositories\Audios)->getByOwnerAndVID($attachmentOwner, $attachmentId);
$returnArr[] = $attachmentObj;
break;
$repository_class = $repositories[$attachment_type]['repo'];
if(!$repository_class) continue;
$attachment_model = (new $repository_class)->{$repositories[$attachment_type]['method']}($attachment_owner, $attachment_id);
$output_attachments[] = $attachment_model;
}
}
return $returnArr;
return $output_attachments;
}
function ovk_scheme(bool $with_slashes = false): string

View file

@ -58,6 +58,7 @@ openvk:
enable: false
account: 100
postSizes:
maxAttachments: 10
maxSize: 60000
processingLimit: 3000
emojiProcessingLimit: 1000