Skip to content

Commit

Permalink
Remove isContributor from DB, load contributors from file into memory
Browse files Browse the repository at this point in the history
  • Loading branch information
SatsAllDay committed Sep 13, 2023
1 parent 4a11435 commit 49f6c55
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 66 deletions.
24 changes: 22 additions & 2 deletions api/resolvers/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
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'
import { bioSchema, emailSchema, settingsSchema, ssValidate, userSchema } from '../../lib/validate'
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) {
Expand Down Expand Up @@ -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)
}
}
}
4 changes: 4 additions & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
k00b
kr
ekzyis
WeAreAllSatoshi
Original file line number Diff line number Diff line change
@@ -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;
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
57 changes: 0 additions & 57 deletions worker/contributors.js

This file was deleted.

4 changes: 0 additions & 4 deletions worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
}

Expand Down

0 comments on commit 49f6c55

Please sign in to comment.