-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync.js
More file actions
370 lines (325 loc) · 10.9 KB
/
sync.js
File metadata and controls
370 lines (325 loc) · 10.9 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* LinuxDo Star - Gist Sync Module
* 通过 GitHub Gist 实现跨设备同步
*/
const SYNC_CONFIG_KEY = 'linuxdo_sync_config';
const GIST_FILENAME = 'linuxdo-stars.json';
const GIST_DESCRIPTION = 'LinuxDo Star Collector - Sync Data (do not delete)';
const SyncManager = {
// ==================== Config ====================
async getConfig() {
return new Promise(r => {
chrome.storage.local.get([SYNC_CONFIG_KEY], res => {
r(res[SYNC_CONFIG_KEY] || { token: '', gistId: '', lastSyncAt: '', autoSync: true, status: 'disconnected' });
});
});
},
async saveConfig(cfg) {
return new Promise(r => chrome.storage.local.set({ [SYNC_CONFIG_KEY]: cfg }, r));
},
// ==================== Core Sync ====================
/**
* Full sync: pull remote → merge → push merged result
* Returns { ok, message, merged? }
*/
async sync() {
const cfg = await this.getConfig();
if (!cfg.token || !cfg.gistId) return { ok: false, message: '未配置同步' };
try {
await this.updateStatus('syncing');
// 1. Read remote
const remote = await this.readGist(cfg.token, cfg.gistId);
// 2. Read local
const local = await StarStorage.getAll();
// 3. Merge
const merged = this.merge(local, remote);
// 4. Save merged locally
await StarStorage.save(merged);
// 5. Push merged to remote
await this.writeGist(cfg.token, cfg.gistId, merged);
// 6. Update config
cfg.lastSyncAt = new Date().toISOString();
await this.saveConfig(cfg);
await this.updateStatus('synced');
return { ok: true, message: '同步成功', merged };
} catch (err) {
console.error('[LinuxDo Star Sync]', err);
await this.updateStatus('error', err.message);
return { ok: false, message: err.message };
}
},
/**
* Push only: local → remote (with merge to avoid overwriting)
*/
async push() {
return this.sync(); // Full sync is safer than blind push
},
/**
* Pull only: remote → local (with merge)
*/
async pull() {
return this.sync(); // Same as sync
},
async updateStatus(status, error) {
const cfg = await this.getConfig();
cfg.status = status;
if (error) cfg.lastError = error;
else delete cfg.lastError;
await this.saveConfig(cfg);
// Notify popup/manage page
try { chrome.runtime.sendMessage({ type: 'SYNC_STATUS', status, error }); } catch {}
},
// ==================== Gist API ====================
async apiRequest(token, url, options = {}) {
const resp = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${token}`,
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
...(options.headers || {}),
},
});
if (!resp.ok) {
const body = await resp.text().catch(() => '');
if (resp.status === 401) throw new Error('Token 无效或已过期');
if (resp.status === 404) throw new Error('Gist 不存在');
throw new Error(`GitHub API 错误 (${resp.status}): ${body.substring(0, 100)}`);
}
return resp.json();
},
/**
* Find existing gist or create a new one
*/
async findOrCreateGist(token) {
// Search user's gists for our file
const gists = await this.apiRequest(token, 'https://api.github.com/gists?per_page=100');
for (const gist of gists) {
if (gist.files[GIST_FILENAME]) {
return gist.id;
}
}
// Not found — create new private gist
const newGist = await this.apiRequest(token, 'https://api.github.com/gists', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
description: GIST_DESCRIPTION,
public: false,
files: {
[GIST_FILENAME]: {
content: JSON.stringify({ collections: {}, bookmarks: {} }, null, 2),
},
},
}),
});
return newGist.id;
},
/**
* Read data from gist
*/
async readGist(token, gistId) {
const gist = await this.apiRequest(token, `https://api.github.com/gists/${gistId}`);
const file = gist.files[GIST_FILENAME];
if (!file) throw new Error('Gist 中未找到数据文件');
try {
const data = JSON.parse(file.content);
// Ensure structure
if (!data.collections) data.collections = {};
if (!data.bookmarks) data.bookmarks = {};
return data;
} catch {
// Corrupted data, return empty
return { collections: {}, bookmarks: {} };
}
},
/**
* Write data to gist
*/
async writeGist(token, gistId, data) {
await this.apiRequest(token, `https://api.github.com/gists/${gistId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
files: {
[GIST_FILENAME]: {
content: JSON.stringify(data, null, 2),
},
},
}),
});
},
/**
* Validate token by getting user info
*/
async validateToken(token) {
try {
const user = await this.apiRequest(token, 'https://api.github.com/user');
return { ok: true, username: user.login, avatar: user.avatar_url };
} catch (err) {
return { ok: false, message: err.message };
}
},
/**
* Connect: validate token → find/create gist → initial sync
*/
async connect(token) {
// 1. Validate token
const validation = await this.validateToken(token);
if (!validation.ok) return { ok: false, message: validation.message };
// 2. Find or create gist
const gistId = await this.findOrCreateGist(token);
// 3. Save config
const cfg = {
token,
gistId,
lastSyncAt: '',
autoSync: true,
status: 'connected',
username: validation.username,
};
await this.saveConfig(cfg);
// 4. Initial sync
const result = await this.sync();
return { ok: true, message: `已连接为 @${validation.username}`, gistId, ...result };
},
/**
* Disconnect: clear token (keep local data)
*/
async disconnect() {
await this.saveConfig({ token: '', gistId: '', lastSyncAt: '', autoSync: false, status: 'disconnected' });
return { ok: true, message: '已断开同步' };
},
// ==================== Merge Algorithm ====================
/**
* Merge local and remote data
* Strategy:
* - Union of all items
* - Per-item latest-wins by updatedAt/starredAt
* - Soft-deletes (_deleted + _deletedAt) propagate: if deleted side is newer, item stays deleted
* - Tombstones older than 7 days are purged after merge
*/
merge(local, remote) {
const result = { collections: {}, bookmarks: {} };
// --- Merge collections ---
const allColIds = new Set([
...Object.keys(local.collections || {}),
...Object.keys(remote.collections || {}),
]);
for (const id of allColIds) {
const l = local.collections?.[id];
const r = remote.collections?.[id];
if (!l) result.collections[id] = r;
else if (!r) result.collections[id] = l;
else result.collections[id] = this._newer(l, r);
}
if (!result.collections.default) {
result.collections.default = {
id: 'default', name: '默认收藏夹', icon: '⭐', color: '#eab308',
createdAt: new Date().toISOString(), order: 0,
};
}
// --- Merge bookmarks (with soft-delete support) ---
const allTopicKeys = new Set([
...Object.keys(local.bookmarks || {}),
...Object.keys(remote.bookmarks || {}),
]);
for (const key of allTopicKeys) {
const l = local.bookmarks?.[key];
const r = remote.bookmarks?.[key];
// One side missing entirely — use the other
if (!l && !r) continue;
if (!l) { result.bookmarks[key] = r; continue; }
if (!r) { result.bookmarks[key] = l; continue; }
// Both exist — check soft-delete
const lDel = l._deleted;
const rDel = r._deleted;
if (lDel && rDel) {
// Both deleted — keep tombstone with latest time
result.bookmarks[key] = this._newer(l, r);
continue;
}
if (lDel && !rDel) {
// Local deleted, remote alive — who's newer?
const lTime = new Date(l._deletedAt || 0).getTime();
const rTime = new Date(r.updatedAt || r.starredAt || 0).getTime();
if (lTime >= rTime) {
// Delete is newer — keep tombstone
result.bookmarks[key] = l;
} else {
// Remote update is newer — resurrect
result.bookmarks[key] = r;
}
continue;
}
if (!lDel && rDel) {
// Remote deleted, local alive — who's newer?
const rTime = new Date(r._deletedAt || 0).getTime();
const lTime = new Date(l.updatedAt || l.starredAt || 0).getTime();
if (rTime >= lTime) {
result.bookmarks[key] = r;
} else {
result.bookmarks[key] = l;
}
continue;
}
// Both alive — merge normally
const merged = { ...this._newer(l, r) };
// Merge posts (with soft-delete)
merged.posts = {};
const allPostKeys = new Set([
...Object.keys(l.posts || {}),
...Object.keys(r.posts || {}),
]);
for (const pk of allPostKeys) {
const lp = l.posts?.[pk];
const rp = r.posts?.[pk];
if (!lp && !rp) continue;
if (!lp) { merged.posts[pk] = rp; continue; }
if (!rp) { merged.posts[pk] = lp; continue; }
const lpDel = lp._deleted;
const rpDel = rp._deleted;
if (lpDel && rpDel) {
merged.posts[pk] = this._newer(lp, rp);
} else if (lpDel && !rpDel) {
const lt = new Date(lp._deletedAt || 0).getTime();
const rt = new Date(rp.updatedAt || rp.starredAt || 0).getTime();
merged.posts[pk] = lt >= rt ? lp : rp;
} else if (!lpDel && rpDel) {
const rt = new Date(rp._deletedAt || 0).getTime();
const lt = new Date(lp.updatedAt || lp.starredAt || 0).getTime();
merged.posts[pk] = rt >= lt ? rp : lp;
} else {
merged.posts[pk] = this._newer(lp, rp);
}
}
result.bookmarks[key] = merged;
}
// Purge old tombstones (> 7 days)
const cutoff = Date.now() - 7 * 24 * 60 * 60 * 1000;
for (const [key, bk] of Object.entries(result.bookmarks)) {
if (bk._deleted && new Date(bk._deletedAt || 0).getTime() < cutoff) {
delete result.bookmarks[key];
continue;
}
if (bk.posts) {
for (const [pk, p] of Object.entries(bk.posts)) {
if (p._deleted && new Date(p._deletedAt || 0).getTime() < cutoff) {
delete bk.posts[pk];
}
}
}
}
return result;
},
return result;
},
/**
* Return the newer of two objects based on updatedAt > starredAt > createdAt
*/
_newer(a, b) {
const ta = new Date(a.updatedAt || a.starredAt || a.createdAt || 0).getTime();
const tb = new Date(b.updatedAt || b.starredAt || b.createdAt || 0).getTime();
return ta >= tb ? { ...a } : { ...b };
},
};