Skip to content

Commit

Permalink
Display supporters over the last 3 months
Browse files Browse the repository at this point in the history
And a few fixes
  • Loading branch information
vicb committed Sep 27, 2024
1 parent 7f455f9 commit eea0ed4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
18 changes: 15 additions & 3 deletions apps/fetcher/src/app/misc/buy-coffee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { fetchResponse } from '@flyxc/common';
import { Secrets } from '@flyxc/secrets';
import { format, subMonths } from 'date-fns';

export type Supporters = {
// visible supporters
Expand All @@ -12,14 +13,21 @@ export type Supporters = {
numSupporters: number;
// total amount
totalAmount: number;
// last3MonthsAmount
last3MonthsAmount: number;
};

export async function fetchSupporters(): Promise<Supporters> {
const supporters: Supporters = {
names: new Set<string>(),
numSupporters: 0,
totalAmount: 0,
last3MonthsAmount: 0,
};

const cutoffDate = subMonths(new Date(), 3);
const cutoffDateString = format(cutoffDate, 'yyyy-MM-dd');

let url = `https://developers.buymeacoffee.com/api/v1/supporters`;
try {
while (url) {
Expand All @@ -32,14 +40,18 @@ export async function fetchSupporters(): Promise<Supporters> {
const data = await response.json();
const users = data.data;
for (const user of users) {
if (user.amount <= 0 || user.is_refunded) {
if (user.is_refunded) {
continue;
}
if (user.support_visibility === 1 && user.payer_name) {
if (user.support_visibility === 1 && user.payer_name.length > 3 && user.payer_name !== 'Someone') {
supporters.names.add(user.payer_name);
}
supporters.numSupporters++;
supporters.totalAmount += user.support_coffees * user.support_coffee_price;
const amount = user.support_coffees * user.support_coffee_price;
supporters.totalAmount += amount;
if (user.support_created_on >= cutoffDateString) {
supporters.last3MonthsAmount += amount;
}
}
url = data.next_page_url;
} else {
Expand Down
9 changes: 5 additions & 4 deletions apps/fetcher/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
removeBeforeFromLiveTrack,
removeDeviceFromLiveTrack,
} from '@flyxc/common';
import { getDatastore, getRedisClient, pushListCap } from '@flyxc/common-node';
import { getDatastore, getRedisClient } from '@flyxc/common-node';
import { Secrets } from '@flyxc/secrets';
import type { Datastore } from '@google-cloud/datastore';
import { program } from 'commander';
Expand Down Expand Up @@ -134,9 +134,10 @@ async function tick(state: protos.FetcherState, datastore: Datastore) {
pipeline
.del(Keys.supporterNames)
.set(Keys.supporterNum, supporters.numSupporters)
.set(Keys.supporterAmount, supporters.totalAmount);
pushListCap(pipeline, Keys.supporterNames, Array.from(supporters.names), 50, 50);

.set(Keys.supporterAmount, supporters.totalAmount)
.set(Keys.supporterLast3MonthsAmount, supporters.last3MonthsAmount)
.del(Keys.supporterNames)
.rpush(Keys.supporterNames, ...Array.from(supporters.names).slice(0, 50));
state.nextSupporterSyncSec = state.lastTickSec + SUPPORTER_SYNC_SEC;
}

Expand Down
10 changes: 6 additions & 4 deletions apps/fxc-front/src/app/components/ui/supporter-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { modalController } from '@ionic/core/components';
import type { TemplateResult } from 'lit';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
import { when } from 'lit/directives/when.js';

@customElement('supporter-modal')
export class SupporterModal extends LitElement {
private supporters = { names: [], amount: 0, number: 0 };
private supporters = { names: [], amount: 0, number: 0, amountLast3Months: 0 };

async connectedCallback(): Promise<void> {
super.connectedCallback();
Expand Down Expand Up @@ -38,7 +39,7 @@ export class SupporterModal extends LitElement {
</p>
<p>
If you can afford it, please consider supporting flyXC with a small donation by clicking the button below.
It will help keep me motivated to maintain and improve it.
It will help keep the contributors motivated to maintain and improve it.
</p>
</ion-text>
Expand All @@ -52,10 +53,11 @@ export class SupporterModal extends LitElement {
<p>
<ion-text color="dark"
>Thanks to the ${this.supporters.number} supporters who have contributed a total of
$${this.supporters.amount}:
>Thanks to the ${this.supporters.number} supporters who have contributed
$${this.supporters.amountLast3Months} over the last 3 months and a total of $${this.supporters.amount}:
<ul>
${this.supporters.names.map((n) => html`<li>${n}</li>`)}
${when(this.supporters.names.length > 30, () => html`<li>...</li>`)}
</ul>
</ion-text>
</p>
Expand Down
8 changes: 4 additions & 4 deletions apps/fxc-server/src/app/routes/supporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import type { Redis } from 'ioredis';
export function getSupportersRouter(redis: Redis): Router {
const router = Router();

// Returns the airspaces info for the first track in the group as JSON.
// Returns 404 if the info are not available (/not ready yet).
// Returns the supporter info
// eslint-disable-next-line @typescript-eslint/no-unused-vars
router.get('/supporters.json', async (req: Request, res: Response) => {
try {
const [names, number, amount] = (
const [names, number, amount, amountLast3Months] = (
await redis
.pipeline()
.lrange(Keys.supporterNames, 0, 100)
.get(Keys.supporterNum)
.get(Keys.supporterAmount)
.get(Keys.supporterLast3MonthsAmount)
.exec()
).map(([_, v]) => v);
return res.json({ names, number, amount });
return res.json({ names, number, amount, amountLast3Months });
} catch (error) {
return res.sendStatus(500);
}
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/lib/redis-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export enum Keys {
supporterNames = 'f:supporters',
supporterNum = 'f:supporters:num',
supporterAmount = 'f:supporters:amount',
supporterLast3MonthsAmount = 'f:supporters:amount3m',

// [List]
proxyInreach = 'p:inreach:logs',
Expand Down

0 comments on commit eea0ed4

Please sign in to comment.