@@ -61,6 +61,9 @@ class Link
6161 #[Database \Column]
6262 public ?string $ image_filename = null ;
6363
64+ #[Database \Column]
65+ public string $ origin = '' ;
66+
6467 #[Database \Column]
6568 public string $ user_id ;
6669
@@ -85,10 +88,7 @@ class Link
8588 public array $ tags = [];
8689
8790 #[Database \Column(computed: true )]
88- public ?string $ source_news_type = null ;
89-
90- #[Database \Column(computed: true )]
91- public ?string $ source_news_resource_id = null ;
91+ public ?string $ initial_collection_id = null ;
9292
9393 #[Database \Column(computed: true )]
9494 public ?\DateTimeImmutable $ published_at = null ;
@@ -160,7 +160,7 @@ public static function copy(self $link, string $user_id): self
160160 $ link_copied ->fetched_code = $ link ->fetched_code ;
161161 $ link_copied ->fetched_count = $ link ->fetched_count ;
162162 $ link_copied ->fetched_retry_at = $ link ->fetched_retry_at ;
163- $ link_copied ->source_type = '' ;
163+ $ link_copied ->setOrigin ( '' ) ;
164164
165165 return $ link_copied ;
166166 }
@@ -321,51 +321,63 @@ public function numberNotes(): int
321321 }
322322 }
323323
324- public function sourceCollection (): ?Collection
324+ /**
325+ * Set the origin of the link.
326+ *
327+ * It is useful to keep the old source_type and source_resource_id columns
328+ * in sync even if they are not used anymore. This is to ease an eventual
329+ * rollback if the new system doesn't work or isn't efficient enough.
330+ */
331+ public function setOrigin (string $ origin ): void
325332 {
326- if (
327- $ this ->source_type !== 'collection ' ||
328- !$ this ->source_resource_id
329- ) {
330- return null ;
331- }
333+ $ this ->origin = $ origin ;
332334
333- return Collection:: find ( $ this ->source_resource_id ) ;
334- }
335+ $ this ->source_type = '' ;
336+ $ this -> source_resource_id = null ;
335337
336- public function sourceUser (): ?User
337- {
338- if (
339- $ this ->source_type !== 'user ' ||
340- !$ this ->source_resource_id
341- ) {
342- return null ;
343- }
338+ if ($ origin ) {
339+ list ($ source_type , $ source_resource_id ) = utils \SourceHelper::extractFromPath ($ origin );
344340
345- return User::find ($ this ->source_resource_id );
341+ if ($ source_type ) {
342+ $ this ->source_type = $ source_type ;
343+ $ this ->source_resource_id = $ source_resource_id ;
344+ }
345+ }
346346 }
347347
348- public function source (): User | Collection | null
348+ public function origin (): ? Origin
349349 {
350- if ($ this ->source_type == 'user ' ) {
351- return $ this ->sourceUser ();
352- } elseif ($ this ->source_type == 'collection ' ) {
353- return $ this ->sourceCollection ();
354- } else {
350+ if (!$ this ->origin ) {
355351 return null ;
356352 }
353+
354+ return new Origin ($ this ->origin );
357355 }
358356
359357 /**
360- * Set the source properties of the link if "from" is a supported internal path.
358+ * Return the (deprecated) source.
359+ *
360+ * @deprecated
361361 */
362- public function setSourceFrom ( string $ from ): void
362+ public function source ( ): ? string
363363 {
364- list ( $ source_type , $ source_resource_id ) = utils \SourceHelper:: extractFromPath ( $ from );
365- if ( $ source_type ) {
366- $ this -> source_type = $ source_type ;
367- $ this -> source_resource_id = $ source_resource_id ;
364+ $ origin = $ this -> origin ( );
365+
366+ if (! $ origin || ! $ origin -> model ) {
367+ return null ;
368368 }
369+
370+ $ source_type = match ($ origin ->model ::class) {
371+ User::class => 'user ' ,
372+ Collection::class => 'collection ' ,
373+ default => '' ,
374+ };
375+
376+ if (!$ source_type ) {
377+ return null ;
378+ }
379+
380+ return "{$ source_type }# {$ origin ->model ->id }" ;
369381 }
370382
371383 /**
@@ -530,9 +542,11 @@ public static function hashUrl(string $url): string
530542 */
531543 public function toJson (User $ context_user ): array
532544 {
545+ $ origin_model = $ this ->origin ();
533546 $ source = null ;
534- if ($ this ->source_type ) {
535- $ source = "{$ this ->source_type }# {$ this ->source_resource_id }" ;
547+
548+ if ($ context_user ->id === $ this ->user_id && $ origin_model ) {
549+ $ source = $ this ->source ();
536550 }
537551
538552 return [
@@ -543,7 +557,7 @@ public function toJson(User $context_user): array
543557 'is_hidden ' => $ this ->is_hidden ,
544558 'reading_time ' => $ this ->reading_time ,
545559 'tags ' => $ this ->tags ,
546- 'source ' => $ source ,
560+ 'source ' => $ source , // @deprecated Can be removed in version 3.0.0.
547561 'is_read ' => $ this ->isReadBy ($ context_user ),
548562 'is_read_later ' => $ this ->isInBookmarksOf ($ context_user ),
549563 'collections ' => array_column ($ this ->collections (), 'id ' ),
0 commit comments