mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 04:26:58 +00:00
fix type errors from SponsorsService
This commit is contained in:
parent
ebbf167f57
commit
000b1f4fe2
2 changed files with 87 additions and 23 deletions
|
@ -4,24 +4,47 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
||||||
import type { MiMeta } from '@/models/_.js';
|
|
||||||
import * as Redis from 'ioredis';
|
import * as Redis from 'ioredis';
|
||||||
|
import type { MiMeta } from '@/models/_.js';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import { RedisKVCache } from '@/misc/cache.js';
|
import { RedisKVCache } from '@/misc/cache.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
|
|
||||||
|
export interface Sponsor {
|
||||||
|
MemberId: number;
|
||||||
|
createdAt: string;
|
||||||
|
type: string;
|
||||||
|
role: string;
|
||||||
|
tier: string;
|
||||||
|
isActive: boolean;
|
||||||
|
totalAmountDonated: number;
|
||||||
|
currency: string;
|
||||||
|
lastTransactionAt: string;
|
||||||
|
lastTransactionAmount: number;
|
||||||
|
profile: string;
|
||||||
|
name: string;
|
||||||
|
company: string | null;
|
||||||
|
description: string | null;
|
||||||
|
image: string | null;
|
||||||
|
email: string | null;
|
||||||
|
newsletterOptIn: unknown | null;
|
||||||
|
twitter: string | null;
|
||||||
|
github: string | null;
|
||||||
|
website: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SponsorsService implements OnApplicationShutdown {
|
export class SponsorsService implements OnApplicationShutdown {
|
||||||
private cache: RedisKVCache<void[]>;
|
private readonly cache: RedisKVCache<Sponsor[]>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(DI.meta)
|
@Inject(DI.meta)
|
||||||
private meta: MiMeta,
|
private readonly meta: MiMeta,
|
||||||
|
|
||||||
@Inject(DI.redis)
|
@Inject(DI.redis)
|
||||||
private redisClient: Redis.Redis,
|
redisClient: Redis.Redis,
|
||||||
) {
|
) {
|
||||||
this.cache = new RedisKVCache<void[]>(this.redisClient, 'sponsors', {
|
this.cache = new RedisKVCache<Sponsor[]>(redisClient, 'sponsors', {
|
||||||
lifetime: 1000 * 60 * 60,
|
lifetime: 1000 * 60 * 60,
|
||||||
memoryCacheLifetime: 1000 * 60,
|
memoryCacheLifetime: 1000 * 60,
|
||||||
fetcher: (key) => {
|
fetcher: (key) => {
|
||||||
|
@ -34,54 +57,54 @@ export class SponsorsService implements OnApplicationShutdown {
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
private async fetchInstanceSponsors() {
|
private async fetchInstanceSponsors(): Promise<Sponsor[]> {
|
||||||
if (!(this.meta.donationUrl && this.meta.donationUrl.includes('opencollective.com'))) {
|
if (!(this.meta.donationUrl && this.meta.donationUrl.includes('opencollective.com'))) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const backers = await fetch(`${this.meta.donationUrl}/members/users.json`).then((response) => response.json());
|
const backers: Sponsor[] = await fetch(`${this.meta.donationUrl}/members/users.json`).then((response) => response.json());
|
||||||
|
|
||||||
// Merge both together into one array and make sure it only has Active subscriptions
|
// Merge both together into one array and make sure it only has Active subscriptions
|
||||||
const allSponsors = [...backers].filter(sponsor => sponsor.isActive === true && sponsor.role === 'BACKER' && sponsor.tier);
|
const allSponsors = [...backers].filter(sponsor => sponsor.isActive && sponsor.role === 'BACKER' && sponsor.tier);
|
||||||
|
|
||||||
// Remove possible duplicates
|
// Remove possible duplicates
|
||||||
return [...new Map(allSponsors.map(v => [v.profile, v])).values()];
|
return [...new Map(allSponsors.map(v => [v.profile, v])).values()];
|
||||||
} catch (error) {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
private async fetchSharkeySponsors() {
|
private async fetchSharkeySponsors(): Promise<Sponsor[]> {
|
||||||
try {
|
try {
|
||||||
const backers = await fetch('https://opencollective.com/sharkey/tiers/backer/all.json').then((response) => response.json());
|
const backers: Sponsor[] = await fetch('https://opencollective.com/sharkey/tiers/backer/all.json').then((response) => response.json());
|
||||||
const sponsorsOC = await fetch('https://opencollective.com/sharkey/tiers/sponsor/all.json').then((response) => response.json());
|
const sponsorsOC: Sponsor[] = await fetch('https://opencollective.com/sharkey/tiers/sponsor/all.json').then((response) => response.json());
|
||||||
|
|
||||||
// Merge both together into one array and make sure it only has Active subscriptions
|
// Merge both together into one array and make sure it only has Active subscriptions
|
||||||
const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive === true);
|
const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive);
|
||||||
|
|
||||||
// Remove possible duplicates
|
// Remove possible duplicates
|
||||||
return [...new Map(allSponsors.map(v => [v.profile, v])).values()];
|
return [...new Map(allSponsors.map(v => [v.profile, v])).values()];
|
||||||
} catch (error) {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async instanceSponsors(forceUpdate: boolean) {
|
public async instanceSponsors(forceUpdate: boolean) {
|
||||||
if (forceUpdate) this.cache.refresh('instance');
|
if (forceUpdate) await this.cache.refresh('instance');
|
||||||
return this.cache.fetch('instance');
|
return this.cache.fetch('instance');
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async sharkeySponsors(forceUpdate: boolean) {
|
public async sharkeySponsors(forceUpdate: boolean) {
|
||||||
if (forceUpdate) this.cache.refresh('sharkey');
|
if (forceUpdate) await this.cache.refresh('sharkey');
|
||||||
return this.cache.fetch('sharkey');
|
return this.cache.fetch('sharkey');
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public onApplicationShutdown(signal?: string | undefined): void {
|
public onApplicationShutdown(): void {
|
||||||
this.cache.dispose();
|
this.cache.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,39 @@ export const meta = {
|
||||||
requireCredential: false,
|
requireCredential: false,
|
||||||
requireCredentialPrivateMode: false,
|
requireCredentialPrivateMode: false,
|
||||||
|
|
||||||
|
res: {
|
||||||
|
type: 'object',
|
||||||
|
nullable: false, optional: false,
|
||||||
|
properties: {
|
||||||
|
sponsor_data: {
|
||||||
|
type: 'array',
|
||||||
|
nullable: false, optional: false,
|
||||||
|
items: {
|
||||||
|
type: 'object',
|
||||||
|
nullable: false, optional: false,
|
||||||
|
properties: {
|
||||||
|
name: {
|
||||||
|
type: 'string',
|
||||||
|
nullable: false, optional: false,
|
||||||
|
},
|
||||||
|
image: {
|
||||||
|
type: 'string',
|
||||||
|
nullable: true, optional: false,
|
||||||
|
},
|
||||||
|
website: {
|
||||||
|
type: 'string',
|
||||||
|
nullable: true, optional: false,
|
||||||
|
},
|
||||||
|
profile: {
|
||||||
|
type: 'string',
|
||||||
|
nullable: false, optional: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// 2 calls per second
|
// 2 calls per second
|
||||||
limit: {
|
limit: {
|
||||||
duration: 1000,
|
duration: 1000,
|
||||||
|
@ -24,6 +57,7 @@ export const meta = {
|
||||||
export const paramDef = {
|
export const paramDef = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
|
// TODO remove this or make staff-only to prevent DoS
|
||||||
forceUpdate: { type: 'boolean', default: false },
|
forceUpdate: { type: 'boolean', default: false },
|
||||||
instance: { type: 'boolean', default: false },
|
instance: { type: 'boolean', default: false },
|
||||||
},
|
},
|
||||||
|
@ -35,12 +69,19 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
constructor(
|
constructor(
|
||||||
private sponsorsService: SponsorsService,
|
private sponsorsService: SponsorsService,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps, me) => {
|
super(meta, paramDef, async (ps) => {
|
||||||
if (ps.instance) {
|
const sponsors = ps.instance
|
||||||
return { sponsor_data: await this.sponsorsService.instanceSponsors(ps.forceUpdate) };
|
? await this.sponsorsService.instanceSponsors(ps.forceUpdate)
|
||||||
} else {
|
: await this.sponsorsService.sharkeySponsors(ps.forceUpdate);
|
||||||
return { sponsor_data: await this.sponsorsService.sharkeySponsors(ps.forceUpdate) };
|
|
||||||
}
|
return {
|
||||||
|
sponsor_data: sponsors.map(s => ({
|
||||||
|
name: s.name,
|
||||||
|
image: s.image,
|
||||||
|
website: s.website,
|
||||||
|
profile: s.profile,
|
||||||
|
})),
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue