Moved getWriteableClubs to serviceAPI

И добавил проверку наличия хоть одной доступной группы для репоста, если таковой нет, второй переключатель в окне репоста блокируется
This commit is contained in:
lalka2016 2023-05-17 20:58:56 +03:00
parent 46dd7754ae
commit 2123ce4ed2
7 changed files with 57 additions and 46 deletions

38
ServiceAPI/Groups.php Normal file
View file

@ -0,0 +1,38 @@
<?php declare(strict_types=1);
namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Clubs;
class Groups implements Handler
{
protected $user;
protected $groups;
function __construct(?User $user)
{
$this->user = $user;
$this->groups = new Clubs;
}
function getWriteableClubs(callable $resolve, callable $reject)
{
$clubs = [];
$wclubs = $this->groups->getWriteableClubs($this->user->getId());
if(count(iterator_to_array($this->groups->getWriteableClubs($this->user->getId()))) == 0) {
$reject("You did not created any groups");
return;
}
foreach($wclubs as $club) {
$clubs[] = [
"name" => $club->getName(),
"id" => $club->getId(),
"avatar" => $club->getAvatarUrl() # если в овк когда-нибудь появится крутой список с аватарками, то можно использовать это поле
];
}
$resolve($clubs);
}
}

View file

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

View file

@ -402,11 +402,9 @@ final class WallPresenter extends OpenVKPresenter
(new RepostNotification($post->getOwner(false), $post, $this->user->identity))->emit();
};
if($where == "wall")
$this->returnJson(["wall_owner" => $this->user->identity->getId()]);
else
$this->returnJson(["wall_owner" => $groupId*-1]);
$this->returnJson([
"wall_owner" => $where == "wall" ? $this->user->identity->getId() : $groupId*-1
]);
}
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>
<div class="like_wrap">
<a n:if="!($forceNoShareLink ?? false)" class="post-share-button" href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}', {$thisUser->getId()})">
<a n:if="!($forceNoShareLink ?? false)" class="post-share-button" href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}')">
<div class="repost-icon" style="opacity: 0.4;"></div>
<span class="likeCnt">{if $post->getRepostCount() > 0}{$post->getRepostCount()}{/if}</span>
</a>

View file

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

View file

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

View file

@ -168,18 +168,18 @@ document.addEventListener("DOMContentLoaded", function() { //BEGIN
}); //END ONREADY DECLS
function repostPost(id, hash, owner) {
async function repostPost(id, hash) {
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/>
<input type="radio" name="type" onchange="signs.removeAttribute('hidden');groupId.removeAttribute('hidden')" value="group" id="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>
<label><input onchange="signed.checked ? signed.checked = false : null" type="checkbox" id="asgroup" name="asGroup">${tr('post_as_group')}</label><br>
<label><input onchange="asgroup.checked = true" type="checkbox" id="signed" name="signed">${tr('add_signature')}</label>
</div>
<br/><br/>`;
let clubs = [];
@ -197,8 +197,8 @@ function repostPost(id, hash, owner) {
}
}
groupId = document.querySelector("#groupId").value;
asGroup = asgroup.value;
signed = signed.value;
asGroup = asgroup.checked == true ? 1 : 0;
signed = signed.checked == true ? 1 : 0;
hash = encodeURIComponent(hash);
xhr = new XMLHttpRequest();
xhr.open("POST", "/wall"+id+"/repost?hash="+hash, true);
@ -215,16 +215,15 @@ function repostPost(id, hash, owner) {
}),
Function.noop
]);
let xhrj = new XMLHttpRequest();
xhrj.open("GET", "id"+owner+"/getWriteableClubs?hash="+hash)
xhrj.send()
xhrj.onload = () =>
try
{
clubs = JSON.parse(xhrj.responseText);
for(const el of clubs)
{
clubs = await API.Groups.getWriteableClubs();
for(const el of clubs) {
document.getElementById("groupId").insertAdjacentHTML("beforeend", `<option value="${el.id}">${el.name}</option>`)
}
} catch(rejection) {
console.error("You did not created any groups")
document.getElementById("group").setAttribute("disabled", "disabled")
}
}