Merge pull request #1524 from ArthurHoaro/fix/rss-sticky

Fixed: Pinned bookmarks are displayed first in ATOM/RSS feeds
This commit is contained in:
ArthurHoaro 2020-09-03 08:45:12 +02:00 committed by GitHub
commit 2835ac7cbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 14 deletions

View file

@ -234,16 +234,17 @@ public function getByUrl($url)
* *
* Also update the urls and ids mapping arrays. * Also update the urls and ids mapping arrays.
* *
* @param string $order ASC|DESC * @param string $order ASC|DESC
* @param bool $ignoreSticky If set to true, sticky bookmarks won't be first
*/ */
public function reorder($order = 'DESC') public function reorder(string $order = 'DESC', bool $ignoreSticky = false): void
{ {
$order = $order === 'ASC' ? -1 : 1; $order = $order === 'ASC' ? -1 : 1;
// Reorder array by dates. // Reorder array by dates.
usort($this->bookmarks, function ($a, $b) use ($order) { usort($this->bookmarks, function ($a, $b) use ($order, $ignoreSticky) {
/** @var $a Bookmark */ /** @var $a Bookmark */
/** @var $b Bookmark */ /** @var $b Bookmark */
if ($a->isSticky() !== $b->isSticky()) { if (false === $ignoreSticky && $a->isSticky() !== $b->isSticky()) {
return $a->isSticky() ? -1 : 1; return $a->isSticky() ? -1 : 1;
} }
return $a->getCreated() < $b->getCreated() ? 1 * $order : -1 * $order; return $a->getCreated() < $b->getCreated() ? 1 * $order : -1 * $order;

View file

@ -114,8 +114,13 @@ public function findByUrl($url)
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function search($request = [], $visibility = null, $caseSensitive = false, $untaggedOnly = false) public function search(
{ $request = [],
$visibility = null,
$caseSensitive = false,
$untaggedOnly = false,
bool $ignoreSticky = false
) {
if ($visibility === null) { if ($visibility === null) {
$visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC;
} }
@ -124,6 +129,10 @@ public function search($request = [], $visibility = null, $caseSensitive = false
$searchtags = isset($request['searchtags']) ? $request['searchtags'] : ''; $searchtags = isset($request['searchtags']) ? $request['searchtags'] : '';
$searchterm = isset($request['searchterm']) ? $request['searchterm'] : ''; $searchterm = isset($request['searchterm']) ? $request['searchterm'] : '';
if ($ignoreSticky) {
$this->bookmarks->reorder('DESC', true);
}
return $this->bookmarkFilter->filter( return $this->bookmarkFilter->filter(
BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
[$searchtags, $searchterm], [$searchtags, $searchterm],

View file

@ -49,10 +49,17 @@ public function findByUrl($url);
* @param string $visibility * @param string $visibility
* @param bool $caseSensitive * @param bool $caseSensitive
* @param bool $untaggedOnly * @param bool $untaggedOnly
* @param bool $ignoreSticky
* *
* @return Bookmark[] * @return Bookmark[]
*/ */
public function search($request = [], $visibility = null, $caseSensitive = false, $untaggedOnly = false); public function search(
$request = [],
$visibility = null,
$caseSensitive = false,
$untaggedOnly = false,
bool $ignoreSticky = false
);
/** /**
* Get a single bookmark by its ID. * Get a single bookmark by its ID.

View file

@ -102,7 +102,7 @@ public function buildData(string $feedType, ?array $userInput)
} }
// Optionally filter the results: // Optionally filter the results:
$linksToDisplay = $this->linkDB->search($userInput); $linksToDisplay = $this->linkDB->search($userInput, null, false, false, true);
$nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput); $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput);

View file

@ -87,7 +87,7 @@ public function testRSSBuildData()
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
// Test first not pinned link (note link) // Test first not pinned link (note link)
$link = $data['links'][array_keys($data['links'])[2]]; $link = $data['links'][array_keys($data['links'])[0]];
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
@ -128,7 +128,7 @@ public function testAtomBuildData()
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
$link = $data['links'][array_keys($data['links'])[2]]; $link = $data['links'][array_keys($data['links'])[0]];
$this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
$this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
} }
@ -173,7 +173,7 @@ public function testBuildDataCount()
$feedBuilder->setLocale(self::$LOCALE); $feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria); $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
$this->assertEquals(3, count($data['links'])); $this->assertEquals(3, count($data['links']));
$link = $data['links'][array_keys($data['links'])[2]]; $link = $data['links'][array_keys($data['links'])[0]];
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
} }
@ -195,7 +195,7 @@ public function testBuildDataPermalinks()
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertTrue($data['usepermalinks']); $this->assertTrue($data['usepermalinks']);
// First link is a permalink // First link is a permalink
$link = $data['links'][array_keys($data['links'])[2]]; $link = $data['links'][array_keys($data['links'])[0]];
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
@ -203,7 +203,7 @@ public function testBuildDataPermalinks()
$this->assertContains('Direct link', $link['description']); $this->assertContains('Direct link', $link['description']);
$this->assertContains('http://host.tld/shaare/WDWyig', $link['description']); $this->assertContains('http://host.tld/shaare/WDWyig', $link['description']);
// Second link is a direct link // Second link is a direct link
$link = $data['links'][array_keys($data['links'])[3]]; $link = $data['links'][array_keys($data['links'])[1]];
$this->assertEquals(8, $link['id']); $this->assertEquals(8, $link['id']);
$this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
$this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']); $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
@ -270,7 +270,7 @@ public function testBuildDataServerSubdir()
); );
// Test first link (note link) // Test first link (note link)
$link = $data['links'][array_keys($data['links'])[2]]; $link = $data['links'][array_keys($data['links'])[0]];
$this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']); $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']);
$this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']); $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']);
$this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']); $this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);