mirror of
https://github.com/openvk/openvk
synced 2024-11-11 01:19:53 +03:00
Репост в группу (#873)
This commit is contained in:
parent
cb6578228e
commit
8ccb98473f
9 changed files with 197 additions and 42 deletions
39
ServiceAPI/Groups.php
Normal file
39
ServiceAPI/Groups.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?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());
|
||||||
|
$count = $this->groups->getWriteableClubsCount($this->user->getId());
|
||||||
|
|
||||||
|
if(!$count) {
|
||||||
|
$reject("You don't have any groups with write access");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($wclubs as $club) {
|
||||||
|
$clubs[] = [
|
||||||
|
"name" => $club->getName(),
|
||||||
|
"id" => $club->getId(),
|
||||||
|
"avatar" => $club->getAvatarUrl() # если в овк когда-нибудь появится крутой список с аватарками, то можно использовать это поле
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$resolve($clubs);
|
||||||
|
}
|
||||||
|
}
|
|
@ -453,25 +453,38 @@ 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();
|
||||||
|
|
||||||
$postArray;
|
$postArray;
|
||||||
if(preg_match('/wall((?:-?)[0-9]+)_([0-9]+)/', $object, $postArray) == 0)
|
if(preg_match('/wall((?:-?)[0-9]+)_([0-9]+)/', $object, $postArray) == 0)
|
||||||
$this->fail(100, "One of the parameters specified was missing or invalid: object is incorrect");
|
$this->fail(100, "One of the parameters specified was missing or invalid: object is incorrect");
|
||||||
|
|
||||||
$post = (new PostsRepo)->getPostById((int) $postArray[1], (int) $postArray[2]);
|
$post = (new PostsRepo)->getPostById((int) $postArray[1], (int) $postArray[2]);
|
||||||
if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid");
|
if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid");
|
||||||
|
|
||||||
$nPost = new Post;
|
$nPost = new Post;
|
||||||
$nPost->setOwner($this->user->getId());
|
$nPost->setOwner($this->user->getId());
|
||||||
$nPost->setWall($this->user->getId());
|
|
||||||
|
if($group_id > 0) {
|
||||||
|
$club = (new ClubsRepo)->get($group_id);
|
||||||
|
if(!$club)
|
||||||
|
$this->fail(42, "Invalid group");
|
||||||
|
|
||||||
|
if(!$club->canBeModifiedBy($this->user))
|
||||||
|
$this->fail(16, "Access to group denied");
|
||||||
|
|
||||||
|
$nPost->setWall($group_id * -1);
|
||||||
|
} else {
|
||||||
|
$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();
|
||||||
$nPost->attach($post);
|
$nPost->attach($post);
|
||||||
|
|
||||||
if($post->getOwner(false)->getId() !== $this->user->getId() && !($post->getOwner() instanceof Club))
|
if($post->getOwner(false)->getId() !== $this->user->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();
|
||||||
|
|
||||||
|
@ -483,6 +496,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();
|
||||||
|
|
||||||
|
|
|
@ -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, Manager};
|
||||||
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;
|
||||||
|
|
||||||
|
@ -9,11 +9,13 @@ class Clubs
|
||||||
{
|
{
|
||||||
private $context;
|
private $context;
|
||||||
private $clubs;
|
private $clubs;
|
||||||
|
private $coadmins;
|
||||||
|
|
||||||
function __construct()
|
function __construct()
|
||||||
{
|
{
|
||||||
$this->context = DatabaseConnection::i()->getContext();
|
$this->context = DatabaseConnection::i()->getContext();
|
||||||
$this->clubs = $this->context->table("groups");
|
$this->clubs = $this->context->table("groups");
|
||||||
|
$this->coadmins = $this->context->table("group_coadmins");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function toClub(?ActiveRow $ar): ?Club
|
private function toClub(?ActiveRow $ar): ?Club
|
||||||
|
@ -70,6 +72,26 @@ class Clubs
|
||||||
];
|
];
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getWriteableClubs(int $id): \Traversable
|
||||||
|
{
|
||||||
|
$result = $this->clubs->where("owner", $id);
|
||||||
|
$coadmins = $this->coadmins->where("user", $id);
|
||||||
|
|
||||||
|
foreach($result as $entry) {
|
||||||
|
yield new Club($entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($coadmins as $coadmin) {
|
||||||
|
$cl = new Manager($coadmin);
|
||||||
|
yield $cl->getClub();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getWriteableClubsCount(int $id): int
|
||||||
|
{
|
||||||
|
return sizeof($this->clubs->where("owner", $id)) + sizeof($this->coadmins->where("user", $id));
|
||||||
|
}
|
||||||
|
|
||||||
use \Nette\SmartObject;
|
use \Nette\SmartObject;
|
||||||
}
|
}
|
||||||
|
|
|
@ -363,21 +363,52 @@ final class WallPresenter extends OpenVKPresenter
|
||||||
$this->assertNoCSRF();
|
$this->assertNoCSRF();
|
||||||
|
|
||||||
$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;
|
||||||
$nPost->setOwner($this->user->id);
|
|
||||||
$nPost->setWall($this->user->id);
|
if($where == "wall") {
|
||||||
|
$nPost->setOwner($this->user->id);
|
||||||
|
$nPost->setWall($this->user->id);
|
||||||
|
} elseif($where == "group") {
|
||||||
|
$nPost->setOwner($this->user->id);
|
||||||
|
$club = (new Clubs)->get((int)$groupId);
|
||||||
|
|
||||||
|
if(!$club || !$club->canBeModifiedBy($this->user->identity))
|
||||||
|
$this->notFound();
|
||||||
|
|
||||||
|
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();
|
||||||
};
|
};
|
||||||
|
|
||||||
$this->returnJson(["wall_owner" => $this->user->identity->getId()]);
|
$this->returnJson([
|
||||||
|
"wall_owner" => $where == "wall" ? $this->user->identity->getId() : $groupId * -1
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderDelete(int $wall, int $post_id): void
|
function renderDelete(int $wall, int $post_id): void
|
||||||
|
|
|
@ -98,9 +98,9 @@
|
||||||
<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)" id="reposts{$post->getPrettyId()}" 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" id="repostsCount{$post->getPrettyId()}">{if $post->getRepostCount() > 0}{$post->getRepostCount()}{/if}</span>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
{if !($forceNoLike ?? false)}
|
{if !($forceNoLike ?? false)}
|
||||||
|
|
|
@ -111,10 +111,10 @@
|
||||||
|
|
|
|
||||||
{/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)" id="reposts{$post->getPrettyId()}" 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 id="repostsCount{$post->getPrettyId()}">{$post->getRepostCount()}</b>)
|
||||||
{/if}
|
{/if}
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
function expand_wall_textarea(id) {
|
function expand_wall_textarea(id) {
|
||||||
var el = document.getElementById('post-buttons'+id);
|
var el = document.getElementById('post-buttons'+id);
|
||||||
var wi = document.getElementById('wall-post-input'+id);
|
var wi = document.getElementById('wall-post-input'+id);
|
||||||
|
@ -65,7 +66,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 +88,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,28 +169,72 @@ document.addEventListener("DOMContentLoaded", function() { //BEGIN
|
||||||
|
|
||||||
}); //END ONREADY DECLS
|
}); //END ONREADY DECLS
|
||||||
|
|
||||||
function repostPost(id, hash) {
|
async function repostPost(id, hash) {
|
||||||
uRepostMsgTxt = tr('your_comment') + ": <textarea id='uRepostMsgInput_"+id+"'></textarea><br/><br/>";
|
uRepostMsgTxt = `
|
||||||
|
<b>${tr('auditory')}:</b> <br/>
|
||||||
MessageBox(tr('share'), uRepostMsgTxt, [tr('send'), tr('cancel')], [
|
<input type="radio" name="type" onchange="signs.setAttribute('hidden', 'hidden');document.getElementById('groupId').setAttribute('hidden', 'hidden')" value="wall" checked>${tr("in_wall")}<br/>
|
||||||
(function() {
|
<input type="radio" name="type" onchange="signs.removeAttribute('hidden');document.getElementById('groupId').removeAttribute('hidden')" value="group" id="group">${tr("in_group")}<br/>
|
||||||
text = document.querySelector("#uRepostMsgInput_"+id).value;
|
<select style="width:50%;" id="groupId" name="groupId" hidden>
|
||||||
hash = encodeURIComponent(hash);
|
</select><br/>
|
||||||
xhr = new XMLHttpRequest();
|
<b>${tr('your_comment')}:</b>
|
||||||
xhr.open("POST", "/wall"+id+"/repost?hash="+hash, true);
|
<textarea id='uRepostMsgInput_${id}'></textarea>
|
||||||
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
<div id="signs" hidden>
|
||||||
xhr.onload = (function() {
|
<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 = [];
|
||||||
|
repostsCount = document.getElementById("repostsCount"+id)
|
||||||
|
prevVal = repostsCount != null ? Number(repostsCount.innerHTML) : 0;
|
||||||
|
|
||||||
|
MessageBox(tr('share'), uRepostMsgTxt, [tr('send'), tr('cancel')], [
|
||||||
|
(function() {
|
||||||
|
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.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);
|
||||||
|
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
||||||
|
xhr.onload = (function() {
|
||||||
if(xhr.responseText.indexOf("wall_owner") === -1)
|
if(xhr.responseText.indexOf("wall_owner") === -1)
|
||||||
MessageBox(tr('error'), tr('error_repost_fail'), [tr('ok')], [Function.noop]);
|
MessageBox(tr('error'), tr('error_repost_fail'), [tr('ok')], [Function.noop]);
|
||||||
else {
|
else {
|
||||||
let jsonR = JSON.parse(xhr.responseText);
|
let jsonR = JSON.parse(xhr.responseText);
|
||||||
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});
|
||||||
}
|
repostsCount != null ?
|
||||||
});
|
repostsCount.innerHTML = prevVal+1 :
|
||||||
xhr.send('text=' + encodeURI(text));
|
document.getElementById("reposts"+id).insertAdjacentHTML("beforeend", "(<b id='repostsCount"+id+"'>1</b>)") //для старого вида постов
|
||||||
}),
|
}
|
||||||
Function.noop
|
});
|
||||||
]);
|
xhr.send('text='+encodeURI(text) + '&type='+type + '&groupId='+groupId + "&asGroup="+asGroup + "&signed="+signed);
|
||||||
|
}),
|
||||||
|
Function.noop
|
||||||
|
]);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
clubs = await API.Groups.getWriteableClubs();
|
||||||
|
for(const el of clubs) {
|
||||||
|
document.getElementById("groupId").insertAdjacentHTML("beforeend", `<option value="${el.id}">${escapeHtml(el.name)}</option>`)
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch(rejection) {
|
||||||
|
console.error(rejection)
|
||||||
|
document.getElementById("group").setAttribute("disabled", "disabled")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setClubAdminComment(clubId, adminId, hash) {
|
function setClubAdminComment(clubId, adminId, hash) {
|
||||||
|
@ -535,4 +578,4 @@ $(document).on("scroll", () => {
|
||||||
$(".floating_sidebar")[0].classList.remove("hide_anim");
|
$(".floating_sidebar")[0].classList.remove("hide_anim");
|
||||||
}, 250);
|
}, 250);
|
||||||
}
|
}
|
||||||
})
|
})
|
|
@ -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";
|
||||||
|
|
|
@ -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" = "нет записей";
|
||||||
|
|
Loading…
Reference in a new issue