Add option to post anonymously

This commit is contained in:
Celestora 2021-11-15 21:45:48 +02:00
parent 762368c89c
commit 0a7bc786b5
11 changed files with 53 additions and 11 deletions

View file

@ -44,11 +44,12 @@ class Photo extends Media
DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete(); 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 = new static;
$photo->setOwner($owner); $photo->setOwner($owner);
$photo->setDescription(iconv_substr($description, 0, 36) . "..."); $photo->setDescription(iconv_substr($description, 0, 36) . "...");
$photo->setAnonymous($anon);
$photo->setCreated(time()); $photo->setCreated(time());
$photo->setFile($file); $photo->setFile($file);
$photo->save(); $photo->save();

View file

@ -36,14 +36,14 @@ class Post extends Postable
* *
* @param bool $honourFlags - check flags * @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($honourFlags && ( ($this->getRecord()->flags & 0b10000000) > 0 )) {
if($this->getRecord()->wall < 0) if($this->getRecord()->wall < 0)
return (new Clubs)->get(abs($this->getRecord()->wall)); return (new Clubs)->get(abs($this->getRecord()->wall));
} }
return parent::getOwner(); return parent::getOwner($real);
} }
function getPrettyId(): string function getPrettyId(): string

View file

@ -29,9 +29,12 @@ abstract class Postable extends Attachable
return DB::i()->getContext()->table($this->tableName); return DB::i()->getContext()->table($this->tableName);
} }
function getOwner(): RowModel function getOwner(bool $real = false): RowModel
{ {
$oid = (int) $this->getRecord()->owner; $oid = (int) $this->getRecord()->owner;
if(!$real && $this->isAnonymous())
$oid = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"];
if($oid > 0) if($oid > 0)
return (new Users)->get($oid); return (new Users)->get($oid);
else else
@ -96,6 +99,11 @@ abstract class Postable extends Attachable
yield (new Users)->get($like->origin); yield (new Users)->get($like->origin);
} }
function isAnonymous(): bool
{
return (bool) $this->getRecord()->anonymous;
}
function toggleLike(User $user): bool function toggleLike(User $user): bool
{ {
$searchData = [ $searchData = [

View file

@ -122,12 +122,13 @@ class Video extends Media
$this->save(); $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 = new Video;
$video->setOwner($owner); $video->setOwner($owner);
$video->setName("Unnamed Video.ogv"); $video->setName("Unnamed Video.ogv");
$video->setDescription(ovk_proc_strtr($description, 300)); $video->setDescription(ovk_proc_strtr($description, 300));
$video->setAnonymous($anon);
$video->setCreated(time()); $video->setCreated(time());
$video->setFile($file); $video->setFile($file);
$video->setUnlisted($unlisted); $video->setUnlisted($unlisted);

View file

@ -159,6 +159,17 @@ final class PhotosPresenter extends OpenVKPresenter
$this->template->comments = iterator_to_array($photo->getComments($this->template->cPage)); $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 function renderEditPhoto(int $ownerId, int $photoId): void
{ {
$this->assertUserLoggedIn(); $this->assertUserLoggedIn();

View file

@ -188,6 +188,8 @@ final class WallPresenter extends OpenVKPresenter
if(false) if(false)
$this->flashFail("err", "Не удалось опубликовать пост", "Пост слишком большой."); $this->flashFail("err", "Не удалось опубликовать пост", "Пост слишком большой.");
$anon = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["enable"] && $this->postParam("anon") === "on";
$flags = 0; $flags = 0;
if($this->postParam("as_group") === "on") if($this->postParam("as_group") === "on")
$flags |= 0b10000000; $flags |= 0b10000000;
@ -199,14 +201,14 @@ final class WallPresenter extends OpenVKPresenter
$video = NULL; $video = NULL;
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) { if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
$album = NULL; $album = NULL;
if($wall > 0 && $wall === $this->user->id) if(!$anon && $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, $anon);
} }
if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK) { 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) { } catch(\DomainException $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл медиаконтента повреждён."); $this->flashFail("err", "Не удалось опубликовать пост", "Файл медиаконтента повреждён.");
@ -222,6 +224,7 @@ final class WallPresenter extends OpenVKPresenter
$post->setWall($wall); $post->setWall($wall);
$post->setCreated(time()); $post->setCreated(time());
$post->setContent($this->postParam("text")); $post->setContent($this->postParam("text"));
$post->setAnonymous($anon);
$post->setFlags($flags); $post->setFlags($flags);
$post->setNsfw($this->postParam("nsfw") === "on"); $post->setNsfw($this->postParam("nsfw") === "on");
$post->save(); $post->save();

View file

@ -1,6 +1,7 @@
{if $attachment instanceof \openvk\Web\Models\Entities\Photo} {if $attachment instanceof \openvk\Web\Models\Entities\Photo}
{if !$attachment->isDeleted()} {if !$attachment->isDeleted()}
<a href="/photo{$attachment->getPrettyId()}"> {var link = "/photo" . ($attachment->isAnonymous() ? ("s/" . base_convert((string) $attachment->getId(), 10, 32)) : $attachment->getPrettyId())}
<a href="{$link}">
<img class="media" src="{$attachment->getURL()}" alt="{$attachment->getDescription()}" /> <img class="media" src="{$attachment->getURL()}" alt="{$attachment->getDescription()}" />
</a> </a>
{else} {else}

View file

@ -9,12 +9,17 @@
Вложение: <span>(unknown)</span> Вложение: <span>(unknown)</span>
</div> </div>
<div n:if="$postOpts ?? true" class="post-opts"> <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} {if !is_null($thisUser) && !is_null($club ?? NULL) && $owner < 0}
{if $club->canBeModifiedBy($thisUser)} {if $club->canBeModifiedBy($thisUser)}
<script> <script>
function onWallAsGroupClick(el) { function onWallAsGroupClick(el) {
_display = el.checked ? "block" : "none"; document.querySelector("#forceSignOpt").style.display = el.checked ? "block" : "none";
document.querySelector("#forceSignOpt").style.display = _display;
{if $anonEnabled}
document.querySelector("#octoberAnonOpt").style.display = el.checked ? "none" : "block";
{/if}
} }
</script> </script>
@ -27,6 +32,10 @@
{/if} {/if}
{/if} {/if}
<label n:if="$anonEnabled" id="octoberAnonOpt">
<input type="checkbox" name="anon" /> Анонимно
</label>
<label> <label>
<input type="checkbox" name="nsfw" /> {_"contains_nsfw"} <input type="checkbox" name="nsfw" /> {_"contains_nsfw"}
</label> </label>

View file

@ -119,6 +119,8 @@ routes:
handler: "Photos->uploadPhoto" handler: "Photos->uploadPhoto"
- url: "/photo{num}_{num}" - url: "/photo{num}_{num}"
handler: "Photos->photo" handler: "Photos->photo"
- url: "/photos/{text}"
handler: "Photos->absolutePhoto"
- url: "/photo{num}_{num}/edit" - url: "/photo{num}_{num}/edit"
handler: "Photos->editPhoto" handler: "Photos->editPhoto"
- url: "/photo{num}_{num}/delete" - url: "/photo{num}_{num}/delete"

View file

@ -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`;

View file

@ -34,6 +34,9 @@ openvk:
messages: messages:
strict: false strict: false
wall: wall:
anonymousPosting:
enable: true
account: 2
postSizes: postSizes:
maxSize: 60000 maxSize: 60000
processingLimit: 3000 processingLimit: 3000