From 4bcd444f8783abfd93d3be8f39966208d0133e7a Mon Sep 17 00:00:00 2001 From: lalka2016 <99399973+lalka2016@users.noreply.github.com> Date: Fri, 28 Jul 2023 20:52:34 +0300 Subject: [PATCH 1/3] Posts: add source param --- VKAPI/Handlers/Wall.php | 67 ++++++++++++++++++- Web/Models/Entities/Post.php | 25 +++++++ Web/Presenters/WallPresenter.php | 5 ++ Web/Presenters/templates/Wall/Feed.xml | 2 +- .../components/post/microblogpost.xml | 3 + .../templates/components/post/oldpost.xml | 3 + .../templates/components/textArea.xml | 23 +++++++ Web/Presenters/templates/components/wall.xml | 2 +- Web/static/css/main.css | 10 +++ install/sqls/00038-posts-source.sql | 2 + locales/en.strings | 2 + locales/ru.strings | 2 + 12 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 install/sqls/00038-posts-source.sql diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php index d52dfce1..0334ae62 100644 --- a/VKAPI/Handlers/Wall.php +++ b/VKAPI/Handlers/Wall.php @@ -132,6 +132,12 @@ final class Wall extends VKAPIRequestHandler "count" => $post->getCommentsCount(), "can_post" => 1 ], + "copyright" => !is_null($post->getSource(false)) ? (object)[ + "id" => 0, + "link" => $post->getSource(false), + "name" => "none", + "type" => "link" + ] : NULL, "likes" => (object)[ "count" => $post->getLikesCount(), "user_likes" => (int) $post->hasLikeFrom($this->getUser()), @@ -307,6 +313,12 @@ final class Wall extends VKAPIRequestHandler "count" => $post->getCommentsCount(), "can_post" => 1 ], + "copyright" => !is_null($post->getSource(false)) ? (object)[ + "id" => 0, + "link" => $post->getSource(false), + "name" => "none", + "type" => "link" + ] : NULL, "likes" => (object)[ "count" => $post->getLikesCount(), "user_likes" => (int) $post->hasLikeFrom($user), @@ -379,7 +391,7 @@ final class Wall extends VKAPIRequestHandler ]; } - function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = ""): object + function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = "", string $copyright = NULL): object { $this->requireUser(); $this->willExecuteWriteAction(); @@ -428,6 +440,11 @@ final class Wall extends VKAPIRequestHandler $post->setContent($message); $post->setFlags($flags); $post->setApi_Source_Name($this->getPlatform()); + + if(!is_null($copyright) && !empty($copyright) && $copyright != "" && preg_match("/^(http:\/\/|https:\/\/)*[а-яА-ЯёЁa-z0-9\-_]+(\.[а-яА-ЯёЁa-z0-9\-_]+)+(\/\S*)*$/iu", $copyright) && iconv_strlen($copyright) < 50) { + $post->setSource($copyright); + } + $post->save(); } catch(\LogicException $ex) { $this->fail(100, "One of the parameters specified was missing or invalid"); @@ -776,6 +793,54 @@ final class Wall extends VKAPIRequestHandler return 1; } + function checkCopyrightLink(string $link) { + $res = (int)(!is_null($link) && !empty($link) && preg_match("/^(http:\/\/|https:\/\/)*[а-яА-ЯёЁa-z0-9\-_]+(\.[а-яА-ЯёЁa-z0-9\-_]+)+(\/\S*)*$/iu", $link) && iconv_strlen($link) < 50); + + if($res == 0) { + $this->fail(3102, "Specified link is incorrect"); + } + + return $res; + } + + function pin(int $owner_id, int $post_id) { + $this->requireUser(); + $this->willExecuteWriteAction(); + + $post = (new PostsRepo)->getPostById($owner_id, $post_id); + if(!$post || $post->isDeleted()) + $this->fail(361, "Invalid post"); + + if(!$post->canBePinnedBy($this->getUser())) + $this->fail(14, "Access to pinning post denied"); + + if(!$post->isPinned()) { + $post->pin(); + return 1; + } else { + $this->fail(50, "Post is already pinned"); + } + } + + function unpin(int $owner_id, int $post_id) { + $this->requireUser(); + $this->willExecuteWriteAction(); + + $post = (new PostsRepo)->getPostById($owner_id, $post_id); + if(!$post || $post->isDeleted()) + $this->fail(361, "Invalid post"); + + if(!$post->canBePinnedBy($this->getUser())) + $this->fail(14, "Access to unpinning post denied"); + + if($post->isPinned()) { + $post->unpin(); + return 1; + } else { + $this->fail(50, "Post is not pinned"); + } + } + private function getApiPhoto($attachment) { return [ "type" => "photo", diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php index 42941901..1501e32e 100644 --- a/Web/Models/Entities/Post.php +++ b/Web/Models/Entities/Post.php @@ -78,6 +78,31 @@ class Post extends Postable { return (bool) $this->getRecord()->pinned; } + + function getSource(bool $format = false) + { + if(!$format) { + return $this->getRecord()->source; + } + + return $this->formatLinks($this->getRecord()->source); + } + + function setSource(string $source) + { + $src = $source; + + if(iconv_strlen($source) > 50) + throw new \LengthException("Link is too long."); + + if(!preg_match("/^(http:\/\/|https:\/\/)*[а-яА-ЯёЁa-z0-9\-_]+(\.[а-яА-ЯёЁa-z0-9\-_]+)+(\/\S*)*$/iu", $source)) + throw new \LogicException("Invalid link"); + + if(!str_contains($source, "https://") && !str_contains($source, "http://")) + $src = "https://" . $source; + + $this->stateChanges("source", $src); + } function isAd(): bool { diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index 727101ff..fffe61e0 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -305,6 +305,11 @@ final class WallPresenter extends OpenVKPresenter $post->setAnonymous($anon); $post->setFlags($flags); $post->setNsfw($this->postParam("nsfw") === "on"); + + if($this->postParam("set_source") === "on" && !is_null($this->postParam("source")) && !empty($this->postParam("source")) && preg_match("/^(http:\/\/|https:\/\/)*[а-яА-ЯёЁa-z0-9\-_]+(\.[а-яА-ЯёЁa-z0-9\-_]+)+(\/\S*)*$/iu", $this->postParam("source")) && iconv_strlen($this->postParam("set_source")) < 50) { + $post->setSource($this->postParam("source")); + } + $post->save(); } catch (\LengthException $ex) { $this->flashFail("err", tr("failed_to_publish_post"), tr("post_is_too_big")); diff --git a/Web/Presenters/templates/Wall/Feed.xml b/Web/Presenters/templates/Wall/Feed.xml index 5ed1e2fb..8aa2720b 100644 --- a/Web/Presenters/templates/Wall/Feed.xml +++ b/Web/Presenters/templates/Wall/Feed.xml @@ -16,7 +16,7 @@
- {include "../components/textArea.xml", route => "/wall" . $thisUser->getId() . "/makePost", graffiti => true, polls => true, notes => true} + {include "../components/textArea.xml", route => "/wall" . $thisUser->getId() . "/makePost", graffiti => true, polls => true, notes => true, hasSource => true}
{foreach $posts as $post} diff --git a/Web/Presenters/templates/components/post/microblogpost.xml b/Web/Presenters/templates/components/post/microblogpost.xml index 98a41d72..d43c05a4 100644 --- a/Web/Presenters/templates/components/post/microblogpost.xml +++ b/Web/Presenters/templates/components/post/microblogpost.xml @@ -77,6 +77,9 @@
 ! Этот пост был размещён за взятку. +
+ {_source}: {$post->getSource(true)|noescape} +
{var $actualAuthor = $post->getOwner(false)} diff --git a/Web/Presenters/templates/components/post/oldpost.xml b/Web/Presenters/templates/components/post/oldpost.xml index c893e289..9c2df213 100644 --- a/Web/Presenters/templates/components/post/oldpost.xml +++ b/Web/Presenters/templates/components/post/oldpost.xml @@ -71,6 +71,9 @@
 ! Этот пост был размещён за взятку.
+
+ {_source}: {$post->getSource(true)|noescape} +
{var $actualAuthor = $post->getOwner(false)} diff --git a/Web/Presenters/templates/components/textArea.xml b/Web/Presenters/templates/components/textArea.xml index f76649d6..2e012bfe 100644 --- a/Web/Presenters/templates/components/textArea.xml +++ b/Web/Presenters/templates/components/textArea.xml @@ -19,6 +19,29 @@
{var $anonEnabled = OPENVK_ROOT_CONF['openvk']['preferences']['wall']['anonymousPosting']['enable']} + {if $hasSource} + + + + + + {/if} + {if !is_null($thisUser) && !is_null($club ?? NULL) && $owner < 0} {if $club->canBeModifiedBy($thisUser)} {/if} {if !is_null($thisUser) && !is_null($club ?? NULL) && $owner < 0} diff --git a/Web/static/js/al_wall.js b/Web/static/js/al_wall.js index bb349c14..123d2e87 100644 --- a/Web/static/js/al_wall.js +++ b/Web/static/js/al_wall.js @@ -262,4 +262,18 @@ async function showArticle(note_id) { u("#articleText").html(`

${note.title}

` + note.html); u("body").removeClass("dimmed"); u("body").addClass("article"); -} \ No newline at end of file +} + +if(document.querySelector("input[name='set_source']") != null) { + document.querySelector("input[name='set_source']").checked = false +} + +$(document).on("change", "input[name='set_source']", (e) => { + if(e.currentTarget.checked) { + document.getElementById("sourceSet").style.display = "block" + e.currentTarget.parentNode.querySelector("span").innerHTML = tr("source") + ":" + } else { + document.getElementById("sourceSet").style.display = "none" + e.currentTarget.parentNode.querySelector("span").innerHTML = tr("set_source") + } +}) \ No newline at end of file From 95b599e3e2629c3b75fee8e6c68c078ed20b0f64 Mon Sep 17 00:00:00 2001 From: lalka2016 <99399973+lalka2016@users.noreply.github.com> Date: Mon, 7 Aug 2023 10:46:07 +0300 Subject: [PATCH 3/3] Newlines --- Web/static/css/main.css | 2 +- Web/static/js/al_wall.js | 2 +- install/sqls/00038-posts-source.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Web/static/css/main.css b/Web/static/css/main.css index 85861059..5834a1ce 100644 --- a/Web/static/css/main.css +++ b/Web/static/css/main.css @@ -2705,4 +2705,4 @@ body.article .floating_sidebar, body.article .page_content { .sourceDiv span { color:grey; font-size: 11px; -} \ No newline at end of file +} diff --git a/Web/static/js/al_wall.js b/Web/static/js/al_wall.js index 123d2e87..f6111659 100644 --- a/Web/static/js/al_wall.js +++ b/Web/static/js/al_wall.js @@ -276,4 +276,4 @@ $(document).on("change", "input[name='set_source']", (e) => { document.getElementById("sourceSet").style.display = "none" e.currentTarget.parentNode.querySelector("span").innerHTML = tr("set_source") } -}) \ No newline at end of file +}) diff --git a/install/sqls/00038-posts-source.sql b/install/sqls/00038-posts-source.sql index 59db1e59..e5e52eb1 100644 --- a/install/sqls/00038-posts-source.sql +++ b/install/sqls/00038-posts-source.sql @@ -1,2 +1,2 @@ ALTER TABLE `posts` - ADD COLUMN `source` TEXT NULL DEFAULT NULL AFTER `api_source_name`; \ No newline at end of file + ADD COLUMN `source` TEXT NULL DEFAULT NULL AFTER `api_source_name`;