Add picking photos from clubs albums

Копипейст и граффити так и не пофикшены
This commit is contained in:
lalka2016 2023-09-20 18:25:10 +03:00
parent ed49596674
commit c7aac887f0
3 changed files with 62 additions and 45 deletions

View file

@ -1,7 +1,7 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\{Photos as PhotosRepo, Albums}; use openvk\Web\Models\Repositories\{Photos as PhotosRepo, Albums, Clubs};
class Photos implements Handler class Photos implements Handler
{ {
@ -22,9 +22,17 @@ class Photos implements Handler
} else { } else {
$album = (new Albums)->get($album); $album = (new Albums)->get($album);
if(!$album || $album->isDeleted() || $album->getOwner()->getId() != $this->user->getId()) if(!$album || $album->isDeleted())
$reject(55, "Invalid ."); $reject(55, "Invalid .");
if($album->getOwner() instanceof User) {
if($album->getOwner()->getId() != $this->user->getId())
$reject(555, "Access to album denied");
} else {
if(!$album->getOwner()->canBeModifiedBy($this->user))
$reject(555, "Access to album denied");
}
$photos = $album->getPhotos($page, 24); $photos = $album->getPhotos($page, 24);
$count = $album->size(); $count = $album->size();
} }
@ -43,9 +51,10 @@ class Photos implements Handler
$resolve($arr); $resolve($arr);
} }
function getAlbums(callable $resolve, callable $reject) function getAlbums(int $club, callable $resolve, callable $reject)
{ {
$albumsRepo = (new Albums); $albumsRepo = (new Albums);
$count = $albumsRepo->getUserAlbumsCount($this->user); $count = $albumsRepo->getUserAlbumsCount($this->user);
$albums = $albumsRepo->getUserAlbums($this->user, 1, $count); $albums = $albumsRepo->getUserAlbums($this->user, 1, $count);
@ -60,6 +69,24 @@ class Photos implements Handler
$arr["items"][] = $res; $arr["items"][] = $res;
} }
if($club > 0) {
$cluber = (new Clubs)->get($club);
if(!$cluber || !$cluber->canBeModifiedBy($this->user))
$reject(1337, "Invalid (club), or you can't modify him");
$clubCount = (new Albums)->getClubAlbumsCount($cluber);
$clubAlbums = (new Albums)->getClubAlbums($cluber, 1, $clubCount);
foreach($clubAlbums as $albumr) {
$res = ["id" => $albumr->getId(), "name" => $albumr->getName()];
$arr["items"][] = $res;
}
$arr["count"] = $arr["count"] + $clubCount;
}
$resolve($arr); $resolve($arr);
} }
} }

View file

@ -77,7 +77,7 @@
<a class="header" href="javascript:toggleMenu({$textAreaId});"> <a class="header" href="javascript:toggleMenu({$textAreaId});">
{_attach} {_attach}
</a> </a>
<a id="photosAttachments"> <a id="photosAttachments" {if !is_null($club ?? NULL) && $club->canBeModifiedBy($thisUser)}data-club="{$club->getId()}"{/if}>
<img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-x-egon.png" /> <img src="/assets/packages/static/openvk/img/oxygen-icons/16x16/mimetypes/application-x-egon.png" />
{_photo} {_photo}
</a> </a>

View file

