From 4046c992f95b2b90be978f8bde34a5950e5eee2d Mon Sep 17 00:00:00 2001
From: mrilyew <99399973+mrilyew@users.noreply.github.com>
Date: Tue, 19 Nov 2024 16:31:17 +0300
Subject: [PATCH] feat(news): add rss param for newsfeed.getGlobal
---
VKAPI/Handlers/Newsfeed.php | 21 +++++++++++++++-
VKAPI/Handlers/Wall.php | 18 +++++++++++++-
Web/Models/Entities/Post.php | 41 +++++++++++++++++++++++++++++++
Web/Presenters/VKAPIPresenter.php | 32 ++++++++++++++++++------
4 files changed, 102 insertions(+), 10 deletions(-)
diff --git a/VKAPI/Handlers/Newsfeed.php b/VKAPI/Handlers/Newsfeed.php
index bf36f2b5..557a3724 100644
--- a/VKAPI/Handlers/Newsfeed.php
+++ b/VKAPI/Handlers/Newsfeed.php
@@ -48,7 +48,7 @@ final class Newsfeed extends VKAPIRequestHandler
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)
+ 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 $rss = 0)
{
$this->requireUser();
@@ -74,6 +74,25 @@ final class Newsfeed extends VKAPIRequestHandler
$rposts = [];
$ids = [];
+ if($rss == 1) {
+ $channel = new \Bhaktaraz\RSSGenerator\Channel();
+ $channel->title("Global Feed — " . OPENVK_ROOT_CONF['openvk']['appearance']['name'])
+ ->description('OVK Global feed')
+ ->url(ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/feed/all");
+
+ foreach($posts as $item) {
+ $post = (new PostsRepo)->get($item->id);
+ if(!$post || $post->isDeleted()) {
+ continue;
+ }
+
+ $output = $post->toRss();
+ $output->appendTo($channel);
+ }
+
+ return $channel;
+ }
+
foreach($posts as $post) {
$rposts[] = (new PostsRepo)->get($post->id)->getPrettyId();
$ids[] = $post->id;
diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php
index f6271e69..2fe34d65 100644
--- a/VKAPI/Handlers/Wall.php
+++ b/VKAPI/Handlers/Wall.php
@@ -20,7 +20,7 @@ use openvk\Web\Models\Repositories\Audios as AudiosRepo;
final class Wall extends VKAPIRequestHandler
{
- function get(int $owner_id, string $domain = "", int $offset = 0, int $count = 30, int $extended = 0, string $filter = "all"): object
+ function get(int $owner_id, string $domain = "", int $offset = 0, int $count = 30, int $extended = 0, string $filter = "all", int $rss = 0): object
{
$this->requireUser();
@@ -86,6 +86,8 @@ final class Wall extends VKAPIRequestHandler
break;
}
+ $iteratorv = iterator_to_array($iteratorv);
+
foreach($iteratorv as $post) {
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
@@ -217,6 +219,20 @@ final class Wall extends VKAPIRequestHandler
$attachments = NULL; # free attachments so it will not clone everythingg
}
+ if($rss == 1) {
+ $channel = new \Bhaktaraz\RSSGenerator\Channel();
+ $channel->title($wallOnwer->getCanonicalName() . " — " . OPENVK_ROOT_CONF['openvk']['appearance']['name'])
+ ->description('Wall of ' . $wallOnwer->getCanonicalName())
+ ->url(ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/wall" . $wallOnwer->getRealId());
+
+ foreach($iteratorv as $item) {
+ $output = $item->toRss();
+ $output->appendTo($channel);
+ }
+
+ return $channel;
+ }
+
if($extended == 1) {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php
index 6aa09889..e86d5904 100644
--- a/Web/Models/Entities/Post.php
+++ b/Web/Models/Entities/Post.php
@@ -373,6 +373,47 @@ class Post extends Postable
return $user->getId() == $this->getOwner(false)->getId();
}
+
+ function toRss(): \Bhaktaraz\RSSGenerator\Item
+ {
+ $domain = ovk_scheme(true).$_SERVER["HTTP_HOST"];
+ $description = $this->getText(false);
+ $description_html = $description;
+ $url = $domain."/wall".$this->getPrettyId();
+
+ $author = $this->getOwner();
+ $author_name = htmlspecialchars($author->getCanonicalName(), ENT_DISALLOWED | ENT_XHTML);
+ if($this->isExplicit())
+ $description_html .= "
".tr('contains_nsfw').".
";
+
+ foreach($this->getChildren() as $child) {
+ if($child instanceof Photo) {
+ $child_page = $domain.$child->getPageURL();
+ $child_url = $child->getURLBySizeId('large');
+ $description_html .= "
";
+ } elseif($child instanceof Video) {
+ $child_page = $domain.'/video'.$child->getPrettyId();
+ $description_html .= "
Video";
+ } elseif($child instanceof Audio) {
+ $description_html .= "
Audio";
+ }
+ }
+
+ $description_html .= "
".tr('author').": " . $author_name . "";
+ if($this->hasSource()) {
+ $description_html .= "
".tr('source').": ".htmlspecialchars($this->getSource(), ENT_DISALLOWED | ENT_XHTML);
+ }
+
+ $item = new \Bhaktaraz\RSSGenerator\Item();
+ $item->title(str_replace("\n", "", ovk_proc_strtr($description, 79)))
+ ->url($url)
+ ->guid($url)
+ ->creator($author_name)
+ ->pubDate($this->getPublicationTime()->timestamp())
+ ->content(str_replace("\n", "
", $description_html));
+
+ return $item;
+ }
use Traits\TRichText;
}
diff --git a/Web/Presenters/VKAPIPresenter.php b/Web/Presenters/VKAPIPresenter.php
index 9eb4565a..e7a5a42f 100644
--- a/Web/Presenters/VKAPIPresenter.php
+++ b/Web/Presenters/VKAPIPresenter.php
@@ -222,9 +222,13 @@ final class VKAPIPresenter extends OpenVKPresenter
if(!is_callable([$handler, $method]))
$this->badMethod($object, $method);
+ $has_rss = false;
$route = new \ReflectionMethod($handler, $method);
$params = [];
foreach($route->getParameters() as $parameter) {
+ if($parameter->getName() == 'rss')
+ $has_rss = true;
+
$val = $this->requestParam($parameter->getName());
if(is_null($val)) {
if($parameter->allowsNull())
@@ -260,15 +264,27 @@ final class VKAPIPresenter extends OpenVKPresenter
$this->fail($ex->getCode(), $ex->getMessage(), $object, $method);
}
- $result = json_encode([
- "response" => $res,
- ]);
+ $result = NULL;
- if($callback) {
- $result = $callback . '(' . $result . ')';
- header('Content-Type: application/javascript');
- } else
- header("Content-Type: application/json");
+ if($this->queryParam("rss") == '1' && $has_rss) {
+ $feed = new \Bhaktaraz\RSSGenerator\Feed();
+ $res->appendTo($feed);
+
+ $result = strval($feed);
+
+ header("Content-Type: application/rss+xml;charset=UTF-8");
+ } else {
+ $result = json_encode([
+ "response" => $res,
+ ]);
+
+ if($callback) {
+ $result = $callback . '(' . $result . ')';
+ header('Content-Type: application/javascript');
+ } else {
+ header("Content-Type: application/json");
+ }
+ }
$size = strlen($result);
header("Content-Length: $size");