Skip to content

Commit 825f4fd

Browse files
authored
Faster mark-private-orgs (refined-github#2789)
1 parent 01bfaef commit 825f4fd

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

source/features/mark-private-orgs.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import './mark-private-orgs.css';
2+
import cache from 'webext-storage-cache';
23
import select from 'select-dom';
34
import eyeClosedIcon from 'octicon/eye-closed.svg';
45
import {getUsername} from '../libs/utils';
56
import features from '../libs/features';
67
import * as api from '../libs/api';
78

9+
const getPublicOrganizations = cache.function(async (username: string): Promise<string[]> => {
10+
// API v4 seems to *require* `org:read` permission AND it includes private organizations as well, which defeats the purpose. There's no way to filter them.
11+
// GitHub's API explorer inexplicably only includes public organizations.
12+
const response = await api.v3(`users/${username}/orgs`);
13+
return response.map((organization: AnyObject) => organization.login);
14+
}, {
15+
cacheKey: ([username]) => __featureName__ + ':' + username,
16+
expiration: 10
17+
});
18+
819
async function init(): Promise<false | void> {
920
const orgs = select.all<HTMLAnchorElement>('.avatar-group-item[data-hovercard-type="organization"]');
1021
if (orgs.length === 0) {
1122
return false;
1223
}
1324

14-
const response = await api.v3(`users/${getUsername()}/orgs`);
15-
const publicOrgs: string[] = response.map((orgData: AnyObject) => `/${orgData.login as string}`);
16-
25+
const publicOrganizations = await getPublicOrganizations(getUsername());
1726
for (const org of orgs) {
18-
if (!publicOrgs.includes(org.pathname)) {
27+
if (!publicOrganizations.includes(org.pathname.slice(1))) {
1928
org.classList.add('rgh-private-org');
2029
org.append(eyeClosedIcon());
2130
}

0 commit comments

Comments
 (0)