@ -42,9 +42,12 @@ function initGraffiti(id) {
canvas.getImage({includeWatermark: false}).toBlob(blob => { canvas.getImage({includeWatermark: false}).toBlob(blob => {
let fName = "Graffiti-" + Math.ceil(performance.now()).toString() + ".jpeg"; let fName = "Graffiti-" + Math.ceil(performance.now()).toString() + ".jpeg";
let image = new File([blob], fName, {type: "image/jpeg", lastModified: new Date().getTime()}); let image = new File([blob], fName, {type: "image/jpeg", lastModified: new Date().getTime()});
let trans = new DataTransfer();
trans.items.add(image);
let formdata = new FormData()
formdata.append("hash", u("meta[name=csrf]").attr("value"))
formdata.append("count", 1)
//ky.post("/photos/upload", {body:})
let fileSelect = document.querySelector("#post-buttons" + id + " input[name='_pic_attachment']"); let fileSelect = document.querySelector("#post-buttons" + id + " input[name='_pic_attachment']");
fileSelect.files = trans.files; fileSelect.files = trans.files;
@ -115,43 +118,20 @@ function setupWallPostInputHandlers(id) {
}); });
u(`#wall-post-input${id}`).on("paste", function(e) { u(`#wall-post-input${id}`).on("paste", function(e) {
let xhr = new XMLHttpRequest() if(e.clipboardData.files.length === 1) {
let formdat = new FormData() let xhr = new XMLHttpRequest()
let iterator = 0 let formdat = new FormData()
formdat.append("photo_0", e.clipboardData.files[0])
formdat.append("count", 1)
formdat.append("hash", u("meta[name=csrf]").attr("value"))
for (let i = 0; i < e.clipboardData.files.length; i++) {
if(getMediaCount() >= 10) {
alert('Не больше 10 пикч');
}
if(e.clipboardData.files[i].type.match('^image/')) { xhr.open("POST", "/photos/upload")
addPhotoMedia(e.clipboardData.files[i])
} xhr.send(formdat)
console.log(e.clipboardData.files);
} }
console.log(formdat)
}); });
function addPhotoMedia(files, preview, id) {
if(getMediaCount() >= 10) {
alert('Не больше 10 пикч');
} else {
u(`#post-buttons${id} .upload`).append(u(`
<div class="upload-item" id="aP${picCount}">
<a href="javascript:removePicture(${picCount})" class="upload-delete">×</a>
<img src="${preview}">
</div>
`));
u(`div#aP${picCount}`).nodes[0].append(u(`<input type="file" accept="image/*" name="attachPic${picCount}" id="attachPic${picCount}" style="display: none;">`).first());
let input = u(`#attachPic${picCount}`).nodes[0];
input.files = files; // нужен рефактор, но щас не
console.log(input);
u(input).trigger("change");
}
}
function getMediaCount() {
return u(`#post-buttons${id} .upload`).nodes[0].children.length;
}
} }
function removePicture(idA) { function removePicture(idA) {
@ -744,15 +724,21 @@ $(document).on("click", "#photosAttachments", async (e) => {
document.querySelector(".ovk-diag-body").style.height = "335px" document.querySelector(".ovk-diag-body").style.height = "335px"
async function insertPhotos(page, album = 0) { async function insertPhotos(page, album = 0) {
u("#loader").remove()
let insertPlace = document.querySelector(".photosInsert .photosList") let insertPlace = document.querySelector(".photosInsert .photosList")
document.querySelector(".photosInsert").insertAdjacentHTML("beforeend", `<img id="loader" style="max-height: 8px;max-width: 36px;" src="/assets/packages/static/openvk/img/loading_mini.gif">`) document.querySelector(".photosInsert").insertAdjacentHTML("beforeend", `<img id="loader" style="max-height: 8px;max-width: 36px;" src="/assets/packages/static/openvk/img/loading_mini.gif">`)
let photos; let photos;
if(album == 0) { try {
photos = await API.Photos.getPhotos(page, 0)
} else {
photos = await API.Photos.getPhotos(page, Number(album)) photos = await API.Photos.getPhotos(page, Number(album))
} catch(e) {
document.querySelector(".photosInsert h4").innerHTML = tr("is_x_photos", -1)
insertPlace.innerHTML = "Invalid album"
console.error(e)
u("#loader").remove()
return;
} }
document.querySelector(".photosInsert h4").innerHTML = tr("is_x_photos", photos.count) document.querySelector(".photosInsert h4").innerHTML = tr("is_x_photos", photos.count)
@ -782,12 +768,12 @@ $(document).on("click", "#photosAttachments", async (e) => {
insertPhotos(1) insertPhotos(1)
let albums = await API.Photos.getAlbums() let albums = await API.Photos.getAlbums(Number(e.currentTarget.dataset.club ?? 0))
for(const alb of albums.items) { for(const alb of albums.items) {
let sel = document.querySelector(".ovk-diag-body #albumSelect") let sel = document.querySelector(".ovk-diag-body #albumSelect")
sel.insertAdjacentHTML("beforeend", `<option value="${alb.id}">${escapeHtml(alb.name)}</option>`) sel.insertAdjacentHTML("beforeend", `<option value="${alb.id}">${ovk_proc_strtr(escapeHtml(alb.name), 20)}</option>`)
} }
$(".photosInsert").on("click", "#showMorePhotos", (e) => { $(".photosInsert").on("click", "#showMorePhotos", (e) => {
@ -797,6 +783,7 @@ $(document).on("click", "#photosAttachments", async (e) => {
$(".topGrayBlock #albumSelect").on("change", (evv) => { $(".topGrayBlock #albumSelect").on("change", (evv) => {
document.querySelector(".photosInsert .photosList").innerHTML = "" document.querySelector(".photosInsert .photosList").innerHTML = ""
insertPhotos(1, evv.currentTarget.value) insertPhotos(1, evv.currentTarget.value)
}) })
@ -900,6 +887,9 @@ $(document).on("click", "#photosAttachments", async (e) => {
u("body").removeClass("dimmed"); u("body").removeClass("dimmed");
u(".ovk-diag-cont").remove(); u(".ovk-diag-cont").remove();
document.querySelector("html").style.overflowY = "scroll" document.querySelector("html").style.overflowY = "scroll"
} else {
// todo: https://vk.com/wall-32295218_78593
alert(result.flash.message)
} }
} }