2024-06-15 11:36:55 +01:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: marie and other Sharkey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-10-14 01:59:09 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-02-21 18:59:31 +00:00
|
|
|
import * as Redis from 'ioredis';
|
2023-10-14 01:59:09 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2024-10-02 17:20:30 +02:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-10-14 01:59:09 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2024-02-21 18:59:31 +00:00
|
|
|
tags: ['meta'],
|
2024-10-02 17:20:30 +02:00
|
|
|
description: 'Get Sharkey Sponsors or Instance Sponsors',
|
2023-10-14 01:59:09 +02:00
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
requireCredentialPrivateMode: false,
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const paramDef = {
|
2024-02-21 18:59:31 +00:00
|
|
|
type: 'object',
|
2023-10-14 01:59:09 +02:00
|
|
|
properties: {
|
2024-02-21 18:59:31 +00:00
|
|
|
forceUpdate: { type: 'boolean', default: false },
|
2024-10-02 17:20:30 +02:00
|
|
|
instance: { type: 'boolean', default: false },
|
2023-10-14 01:59:09 +02:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.redis) private redisClient: Redis.Redis,
|
2024-10-02 17:20:30 +02:00
|
|
|
private metaService: MetaService,
|
2023-10-14 01:59:09 +02:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2024-09-15 20:04:29 +02:00
|
|
|
let totalSponsors;
|
2024-10-02 18:27:11 +02:00
|
|
|
const cachedSponsors = await this.redisClient.get('sponsors');
|
|
|
|
const cachedInstanceSponsors = await this.redisClient.get('instanceSponsors');
|
2024-09-15 20:04:29 +02:00
|
|
|
|
2024-10-02 17:20:30 +02:00
|
|
|
if (!ps.forceUpdate && !ps.instance && cachedSponsors) {
|
2024-09-15 20:04:29 +02:00
|
|
|
totalSponsors = JSON.parse(cachedSponsors);
|
2024-10-02 18:27:11 +02:00
|
|
|
} else if (ps.instance && !ps.forceUpdate && cachedInstanceSponsors) {
|
|
|
|
totalSponsors = JSON.parse(cachedInstanceSponsors);
|
2024-10-02 17:20:30 +02:00
|
|
|
} else if (!ps.instance) {
|
2024-02-21 18:59:31 +00:00
|
|
|
try {
|
2024-09-15 20:04:29 +02:00
|
|
|
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());
|
|
|
|
|
|
|
|
// Merge both together into one array and make sure it only has Active subscriptions
|
2024-09-16 19:02:06 +02:00
|
|
|
const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive === true);
|
2024-09-15 20:04:29 +02:00
|
|
|
|
|
|
|
// Remove possible duplicates
|
|
|
|
totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
|
2023-10-14 01:59:09 +02:00
|
|
|
|
2024-09-15 20:04:29 +02:00
|
|
|
await this.redisClient.set('sponsors', JSON.stringify(totalSponsors), 'EX', 3600);
|
2024-02-21 18:59:31 +00:00
|
|
|
} catch (error) {
|
2024-09-15 20:04:29 +02:00
|
|
|
totalSponsors = [];
|
2024-02-21 18:59:31 +00:00
|
|
|
}
|
2024-10-02 17:20:30 +02:00
|
|
|
} 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
|
2024-10-02 18:25:06 +02:00
|
|
|
const allSponsors = [...backers].filter(sponsor => sponsor.isActive === true && sponsor.role === 'BACKER' && sponsor.tier);
|
2024-10-02 17:20:30 +02:00
|
|
|
|
|
|
|
// Remove possible duplicates
|
|
|
|
totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
|
2024-10-02 18:27:11 +02:00
|
|
|
|
|
|
|
await this.redisClient.set('instanceSponsors', JSON.stringify(totalSponsors), 'EX', 3600);
|
2024-10-02 17:20:30 +02:00
|
|
|
} else {
|
|
|
|
totalSponsors = [];
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
totalSponsors = [];
|
|
|
|
}
|
2024-02-21 18:59:31 +00:00
|
|
|
}
|
2024-09-15 20:04:29 +02:00
|
|
|
return { sponsor_data: totalSponsors };
|
2024-02-21 18:59:31 +00:00
|
|
|
});
|
|
|
|
}
|
2023-10-14 01:59:09 +02:00
|
|
|
}
|