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"); $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

@ -301,7 +301,7 @@ final class WallPresenter extends OpenVKPresenter
if(!is_null($poll)) if(!is_null($poll))
$post->attach($poll); $post->attach($poll);
if($wall > 0 && $wall !== $this->user->identity->getId()) if($wall > 0 && $wall !== $this->user->identity->getId())
(new WallPostNotification($wallOwner, $post, $this->user->identity))->emit(); (new WallPostNotification($wallOwner, $post, $this->user->identity))->emit();
@ -402,11 +402,9 @@ final class WallPresenter extends OpenVKPresenter
(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([
$this->returnJson(["wall_owner" => $this->user->identity->getId()]); "wall_owner" => $where == "wall" ? $this->user->identity->getId() : $groupId*-1
]);
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)}', {$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> <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)}', {$thisUser->getId()})"{/ifset}> <a n:if="!($forceNoShareLink ?? false)" class="post-share-button" {ifset $thisUser} href="javascript:repostPost('{$post->getPrettyId()}', '{rawurlencode($csrfToken)}')"{/ifset}>
{_share} {_share}
{if $post->getRepostCount() > 0} {if $post->getRepostCount() > 0}
(<b>{$post->getRepostCount()}</b>) (<b>{$post->getRepostCount()}</b>)

View file

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

View file

@ -168,18 +168,18 @@ document.addEventListener("DOMContentLoaded", function() { //BEGIN
}); //END ONREADY DECLS }); //END ONREADY DECLS
function repostPost(id, hash, owner) { async function repostPost(id, hash) {
uRepostMsgTxt = ` uRepostMsgTxt = `
<b>${tr('auditory')}:</b> <br/> <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.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 style="width:50%;" id="groupId" name="groupId" hidden>
</select><br/> </select><br/>
<b>${tr('your_comment')}:</b> <b>${tr('your_comment')}:</b>
<textarea id='uRepostMsgInput_${id}'></textarea> <textarea id='uRepostMsgInput_${id}'></textarea>
<div id="signs" hidden> <div id="signs" hidden>
<label><input type="checkbox" id="asgroup" name="asGroup" value="1">${tr('post_as_group')}</label><br> <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" value="1">${tr('add_signature')}</label> <label><input onchange="asgroup.checked = true" type="checkbox" id="signed" name="signed">${tr('add_signature')}</label>
</div> </div>
<br/><br/>`; <br/><br/>`;
let clubs = []; let clubs = [];
@ -197,8 +197,8 @@ function repostPost(id, hash, owner) {
} }
} }
groupId = document.querySelector("#groupId").value; groupId = document.querySelector("#groupId").value;
asGroup = asgroup.value; asGroup = asgroup.checked == true ? 1 : 0;
signed = signed.value; signed = signed.checked == true ? 1 : 0;
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);
@ -215,16 +215,15 @@ function repostPost(id, hash, owner) {
}), }),
Function.noop Function.noop
]); ]);
let xhrj = new XMLHttpRequest(); try
xhrj.open("GET", "id"+owner+"/getWriteableClubs?hash="+hash)
xhrj.send()
xhrj.onload = () =>
{ {
clubs = JSON.parse(xhrj.responseText); clubs = await API.Groups.getWriteableClubs();
for(const el of clubs) for(const el of clubs) {
{
document.getElementById("groupId").insertAdjacentHTML("beforeend", `<option value="${el.id}">${el.name}</option>`) 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")
} }
} }
@ -484,4 +483,4 @@ $(document).on("scroll", () => {
$(".floating_sidebar")[0].classList.remove("hide_anim"); $(".floating_sidebar")[0].classList.remove("hide_anim");
}, 250); }, 250);
} }
}) })