Repost to groups

This commit is contained in:
makar0nny 2023-05-08 11:36:24 +03:00 committed by lalka2016
parent 5ec34267cc
commit b0ec86edbc
10 changed files with 133 additions and 21 deletions

View file

@ -453,7 +453,7 @@ final class Wall extends VKAPIRequestHandler
return (object)["post_id" => $post->getVirtualId()]; return (object)["post_id" => $post->getVirtualId()];
} }
function repost(string $object, string $message = "") { function repost(string $object, string $message = "", int $group_id = 0) {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
@ -466,7 +466,19 @@ final class Wall extends VKAPIRequestHandler
$nPost = new Post; $nPost = new Post;
$nPost->setOwner($this->user->getId()); $nPost->setOwner($this->user->getId());
if($group_id > 0)
{
$club = (new ClubsRepo)->get($group_id);
if(!$club || !$club->canBeModifiedBy($this->user))
{
$this->fail(42, "Invalid group");
}
$nPost->setWall($group_id*-1);
}
else
{
$nPost->setWall($this->user->getId()); $nPost->setWall($this->user->getId());
}
$nPost->setContent($message); $nPost->setContent($message);
$nPost->setApi_Source_Name($this->getPlatform()); $nPost->setApi_Source_Name($this->getPlatform());
$nPost->save(); $nPost->save();
@ -483,6 +495,7 @@ final class Wall extends VKAPIRequestHandler
]; ];
} }
function getComments(int $owner_id, int $post_id, bool $need_likes = true, int $offset = 0, int $count = 10, string $fields = "sex,screen_name,photo_50,photo_100,online_info,online", string $sort = "asc", bool $extended = false) { function getComments(int $owner_id, int $post_id, bool $need_likes = true, int $offset = 0, int $count = 10, string $fields = "sex,screen_name,photo_50,photo_100,online_info,online", string $sort = "asc", bool $extended = false) {
$this->requireUser(); $this->requireUser();

View file

@ -1,7 +1,7 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories; namespace openvk\Web\Models\Repositories;
use openvk\Web\Models\Entities\Club; use openvk\Web\Models\Entities\Club;
use openvk\Web\Models\Repositories\Aliases; use openvk\Web\Models\Repositories\{Aliases, Users};
use Nette\Database\Table\ActiveRow; use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
@ -71,5 +71,13 @@ class Clubs
*/ */
} }
function getOwnedClubs(int $id): \Traversable
{
# infoapp
$result = DatabaseConnection::i()->getConnection()->query("SELECT * FROM `groups` WHERE `owner` = $id ORDER BY `id`");
foreach($result as $entry)
yield $this->get($entry->id);
}
use \Nette\SmartObject; use \Nette\SmartObject;
} }

View file

@ -707,4 +707,23 @@ final class UserPresenter extends OpenVKPresenter
$this->redirect("/settings"); $this->redirect("/settings");
} }
} }
function renderOwnedClubs(int $id)
{
if($this->user->id == $id)
{
$this->assertUserLoggedIn();
$clubs = iterator_to_array((new Clubs)->getOwnedClubs($id));
$json = array();
foreach($clubs as $club)
{
$json[]=array("name"=>$club->getName(),"id"=>$club->getId());
}
$this->returnJson($json);
}
else
{
$this->returnJson(["You are not allowed to see user-created groups"]);
}
}
} }

View file

