5
5
*/
6
6
7
7
import Base from './Base' ;
8
- import { PERMISSION_CAN_COMMENT , PERMISSION_CAN_VIEW_ANNOTATIONS , ERROR_CODE_FETCH_ACTIVITY } from '../constants' ;
8
+ import {
9
+ ERROR_CODE_FETCH_ACTIVITY ,
10
+ FILE_ACTIVITY_TYPE_ANNOTATION ,
11
+ FILE_ACTIVITY_TYPE_COMMENT ,
12
+ PERMISSION_CAN_COMMENT ,
13
+ PERMISSION_CAN_VIEW_ANNOTATIONS ,
14
+ } from '../constants' ;
9
15
import type { BoxItemPermission } from '../common/types/core' ;
10
16
import type { ElementsXhrError } from '../common/types/api' ;
11
17
import type { FileActivity , FileActivityTypes } from '../common/types/feed' ;
12
18
13
19
// We only show the latest reply in the UI
14
20
const REPLY_LIMIT = 1 ;
15
21
16
- const getFileActivityQueryParams = ( fileID : string , activityTypes ?: FileActivityTypes [ ] = [ ] ) => {
22
+ const getFileActivityQueryParams = (
23
+ fileID : string ,
24
+ activityTypes ?: FileActivityTypes [ ] = [ ] ,
25
+ shouldShowReplies ?: boolean = false ,
26
+ ) => {
17
27
const baseEndpoint = `/file_activities?file_id=${ fileID } ` ;
18
28
const hasActivityTypes = ! ! activityTypes && ! ! activityTypes . length ;
19
- const enabledRepliesQueryParam = `&enable_replies=true&reply_limit=${ REPLY_LIMIT } ` ;
29
+ const enableReplies = shouldShowReplies ? 'true' : 'false' ;
30
+ const enabledRepliesQueryParam = `&enable_replies=${ enableReplies } &reply_limit=${ REPLY_LIMIT } ` ;
20
31
const activityTypeQueryParam = hasActivityTypes ? `&activity_types=${ activityTypes . join ( ) } ` : '' ;
21
32
22
33
return `${ baseEndpoint } ${ activityTypeQueryParam } ${ enabledRepliesQueryParam } ` ;
@@ -28,10 +39,11 @@ class FileActivities extends Base {
28
39
*
29
40
* @param {string } [id] - a box file id
30
41
* @param {Array<FileActivityTypes> } activityTypes - optional. Array of File Activity types to filter by, returns all Activity Types if omitted.
42
+ * @param {boolean } shouldShowReplies - optional. Specify if replies should be included in the response
31
43
* @return {string } base url for files
32
44
*/
33
- getFilteredUrl ( id : string , activityTypes ?: FileActivityTypes [ ] ) : string {
34
- return `${ this . getBaseApiUrl ( ) } ${ getFileActivityQueryParams ( id , activityTypes ) } ` ;
45
+ getFilteredUrl ( id : string , activityTypes ?: FileActivityTypes [ ] , shouldShowReplies ?: boolean ) : string {
46
+ return `${ this . getBaseApiUrl ( ) } ${ getFileActivityQueryParams ( id , activityTypes , shouldShowReplies ) } ` ;
35
47
}
36
48
37
49
/**
@@ -42,6 +54,7 @@ class FileActivities extends Base {
42
54
* @param {string } fileId - the file id
43
55
* @param {BoxItemPermission } permissions - the permissions for the file
44
56
* @param {number } repliesCount - number of replies to return, by default all replies are returned
57
+ * @param {boolean } shouldShowReplies - specify if replies should be included in the response
45
58
* @param {Function } successCallback - the success callback
46
59
* @returns {void }
47
60
*/
@@ -51,13 +64,15 @@ class FileActivities extends Base {
51
64
fileID,
52
65
permissions,
53
66
repliesCount,
67
+ shouldShowReplies,
54
68
successCallback,
55
69
} : {
56
70
activityTypes : FileActivityTypes [ ] ,
57
71
errorCallback : ( e : ElementsXhrError , code : string ) => void ,
58
72
fileID : string ,
59
73
permissions : BoxItemPermission ,
60
74
repliesCount ?: number ,
75
+ shouldShowReplies ?: boolean ,
61
76
successCallback : ( activity : FileActivity ) => void ,
62
77
} ) : void {
63
78
this. errorCode = ERROR_CODE_FETCH_ACTIVITY ;
@@ -66,8 +81,12 @@ class FileActivities extends Base {
66
81
throw new Error ( 'Missing file id!' ) ;
67
82
}
68
83
69
- this . checkApiCallValidity ( PERMISSION_CAN_COMMENT , permissions , fileID ) ;
70
- this . checkApiCallValidity ( PERMISSION_CAN_VIEW_ANNOTATIONS , permissions , fileID ) ;
84
+ if ( activityTypes . includes ( FILE_ACTIVITY_TYPE_COMMENT ) ) {
85
+ this . checkApiCallValidity ( PERMISSION_CAN_COMMENT , permissions , fileID ) ;
86
+ }
87
+ if ( activityTypes . includes ( FILE_ACTIVITY_TYPE_ANNOTATION ) ) {
88
+ this . checkApiCallValidity ( PERMISSION_CAN_VIEW_ANNOTATIONS , permissions , fileID ) ;
89
+ }
71
90
} catch ( e ) {
72
91
errorCallback ( e , this . errorCode ) ;
73
92
return ;
@@ -80,7 +99,7 @@ class FileActivities extends Base {
80
99
requestData : {
81
100
...( repliesCount ? { replies_count : repliesCount } : null ) ,
82
101
} ,
83
- url : this . getFilteredUrl ( fileID , activityTypes ) ,
102
+ url : this . getFilteredUrl ( fileID , activityTypes , shouldShowReplies ) ,
84
103
} ) ;
85
104
}
86
105
}
0 commit comments