upd(backend): add support for instance oc urls

This commit is contained in:
Marie 2024-10-02 17:20:30 +02:00
parent 0bd6d48c7e
commit 1e0d372eb5
No known key found for this signature in database
GPG Key ID: 7ADF6C9CD9A28555

View File

@ -7,10 +7,11 @@ import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis'; import * as Redis from 'ioredis';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { MetaService } from '@/core/MetaService.js';
export const meta = { export const meta = {
tags: ['meta'], tags: ['meta'],
description: 'Get Sharkey Sponsors', description: 'Get Sharkey Sponsors or Instance Sponsors',
requireCredential: false, requireCredential: false,
requireCredentialPrivateMode: false, requireCredentialPrivateMode: false,
@ -20,6 +21,7 @@ export const paramDef = {
type: 'object', type: 'object',
properties: { properties: {
forceUpdate: { type: 'boolean', default: false }, forceUpdate: { type: 'boolean', default: false },
instance: { type: 'boolean', default: false },
}, },
required: [], required: [],
} as const; } as const;
@ -28,14 +30,15 @@ export const paramDef = {
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor( constructor(
@Inject(DI.redis) private redisClient: Redis.Redis, @Inject(DI.redis) private redisClient: Redis.Redis,
private metaService: MetaService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
let totalSponsors; let totalSponsors;
const cachedSponsors = await this.redisClient.get('sponsors'); const cachedSponsors = await this.redisClient.get('sponsors');
if (!ps.forceUpdate && cachedSponsors) { if (!ps.forceUpdate && !ps.instance && cachedSponsors) {
totalSponsors = JSON.parse(cachedSponsors); totalSponsors = JSON.parse(cachedSponsors);
} else { } else if (!ps.instance) {
try { try {
const backers = await fetch('https://opencollective.com/sharkey/tiers/backer/all.json').then((response) => response.json()); const backers = 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 = await fetch('https://opencollective.com/sharkey/tiers/sponsor/all.json').then((response) => response.json());
@ -50,6 +53,25 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
} catch (error) { } catch (error) {
totalSponsors = []; totalSponsors = [];
} }
} else {
try {
const meta = await this.metaService.fetch();
if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) {
totalSponsors = [];
} else if (meta.donationUrl) {
const backers = await fetch(`${meta.donationUrl}/members/users.json`).then((response) => response.json());
// 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');
// Remove possible duplicates
totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
} else {
totalSponsors = [];
}
} catch (error) {
totalSponsors = [];
}
} }
return { sponsor_data: totalSponsors }; return { sponsor_data: totalSponsors };
}); });