From 0a7bc786b5b8f00cd42402020e943a00529f95e4 Mon Sep 17 00:00:00 2001 From: Celestora Date: Mon, 15 Nov 2021 21:45:48 +0200 Subject: [PATCH] Add option to post anonymously --- Web/Models/Entities/Photo.php | 3 ++- Web/Models/Entities/Post.php | 4 ++-- Web/Models/Entities/Postable.php | 10 +++++++++- Web/Models/Entities/Video.php | 3 ++- Web/Presenters/PhotosPresenter.php | 11 +++++++++++ Web/Presenters/WallPresenter.php | 9 ++++++--- Web/Presenters/templates/components/attachment.xml | 3 ++- Web/Presenters/templates/components/textArea.xml | 13 +++++++++++-- Web/routes.yml | 2 ++ install/sqls/00011-anonymous-posting.sql | 3 +++ openvk-example.yml | 3 +++ 11 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 install/sqls/00011-anonymous-posting.sql diff --git a/Web/Models/Entities/Photo.php b/Web/Models/Entities/Photo.php index 78417a56..ecda93f6 100644 --- a/Web/Models/Entities/Photo.php +++ b/Web/Models/Entities/Photo.php @@ -44,11 +44,12 @@ class Photo extends Media DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete(); } - static function fastMake(int $owner, string $description = "", array $file, ?Album $album = NULL): Photo + static function fastMake(int $owner, string $description = "", array $file, ?Album $album = NULL, bool $anon = false): Photo { $photo = new static; $photo->setOwner($owner); $photo->setDescription(iconv_substr($description, 0, 36) . "..."); + $photo->setAnonymous($anon); $photo->setCreated(time()); $photo->setFile($file); $photo->save(); diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php index 17c096fa..7d0cc486 100644 --- a/Web/Models/Entities/Post.php +++ b/Web/Models/Entities/Post.php @@ -36,14 +36,14 @@ class Post extends Postable * * @param bool $honourFlags - check flags */ - function getOwner(bool $honourFlags = true): RowModel + function getOwner(bool $honourFlags = true, bool $real = false): RowModel { if($honourFlags && ( ($this->getRecord()->flags & 0b10000000) > 0 )) { if($this->getRecord()->wall < 0) return (new Clubs)->get(abs($this->getRecord()->wall)); } - return parent::getOwner(); + return parent::getOwner($real); } function getPrettyId(): string diff --git a/Web/Models/Entities/Postable.php b/Web/Models/Entities/Postable.php index 26670e94..b5749819 100644 --- a/Web/Models/Entities/Postable.php +++ b/Web/Models/Entities/Postable.php @@ -29,9 +29,12 @@ abstract class Postable extends Attachable return DB::i()->getContext()->table($this->tableName); } - function getOwner(): RowModel + function getOwner(bool $real = false): RowModel { $oid = (int) $this->getRecord()->owner; + if(!$real && $this->isAnonymous()) + $oid = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"]; + if($oid > 0) return (new Users)->get($oid); else @@ -96,6 +99,11 @@ abstract class Postable extends Attachable yield (new Users)->get($like->origin); } + function isAnonymous(): bool + { + return (bool) $this->getRecord()->anonymous; + } + function toggleLike(User $user): bool { $searchData = [ diff --git a/Web/Models/Entities/Video.php b/Web/Models/Entities/Video.php index 492dafed..17500e8d 100644 --- a/Web/Models/Entities/Video.php +++ b/Web/Models/Entities/Video.php @@ -122,12 +122,13 @@ class Video extends Media $this->save(); } - static function fastMake(int $owner, string $description = "", array $file, bool $unlisted = true): Video + static function fastMake(int $owner, string $description = "", array $file, bool $unlisted = true, bool $anon = false): Video { $video = new Video; $video->setOwner($owner); $video->setName("Unnamed Video.ogv"); $video->setDescription(ovk_proc_strtr($description, 300)); + $video->setAnonymous($anon); $video->setCreated(time()); $video->setFile($file); $video->setUnlisted($unlisted); diff --git a/Web/Presenters/PhotosPresenter.php b/Web/Presenters/PhotosPresenter.php index 15438bdd..cefe69ac 100644 --- a/Web/Presenters/PhotosPresenter.php +++ b/Web/Presenters/PhotosPresenter.php @@ -159,6 +159,17 @@ final class PhotosPresenter extends OpenVKPresenter $this->template->comments = iterator_to_array($photo->getComments($this->template->cPage)); } + function renderAbsolutePhoto(string $id): void + { + $id = (int) base_convert($id, 32, 10); + $photo = $this->photos->get($id); + if(!$photo || $photo->isDeleted()) + $this->notFound(); + + $this->template->_template = "Photos/Photo.xml"; + $this->renderPhoto($photo->getOwner(true)->getId(), $photo->getVirtualId()); + } + function renderEditPhoto(int $ownerId, int $photoId): void { $this->assertUserLoggedIn(); diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index 51734a3d..325b84a2 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -188,6 +188,8 @@ final class WallPresenter extends OpenVKPresenter if(false) $this->flashFail("err", "Не удалось опубликовать пост", "Пост слишком большой."); + $anon = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["enable"] && $this->postParam("anon") === "on"; + $flags = 0; if($this->postParam("as_group") === "on") $flags |= 0b10000000; @@ -199,14 +201,14 @@ final class WallPresenter extends OpenVKPresenter $video = NULL; if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) { $album = NULL; - if($wall > 0 && $wall === $this->user->id) + if(!$anon && $wall > 0 && $wall === $this->user->id) $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, $anon); } if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK) { - $video = Video::fastMake($this->user->id, $this->postParam("text"), $_FILES["_vid_attachment"]); + $video = Video::fastMake($this->user->id, $this->postParam("text"), $_FILES["_vid_attachment"], $anon); } } catch(\DomainException $ex) { $this->flashFail("err", "Не удалось опубликовать пост", "Файл медиаконтента повреждён."); @@ -222,6 +224,7 @@ final class WallPresenter extends OpenVKPresenter $post->setWall($wall); $post->setCreated(time()); $post->setContent($this->postParam("text")); + $post->setAnonymous($anon); $post->setFlags($flags); $post->setNsfw($this->postParam("nsfw") === "on"); $post->save(); diff --git a/Web/Presenters/templates/components/attachment.xml b/Web/Presenters/templates/components/attachment.xml index 35f1901a..9e9271f2 100644 --- a/Web/Presenters/templates/components/attachment.xml +++ b/Web/Presenters/templates/components/attachment.xml @@ -1,6 +1,7 @@ {if $attachment instanceof \openvk\Web\Models\Entities\Photo} {if !$attachment->isDeleted()} - + {var link = "/photo" . ($attachment->isAnonymous() ? ("s/" . base_convert((string) $attachment->getId(), 10, 32)) : $attachment->getPrettyId())} + {$attachment->getDescription()} {else} diff --git a/Web/Presenters/templates/components/textArea.xml b/Web/Presenters/templates/components/textArea.xml index 814077f3..2033d393 100644 --- a/Web/Presenters/templates/components/textArea.xml +++ b/Web/Presenters/templates/components/textArea.xml @@ -9,12 +9,17 @@ Вложение: (unknown)
+ {var anonEnabled = OPENVK_ROOT_CONF['openvk']['preferences']['wall']['anonymousPosting']['enable']} + {if !is_null($thisUser) && !is_null($club ?? NULL) && $owner < 0} {if $club->canBeModifiedBy($thisUser)} @@ -27,6 +32,10 @@ {/if} {/if} + + diff --git a/Web/routes.yml b/Web/routes.yml index 5bd51261..d86a96fb 100644 --- a/Web/routes.yml +++ b/Web/routes.yml @@ -119,6 +119,8 @@ routes: handler: "Photos->uploadPhoto" - url: "/photo{num}_{num}" handler: "Photos->photo" + - url: "/photos/{text}" + handler: "Photos->absolutePhoto" - url: "/photo{num}_{num}/edit" handler: "Photos->editPhoto" - url: "/photo{num}_{num}/delete" diff --git a/install/sqls/00011-anonymous-posting.sql b/install/sqls/00011-anonymous-posting.sql new file mode 100644 index 00000000..e8416c12 --- /dev/null +++ b/install/sqls/00011-anonymous-posting.sql @@ -0,0 +1,3 @@ +ALTER TABLE `posts` ADD `anonymous` BOOLEAN NOT NULL DEFAULT FALSE AFTER `pinned`; +ALTER TABLE `videos` ADD `anonymous` BOOLEAN NOT NULL DEFAULT FALSE AFTER `unlisted`; +ALTER TABLE `photos` ADD `anonymous` BOOLEAN NOT NULL DEFAULT FALSE AFTER `hash`; \ No newline at end of file diff --git a/openvk-example.yml b/openvk-example.yml index ee2aaed7..029a0563 100644 --- a/openvk-example.yml +++ b/openvk-example.yml @@ -34,6 +34,9 @@ openvk: messages: strict: false wall: + anonymousPosting: + enable: true + account: 2 postSizes: maxSize: 60000 processingLimit: 3000