Skip to content

Commit 05ec24e

Browse files
committed
Use annotation service in annotations seeder
Summary: Makes the seeder dependent on the Annotation service actually working, but the consolidation is probably worth it. Test Plan: - `make db-reset` should give you functioning comments/notes on documents. Reviewers: sethetter Reviewed By: sethetter Differential Revision: https://phabricator.opengovfoundation.org/D116
1 parent 4eb19d2 commit 05ec24e

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

database/seeds/AnnotationsTableSeeder.php

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function run()
1515
$adminUser = User::find(2);
1616
$users = collect([$regularUser, $adminUser]);
1717
$docs = Document::all();
18+
$annotationService = App::make('App\Services\Annotations');
1819

1920
foreach ($docs as $doc) {
2021
// All the comments
@@ -27,20 +28,19 @@ public function run()
2728
continue;
2829
}
2930

30-
$comments = factory(Annotation::class, $numComments)->create([
31-
'user_id' => $users->random()->id,
32-
])->each(function ($ann) use ($doc, $users) {
33-
$doc->annotations()->save($ann);
34-
$doc->allAnnotations()->save($ann);
35-
$comment = factory(AnnotationTypes\Comment::class)->create();
36-
$comment->annotation()->save($ann);
37-
$ann->user()->associate($users->random());
31+
$fakeComments = factory(AnnotationTypes\Comment::class, $numComments)->make();
32+
$comments = $fakeComments->map(function ($fakeComment) use ($doc, $users, $annotationService) {
33+
$commentData = [
34+
'text' => $fakeComment->content,
35+
];
36+
return $annotationService
37+
->createAnnotationComment($doc, $users->random(), $commentData);
3838
});
3939

4040
// Make some of them notes
4141
$numNotes = round($comments->count() * max(0, min(1, config('madison.seeder.comments_percentage_notes'))));
4242
if ($numNotes) {
43-
$notes = $comments->random($numNotes)->each(function ($comment) use ($doc) {
43+
$notes = $comments->random($numNotes)->each(function ($comment) use ($doc, $annotationService) {
4444
$content = $doc->content()->first()->content;
4545
$contentLines = preg_split('/\\n\\n/', $content);
4646
$paragraphNumber = rand(1, count($contentLines));
@@ -49,48 +49,42 @@ public function run()
4949
$endOffset = rand($startOffset, $endParagraphOffset);
5050

5151
// create range annotation
52-
$annotation = factory(Annotation::class)->create([
53-
'user_id' => $comment->user->id,
54-
]);
55-
$range = factory(AnnotationTypes\Range::class)->create([
52+
$rangeData = [
5653
'start' => '/p['.$paragraphNumber.']',
5754
'end' => '/p['.$paragraphNumber.']',
58-
'start_offset' => $startOffset,
59-
'end_offset' => $endOffset,
60-
]);
61-
$range->annotation()->save($annotation);
55+
'startOffset' => $startOffset,
56+
'endOffset' => $endOffset,
57+
];
58+
$annotationService
59+
->createAnnotationRange($comment, $comment->user, $rangeData);
6260

63-
// mark comment with range
64-
$comment->annotations()->save($annotation);
65-
$doc->allAnnotations()->save($annotation);
66-
$comment->annotation_subtype = 'note';
61+
$comment->annotation_subtype = Annotation::SUBTYPE_NOTE;
6762
$comment->save();
6863
});
6964
}
7065

7166
// Reply to some of them
7267
$numReplied = round($comments->count() * max(0, min(1, config('madison.seeder.comments_percentage_replied'))));
7368
if ($numReplied) {
74-
$replies = $comments->random($numReplied)->each(function ($comment) use ($doc, $users) {
69+
$replies = $comments->random($numReplied)->each(function ($comment) use ($users, $annotationService) {
7570
$numReplies = rand(
7671
config('madison.seeder.num_replies_per_comment_min'),
7772
config('madison.seeder.num_replies_per_comment_max')
7873
);
7974

8075
if ($numReplies) {
81-
$replies = factory(Annotation::class, $numReplies)->create([
82-
'user_id' => $users->random()->id,
83-
])->each(function ($annotation) use ($doc, $comment) {
84-
$reply = factory(AnnotationTypes\Comment::class)->create();
85-
$reply->annotation()->save($annotation);
76+
$fakeComments = factory(AnnotationTypes\Comment::class, $numReplies)->make();
77+
$replies = $fakeComments->map(function ($fakeComment) use ($users, $annotationService, $comment) {
78+
$commentData = [
79+
'text' => $fakeComment->content,
80+
];
8681

87-
// mark comment with reply
88-
$comment->annotations()->save($annotation);
89-
$doc->allAnnotations()->save($annotation);
9082
if ($comment->isNote()) {
91-
$comment->annotation_subtype = 'note';
83+
$commentData['subtype'] = Annotation::SUBTYPE_NOTE;
9284
}
93-
$comment->save();
85+
86+
return $annotationService
87+
->createAnnotationComment($comment, $users->random(), $commentData);
9488
});
9589
}
9690
});

0 commit comments

Comments
 (0)