Page MenuHomePhabricator

Create common serialization method for SuggestedEdits and ApiQueryGrowthTasks
Open, Needs TriagePublic

Description

We currently have TaskSet implementing JsonSerializable, and that is used in CacheDecorator when we cache a TaskSet for a user.

However when we ship JSON for use in client-side code, we end up using a custom format in both Suggested Edits and ApiQueryGrowthTasks:

ApiQueryGrowthTasks:

'@phan-var TaskSet $tasks';
		foreach ( $tasks as $i => $task ) {
			$title = Title::newFromLinkTarget( $task->getTitle() );
			$extraData = [
				'tasktype' => $task->getTaskType()->getId(),
				'difficulty' => $task->getTaskType()->getDifficulty(),
				'order' => $i,
				'qualityGateIds' => $task->getTaskType()->getQualityGateIds(),
				'qualityGateConfig' => $tasks->getQualityGateConfig(),
				'token' => $task->getToken()
			];
// snip

SuggestedEdits.php:

$data['task-preview'] = [
					'tasktype' => $task->getTaskType()->getId(),
					'difficulty' => $task->getTaskType()->getDifficulty(),
					'qualityGateIds' => $task->getTaskType()->getQualityGateIds(),
					'qualityGateConfig' => $tasks->getQualityGateConfig(),
					'title' => $title->getPrefixedText(),
					'topics' => $task->getTopicScores(),
					// The front-end code for constructing SuggestedEditCardWidget checks
					// to see if pageId is set in order to construct a tracking URL.
					'pageId' => $title->getArticleID(),
					'token' => $task->getToken()
				];

Ideally we could have a common method for outputting JSON that would be used for the client. I don't think we want the existing JSON serialization for TaskSet because that contains a lot more data than the client actually needs, so two possibilites are 1) a shared static method for converting a TaskSet into a light-weight client-side array, or 2) a ClientSideTaskSet that impelments JsonSerilazable in a way that discards unnecessary data and returns the existing structured used in ApiQueryGrowthTasks.

Event Timeline

Partly the difference is because the API framework will automatically add page ID / title. Also, order only makes sense for the API (where we can't control the order of results - although I think that was added before we started to randomize the order of search results so it's less relevant today). I'm not sure why the topic score is missing from the API (although TBH we should probably just get rid of it, I don't think we ever used it for analysis).

In general IMO code almost-duplication in the view layer is usually fine, just like you often have code for similar but not quite identical HTML pages, you have code for similar but not quite identical JSON data as well. If it forces code almost-duplication in the client side business logic, that's more problematic - the code for creating an mw.libs.ge.TaskData object from such JSON should ideally be centralized.