Fixed: Pinned bookmarks are displayed first in ATOM/RSS feeds

Fixes #1485
This commit is contained in:
ArthurHoaro 2020-08-29 10:06:40 +02:00
parent bea062149e
commit a8e210faa6
5 changed files with 31 additions and 14 deletions

View file

@ -234,16 +234,17 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess
*
* 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;
// 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 $b Bookmark */
if ($a->isSticky() !== $b->isSticky()) {
if (false === $ignoreSticky && $a->isSticky() !== $b->isSticky()) {
return $a->isSticky() ? -1 : 1;
}
return $a->getCreated() < $b->getCreated() ? 1 * $order : -1 * $order;

View file

@ -114,8 +114,13 @@ class BookmarkFileService implements BookmarkServiceInterface
/**
* @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) {
$visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC;
}
@ -124,6 +129,10 @@ class BookmarkFileService implements BookmarkServiceInterface
$searchtags = isset($request['searchtags']) ? $request['searchtags'] : '';
$searchterm = isset($request['searchterm']) ? $request['searchterm'] : '';
if ($ignoreSticky) {
$this->bookmarks->reorder('DESC', true);
}
return $this->bookmarkFilter->filter(
BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
[$searchtags, $searchterm],

View file

@ -49,10 +49,17 @@ interface BookmarkServiceInterface
* @param string $visibility
* @param bool $caseSensitive
* @param bool $untaggedOnly
* @param bool $ignoreSticky
*
* @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.

View file

@ -102,7 +102,7 @@ class FeedBuilder
}
// Optionally filter the results:
$linksToDisplay = $this->linkDB->search($userInput);
$linksToDisplay = $this->linkDB->search($userInput, null, false, false, true);
$nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput);

View file

@ -87,7 +87,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
// 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(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
@ -128,7 +128,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$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('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
}
@ -173,7 +173,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
$feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
$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(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
}
@ -195,7 +195,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertTrue($data['usepermalinks']);
// 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(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
@ -203,7 +203,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
$this->assertContains('Direct link', $link['description']);
$this->assertContains('http://host.tld/shaare/WDWyig', $link['description']);
// 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(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
$this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
@ -270,7 +270,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
);
// 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['url']);
$this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);