Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions VKAPI/Handlers/Newsfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ public function get(string $fields = "", string $start_from = "", int $start_tim
}, iterator_to_array($subs));
$ids[] = $this->getUser()->getId();

$posts = DatabaseConnection::i()
->getContext()
->table("posts")
->select("id, created")
->where("wall IN (?)", $ids)
->where("deleted", 0)
->where("suggested", 0)
->where("archived", 0)
$posts = (new PostsRepo())->getFeedSelection($ids)
->where("created <= ?", $cursorTime)
->where("created < ? OR id < ?", $cursorTime, $cursorId)
->where("? <= created", empty($start_time) ? 0 : $start_time)
Expand Down Expand Up @@ -86,29 +79,15 @@ public function getGlobal(string $fields = "", string $start_from = "", int $sta

[$cursorTime, $cursorId] = $this->parseCursor($start_from);

$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 AND `posts`.`archived` = 0";

if ($with_alien_wall_posts == 0) {
$queryBase .= " AND ((`posts`.`wall` < 0 AND (`posts`.`flags` & 128) > 0) OR (`posts`.`wall` > 0 AND `posts`.`wall` = `posts`.`owner`))";
}

if ($this->getUser()->getNsfwTolerance() === User::NSFW_INTOLERANT) {
$queryBase .= " AND `nsfw` = 0";
}

if ($return_banned == 0) {
$ignored_sources_ids = $this->getUser()->getIgnoredSources(0, OPENVK_ROOT_CONF['openvk']['preferences']['newsfeed']['ignoredSourcesLimit'] ?? 50, true);

if (sizeof($ignored_sources_ids) > 0) {
$imploded_ids = implode("', '", $ignored_sources_ids);
$queryBase .= " AND `posts`.`wall` NOT IN ('$imploded_ids')";
}
}

$start_time = empty($start_time) ? 0 : $start_time;
$end_time = empty($end_time) ? PHP_INT_MAX : $end_time;

$queryBase = (new PostsRepo())->getGlobalFeedQuery(
user: $this->getUser(),
with_alien_wall_posts: $with_alien_wall_posts === 1,
return_banned: $return_banned === 1
);

$cursorFilter = " AND (`posts`.`created` < {$cursorTime} OR (`posts`.`created` = {$cursorTime} AND `posts`.`id` < {$cursorId}))";

$posts = DatabaseConnection::i()->getConnection()->query(
Expand Down Expand Up @@ -173,13 +152,7 @@ public function search(string $q = "", int $extended = 1, int $count = 30, int $

$postsRepo = new PostsRepo();

$queryBase = DatabaseConnection::i()->getContext()
->table("posts")
->select("id, created")
->where("content LIKE ?", "%{$q}%")
->where("deleted", 0)
->where("suggested", 0)
->where("archived", 0)
$queryBase = $postsRepo->getSearchSelection($q)->select("id, created")
->where("created <= ?", $cursorTime)
->where("created < ? OR id < ?", $cursorTime, $cursorId)
->where("? <= created", $start_time)
Expand Down
78 changes: 65 additions & 13 deletions VKAPI/Handlers/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public function get(int $owner_id, string $domain = "", int $offset = 0, int $co
$cnt = $posts->getOthersCountOnUserWall($owner_id);
break;
case "postponed":
$this->fail(42, "Postponed posts are not implemented.");
if (!$wallOnwer->canPlanPosts($this->getUser())) {
$this->fail(15, "Access denied");
}
$iteratorv = $posts->getPlannedPostsFromWall($owner_id, 1, $count, $offset);
$cnt = $posts->getPlannedCountOnUserWall($owner_id);
break;
case "suggests":
if ($owner_id < 0) {
Expand Down Expand Up @@ -336,7 +340,7 @@ public function getById(string $posts, int $extended = 0, string $fields = "", U

foreach ($psts as $pst) {
$id = explode("_", $pst);
$post = (new PostsRepo())->getPostById(intval($id[0]), intval($id[1]), true);
$post = (new PostsRepo())->getPostById(intval($id[0]), intval($id[1]), true, true);

if ($post && !$post->isDeleted()) {
if (!$post->canBeViewedBy($user)) {
Expand All @@ -347,6 +351,10 @@ public function getById(string $posts, int $extended = 0, string $fields = "", U
continue;
}

if ($post->isPlanned() && !$post->getOwner()->canPlanPosts($this->getUser())) {
continue;
}

$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments = [];
$repost = []; // чел высрал семь сигарет 😳 помянем 🕯
Expand Down Expand Up @@ -571,7 +579,8 @@ public function post(
int $explicit = 0,
float $lat = null,
float $long = null,
string $place_name = ''
string $place_name = '',
int $publish_date = 0
): object {
$this->requireUser();
$this->willExecuteWriteAction();
Expand All @@ -597,16 +606,26 @@ public function post(
}

if ($post_id > 0) {
if ($owner_id > 0) {
$this->fail(62, "Suggested posts available only at groups");
}

$post = (new PostsRepo())->getPostById($owner_id, $post_id, true);
$post = (new PostsRepo())->getPostById($owner_id, $post_id, true, true);

if (!$post || $post->isDeleted()) {
$this->fail(32, "Invald post");
}

if ($post->isPlanned()) {
if (!$wallOwner->canPlanPosts($this->getUser())) {
$this->fail(16, "Access denied");
}

$post->setCreated(time());
$post->save();
return (object) ["post_id" => $post->getVirtualId()];
}

if ($owner_id > 0) {
$this->fail(62, "Suggested posts available only at groups");
}

if ($post->getSuggestionType() == 0) {
$this->fail(20, "Post is not suggested");
}
Expand Down Expand Up @@ -679,11 +698,27 @@ public function post(
$this->fail(100, "Required parameter 'message' missing.");
}

$time = time();
$planned = false;

if ($publish_date > 0) {
if (!$wallOwner->canPlanPosts($this->getUser())) {
$this->fail(7, "Access to planning posts denied");
}

if ($publish_date < time()) {
$this->fail(102, "publish_date must be in future");
}

$time = $publish_date;
$planned = true;
}

try {
$post = new Post();
$post->setOwner($this->getUser()->getId());
$post->setWall($owner_id);
$post->setCreated(time());
$post->setCreated($time);
$post->setContent($message);
$post->setFlags($flags);
$post->setApi_Source_Name($this->getPlatform());
Expand Down Expand Up @@ -755,7 +790,7 @@ public function post(
$post->attach($attachment);
}

if ($owner_id > 0 && $owner_id !== $this->getUser()->getId()) {
if ($owner_id > 0 && $owner_id !== $this->getUser()->getId() && !$planned) {
(new WallPostNotification($wallOwner, $post, $this->getUser()))->emit();
}

Expand Down Expand Up @@ -1161,7 +1196,7 @@ public function delete(int $owner_id, int $post_id)
$this->requireUser();
$this->willExecuteWriteAction();

$post = (new PostsRepo())->getPostById($owner_id, $post_id, true);
$post = (new PostsRepo())->getPostById($owner_id, $post_id, true, true);
if (!$post || $post->isDeleted()) {
$this->fail(15, "Not found");
}
Expand All @@ -1187,7 +1222,7 @@ public function delete(int $owner_id, int $post_id)
}
}

public function edit(int $owner_id, int $post_id, string $message = "", string $attachments = "", string $copyright = null, int $explicit = -1, int $from_group = 0, int $signed = 0)
public function edit(int $owner_id, int $post_id, string $message = "", string $attachments = "", string $copyright = null, int $explicit = -1, int $from_group = 0, int $signed = 0, int $publish_date = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();
Expand All @@ -1205,7 +1240,7 @@ public function edit(int $owner_id, int $post_id, string $message = "", string $
$this->fail(-66, "Post will be empty, don't saving.");
}

$post = (new PostsRepo())->getPostById($owner_id, $post_id, true);
$post = (new PostsRepo())->getPostById($owner_id, $post_id, true, true);

if (!$post || $post->isDeleted()) {
$this->fail(102, "Invalid post");
Expand Down Expand Up @@ -1249,6 +1284,23 @@ public function edit(int $owner_id, int $post_id, string $message = "", string $

$post->setFlags($flags);

if ($publish_date > 0 && $publish_date != $post->getCreated()) {

if (!$wallOwner?->canPlanPosts($this->getUser())) {
$this->fail(7, "Access to planning posts denied");
}

if ($post->getCreated() < time()) {
$this->fail(102, "Post is already published");
}

if ($publish_date < time()) {
$this->fail(102, "Publish date must be in future");
}

$post->setCreated($publish_date);
}

$post->save(true);

if ($attachments == 'remove' || sizeof($final_attachments) > 0) {
Expand Down
6 changes: 6 additions & 0 deletions Web/Models/Entities/Club.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,10 @@ public function getFinishDate(): ?DateTime
{
return !is_null($this->getRecord()->finish_date) ? new DateTime($this->getRecord()->finish_date) : null;
}

public function canPlanPosts(?User $user): bool
{
return $user && $this->canBeModifiedBy($user);
}

}
14 changes: 14 additions & 0 deletions Web/Models/Entities/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public function setArchived(bool $archived): void
}
}

public function getCreated(): int
{
return $this->getRecord()->created;
}

public function isPlanned(): bool
{
return $this->getRecord()->created > time();
}

public function getOwnerPost(): int
{
return $this->getOwner(false)->getId();
Expand Down Expand Up @@ -308,6 +318,10 @@ public function canBePinnedBy(User $user = null): bool
return false;
}

if ($this->isPlanned()) {
return false;
}

if ($this->getTargetWall() < 0) {
$club = (new Clubs())->get(abs($this->getTargetWall()));

Expand Down
6 changes: 6 additions & 0 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -1937,4 +1937,10 @@ public function resetEvents(array $list): void
$this->stateChanges("events_refresh_time", time());
$this->save();
}

public function canPlanPosts(?User $user): bool
{
return $this->getId() == $user?->getId();
}

}
Loading