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

dev: Rename the Link "via" fields to "source" #662

Merged
merged 1 commit into from
Apr 1, 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
2 changes: 1 addition & 1 deletion src/assets/stylesheets/custom/news.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
margin-bottom: var(--space-small);
}

.news__via {
.news__source {
font-style: italic;
}

Expand Down
10 changes: 5 additions & 5 deletions src/controllers/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public function create(Request $request): Response
foreach ($links as $news_link) {
$link = $user->obtainLink($news_link);

// If the link has already a via info, we want to keep it (it might
// have been get via a followed collection, and put in the
// If the link has already a source info, we want to keep it (it
// might have been get via a followed collection, and put in the
// bookmarks then)
if (!$link->via_type && $news_link->via_news_type !== null) {
$link->via_type = $news_link->via_news_type;
$link->via_resource_id = $news_link->via_news_resource_id;
if (!$link->source_type && $news_link->source_news_type !== null) {
$link->source_type = $news_link->source_news_type;
$link->source_resource_id = $news_link->source_news_resource_id;
}

$link->save();
Expand Down
16 changes: 8 additions & 8 deletions src/controllers/collections/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public function create(Request $request): Response
$options['hidden'] = $collection->sharedWith($user);
$collection_links = $collection->links(options: $options);
$links = $user->obtainLinks($collection_links);
list($via_type, $via_resource_id) = utils\ViaHelper::extractFromPath($from);
list($source_type, $source_resource_id) = utils\SourceHelper::extractFromPath($from);

$links_to_create = [];
foreach ($links as $link) {
if (!$link->isPersisted()) {
$link->created_at = \Minz\Time::now();
if ($via_type) {
$link->via_type = $via_type;
$link->via_resource_id = $via_resource_id;
if ($source_type) {
$link->source_type = $source_type;
$link->source_resource_id = $source_resource_id;
}
$links_to_create[] = $link;
}
Expand Down Expand Up @@ -132,15 +132,15 @@ public function later(Request $request): Response
$collection_links = $collection->links(options: $options);
$links = $user->obtainLinks($collection_links);

list($via_type, $via_resource_id) = utils\ViaHelper::extractFromPath($from);
list($source_type, $source_resource_id) = utils\SourceHelper::extractFromPath($from);

$links_to_create = [];
foreach ($links as $link) {
if (!$link->isPersisted()) {
$link->created_at = \Minz\Time::now();
if ($via_type) {
$link->via_type = $via_type;
$link->via_resource_id = $via_resource_id;
if ($source_type) {
$link->source_type = $source_type;
$link->source_resource_id = $source_resource_id;
}
$links_to_create[] = $link;
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/links/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function update(Request $request): Response

if (!auth\LinksAccess::canUpdate($user, $link)) {
$link = $user->obtainLink($link);
utils\ViaHelper::setLinkVia($link, $from);
utils\SourceHelper::setLinkSource($link, $from);
}

$link->is_hidden = $is_hidden;
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/links/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function create(Request $request): Response

$link = $user->obtainLink($link);
if (!$link->isPersisted()) {
utils\ViaHelper::setLinkVia($link, $from);
utils\SourceHelper::setLinkSource($link, $from);
$link->save();
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public function later(Request $request): Response

$link = $user->obtainLink($link);
if (!$link->isPersisted()) {
utils\ViaHelper::setLinkVia($link, $from);
utils\SourceHelper::setLinkSource($link, $from);
$link->save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function migrate(): bool
}

// Update the new "via_*" info of the link
$link->via_type = $db_news_link['via_type'];
$resource_id = $db_news_link['link_id'];
if (!$resource_id) {
$resource_id = $db_news_link['via_collection_id'];
}
$link->via_resource_id = $resource_id;
// $link->via_type = $db_news_link['via_type'];
// $resource_id = $db_news_link['link_id'];
// if (!$resource_id) {
// $resource_id = $db_news_link['via_collection_id'];
// }
// $link->via_resource_id = $resource_id;
$link->save();

// And attach the link to the corresponding collection:
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/Migration202404010001RenameLinksViaToSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace flusio\migrations;

class Migration202404010001RenameLinksViaToSource
{
public function migrate(): bool
{
$database = \Minz\Database::get();

$database->exec(<<<'SQL'
ALTER TABLE links RENAME COLUMN via_type TO source_type;
ALTER TABLE links RENAME COLUMN via_resource_id TO source_resource_id;
SQL);

return true;
}

public function rollback(): bool
{
$database = \Minz\Database::get();

$database->exec(<<<'SQL'
ALTER TABLE links RENAME COLUMN source_type TO via_type;
ALTER TABLE links RENAME COLUMN source_resource_id TO via_resource_id;
SQL);

return true;
}
}
29 changes: 13 additions & 16 deletions src/models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ class Link
public ?string $feed_entry_id = null;

#[Database\Column]
public string $via_type = '';
public string $source_type = '';

#[Database\Column]
public ?string $via_resource_id = null;
public ?string $source_resource_id = null;

#[Database\Column(computed: true)]
public ?string $via_news_type = null;
public ?string $source_news_type = null;

#[Database\Column(computed: true)]
public ?string $via_news_resource_id = null;
public ?string $source_news_resource_id = null;

#[Database\Column(computed: true)]
public ?\DateTimeImmutable $published_at = null;
Expand Down Expand Up @@ -126,7 +126,7 @@ public static function copy(self $link, string $user_id): self
$link_copied->fetched_at = $link->fetched_at;
$link_copied->fetched_code = $link->fetched_code;
$link_copied->fetched_count = $link->fetched_count;
$link_copied->via_type = '';
$link_copied->source_type = '';

return $link_copied;
}
Expand Down Expand Up @@ -165,31 +165,28 @@ public function messages(): array
return Message::listByLink($this->id);
}

public function viaCollection(): ?Collection
public function sourceCollection(): ?Collection
{
if (
$this->via_type !== 'collection' ||
!$this->via_resource_id
$this->source_type !== 'collection' ||
!$this->source_resource_id
) {
return null;
}

return Collection::find($this->via_resource_id);
return Collection::find($this->source_resource_id);
}

/**
* @return \flusio\models\User|null
*/
public function viaUser(): ?User
public function sourceUser(): ?User
{
if (
$this->via_type !== 'user' ||
!$this->via_resource_id
$this->source_type !== 'user' ||
!$this->source_resource_id
) {
return null;
}

return User::find($this->via_resource_id);
return User::find($this->source_resource_id);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/models/dao/links/NewsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function listFromBookmarksForNews(
}

$sql = <<<SQL
SELECT l.*, lc.created_at AS published_at, 'bookmarks' AS via_news_type
SELECT l.*, lc.created_at AS published_at, 'bookmarks' AS source_news_type
FROM links l, collections c, links_to_collections lc

WHERE lc.link_id = l.id
Expand Down Expand Up @@ -117,7 +117,7 @@ public static function listFromFollowedCollectionsForNews(
AND lc_exclude.collection_id = c_exclude.id
)

SELECT l.*, lc.created_at AS published_at, 'collection' AS via_news_type, c.id AS via_news_resource_id
SELECT l.*, lc.created_at AS published_at, 'collection' AS source_news_type, c.id AS source_news_resource_id
FROM collections c, links_to_collections lc, followed_collections fc, links l

LEFT JOIN excluded_links
Expand Down
4 changes: 2 additions & 2 deletions src/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ CREATE TABLE links (

feed_entry_id TEXT,

via_type TEXT NOT NULL DEFAULT '',
via_resource_id TEXT,
source_type TEXT NOT NULL DEFAULT '',
source_resource_id TEXT,

search_index TSVECTOR GENERATED ALWAYS AS (to_tsvector('french', title || ' ' || url)) STORED,
url_lookup TEXT GENERATED ALWAYS AS (simplify_url(url)) STORED,
Expand Down
16 changes: 8 additions & 8 deletions src/utils/ViaHelper.php → src/utils/SourceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class ViaHelper
class SourceHelper
{
/**
* Set the via_* properties of a link if possible.
* Set the source_* properties of a link if possible.
*/
public static function setLinkVia(models\Link $link, string $from): void
public static function setLinkSource(models\Link $link, string $from): void
{
list($via_type, $via_resource_id) = self::extractFromPath($from);
if ($via_type) {
$link->via_type = $via_type;
$link->via_resource_id = $via_resource_id;
list($source_type, $source_resource_id) = self::extractFromPath($from);
if ($source_type) {
$link->source_type = $source_type;
$link->source_resource_id = $source_resource_id;
}
}

/**
* Return the via type and resource id from a path.
* Return the source type and resource id from a path.
*
* For instance:
*
Expand Down
2 changes: 1 addition & 1 deletion src/views/bookmarks/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<?= $this->include('links/_link.phtml', [
'link' => $link,
'from' => \Minz\Url::for('bookmarks', $current_url_params),
'display_via' => $link->via_type !== 'bookmarks',
'display_source' => $link->source_type !== 'bookmarks',
'display_comments' => true,
'display_edit' => true,
'display_repair' => true,
Expand Down
2 changes: 1 addition & 1 deletion src/views/collections/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
<?= $this->include('links/_link.phtml', [
'link' => $link,
'from' => \Minz\Url::for('collection', $current_url_params),
'display_via' => $link->via_type !== 'bookmarks',
'display_source' => $link->source_type !== 'bookmarks',
'display_comments' => true,
'display_hidden' => $collection->is_public && $link->is_hidden,
'display_edit' => $link->user_id === $current_user->id,
Expand Down
36 changes: 18 additions & 18 deletions src/views/links/_link.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
$display_mark_as_read = $display_mark_as_read ?? false;
$display_mark_as_unread = $display_mark_as_unread ?? false;
$display_never = $display_never ?? false;
$display_via = $display_via ?? false;
if ($display_via && !$link->via_type) {
$display_via = false;
$display_source = $display_source ?? false;
if ($display_source && !$link->source_type) {
$display_source = false;
}
$storing_must_mark_as_read = $storing_must_mark_as_read ?? false;

$via_collection = $display_via ? $link->viaCollection() : null;
$via_user = $display_via ? $link->viaUser() : null;
$source_collection = $display_source ? $link->sourceCollection() : null;
$source_user = $display_source ? $link->sourceUser() : null;

$is_current_user_owner = $current_user && $current_user->id === $link->user_id;
$in_error = $link->inError();
Expand Down Expand Up @@ -151,32 +151,32 @@
<?php endif; ?>
</p>

<?php if ($display_via): ?>
<p class="link__text news__via">
<?php if ($link->via_type === 'bookmarks'): ?>
<?php if ($display_source): ?>
<p class="link__text news__source">
<?php if ($link->source_type === 'bookmarks'): ?>
<?= _f('via your <strong><a class="anchor--hidden" href="%s">bookmarks</a></strong>', url('bookmarks')) ?>
<?php elseif ($via_collection): ?>
<?php if ($via_collection->type === 'collection'): ?>
<?php $owner = $via_collection->owner(); ?>
<?php elseif ($source_collection): ?>
<?php if ($source_collection->type === 'collection'): ?>
<?php $owner = $source_collection->owner(); ?>
<?= _f(
'via <strong><a class="anchor--hidden" href="%s">%s</a></strong> by <a class="anchor--hidden" href="%s">%s</a>',
url('collection', ['id' => $via_collection->id]),
protect($via_collection->name()),
url('collection', ['id' => $source_collection->id]),
protect($source_collection->name()),
url('profile', ['id' => $owner->id]),
protect($owner->username)
) ?>
<?php else: ?>
<?= _f(
'via <strong><a class="anchor--hidden" href="%s">%s</a></strong>',
url('collection', ['id' => $via_collection->id]),
protect($via_collection->name())
url('collection', ['id' => $source_collection->id]),
protect($source_collection->name())
) ?>
<?php endif; ?>
<?php elseif ($via_user): ?>
<?php elseif ($source_user): ?>
<?= _f(
'via <strong><a class="anchor--hidden" href="%s">%s</a></strong>',
url('profile', ['id' => $via_user->id]),
protect($via_user->username)
url('profile', ['id' => $source_user->id]),
protect($source_user->username)
) ?>
<?php endif; ?>
</p>
Expand Down
2 changes: 1 addition & 1 deletion src/views/links/search.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<?= $this->include('links/_link.phtml', [
'link' => $link,
'from' => \Minz\Url::for('links', $current_url_params),
'display_via' => true,
'display_source' => true,
'display_comments' => true,
'display_hidden' => $link->is_hidden,
'display_edit' => true,
Expand Down
4 changes: 2 additions & 2 deletions src/views/links/searches/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@

<p class="card__text card__text--oneline">
<?php
$via = _f('Feed %s', protect($feed->feedWebsite()));
$source = _f('Feed %s', protect($feed->feedWebsite()));

if ($feed->number_links === 0):
$number_links = _('no links');
Expand All @@ -107,7 +107,7 @@
endif;
?>

<span class="card__ellipsis"><?= $via ?></span>&nbsp;·&nbsp;<?= $number_links ?>
<span class="card__ellipsis"><?= $source ?></span>&nbsp;·&nbsp;<?= $number_links ?>
</p>
</a>

Expand Down
2 changes: 1 addition & 1 deletion src/views/news/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
'from' => \Minz\Url::for('news'),
'display_edit' => true,
'display_repair' => true,
'display_via' => true,
'display_source' => true,
'display_read_later' => true,
'display_mark_as_read' => true,
'display_never' => true,
Expand Down
Loading
Loading