fix comment sorter

background: we have a flat hierarchy of comments, not a tree. therefore we
can also remove again the unnecessary additions.

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
pull/6982/head
Arthur Schiwon 7 years ago
parent 61db8615f4
commit d5f1cef642
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
  1. 2
      apps/comments/js/commentstabview.js
  2. 15
      apps/comments/lib/Collaboration/CommentersSorter.php
  3. 23
      apps/comments/tests/Unit/Collaboration/CommentersSorterTest.php
  4. 76
      lib/private/Comments/Manager.php
  5. 22
      lib/public/Comments/ICommentsManager.php
  6. 31
      tests/lib/Comments/ManagerTest.php

@ -238,7 +238,7 @@
search: query,
itemType: 'files',
itemId: s.model.get('id'),
sorter: 'comments|share-recipients',
sorter: 'commenters|share-recipients',
limit: OC.appConfig.comments.maxAutoCompleteResults
},
function (data) {

@ -84,12 +84,23 @@ class CommentersSorter implements ISorter {
* @return array
*/
protected function retrieveCommentsInformation($type, $id) {
$comments = $this->commentsManager->getForObject($type, $id, 1);
$comments = $this->commentsManager->getForObject($type, $id);
if(count($comments) === 0) {
return [];
}
return $this->commentsManager->getActorsInTree($comments[0]->getTopmostParentId());
$actors = [];
foreach ($comments as $comment) {
if(!isset($actors[$comment->getActorType()])) {
$actors[$comment->getActorType()] = [];
}
if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
$actors[$comment->getActorType()][$comment->getActorId()] = 1;
} else {
$actors[$comment->getActorType()][$comment->getActorId()]++;
}
}
return $actors;
}
protected function compare(array $a, array $b, array $commenters) {

@ -27,6 +27,7 @@ namespace OCA\Comments\Tests\Unit\Collaboration;
use OCA\Comments\Collaboration\CommentersSorter;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\IConfig;
use Test\TestCase;
class CommentersSorterTest extends TestCase {
@ -48,13 +49,25 @@ class CommentersSorterTest extends TestCase {
* @param $data
*/
public function testSort($data) {
$this->commentsManager->expects($this->once())
->method('getForObject')
->willReturn([$this->createMock(IComment::class)]);
$commentMocks = [];
foreach($data['actors'] as $actorType => $actors) {
foreach ($actors as $actorId => $noOfComments) {
for($i=0;$i<$noOfComments;$i++) {
$mock = $this->createMock(IComment::class);
$mock->expects($this->atLeastOnce())
->method('getActorType')
->willReturn($actorType);
$mock->expects($this->atLeastOnce())
->method('getActorId')
->willReturn($actorId);
$commentMocks[] = $mock;
}
}
}
$this->commentsManager->expects($this->once())
->method('getActorsInTree')
->willReturn($data['actors']);
->method('getForObject')
->willReturn($commentMocks);
$workArray = $data['input'];
$this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => '24']);

@ -317,82 +317,6 @@ class Manager implements ICommentsManager {
return $tree;
}
/**
* Get the actor types and ID that commented in the tree specified by the ID
*
* @param string $id
* @return array
* @since 13.0.0
*
* The return array looks like this:
*
* [
* 'user' => [
* 'alice' => 2,
* 'bob' => 3
* ],
* 'robot' => [
* 'r2-d2' => 5,
* 'c-3po' => 17,
* ]
* ]
*/
public function getActorsInTree($id) {
$tree = $this->getTree($id);
$actors = [];
$this->walkTree($tree, $actors, [$this, 'extractActor']);
return $actors;
}
/**
* @param IComment $comment
* @param array &$actors
*
* build an array that looks like:
*
* [
* 'user' => [
* 'alice' => 2,
* 'bob' => 3
* ],
* 'robot' => [
* 'r2-d2' => 5,
* 'c-3po' => 17,
* ]
* ]
*
*/
protected function extractActor(IComment $comment, &$actors) {
if(!isset($actors[$comment->getActorType()])) {
$actors[$comment->getActorType()] = [];
}
if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
$actors[$comment->getActorType()][$comment->getActorId()] = 1;
} else {
$actors[$comment->getActorType()][$comment->getActorId()] += 1;
}
}
/**
* walks through a comment tree (as returned by getTree() and invokes a callback
* with the current IComment instance (and optionally custom parameters)
*
* @param array $node
* @param array &$results
* @param callable $callback
* @param array|null $parameters
*/
protected function walkTree($node, array &$results, callable $callback, array $parameters = null) {
if(isset($node['replies'])) {
foreach ($node['replies'] as $subNode) {
$this->walkTree($subNode, $results, $callback, $parameters);
}
}
if(isset($node['comment']) && $node['comment'] instanceof IComment) {
$callback($node['comment'], $results, $parameters);
}
}
/**
* returns comments for a specific object (e.g. a file).
*

@ -137,28 +137,6 @@ interface ICommentsManager {
*/
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user);
/**
* Get the actor types and ID that commented in the tree specified by the ID
*
* @param string $id
* @return array
* @since 13.0.0
*
* The return array looks like this:
*
* [
* 'users' => [
* 'alice',
* 'bob',
* ],
* 'robots' => [
* 'r2-d2',
* 'c-3po',
* ]
* ]
*/
public function getActorsInTree($id);
/**
* creates a new comment and returns it. At this point of time, it is not
* saved in the used data storage. Use save() after setting other fields

@ -253,37 +253,6 @@ class ManagerTest extends TestCase {
} while (count($comments) > 0);
}
public function testGetActorsInTree() {
$manager = $this->getManager();
$headId = $this->addDatabaseEntry(0, 0);
$id = $this->addDatabaseEntry($headId, $headId, new \DateTime('-3 hours'));
$comment = $manager->get($id)->setActor('users', 'bob');
$manager->save($comment);
$this->addDatabaseEntry($headId, $headId, new \DateTime('-2 hours'));
$this->addDatabaseEntry($headId, $headId, new \DateTime('-2 hours'));
$id = $this->addDatabaseEntry($headId, $headId, new \DateTime('-1 hour'));
$comment = $manager->get($id)->setActor('users', 'bob');
$manager->save($comment);
$id = $this->addDatabaseEntry($headId, $headId, new \DateTime('-4 hour'));
$comment = $manager->get($id)->setActor('users', 'cynthia');
$manager->save($comment);
$actors = $manager->getActorsInTree($headId);
$this->assertTrue(isset($actors['users']));
$this->assertCount(3, $actors['users']);
$this->assertTrue(isset($actors['users']['alice']));
$this->assertTrue(isset($actors['users']['bob']));
$this->assertTrue(isset($actors['users']['cynthia']));
$this->assertSame(3, $actors['users']['alice']);
$this->assertSame(2, $actors['users']['bob']);
$this->assertSame(1, $actors['users']['cynthia']);
}
public function testGetForObjectWithDateTimeConstraint() {
$this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
$this->addDatabaseEntry(0, 0, new \DateTime('-5 hours'));

Loading…
Cancel
Save