Skip to content

Commit

Permalink
imp: Improve performance when checking if news are available
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Oct 17, 2024
1 parent b30022d commit 4101468
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
Binary file modified locales/fr_FR/LC_MESSAGES/main.mo
Binary file not shown.
6 changes: 3 additions & 3 deletions locales/fr_FR/LC_MESSAGES/main.po
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
msgid ""
msgstr ""
"Project-Id-Version: Flus\n"
"POT-Creation-Date: 2024-10-04 19:18+0200\n"
"PO-Revision-Date: 2024-10-04 19:18+0200\n"
"POT-Creation-Date: 2024-10-17 17:36+0200\n"
"PO-Revision-Date: 2024-10-17 17:36+0200\n"
"Last-Translator: Marien Fressinaud <dev@marienfressinaud.fr>\n"
"Language-Team: \n"
"Language: fr_FR\n"
Expand Down Expand Up @@ -50,7 +50,7 @@ msgstr "Afficher"
#: controllers/Feeds.php:107 controllers/Groups.php:90
#: controllers/Links.php:293 controllers/Links.php:465
#: controllers/Mastodon.php:211 controllers/Mastodon.php:297
#: controllers/News.php:77 controllers/Passwords.php:89
#: controllers/News.php:76 controllers/Passwords.php:89
#: controllers/Passwords.php:192 controllers/Registrations.php:103
#: controllers/Sessions.php:85 controllers/Support.php:70
#: controllers/collections/Filters.php:105
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ public function showAvailable(Request $request): Response
]);
}

$links = models\Link::listFromFollowedCollections($user->id, max:2);

return Response::json(200, [
'available' => count($links) > 0,
'available' => models\Link::anyFromFollowedCollections($user->id),
]);
}
}
72 changes: 72 additions & 0 deletions src/models/dao/links/NewsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,78 @@ public static function listFromFollowedCollections(string $user_id, int $max): a
return self::fromDatabaseRows($results);
}

/**
* Return whether there are any public links listed in followed collections
* of the given user.
*/
public static function anyFromFollowedCollections(string $user_id): bool
{
$values = [
':user_id' => $user_id,
':until_hard_limit' => \Minz\Time::ago(1, 'year')->format(Database\Column::DATETIME_FORMAT),
':until_strict' => \Minz\Time::ago(1, 'day')->format(Database\Column::DATETIME_FORMAT),
':until_normal' => \Minz\Time::ago(1, 'week')->format(Database\Column::DATETIME_FORMAT),
];

$sql = <<<SQL
SELECT 1
WHERE EXISTS (
SELECT l.id
FROM collections c, links_to_collections lc, followed_collections fc, links l
WHERE fc.user_id = :user_id
AND fc.collection_id = lc.collection_id
AND lc.link_id = l.id
AND lc.collection_id = c.id
AND (
(l.is_hidden = false AND c.is_public = true)
OR c.user_id = :user_id
OR EXISTS (
SELECT 1 FROM collection_shares cs
WHERE cs.user_id = :user_id
AND cs.collection_id = c.id
)
)
AND NOT EXISTS (
SELECT 1
FROM links l_exclude, collections c_exclude, links_to_collections lc_exclude
WHERE c_exclude.user_id = :user_id
AND l_exclude.user_id = :user_id
AND l_exclude.url_hash = l.url_hash
AND (
c_exclude.type = 'news'
OR c_exclude.type = 'bookmarks'
OR c_exclude.type = 'read'
OR c_exclude.type = 'never'
)
AND lc_exclude.link_id = l_exclude.id
AND lc_exclude.collection_id = c_exclude.id
)
AND l.user_id != :user_id
AND lc.created_at >= :until_hard_limit
AND (
(fc.time_filter = 'strict' AND lc.created_at >= :until_strict) OR
(fc.time_filter = 'normal' AND lc.created_at >= :until_normal) OR
(fc.time_filter = 'all' AND lc.created_at >= fc.created_at - INTERVAL '1 week')
)
)
SQL;

$database = Database::get();
$statement = $database->prepare($sql);
$statement->execute($values);

return $statement->fetch() !== false;
}

/**
* Mark the relevant links to be grouped by sources in the given collection.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/models/dao/links/NewsQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,58 @@ public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfLinkIs

$this->assertSame(0, count($links));
}

public function testAnyFromFollowedCollectionsCanReturnTrue(): void
{
$published_at = \Minz\Time::ago(1, 'day');
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
]);
$collection = CollectionFactory::create([
'user_id' => $this->other_user->id,
'type' => 'collection',
'is_public' => true,
]);
LinkToCollectionFactory::create([
'created_at' => $published_at,
'collection_id' => $collection->id,
'link_id' => $link->id,
]);
FollowedCollectionFactory::create([
'user_id' => $this->user->id,
'collection_id' => $collection->id,
]);

$result = models\Link::anyFromFollowedCollections($this->user->id);

$this->assertTrue($result);
}

public function testAnyFromFollowedCollectionsCanReturnFalse(): void
{
$published_at = \Minz\Time::ago(1, 'day');
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => true, // Note the link is hidden
]);
$collection = CollectionFactory::create([
'user_id' => $this->other_user->id,
'type' => 'collection',
'is_public' => true,
]);
LinkToCollectionFactory::create([
'created_at' => $published_at,
'collection_id' => $collection->id,
'link_id' => $link->id,
]);
FollowedCollectionFactory::create([
'user_id' => $this->user->id,
'collection_id' => $collection->id,
]);

$result = models\Link::anyFromFollowedCollections($this->user->id);

$this->assertFalse($result);
}
}

0 comments on commit 4101468

Please sign in to comment.