Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp: Improve performance when checking if news are available #712

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 2 additions & 7 deletions src/controllers/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Minz\Response;
use App\auth;
use App\models;
use App\services;
use App\utils;

/**
Expand Down Expand Up @@ -78,8 +77,7 @@ public function create(Request $request): Response
]);
}

$news_picker = new services\NewsPicker($user);
$links = $news_picker->pick(max: 50);
$links = models\Link::listFromFollowedCollections($user->id, max:50);

$news = $user->news();

Expand Down Expand Up @@ -123,11 +121,8 @@ public function showAvailable(Request $request): Response
]);
}

$news_picker = new services\NewsPicker($user);
$links = $news_picker->pick(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
32 changes: 0 additions & 32 deletions src/services/NewsPicker.php

This file was deleted.

Loading
Loading