merge: Show instance sponsors if OC is set as donation url (!642)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/642

Approved-by: Hazelnoot <acomputerdog@gmail.com>
Approved-by: Julia <julia@insertdomain.name>
This commit is contained in:
Marie 2024-10-08 18:07:58 +00:00
commit 72a0f16b38
6 changed files with 165 additions and 28 deletions

View file

@ -149,6 +149,7 @@ import { ApQuestionService } from './activitypub/models/ApQuestionService.js';
import { QueueModule } from './QueueModule.js'; import { QueueModule } from './QueueModule.js';
import { QueueService } from './QueueService.js'; import { QueueService } from './QueueService.js';
import { LoggerService } from './LoggerService.js'; import { LoggerService } from './LoggerService.js';
import { SponsorsService } from './SponsorsService.js';
import type { Provider } from '@nestjs/common'; import type { Provider } from '@nestjs/common';
//#region 文字列ベースでのinjection用(循環参照対応のため) //#region 文字列ベースでのinjection用(循環参照対応のため)
@ -295,6 +296,8 @@ const $ApPersonService: Provider = { provide: 'ApPersonService', useExisting: Ap
const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting: ApQuestionService }; const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting: ApQuestionService };
//#endregion //#endregion
const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: SponsorsService };
@Module({ @Module({
imports: [ imports: [
QueueModule, QueueModule,
@ -443,6 +446,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApQuestionService, ApQuestionService,
QueueService, QueueService,
SponsorsService,
//#region 文字列ベースでのinjection用(循環参照対応のため) //#region 文字列ベースでのinjection用(循環参照対応のため)
$LoggerService, $LoggerService,
$AbuseReportService, $AbuseReportService,
@ -586,6 +591,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApPersonService, $ApPersonService,
$ApQuestionService, $ApQuestionService,
//#endregion //#endregion
$SponsorsService,
], ],
exports: [ exports: [
QueueModule, QueueModule,
@ -731,6 +738,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApQuestionService, ApQuestionService,
QueueService, QueueService,
SponsorsService,
//#region 文字列ベースでのinjection用(循環参照対応のため) //#region 文字列ベースでのinjection用(循環参照対応のため)
$LoggerService, $LoggerService,
$AbuseReportService, $AbuseReportService,
@ -873,6 +882,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApPersonService, $ApPersonService,
$ApQuestionService, $ApQuestionService,
//#endregion //#endregion
$SponsorsService,
], ],
}) })
export class CoreModule { } export class CoreModule { }

View file

