@@ -10,45 +10,56 @@ const logger = debug('app:NewsItem');
10
10
let newPostIdCounter = 100 ;
11
11
12
12
export class NewsItem {
13
+ /** ID of submitter */
13
14
public readonly id : number ;
15
+ /** Count of comments on the post */
14
16
public readonly commentCount : number ;
17
+ /** List of comments */
15
18
public readonly comments ;
16
- public readonly creationTime ;
17
- public readonly hides ;
18
- public readonly submitterId ;
19
- public readonly text : string ;
19
+ /** Post creation time, number of ms since 1970 */
20
+ public readonly creationTime : number ;
21
+ /** IDs of users who hid the post */
22
+ public readonly hides : string [ ] ;
23
+ public readonly hiddenCount : number ;
24
+ /** ID of user who submitted */
25
+ public readonly submitterId : string ;
26
+ /** Body text */
27
+ public readonly text : string | null ;
28
+ /** Post title */
20
29
public readonly title : string ;
21
- public upvoteCount ;
30
+ /** Number of upvotes */
31
+ public upvoteCount : number ;
22
32
public readonly upvotes ;
23
33
public readonly url ;
24
34
25
35
public readonly hidden ?: boolean ; // TODO: exists?
26
36
27
- constructor ( props ) {
28
- if ( ! props . id ) {
29
- throw new Error ( `Error instantiating News Item, id is required: ${ props . id } ` ) ;
37
+ constructor ( fields ) {
38
+ if ( ! fields . id ) {
39
+ throw new Error ( `Error instantiating News Item, id is required: ${ fields . id } ` ) ;
30
40
}
31
- if ( ! props . submitterId ) {
32
- throw new Error ( `Error instantiating News Item, submitterId is required: ${ props . id } ` ) ;
41
+ if ( ! fields . submitterId ) {
42
+ throw new Error ( `Error instantiating News Item, submitterId is required: ${ fields . id } ` ) ;
33
43
}
34
- if ( ! props . title ) {
35
- throw new Error ( `Error instantiating News Item, title is required: ${ props . id } ` ) ;
44
+ if ( ! fields . title ) {
45
+ throw new Error ( `Error instantiating News Item, title is required: ${ fields . id } ` ) ;
36
46
}
37
- if ( props . url && ! isValidUrl ( props . url ) ) {
38
- throw new Error ( `Error instantiating News Item ${ props . id } , invalid URL: ${ props . url } ` ) ;
47
+ if ( fields . url && ! isValidUrl ( fields . url ) ) {
48
+ throw new Error ( `Error instantiating News Item ${ fields . id } , invalid URL: ${ fields . url } ` ) ;
39
49
}
40
50
41
- this . id = props . id || ( newPostIdCounter += 1 ) ;
42
- this . commentCount = props . commentCount || 0 ;
43
- this . comments = props . comments || [ ] ;
44
- this . creationTime = props . creationTime || + new Date ( ) ;
45
- this . hides = props . hides || [ ] ;
46
- this . submitterId = props . submitterId ;
47
- this . text = props . text || null ;
48
- this . title = props . title ;
49
- this . upvoteCount = props . upvoteCount || 1 ;
50
- this . upvotes = props . upvotes || [ props . submitterId ] ;
51
- this . url = props . url ;
51
+ this . id = fields . id || ( newPostIdCounter += 1 ) ;
52
+ this . commentCount = fields . commentCount || 0 ;
53
+ this . comments = fields . comments || [ ] ;
54
+ this . creationTime = fields . creationTime || + new Date ( ) ;
55
+ this . hides = fields . hides || [ ] ;
56
+ this . hiddenCount = this . hides . length ;
57
+ this . submitterId = fields . submitterId ;
58
+ this . text = fields . text || null ;
59
+ this . title = fields . title ;
60
+ this . upvoteCount = fields . upvoteCount || 1 ;
61
+ this . upvotes = fields . upvotes || [ fields . submitterId ] ;
62
+ this . url = fields . url ;
52
63
}
53
64
54
65
static getNewsItem = id => cache . getNewsItem ( id ) || DB . getNewsItem ( id ) || HNDB . fetchNewsItem ( id ) ;
0 commit comments