@ -364,21 +364,51 @@ final class WallPresenter extends OpenVKPresenter
$post = $this->posts->getPostById($wall, $post_id); $post = $this->posts->getPostById($wall, $post_id);
if(!$post || $post->isDeleted()) $this->notFound(); if(!$post || $post->isDeleted()) $this->notFound();
$where = $this->postParam("type") ?? "wall";
$groupId = NULL;
$flags = 0;
if($where == "group")
{
$groupId = $this->postParam("groupId");
}
if(!is_null($this->user)) { if(!is_null($this->user)) {
$nPost = new Post; $nPost = new Post;
if($where == "wall")
{
$nPost->setOwner($this->user->id); $nPost->setOwner($this->user->id);
$nPost->setWall($this->user->id); $nPost->setWall($this->user->id);
}
elseif($where == "group")
{
$nPost->setOwner($this->user->id);
if($this->postParam("asGroup") == 1)
{
$flags |= 0b10000000;
}
if($this->postParam("signed") == 1)
{
$flags |= 0b01000000;
}
$nPost->setWall($groupId*-1);
}
$nPost->setContent($this->postParam("text")); $nPost->setContent($this->postParam("text"));
$nPost->setFlags($flags);
$nPost->save(); $nPost->save();
$nPost->attach($post); $nPost->attach($post);
if($post->getOwner(false)->getId() !== $this->user->identity->getId() && !($post->getOwner() instanceof Club)) if($post->getOwner(false)->getId() !== $this->user->identity->getId() && !($post->getOwner() instanceof Club))
(new RepostNotification($post->getOwner(false), $post, $this->user->identity))->emit(); (new RepostNotification($post->getOwner(false), $post, $this->user->identity))->emit();
}; };
if($where == "wall")
{
$this->returnJson(["wall_owner" => $this->user->identity->getId()]); $this->returnJson(["wall_owner" => $this->user->identity->getId()]);
} }
else
{
$this->returnJson(["wall_owner" => $groupId*-1]);
}
}
function renderDelete(int $wall, int $post_id): void function renderDelete(int $wall, int $post_id): void
{ {

View file

@ -96,7 +96,7 @@
<a n:if="!($forceNoCommentsLink ?? false) && $commentsCount == 0" href="javascript:expand_comment_textarea({$commentTextAreaId})">{_comment}</a> <a n:if="!($forceNoCommentsLink ?? false) && $commentsCount == 0" href="javascript:expand_comment_textarea({$commentTextAreaId})">{_comment}</a>
<div class="like_wrap"> <div class="like_wrap">
<a n:if="!($forceNoShareLink ?? false)" class="post-share-button" href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}')"> <a n:if="!($forceNoShareLink ?? false)" class="post-share-button" href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}', {$thisUser->getId()})">
<div class="repost-icon" style="opacity: 0.4;"></div> <div class="repost-icon" style="opacity: 0.4;"></div>
<span class="likeCnt">{if $post->getRepostCount() > 0}{$post->getRepostCount()}{/if}</span> <span class="likeCnt">{if $post->getRepostCount() > 0}{$post->getRepostCount()}{/if}</span>
</a> </a>

View file

@ -107,7 +107,7 @@
&nbsp;|&nbsp; &nbsp;|&nbsp;
{/if} {/if}
<a n:if="!($forceNoShareLink ?? false)" class="post-share-button" {ifset $thisUser} href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}')"{/ifset}> <a n:if="!($forceNoShareLink ?? false)" class="post-share-button" {ifset $thisUser} href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}', {$thisUser->getId()})"{/ifset}>
{_share} {_share}
{if $post->getRepostCount() > 0} {if $post->getRepostCount() > 0}
(<b>{$post->getRepostCount()}</b>) (<b>{$post->getRepostCount()}</b>)

View file

@ -93,6 +93,8 @@ routes:
handler: "User->increaseRating" handler: "User->increaseRating"
- url: "/id{num}" - url: "/id{num}"
handler: "User->view" handler: "User->view"
- url: "/id{num}/getOwnedClubs"
handler: "User->ownedClubs"
- url: "/friends{num}" - url: "/friends{num}"
handler: "User->friends" handler: "User->friends"
- url: "/edit" - url: "/edit"

View file

