Add 'legacyPrimary' links to API data for users without DT-enhanced HTML

The 'legacyPrimary' links will take you to the section
the comment is in and should be used when you don't have
access to comment IDs.

Bug: T296018
Change-Id: I944feb90e7c3a69f81366f42fa110c58cac26dbb
This commit is contained in:
Ed Sanders 2021-12-09 00:13:21 +00:00
parent 69ba8582a9
commit dd896deb45
4 changed files with 67 additions and 0 deletions

View file

@ -36,6 +36,11 @@ trait DiscussionToolsEventTrait {
*/
abstract protected function getBundledEvents();
/**
* @return int[]|false
*/
abstract protected function getBundledIds();
/**
* Get a link to the individual comment, if available.
*
@ -104,4 +109,31 @@ trait DiscussionToolsEventTrait {
return $this->language->truncateForVisual( $content, EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH );
}
/**
* Add mark-as-read params to a link array
*
* Taken from EchoEventPresentationModel::getPrimaryLinkWithMarkAsRead
* TODO: Upstream to Echo?
*
* @param array $link Link
* @return array
*/
protected function addMarkAsRead( $link ) {
global $wgEchoCrossWikiNotifications;
if ( $link ) {
$eventIds = [ $this->event->getId() ];
if ( $this->getBundledIds() ) {
$eventIds = array_merge( $eventIds, $this->getBundledIds() );
}
$queryParams = [ 'markasread' => implode( '|', $eventIds ) ];
if ( $wgEchoCrossWikiNotifications ) {
$queryParams['markasreadwiki'] = wfWikiID();
}
$link['url'] = wfAppendQuery( $link['url'], $queryParams );
}
return $link;
}
}

View file

@ -44,4 +44,15 @@ class EnhancedEchoEditUserTalkPresentationModel extends EchoEditUserTalkPresenta
}
return parent::getBodyMessage();
}
/**
* @inheritDoc
*/
public function jsonSerialize() {
$array = parent::jsonSerialize();
$array['links']['legacyPrimary'] = $this->addMarkAsRead( parent::getPrimaryLink() ) ?: [];
return $array;
}
}

View file

@ -27,4 +27,15 @@ class EnhancedEchoMentionPresentationModel extends EchoMentionPresentationModel
}
return $linkInfo;
}
/**
* @inheritDoc
*/
public function jsonSerialize() {
$array = parent::jsonSerialize();
$array['links']['legacyPrimary'] = $this->addMarkAsRead( parent::getPrimaryLink() ) ?: [];
return $array;
}
}

View file

@ -59,6 +59,19 @@ class SubscribedNewCommentPresentationModel extends EchoEventPresentationModel {
];
}
/**
* @inheritDoc
*/
public function jsonSerialize() {
$array = parent::jsonSerialize();
$legacyPrimaryLink = $this->getPrimaryLink();
$legacyPrimaryLink['url'] = $this->section->getTitleWithSection()->getFullURL();
$array['links']['legacyPrimary'] = $this->addMarkAsRead( $legacyPrimaryLink ) ?: [];
return $array;
}
/**
* @inheritDoc
*/