This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 394
/
user-store.js
262 lines (218 loc) · 6.62 KB
/
user-store.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import yubikiri from 'yubikiri';
import {Emitter, CompositeDisposable} from 'event-kit';
import RelayNetworkLayerManager from '../relay-network-layer-manager';
import Author, {nullAuthor} from './author';
import {UNAUTHENTICATED, INSUFFICIENT} from '../shared/keytar-strategy';
import ModelObserver from './model-observer';
// This is a guess about what a reasonable value is. Can adjust if performance is poor.
const MAX_COMMITS = 5000;
export const source = {
PENDING: Symbol('pending'),
GITLOG: Symbol('git log'),
GITHUBAPI: Symbol('github API'),
};
class GraphQLCache {
// One hour
static MAX_AGE_MS = 3.6e6
constructor() {
this.bySlug = new Map();
}
get(remote) {
const slug = remote.getSlug();
const {ts, data} = this.bySlug.get(slug) || {
ts: -Infinity,
data: {},
};
if (Date.now() - ts > this.constructor.MAX_AGE_MS) {
this.bySlug.delete(slug);
return null;
}
return data;
}
set(remote, data) {
this.bySlug.set(remote.getSlug(), {ts: Date.now(), data});
}
}
export default class UserStore {
constructor({repository, login, config}) {
this.emitter = new Emitter();
this.subs = new CompositeDisposable();
// TODO: [ku 3/2018] Consider using Dexie (indexDB wrapper) like Desktop and persist users across sessions
this.allUsers = new Map();
this.excludedUsers = new Set();
this.users = [];
this.committer = nullAuthor;
this.last = {
source: source.PENDING,
repository: null,
excludedUsers: this.excludedUsers,
};
this.cache = new GraphQLCache();
this.repositoryObserver = new ModelObserver({
fetchData: r => yubikiri({
committer: r.getCommitter(),
authors: r.getAuthors({max: MAX_COMMITS}),
remotes: r.getRemotes(),
}),
didUpdate: () => this.loadUsers(),
});
this.repositoryObserver.setActiveModel(repository);
this.loginObserver = new ModelObserver({
didUpdate: () => this.loadUsers(),
});
this.loginObserver.setActiveModel(login);
this.subs.add(
config.observe('github.excludedUsers', value => {
this.excludedUsers = new Set(
(value || '').split(/\s*,\s*/).filter(each => each.length > 0),
);
return this.loadUsers();
}),
);
}
dispose() {
this.subs.dispose();
this.emitter.dispose();
}
async loadUsers() {
const data = this.repositoryObserver.getActiveModelData();
if (!data) {
return;
}
this.setCommitter(data.committer);
const githubRemotes = data.remotes.filter(remote => remote.isGithubRepo());
if (githubRemotes.length === 0) {
this.addUsers(data.authors, source.GITLOG);
} else {
await this.loadUsersFromGraphQL(githubRemotes);
}
}
loadUsersFromGraphQL(remotes) {
return Promise.all(
remotes.map(remote => this.loadMentionableUsers(remote)),
);
}
async loadMentionableUsers(remote) {
const cached = this.cache.get(remote);
if (cached !== null) {
this.addUsers(cached, source.GITHUBAPI);
return;
}
const loginModel = this.loginObserver.getActiveModel();
if (!loginModel) {
return;
}
const token = await loginModel.getToken('https://api.github.com');
if (token === UNAUTHENTICATED || token === INSUFFICIENT) {
return;
}
const fetchQuery = RelayNetworkLayerManager.getFetchQuery('https://api.github.com/graphql', token);
let hasMore = true;
let cursor = null;
const remoteUsers = [];
while (hasMore) {
const response = await fetchQuery({
name: 'GetMentionableUsers',
text: `
query GetMentionableUsers($owner: String!, $name: String!, $first: Int!, $after: String) {
repository(owner: $owner, name: $name) {
mentionableUsers(first: $first, after: $after) {
nodes {
login
email
name
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`,
}, {
owner: remote.getOwner(),
name: remote.getRepo(),
first: 100,
after: cursor,
});
if (response.errors && response.errors.length > 1) {
// eslint-disable-next-line no-console
console.error(`Error fetching mentionable users:\n${response.errors.map(e => e.message).join('\n')}`);
}
if (!response.data) {
break;
}
const connection = response.data.repository.mentionableUsers;
const authors = connection.nodes.map(node => {
if (node.email === '') {
node.email = `${node.login}@users.noreply.github.com`;
}
return new Author(node.email, node.name, node.login);
});
this.addUsers(authors, source.GITHUBAPI);
remoteUsers.push(...authors);
cursor = connection.pageInfo.endCursor;
hasMore = connection.pageInfo.hasNextPage;
}
this.cache.set(remote, remoteUsers);
}
addUsers(users, nextSource) {
let changed = false;
if (
nextSource !== this.last.source ||
this.repositoryObserver.getActiveModel() !== this.last.repository ||
this.excludedUsers !== this.last.excludedUsers
) {
changed = true;
this.allUsers.clear();
}
for (const author of users) {
if (!this.allUsers.has(author.getEmail())) {
changed = true;
}
this.allUsers.set(author.getEmail(), author);
}
if (changed) {
this.finalize();
}
this.last.source = nextSource;
this.last.repository = this.repositoryObserver.getActiveModel();
this.last.excludedUsers = this.excludedUsers;
}
finalize() {
// TODO: [ku 3/2018] consider sorting based on most recent authors or commit frequency
const users = [];
for (const author of this.allUsers.values()) {
if (author.matches(this.committer)) { continue; }
if (author.isNoReply()) { continue; }
if (this.excludedUsers.has(author.getEmail())) { continue; }
users.push(author);
}
users.sort(Author.compare);
this.users = users;
this.didUpdate();
}
setRepository(repository) {
this.repositoryObserver.setActiveModel(repository);
}
setLoginModel(login) {
this.loginObserver.setActiveModel(login);
}
setCommitter(committer) {
const changed = !this.committer.matches(committer);
this.committer = committer;
if (changed) {
this.finalize();
}
}
didUpdate() {
this.emitter.emit('did-update', this.getUsers());
}
onDidUpdate(callback) {
return this.emitter.on('did-update', callback);
}
getUsers() {
return this.users;
}
}