|
| 1 | +import Base from '../../../src/core/Base.mjs'; |
| 2 | +import config from './config.mjs'; |
| 3 | +import GitHub from './GitHub.mjs'; |
| 4 | +import Storage from './Storage.mjs'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @summary Opt-Out Service for DevIndex. |
| 8 | + * |
| 9 | + * Checks the stargazers of the `neomjs/devindex-opt-out` repository |
| 10 | + * to automatically blacklist users and remove them from the index. |
| 11 | + * |
| 12 | + * @class DevIndex.services.OptOut |
| 13 | + * @extends Neo.core.Base |
| 14 | + * @singleton |
| 15 | + */ |
| 16 | +class OptOut extends Base { |
| 17 | + static config = { |
| 18 | + /** |
| 19 | + * @member {String} className='DevIndex.services.OptOut' |
| 20 | + * @protected |
| 21 | + */ |
| 22 | + className: 'DevIndex.services.OptOut', |
| 23 | + /** |
| 24 | + * @member {Boolean} singleton=true |
| 25 | + * @protected |
| 26 | + */ |
| 27 | + singleton: true, |
| 28 | + /** |
| 29 | + * @member {String} optOutRepoOwner='neomjs' |
| 30 | + */ |
| 31 | + optOutRepoOwner: 'neomjs', |
| 32 | + /** |
| 33 | + * @member {String} optOutRepoName='devindex-opt-out' |
| 34 | + */ |
| 35 | + optOutRepoName: 'devindex-opt-out' |
| 36 | + } |
| 37 | + |
| 38 | + async run() { |
| 39 | + console.log('[OptOut] Checking for new opt-out requests...'); |
| 40 | + const syncState = await Storage.getOptOutSync(); |
| 41 | + const lastCheck = syncState.lastCheck; |
| 42 | + let newLastCheck = lastCheck; |
| 43 | + |
| 44 | + let hasNextPage = true; |
| 45 | + let cursor = null; |
| 46 | + let optedOutLogins = []; |
| 47 | + |
| 48 | + while (hasNextPage) { |
| 49 | + const query = ` |
| 50 | + query($owner: String!, $name: String!, $cursor: String) { |
| 51 | + repository(owner: $owner, name: $name) { |
| 52 | + stargazers(first: 100, orderBy: {field: STARRED_AT, direction: DESC}, after: $cursor) { |
| 53 | + pageInfo { |
| 54 | + hasNextPage |
| 55 | + endCursor |
| 56 | + } |
| 57 | + edges { |
| 58 | + starredAt |
| 59 | + node { |
| 60 | + login |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }`; |
| 66 | + |
| 67 | + const variables = { |
| 68 | + owner: this.optOutRepoOwner, |
| 69 | + name: this.optOutRepoName, |
| 70 | + cursor: cursor |
| 71 | + }; |
| 72 | + |
| 73 | + let data; |
| 74 | + try { |
| 75 | + data = await GitHub.query(query, variables, 3, 'OptOut'); |
| 76 | + } catch (err) { |
| 77 | + // If repo doesn't exist yet, just warn and exit gracefully |
| 78 | + if (err.message.includes('NOT_FOUND') || err.message.includes('Could not resolve')) { |
| 79 | + console.warn(`[OptOut] Opt-out repository ${this.optOutRepoOwner}/${this.optOutRepoName} not found. Skipping.`); |
| 80 | + return; |
| 81 | + } |
| 82 | + throw err; |
| 83 | + } |
| 84 | + |
| 85 | + const stargazers = data?.repository?.stargazers; |
| 86 | + if (!stargazers) break; |
| 87 | + |
| 88 | + const edges = stargazers.edges || []; |
| 89 | + let stopFetching = false; |
| 90 | + |
| 91 | + for (const edge of edges) { |
| 92 | + const starredAt = edge.starredAt; |
| 93 | + const login = edge.node.login; |
| 94 | + |
| 95 | + // Stop if we reached stars we've already processed |
| 96 | + if (lastCheck && starredAt <= lastCheck) { |
| 97 | + stopFetching = true; |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + // Keep track of the newest timestamp to save later |
| 102 | + if (!newLastCheck || starredAt > newLastCheck) { |
| 103 | + newLastCheck = starredAt; |
| 104 | + } |
| 105 | + |
| 106 | + optedOutLogins.push(login); |
| 107 | + } |
| 108 | + |
| 109 | + if (stopFetching) { |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + hasNextPage = stargazers.pageInfo.hasNextPage; |
| 114 | + cursor = stargazers.pageInfo.endCursor; |
| 115 | + } |
| 116 | + |
| 117 | + if (optedOutLogins.length > 0) { |
| 118 | + console.log(`[OptOut] Found ${optedOutLogins.length} new opt-out requests.`); |
| 119 | + |
| 120 | + // 1. Add to blacklist |
| 121 | + await Storage.addToBlacklist(optedOutLogins); |
| 122 | + |
| 123 | + // 2. Remove from rich data (users.jsonl) |
| 124 | + await Storage.deleteUsers(optedOutLogins); |
| 125 | + |
| 126 | + // 3. Remove from tracker |
| 127 | + const trackerUpdates = optedOutLogins.map(login => ({ login, delete: true })); |
| 128 | + await Storage.updateTracker(trackerUpdates); |
| 129 | + |
| 130 | + // 4. Remove from failed list (Penalty Box) |
| 131 | + await Storage.updateFailed(optedOutLogins, false); |
| 132 | + |
| 133 | + // Update sync state |
| 134 | + await Storage.saveOptOutSync({ lastCheck: newLastCheck }); |
| 135 | + console.log(`[OptOut] Processed opt-outs and updated sync state to ${newLastCheck}.`); |
| 136 | + } else { |
| 137 | + console.log('[OptOut] No new opt-out requests found.'); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +export default Neo.setupClass(OptOut); |
0 commit comments