openvk/VKAPI/Handlers/Newsfeed.php

210 lines
7.8 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use Chandler\Database\DatabaseConnection;
2023-09-26 17:58:14 +03:00
use openvk\Web\Models\Repositories\Posts as PostsRepo, Users as UsersRepo, Clubs;
use openvk\Web\Models\Entities\User;
2023-09-26 17:58:14 +03:00
use openvk\VKAPI\Handlers\{Wall, Users, Groups};
final class Newsfeed extends VKAPIRequestHandler
{
function get(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0)
{
$this->requireUser();
if($forGodSakePleaseDoNotReportAboutMyOnlineActivity == 0)
{
$this->getUser()->updOnline($this->getPlatform());
}
$id = $this->getUser()->getId();
$subs = DatabaseConnection::i()
->getContext()
->table("subscriptions")
->where("follower", $id);
$ids = array_map(function($rel) {
return $rel->target * ($rel->model === "openvk\Web\Models\Entities\User" ? 1 : -1);
}, iterator_to_array($subs));
$ids[] = $this->getUser()->getId();
$posts = DatabaseConnection::i()
2022-07-21 22:13:09 +03:00
->getContext()
->table("posts")
->select("id")
->where("wall IN (?)", $ids)
->where("deleted", 0)
->where("id < (?)", empty($start_from) ? PHP_INT_MAX : $start_from)
->where("? <= created", empty($start_time) ? 0 : $start_time)
->where("? >= created", empty($end_time) ? PHP_INT_MAX : $end_time)
2022-07-21 22:13:09 +03:00
->order("created DESC");
$rposts = [];
foreach($posts->page((int) ($offset + 1), $count) as $post)
$rposts[] = (new PostsRepo)->get($post->id)->getPrettyId();
$response = (new Wall)->getById(implode(',', $rposts), $extended, $fields, $this->getUser());
2022-09-04 20:08:22 +03:00
$response->next_from = end(end($posts->page((int) ($offset + 1), $count))); // ну и костыли пиздец конечно)
return $response;
}
function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $return_banned = 0)
{
$this->requireUser();
2023-12-02 20:21:16 +03:00
$queryBase = "FROM `posts` LEFT JOIN `groups` ON GREATEST(`posts`.`wall`, 0) = 0 AND `groups`.`id` = ABS(`posts`.`wall`) LEFT JOIN `profiles` ON LEAST(`posts`.`wall`, 0) = 0 AND `profiles`.`id` = ABS(`posts`.`wall`)";
$queryBase .= "WHERE (`groups`.`hide_from_global_feed` = 0 OR `groups`.`name` IS NULL) AND (`profiles`.`profile_type` = 0 OR `profiles`.`first_name` IS NULL) AND `posts`.`deleted` = 0 AND `posts`.`suggested` = 0";
if($this->getUser()->getNsfwTolerance() === User::NSFW_INTOLERANT)
$queryBase .= " AND `nsfw` = 0";
2023-09-26 17:58:14 +03:00
if(($ignoredCount = $this->getUser()->getIgnoredSourcesCount()) > 0 && $return_banned == 0) {
2023-09-26 17:58:14 +03:00
$sources = implode("', '", $this->getUser()->getIgnoredSources(1, $ignoredCount, true));
$queryBase .= " AND `posts`.`wall` NOT IN ('$sources')";
}
$start_from = empty($start_from) ? PHP_INT_MAX : $start_from;
$start_time = empty($start_time) ? 0 : $start_time;
$end_time = empty($end_time) ? PHP_INT_MAX : $end_time;
$posts = DatabaseConnection::i()->getConnection()->query("SELECT `posts`.`id` " . $queryBase . " AND `posts`.`id` <= " . $start_from . " AND " . $start_time . " <= `posts`.`created` AND `posts`.`created` <= " . $end_time . " ORDER BY `created` DESC LIMIT " . $count . " OFFSET " . $offset);
2023-09-26 17:58:14 +03:00
$rposts = [];
$ids = [];
foreach($posts as $post) {
$rposts[] = (new PostsRepo)->get($post->id)->getPrettyId();
$ids[] = $post->id;
}
$response = (new Wall)->getById(implode(',', $rposts), $extended, $fields, $this->getUser());
$response->next_from = end($ids);
return $response;
}
2023-09-26 17:58:14 +03:00
function getBanned(int $extended = 0, string $fields = "", string $name_case = "nom")
{
$this->requireUser();
$count = 50;
$offset = 0;
$banned = array_slice($this->getUser()->getIgnoredSources(1, $count + $offset, true), $offset);
if($extended == 0) {
$retArr/*d*/ = [
"groups" => [],
"members" => [] # why
];
foreach($banned as $ban) {
if($ban > 0) {
$retArr["members"][] = $ban;
} else {
$retArr["groups"][] = $ban;
}
}
return $retArr;
} else {
$retArr = [
"groups" => [],
"profiles" => []
];
$usIds = "";
$clubIds = "";
foreach($banned as $ban) {
if($ban > 0) {
$usIds .= $ban . ",";
} else {
$clubIds .= ($ban * -1) . ",";
}
}
$retArr["profiles"][] = (new Users)->get($usIds, $fields);
$retArr["groups"][] = (new Groups)->getById($clubIds, $fields);
return $retArr;
}
}
function addBan(string $user_ids = "", string $group_ids = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
if(empty($user_ids) && empty($group_ids))
$this->fail(52, "Provide 'user_ids' or 'groups_ids'");
$arr = [];
if(!empty($user_ids)) {
$arr = array_merge($arr, array_map(function($el) {
return (int)$el;
}, explode(",", $user_ids)));
}
if(!empty($group_ids)) {
$arr = array_merge($arr, array_map(function($el) {
return abs((int)$el) * -1;
}, explode(",", $group_ids)));
}
$arr = array_unique($arr);
if(sizeof($arr) > 10 || sizeof($arr) < 1)
$this->fail(20, "You can ignore only 10 users/groups at once");
$successes = 0;
foreach($arr as $ar) {
$entity = getEntity($ar);
if(!$entity || $entity->isHideFromGlobalFeedEnabled() || $entity->isIgnoredBy($this->getUser())) continue;
$entity->toggleIgnore($this->getUser());
$successes += 1;
}
return (int)($successes > 0);
}
function deleteBan(string $user_ids = "", string $group_ids = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
if(empty($user_ids) && empty($group_ids))
$this->fail(52, "Provide 'user_ids' or 'groups_ids'");
$arr = [];
if(!empty($user_ids)) {
$arr = array_merge($arr, array_map(function($el) {
return (int)$el;
}, explode(",", $user_ids)));
}
if(!empty($group_ids)) {
$arr = array_merge($arr, array_map(function($el) {
return abs((int)$el) * -1;
}, explode(",", $group_ids)));
}
$arr = array_unique($arr);
if(sizeof($arr) > 10 || sizeof($arr) < 1)
$this->fail(20, "You can unignore only 10 users/groups at once");
$successes = 0;
foreach($arr as $ar) {
$entity = getEntity($ar);
if(!$entity || $entity->isHideFromGlobalFeedEnabled() || !$entity->isIgnoredBy($this->getUser())) continue;
$entity->toggleIgnore($this->getUser());
$successes += 1;
}
return (int)($successes > 0);
}
}