-
Notifications
You must be signed in to change notification settings - Fork 56
/
comment.ts
75 lines (66 loc) · 1.98 KB
/
comment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { association, Factory, faker, trait, Trait } from 'ember-cli-mirage';
import Comment from 'ember-osf-web/models/comment';
import { guid } from './utils';
export interface CommentTraits {
withReplies: Trait;
asAbuse: Trait;
}
export default Factory.extend<Comment & CommentTraits>({
id: guid('comment'),
node: association() as Comment['node'],
user: association() as Comment['user'],
afterCreate(comment) {
if (!comment.targetID && !comment.targetType) {
comment.update({
targetID: comment.node.id,
targetType: comment.node.nodeType,
});
}
},
page: 'node',
content() {
return faker.lorem.sentences(faker.random.number({ min: 1, max: 4 }));
},
dateCreated() {
return faker.date.past(3);
},
dateModified() {
return faker.date.past(3);
},
modified: false,
deleted: false,
canEdit: true,
hasReport: false,
isAbuse: false,
isHam: false,
targetID: '',
targetType: '',
hasChildren: false,
withReplies: trait<Comment>({
afterCreate(comment, server) {
const siblings = server.schema.comments.where({ targetID: comment.targetID });
const count = faker.random.number({ min: 0, max: siblings.length - 1 });
server.createList(
'comment',
count,
{ node: comment.node, targetID: comment.id, targetType: 'comments' },
'withReplies',
);
if (count) {
comment.update({
hasChildren: true,
});
}
},
}),
asAbuse: trait<Comment>({
afterCreate(comment, server) {
server.create('comment-report', { comment });
},
}),
});
declare module 'ember-cli-mirage/types/registries/schema' {
export default interface MirageSchemaRegistry {
comments: Comment;
} // eslint-disable-line semi
}