diff --git a/prisma/migrations/20240910000406_auto_weekly_posts/migration.sql b/prisma/migrations/20240910000406_auto_weekly_posts/migration.sql new file mode 100644 index 00000000..ad174f44 --- /dev/null +++ b/prisma/migrations/20240910000406_auto_weekly_posts/migration.sql @@ -0,0 +1,37 @@ +CREATE OR REPLACE FUNCTION schedule_weekly_posts_job() +RETURNS INTEGER +LANGUAGE plpgsql +AS $$ +DECLARE +BEGIN + INSERT INTO pgboss.schedule (name, cron, timezone, data) + VALUES + ( + 'weeklyPost-meme-mon', '0 10 * * 1', 'America/Chicago', + jsonb_build_object( + 'title', 'Meme Monday - Best Bitcoin Meme Gets 10,000 Sats', + 'text', E'Time for another round of Meme Monday!\n\nWe have another 10,000 sats up for grabs for this week''s winner.\n\nThe sats will be given to the stacker with the best Bitcoin meme as voted by the "top" filter on this thread at 10am CT tomorrow.\n\nTo post an image on SN, check out our docs [here](https://stacker.news/faq#how-do-i-post-images-on-stacker-news).\n\nSend your best 👇', + 'bounty', 10000) + ), + ( + 'weeklyPost-what-wed', '0 10 * * 3', 'America/Chicago', + jsonb_build_object( + 'title', 'What are you working on this week?', + 'text', E'Calling all stackers!\n\nLeave a comment below to let the SN community know what you''re working on this week. It doesn''t matter how big or small your project is, or how much progress you''ve made.\n\nJust share what you''re up to, and let the community know if you want any feedback or help.' + ) + ), + ( + 'weeklyPost-fact-fri', '0 10 * * 5', 'America/Chicago', + jsonb_build_object( + 'title', 'Fun Fact Friday - Best Fun Fact Gets 10,000 Sats', + 'text', E'Let''s hear all your best fun facts, any topic counts!\n\nThe best comment as voted by the "top" filter at 10am CT tomorrow gets 10,000 sats.\n\nBonus sats for including a source link to your fun fact!\n\nSend your best 👇', + 'bounty', 10000) + ) ON CONFLICT DO NOTHING; + return 0; +EXCEPTION WHEN OTHERS THEN + return 0; +END; +$$; + +SELECT schedule_weekly_posts_job(); +DROP FUNCTION IF EXISTS schedule_weekly_posts_job; \ No newline at end of file diff --git a/worker/index.js b/worker/index.js index 759d6ad7..e2a5a795 100644 --- a/worker/index.js +++ b/worker/index.js @@ -34,6 +34,7 @@ import { } from './paidAction.js' import { thisDay } from './thisDay.js' import { isServiceEnabled } from '@/lib/sndev.js' +import { payWeeklyPostBounty, weeklyPost } from './weeklyPosts.js' const { ApolloClient, HttpLink, InMemoryCache } = apolloClient @@ -116,6 +117,8 @@ async function work () { await boss.work('imgproxy', jobWrapper(imgproxy)) await boss.work('deleteUnusedImages', jobWrapper(deleteUnusedImages)) } + await boss.work('weeklyPost-*', jobWrapper(weeklyPost)) + await boss.work('payWeeklyPostBounty', jobWrapper(payWeeklyPostBounty)) await boss.work('repin-*', jobWrapper(repin)) await boss.work('trust', jobWrapper(trust)) await boss.work('timestampItem', jobWrapper(timestampItem)) diff --git a/worker/thisDay.js b/worker/thisDay.js index 2d4b5e64..cb4dcfd3 100644 --- a/worker/thisDay.js +++ b/worker/thisDay.js @@ -1,11 +1,11 @@ import { datePivot } from '@/lib/time' import gql from 'graphql-tag' import { numWithUnits, abbrNum } from '@/lib/format' -import { paidActions } from '@/api/paidAction' import { USER_ID } from '@/lib/constants' import { getForwardUsers } from '@/api/resolvers/item' +import { autoPost } from './weeklyPosts' -export async function thisDay ({ models, apollo }) { +export async function thisDay ({ models, apollo, lnd, boss }) { const days = [] let yearsAgo = 1 while (datePivot(new Date(), { years: -yearsAgo }) > new Date('2021-06-10')) { @@ -33,16 +33,22 @@ ${topStackers(days)} ${topComments(days)} ${topSubs(days)}` - const user = await models.user.findUnique({ where: { id: USER_ID.sn } }) const forward = days.map(({ data }) => data.users.users?.[0]?.name).filter(Boolean).map(name => ({ nym: name, pct: 10 })) forward.push({ nym: 'Undisciplined', pct: 50 }) const forwardUsers = await getForwardUsers(models, forward) - await models.$transaction(async tx => { - const context = { tx, cost: BigInt(1), user, models } - const result = await paidActions.ITEM_CREATE.perform({ - text, title: `This Day on SN: ${date}`, subName: 'meta', userId: USER_ID.sn, forwardUsers - }, context) - await paidActions.ITEM_CREATE.onPaid(result, context) + + await autoPost({ + data: { + text, + title: `This Day on SN: ${date}`, + subName: 'meta', + userId: USER_ID.sn, + forwardUsers + }, + models, + apollo, + lnd, + boss }) } diff --git a/worker/weeklyPosts.js b/worker/weeklyPosts.js new file mode 100644 index 00000000..ca96c73f --- /dev/null +++ b/worker/weeklyPosts.js @@ -0,0 +1,51 @@ +import performPaidAction from '@/api/paidAction' +import { USER_ID } from '@/lib/constants' +import { datePivot } from '@/lib/time' +import gql from 'graphql-tag' + +export async function autoPost ({ data: item, models, apollo, lnd, boss }) { + return await performPaidAction('ITEM_CREATE', + { ...item, subName: 'meta', userId: USER_ID.sn, apiKey: true }, + { models, me: { id: USER_ID.sn }, lnd, forceFeeCredits: true }) +} + +export async function weeklyPost (args) { + const { result: { id, bounty } } = await autoPost(args) + + if (bounty) { + args.boss.send('payWeeklyPostBounty', { id }, { startAfter: datePivot(new Date(), { hours: 24 }) }) + } +} + +export async function payWeeklyPostBounty ({ data: { id }, models, apollo, lnd }) { + const itemQ = await apollo.query({ + query: gql` + query item($id: ID!) { + item(id: $id) { + userId + bounty + bountyPaidTo + comments(sort: "top") { + id + } + } + }`, + variables: { id } + }) + + const item = itemQ.data.item + + if (item.bountyPaidTo?.length > 0) { + throw new Error('Bounty already paid') + } + + const winner = item.comments[0] + + if (!winner) { + throw new Error('No winner') + } + + await performPaidAction('ZAP', + { id: winner.id, sats: item.bounty }, + { models, me: { id: USER_ID.sn }, lnd, forceFeeCredits: true }) +}