use ORM when subscribing feeds

This commit is contained in:
Andrew Dolgov 2021-03-02 10:11:42 +03:00
parent dae0476159
commit 7cf12233d7
2 changed files with 26 additions and 27 deletions

View File

@ -986,17 +986,18 @@ class Feeds extends Handler_Protected {
* to get all possible feeds. * to get all possible feeds.
* 5 - Couldn't download the URL content. * 5 - Couldn't download the URL content.
* 6 - Content is an invalid XML. * 6 - Content is an invalid XML.
* 7 - Error while creating feed database entry.
*/ */
static function _subscribe($url, $cat_id = 0, static function _subscribe($url, $cat_id = 0,
$auth_login = '', $auth_pass = '') { $auth_login = '', $auth_pass = '') : array {
$pdo = Db::pdo(); $pdo = Db::pdo();
$url = UrlHelper::validate($url); $url = UrlHelper::validate($url);
if (!$url) return array("code" => 2); if (!$url) return ["code" => 2];
$contents = @UrlHelper::fetch($url, false, $auth_login, $auth_pass); $contents = UrlHelper::fetch($url, false, $auth_login, $auth_pass);
PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_SUBSCRIBE_FEED, PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_SUBSCRIBE_FEED,
function ($result) use (&$contents) { function ($result) use (&$contents) {
@ -1024,35 +1025,33 @@ class Feeds extends Handler_Protected {
$url = key($feedUrls); $url = key($feedUrls);
} }
if (!$cat_id) $cat_id = null; $feed = ORM::for_table('ttrss_feeds')
->where('feed_url', $url)
->where('owner_uid', $_SESSION['uid'])
->find_one();
$sth = $pdo->prepare("SELECT id FROM ttrss_feeds if ($feed) {
WHERE feed_url = ? AND owner_uid = ?"); return ["code" => 0, "feed_id" => $feed->id];
$sth->execute([$url, $_SESSION['uid']]);
if ($row = $sth->fetch()) {
return array("code" => 0, "feed_id" => (int) $row["id"]);
} else { } else {
$sth = $pdo->prepare( $feed = ORM::for_table('ttrss_feeds')->create();
"INSERT INTO ttrss_feeds
(owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
VALUES (?, ?, ?, ?, ?, ?, 0, false)");
$sth->execute([$_SESSION['uid'], $url, "[Unknown]", $cat_id, (string)$auth_login, (string)$auth_pass]); $feed->set([
'owner_uid' => $_SESSION['uid'],
'feed_url' => $url,
'title' => "[Unknown]",
'cat_id' => $cat_id ? $cat_id : null,
'auth_login' => (string)$auth_login,
'auth_pass' => (string)$auth_pass,
'update_method' => 0,
'auth_pass_encrypted' => false,
]);
$sth = $pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ? if ($feed->save()) {
AND owner_uid = ?"); RSSUtils::set_basic_feed_info($feed->id);
$sth->execute([$url, $_SESSION['uid']]); return ["code" => 1, "feed_id" => (int) $feed->id];
$row = $sth->fetch();
$feed_id = $row["id"];
if ($feed_id) {
RSSUtils::set_basic_feed_info($feed_id);
} }
return array("code" => 1, "feed_id" => (int) $feed_id); return ["code" => 7];
} }
} }

View File

@ -154,7 +154,7 @@ class Bookmarklets extends Plugin {
<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a> <a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
</form> </form>
<?php <?php
} else { } else if (!$feed_urls) {
?> ?>
<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a> <a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
<?php <?php