@ -0,0 +1,88 @@
/*
* SPDX-FileCopyrightText: marie and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import * as Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
import { MetaService } from '@/core/MetaService.js';
import { RedisKVCache } from '@/misc/cache.js';
import { bindThis } from '@/decorators.js';
@Injectable()
export class SponsorsService implements OnApplicationShutdown {
private cache: RedisKVCache<void[]>;
constructor(
@Inject(DI.redis)
private redisClient: Redis.Redis,
private metaService: MetaService,
) {
this.cache = new RedisKVCache<void[]>(this.redisClient, 'sponsors', {
lifetime: 1000 * 60 * 60,
memoryCacheLifetime: 1000 * 60,
fetcher: (key) => {
if (key === 'instance') return this.fetchInstanceSponsors();
return this.fetchSharkeySponsors();
},
toRedisConverter: (value) => JSON.stringify(value),
fromRedisConverter: (value) => JSON.parse(value),
});
}
@bindThis
private async fetchInstanceSponsors() {
const meta = await this.metaService.fetch();
if (!(meta.donationUrl && meta.donationUrl.includes('opencollective.com'))) {
return [];
}
try {
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' && sponsor.tier);
// Remove possible duplicates
return [...new Map(allSponsors.map(v => [v.profile, v])).values()];
} catch (error) {
return [];
}
}
@bindThis
private async fetchSharkeySponsors() {
try {
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
const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive === true);
// Remove possible duplicates
return [...new Map(allSponsors.map(v => [v.profile, v])).values()];
} catch (error) {
return [];
}
}
@bindThis
public async instanceSponsors(forceUpdate: boolean) {
if (forceUpdate) this.cache.refresh('instance');
return this.cache.fetch('instance');
}
@bindThis
public async sharkeySponsors(forceUpdate: boolean) {
if (forceUpdate) this.cache.refresh('sharkey');
return this.cache.fetch('sharkey');
}
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.cache.dispose();
}
}

View file

@ -3,14 +3,13 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Inject, Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
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 { SponsorsService } from '@/core/SponsorsService.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 +19,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;
@ -27,31 +27,14 @@ export const paramDef = {
@Injectable() @Injectable()
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, private sponsorsService: SponsorsService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
let totalSponsors; if (ps.instance) {
const cachedSponsors = await this.redisClient.get('sponsors'); return { sponsor_data: await this.sponsorsService.instanceSponsors(ps.forceUpdate) };
if (!ps.forceUpdate && cachedSponsors) {
totalSponsors = JSON.parse(cachedSponsors);
} else { } else {
try { return { sponsor_data: await this.sponsorsService.sharkeySponsors(ps.forceUpdate) };
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
const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive === true);
// Remove possible duplicates
totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
await this.redisClient.set('sponsors', JSON.stringify(totalSponsors), 'EX', 3600);
} catch (error) {
totalSponsors = [];
}
} }
return { sponsor_data: totalSponsors };
}); });
} }
} }

View file

@ -116,6 +116,22 @@ SPDX-License-Identifier: AGPL-3.0-only
</FormSection> </FormSection>
</FormSuspense> </FormSuspense>
<FormSection v-if="sponsors[0].length > 0">
<template #label>Our lovely Sponsors</template>
<div :class="$style.contributors">
<span
v-for="sponsor in sponsors[0]"
:key="sponsor"
style="margin-bottom: 0.5rem;"
>
<a :href="sponsor.website || sponsor.profile" target="_blank" :class="$style.contributor">
<img :src="sponsor.image || `https://ui-avatars.com/api/?background=0D8ABC&color=fff&name=${sponsor.name}`" :class="$style.contributorAvatar">
<span :class="$style.contributorUsername">{{ sponsor.name }}</span>
</a>
</span>
</div>
</FormSection>
<FormSection> <FormSection>
<template #label>Well-known resources</template> <template #label>Well-known resources</template>
<div class="_gaps_s"> <div class="_gaps_s">
@ -130,6 +146,7 @@ SPDX-License-Identifier: AGPL-3.0-only
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { ref } from 'vue';
import sanitizeHtml from '@/scripts/sanitize-html.js'; import sanitizeHtml from '@/scripts/sanitize-html.js';
import { host, version } from '@/config.js'; import { host, version } from '@/config.js';
import { i18n } from '@/i18n.js'; import { i18n } from '@/i18n.js';
@ -144,7 +161,10 @@ import MkFolder from '@/components/MkFolder.vue';
import MkKeyValue from '@/components/MkKeyValue.vue'; import MkKeyValue from '@/components/MkKeyValue.vue';
import MkLink from '@/components/MkLink.vue'; import MkLink from '@/components/MkLink.vue';
const sponsors = ref([]);
const initStats = () => misskeyApi('stats', {}); const initStats = () => misskeyApi('stats', {});
await misskeyApi('sponsors', { instance: true }).then((res) => sponsors.value.push(res.sponsor_data));
</script> </script>
<style lang="scss" module> <style lang="scss" module>
@ -207,4 +227,37 @@ const initStats = () => misskeyApi('stats', {});
.ruleText { .ruleText {
padding-top: 6px; padding-top: 6px;
} }
.contributors {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: 12px;
}
.contributor {
display: flex;
align-items: center;
padding: 12px;
background: var(--buttonBg);
border-radius: var(--radius-sm);
&:hover {
text-decoration: none;
background: var(--buttonHoverBg);
}
&.active {
color: var(--accent);
background: var(--buttonHoverBg);
}
}
.contributorAvatar {
width: 30px;
border-radius: var(--radius-full);
}
.contributorUsername {
margin-left: 12px;
}
</style> </style>

View file

@ -4269,7 +4269,7 @@ declare module '../api.js' {
): Promise<SwitchCaseResponseType<E, P>>; ): Promise<SwitchCaseResponseType<E, P>>;
/** /**
* Get Sharkey Sponsors * Get Sharkey Sponsors or Instance Sponsors
* *
* **Credential required**: *No* * **Credential required**: *No*
*/ */

View file

@ -3689,7 +3689,7 @@ export type paths = {
'/sponsors': { '/sponsors': {
/** /**
* sponsors * sponsors
* @description Get Sharkey Sponsors * @description Get Sharkey Sponsors or Instance Sponsors
* *
* **Credential required**: *No* * **Credential required**: *No*
*/ */
@ -28079,7 +28079,7 @@ export type operations = {
}; };
/** /**
* sponsors * sponsors
* @description Get Sharkey Sponsors * @description Get Sharkey Sponsors or Instance Sponsors
* *
* **Credential required**: *No* * **Credential required**: *No*
*/ */
@ -28089,6 +28089,8 @@ export type operations = {
'application/json': { 'application/json': {
/** @default false */ /** @default false */
forceUpdate?: boolean; forceUpdate?: boolean;
/** @default false */
instance?: boolean;
}; };
}; };
}; };