@ -65,7 +65,6 @@ function toggleMenu(id) {
}); });
} }
} }
document.addEventListener("DOMContentLoaded", function() { //BEGIN document.addEventListener("DOMContentLoaded", function() { //BEGIN
u("#_photoDelete").on("click", function(e) { u("#_photoDelete").on("click", function(e) {
@ -88,7 +87,6 @@ document.addEventListener("DOMContentLoaded", function() { //BEGIN
return e.preventDefault(); return e.preventDefault();
}); });
/* @rem-pai why this func wasn't named as "#_deleteDialog"? It looks universal IMO */ /* @rem-pai why this func wasn't named as "#_deleteDialog"? It looks universal IMO */
u("#_noteDelete").on("click", function(e) { u("#_noteDelete").on("click", function(e) {
@ -170,12 +168,37 @@ document.addEventListener("DOMContentLoaded", function() { //BEGIN
}); //END ONREADY DECLS }); //END ONREADY DECLS
function repostPost(id, hash) { function repostPost(id, hash, owner) {
uRepostMsgTxt = tr('your_comment') + ": <textarea id='uRepostMsgInput_"+id+"'></textarea><br/><br/>"; uRepostMsgTxt = `
<b>${tr('auditory')}:</b> <br/>
<input type="radio" name="type" onchange="signs.setAttribute('hidden', 'hidden');groupId.setAttribute('hidden', 'hidden')" value="wall" checked>${tr("in_wall")}<br/>
<input type="radio" name="type" onchange="signs.removeAttribute('hidden');groupId.removeAttribute('hidden')" value="group">${tr("in_group")}<br/>
<select style="width:50%;" id="groupId" name="groupId" hidden>
</select><br/>
<b>${tr('your_comment')}:</b>
<textarea id='uRepostMsgInput_${id}'></textarea>
<div id="signs" hidden>
<label><input type="checkbox" id="asgroup" name="asGroup" value="1">${tr('post_as_group')}</label><br>
<label><input onchange="asgroup.checked = true" type="checkbox" id="signed" name="signed" value="1">${tr('add_signature')}</label>
</div>
<br/><br/>`;
let clubs = [];
MessageBox(tr('share'), uRepostMsgTxt, [tr('send'), tr('cancel')], [ MessageBox(tr('share'), uRepostMsgTxt, [tr('send'), tr('cancel')], [
(function() { (function() {
text = document.querySelector("#uRepostMsgInput_"+id).value; text = document.querySelector("#uRepostMsgInput_"+id).value;
type = "user";
radios = document.querySelectorAll('input[name="type"]')
for(const r of radios)
{
if(r.checked)
{
type = r.value;
break;
}
}
groupId = document.querySelector("#groupId").value;
asGroup = asgroup.value;
signed = signed.value;
hash = encodeURIComponent(hash); hash = encodeURIComponent(hash);
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open("POST", "/wall"+id+"/repost?hash="+hash, true); xhr.open("POST", "/wall"+id+"/repost?hash="+hash, true);
@ -188,10 +211,21 @@ function repostPost(id, hash) {
NewNotification(tr('information_-1'), tr('shared_succ'), null, () => {window.location.href = "/wall" + jsonR.wall_owner}); NewNotification(tr('information_-1'), tr('shared_succ'), null, () => {window.location.href = "/wall" + jsonR.wall_owner});
} }
}); });
xhr.send('text=' + encodeURI(text)); xhr.send('text=' + encodeURI(text) + '&type=' + encodeURI(type) + '&groupId=' + encodeURI(groupId) + "&asGroup="+encodeURI(asGroup) + "&signed="+encodeURI(signed));
}), }),
Function.noop Function.noop
]); ]);
let xhrj = new XMLHttpRequest();
xhrj.open("GET", "id"+owner+"/getOwnedClubs?hash="+hash)
xhrj.send()
xhrj.onload = () =>
{
clubs = JSON.parse(xhrj.responseText);
for(const el of clubs)
{
document.getElementById("groupId").insertAdjacentHTML("beforeend", `<option value="${el.id}">${el.name}</option>`)
}
}
} }
function setClubAdminComment(clubId, adminId, hash) { function setClubAdminComment(clubId, adminId, hash) {

View file

@ -175,6 +175,9 @@
"pinned" = "pinned"; "pinned" = "pinned";
"comments_tip" = "Be first, who leaves a comment at this post!"; "comments_tip" = "Be first, who leaves a comment at this post!";
"your_comment" = "Your comment"; "your_comment" = "Your comment";
"auditory" = "Auditory";
"in_wall" = "to user's wall";
"in_group" = "to group";
"shown" = "Shown"; "shown" = "Shown";
"x_out_of" = "$1 of"; "x_out_of" = "$1 of";
"wall_zero" = "no posts"; "wall_zero" = "no posts";

View file

@ -157,6 +157,9 @@
"pinned" = "закреплено"; "pinned" = "закреплено";
"comments_tip" = "Будьте первым, кто оставит комментарий!"; "comments_tip" = "Будьте первым, кто оставит комментарий!";
"your_comment" = "Ваш комментарий"; "your_comment" = "Ваш комментарий";
"auditory" = "Аудитория";
"in_wall" = "на стену";
"in_group" = "в группу";
"shown" = "Показано"; "shown" = "Показано";
"x_out_of" = "$1 из"; "x_out_of" = "$1 из";
"wall_zero" = "нет записей"; "wall_zero" = "нет записей";