diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 45cb14e42b..3e2664de70 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -1,3 +1,5 @@ +import { readFile } from 'fs/promises' +import { join, resolve } from 'path' import { GraphQLError } from 'graphql' import { decodeCursor, LIMIT, nextCursorEncoded } from '../../lib/cursor' import { msatsToSats } from '../../lib/format' @@ -5,6 +7,20 @@ import { bioSchema, emailSchema, settingsSchema, ssValidate, userSchema } from ' import { getItem, updateItem, filterClause, createItem } from './item' import { datePivot } from '../../lib/time' +const contributors = new Set() + +const loadContributors = async (set) => { + try { + const fileContent = await readFile(resolve(join(process.cwd(), 'contributors.txt')), 'utf-8') + fileContent.split('\n') + .map(line => line.trim()) + .filter(line => !!line) + .forEach(name => set.add(name)) + } catch (err) { + console.error('Error loading contributors', err) + } +} + export function within (table, within) { let interval = ' AND "' + table + '".created_at >= $1 - INTERVAL ' switch (within) { @@ -771,10 +787,14 @@ export default { return !!subscription }, isContributor: async (user, args, { me }) => { + // lazy init contributors only once + if (contributors.size === 0) { + await loadContributors(contributors) + } if (me?.id === user.id) { - return user.isContributor + return contributors.has(user.name) } - return !user.hideIsContributor && user.isContributor + return !user.hideIsContributor && contributors.has(user.name) } } } diff --git a/contributors.txt b/contributors.txt new file mode 100644 index 0000000000..5f59b5215f --- /dev/null +++ b/contributors.txt @@ -0,0 +1,4 @@ +k00b +kr +ekzyis +WeAreAllSatoshi diff --git a/prisma/migrations/20230906010648_verified_contributors/migration.sql b/prisma/migrations/20230906010648_verified_contributors/migration.sql index 82c979ae42..3ce4fb00a3 100644 --- a/prisma/migrations/20230906010648_verified_contributors/migration.sql +++ b/prisma/migrations/20230906010648_verified_contributors/migration.sql @@ -1,3 +1,2 @@ -- AlterTable -ALTER TABLE "users" ADD COLUMN "hideIsContributor" BOOLEAN NOT NULL DEFAULT false, -ADD COLUMN "isContributor" BOOLEAN NOT NULL DEFAULT false; +ALTER TABLE "users" ADD COLUMN "hideIsContributor" BOOLEAN NOT NULL DEFAULT false; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c9f173f4a4..81a34c0e61 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -88,7 +88,6 @@ model User { followers UserSubscription[] @relation("follower") followees UserSubscription[] @relation("followee") hideWelcomeBanner Boolean @default(false) - isContributor Boolean @default(false) hideIsContributor Boolean @default(false) @@index([createdAt], map: "users.created_at_index") diff --git a/worker/contributors.js b/worker/contributors.js deleted file mode 100644 index 3042a9bd9d..0000000000 --- a/worker/contributors.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Instructions: Once you make a contribution to the repo, add yourself to this map, - * where the key is your user id in stacker.news prod, - * and the value map contains your GitHub username, and your SN nym. - * Right now, the GitHub usernames and SN nyms are not used, but maybe we'll use them - * in the future for a fancier UI decoration. - */ -const contributorMap = { - 616: { - github: 'huumn', - sn: 'k00b' - }, - 946: { - github: 'kerooke', - sn: 'kr' - }, - 6030: { - github: 'ekzyis', - sn: 'ekzyis' - }, - 11275: { - github: 'SatsAllDay', - sn: 'WeAreAllSatoshi' - } -} - -/** - * Bootstrap known contributors in the DB upon start-up - * Only needs to run one time per deploy - */ -function contributors ({ models }) { - return async function () { - try { - const { count } = await models.user.updateMany({ - where: { - id: { - in: Object.keys(contributorMap).map(Number) - } - }, - data: { - isContributor: true - } - }) - console.log(`set ${count} users as contributors`) - const users = await models.user.findMany({ - where: { - isContributor: true - } - }) - console.log(`Contributors: ${users.map(user => user.name).join(', ')}`) - } catch (err) { - console.error('Error bootstrapping contributors', err) - } - } -} - -module.exports = { contributors } diff --git a/worker/index.js b/worker/index.js index 11f44b3b2a..0c93ac8eea 100644 --- a/worker/index.js +++ b/worker/index.js @@ -11,7 +11,6 @@ const { indexItem, indexAllItems } = require('./search') const { timestampItem } = require('./ots') const { computeStreaks, checkStreak } = require('./streak') const { nip57 } = require('./nostr') -const { contributors } = require('./contributors') const fetch = require('cross-fetch') const { authenticatedLndGrpc } = require('ln-service') @@ -64,9 +63,6 @@ async function work () { await boss.work('views', views(args)) await boss.work('rankViews', rankViews(args)) - // Run once - await contributors(args)() - console.log('working jobs') }