Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
06209ef5
Unverified
Commit
06209ef5
authored
May 14, 2019
by
Robert Knight
Committed by
GitHub
May 14, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1122 from hypothesis/simplify-groups-load
Refactor groups service's `load` method
parents
962abf49
c913999a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
76 deletions
+82
-76
groups.js
src/sidebar/services/groups.js
+82
-76
No files found.
src/sidebar/services/groups.js
View file @
06209ef5
...
@@ -154,10 +154,12 @@ function groups(
...
@@ -154,10 +154,12 @@ function groups(
}
}
/**
/**
* Fetch the list of applicable groups from the API.
* Fetch groups from the API, load them into the store and set the focused
* group.
*
*
* The list of applicable groups depends on the current userid and the URI of
* The groups that are fetched depend on the current user, the URI of
* the attached frames.
* the current document, and whether any direct-links were followed (either
* to an annotation or group).
*
*
* @return {Promise<Group[]>}
* @return {Promise<Group[]>}
*/
*/
...
@@ -168,12 +170,19 @@ function groups(
...
@@ -168,12 +170,19 @@ function groups(
}
}
const
directLinkedGroupId
=
store
.
getState
().
directLinkedGroupId
;
const
directLinkedGroupId
=
store
.
getState
().
directLinkedGroupId
;
const
directLinkedAnnId
=
store
.
getState
().
directLinkedAnnotationId
;
const
directLinkedAnnId
=
store
.
getState
().
directLinkedAnnotationId
;
const
params
=
{
expand
:
[
'organization'
,
'scopes'
],
};
let
directLinkedAnnotationGroupId
=
null
;
let
directLinkedAnnotationGroupId
=
null
;
// Step 1: Get the URI of the active document, so we can fetch groups
// associated with that document.
return
uri
return
uri
.
then
(
uri
=>
{
.
then
(
uri
=>
{
const
params
=
{
// Step 2: Concurrently fetch the groups the user is a member of,
expand
:
[
'organization'
,
'scopes'
],
// the groups associated with the current document and the annotation
};
// or group that was direct-linked (if any).
if
(
authority
)
{
if
(
authority
)
{
params
.
authority
=
authority
;
params
.
authority
=
authority
;
}
}
...
@@ -192,26 +201,27 @@ function groups(
...
@@ -192,26 +201,27 @@ function groups(
auth
.
tokenGetter
(),
auth
.
tokenGetter
(),
];
];
// If there is a directLinkedAnnId, fetch the annotation to see if there needs
// If there is a direct-linked annotation, fetch the annotation to see
// to be a second api request to fetch its group since the group may not be in
// if there needs to be a second API request to fetch its group since
// the results returned by group.list, profile.groups, or the direct-linked group.
// the group may not be in the results returned by group.list,
let
selectedAnnApi
=
Promise
.
resolve
(
null
);
// profile.groups, or the direct-linked group.
let
directLinkedAnnApi
=
Promise
.
resolve
(
null
);
if
(
directLinkedAnnId
)
{
if
(
directLinkedAnnId
)
{
select
edAnnApi
=
api
.
annotation
directLink
edAnnApi
=
api
.
annotation
.
get
({
id
:
directLinkedAnnId
})
.
get
({
id
:
directLinkedAnnId
})
.
catch
(()
=>
{
.
catch
(()
=>
{
// If the annotation does not exist or the user doesn't have permission.
// If the annotation does not exist or the user doesn't have permission.
return
null
;
return
null
;
});
});
}
}
groupApiRequests
=
groupApiRequests
.
concat
(
select
edAnnApi
);
groupApiRequests
=
groupApiRequests
.
concat
(
directLink
edAnnApi
);
// If there is a direct
LinkedGroupId, add an api
request to get that
// If there is a direct
-linked group, add an API
request to get that
// particular group since it may not be in the results returned by
// particular group since it may not be in the results returned by
// group.list or profile.groups.
// group.list or profile.groups.
let
select
edGroupApi
=
Promise
.
resolve
(
null
);
let
directLink
edGroupApi
=
Promise
.
resolve
(
null
);
if
(
directLinkedGroupId
)
{
if
(
directLinkedGroupId
)
{
select
edGroupApi
=
fetchGroup
({
directLink
edGroupApi
=
fetchGroup
({
id
:
directLinkedGroupId
,
id
:
directLinkedGroupId
,
expand
:
params
.
expand
,
expand
:
params
.
expand
,
}).
then
(
group
=>
{
}).
then
(
group
=>
{
...
@@ -224,76 +234,72 @@ function groups(
...
@@ -224,76 +234,72 @@ function groups(
return
group
;
return
group
;
});
});
}
}
groupApiRequests
=
groupApiRequests
.
concat
(
selectedGroupApi
);
groupApiRequests
=
groupApiRequests
.
concat
(
directLinkedGroupApi
);
return
Promise
.
all
(
groupApiRequests
).
then
(
return
Promise
.
all
(
groupApiRequests
);
([
myGroups
,
featuredGroups
,
token
,
selectedAnn
,
selectedGroup
])
=>
{
})
// If there is a direct-linked group, add it to the featured groups list.
.
then
(
const
allFeaturedGroups
=
([
selectedGroup
!==
null
&&
myGroups
,
!
featuredGroups
.
some
(
g
=>
g
.
id
===
selectedGroup
.
id
)
featuredGroups
,
?
featuredGroups
.
concat
([
selectedGroup
])
token
,
:
featuredGroups
;
directLinkedAnn
,
directLinkedGroup
,
// If there's a selected annotation it may require an extra api call
])
=>
{
// to fetch its group.
// Step 3. Add the direct-linked group to the list of featured groups,
if
(
selectedAnn
)
{
// and if there was a direct-linked annotation, fetch its group if we
// Set the directLinkedAnnotationGroupId to be used later in
// don't already have it.
// the filterGroups method.
directLinkedAnnotationGroupId
=
selectedAnn
.
group
;
// If there is a direct-linked group, add it to the featured groups list.
let
allFeaturedGroups
=
const
selectedAnnGroup
=
myGroups
directLinkedGroup
!==
null
&&
.
concat
(
allFeaturedGroups
)
!
featuredGroups
.
some
(
g
=>
g
.
id
===
directLinkedGroup
.
id
)
.
some
(
g
=>
g
.
id
===
selectedAnn
.
group
);
?
featuredGroups
.
concat
([
directLinkedGroup
])
:
featuredGroups
;
// If the direct-linked annotation's group has not already been fetched,
// fetch it.
// If there's a direct-linked annotation it may require an extra API call
if
(
!
selectedAnnGroup
)
{
// to fetch its group.
return
fetchGroup
({
if
(
directLinkedAnn
)
{
id
:
selectedAnn
.
group
,
// Set the directLinkedAnnotationGroupId to be used later in
expand
:
params
.
expand
,
// the filterGroups method.
}).
then
(
directLinkedAnnGroup
=>
{
directLinkedAnnotationGroupId
=
directLinkedAnn
.
group
;
// If the directLinkedAnnotation's group fetch failed, return
// the list of groups without it.
const
directLinkedAnnGroup
=
myGroups
if
(
!
directLinkedAnnGroup
)
{
.
concat
(
allFeaturedGroups
)
return
[
.
some
(
g
=>
g
.
id
===
directLinkedAnn
.
group
);
combineGroups
(
myGroups
,
allFeaturedGroups
,
documentUri
),
token
,
// If the direct-linked annotation's group has not already been fetched,
];
// fetch it.
}
if
(
!
directLinkedAnnGroup
)
{
const
initialFeaturedGroups
=
allFeaturedGroups
;
// If the directLinkedAnnotation's group fetch was successful,
allFeaturedGroups
=
fetchGroup
({
// combine it with the other groups.
id
:
directLinkedAnn
.
group
,
return
[
expand
:
params
.
expand
,
combineGroups
(
}).
then
(
directLinkedAnnGroup
=>
{
myGroups
,
if
(
!
directLinkedAnnGroup
)
{
allFeaturedGroups
.
concat
(
directLinkedAnnGroup
),
return
initialFeaturedGroups
;
documentUri
}
),
return
initialFeaturedGroups
.
concat
(
directLinkedAnnGroup
);
token
,
});
];
});
}
}
}
// If there is no direct-linked annotation, return the list of groups without it.
return
[
combineGroups
(
myGroups
,
allFeaturedGroups
,
documentUri
),
token
,
];
}
}
);
return
Promise
.
all
([
myGroups
,
allFeaturedGroups
,
documentUri
,
token
]);
})
}
.
then
(([
groups
,
token
])
=>
{
)
.
then
(([
myGroups
,
featuredGroups
,
documentUri
,
token
])
=>
{
// Step 4. Combine all the groups into a single list and set additional
// metadata on them that will be used elsewhere in the app.
const
isLoggedIn
=
token
!==
null
;
const
isLoggedIn
=
token
!==
null
;
return
filterGroups
(
const
groups
=
filterGroups
(
groups
,
combineGroups
(
myGroups
,
featuredGroups
,
documentUri
)
,
isLoggedIn
,
isLoggedIn
,
directLinkedAnnotationGroupId
,
directLinkedAnnotationGroupId
,
directLinkedGroupId
directLinkedGroupId
);
);
})
.
then
(
groups
=>
{
injectOrganizations
(
groups
);
injectOrganizations
(
groups
);
// Step 5. Load the groups into the store and focus the appropriate
// group.
const
isFirstLoad
=
store
.
allGroups
().
length
===
0
;
const
isFirstLoad
=
store
.
allGroups
().
length
===
0
;
const
prevFocusedGroup
=
localStorage
.
getItem
(
STORAGE_KEY
);
const
prevFocusedGroup
=
localStorage
.
getItem
(
STORAGE_KEY
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment