mirror of
https://activitypub.software/TransFem-org/Sharkey
synced 2024-11-21 21:45:11 +00:00
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:
commit
72a0f16b38
6 changed files with 165 additions and 28 deletions
|
@ -149,6 +149,7 @@ import { ApQuestionService } from './activitypub/models/ApQuestionService.js';
|
|||
import { QueueModule } from './QueueModule.js';
|
||||
import { QueueService } from './QueueService.js';
|
||||
import { LoggerService } from './LoggerService.js';
|
||||
import { SponsorsService } from './SponsorsService.js';
|
||||
import type { Provider } from '@nestjs/common';
|
||||
|
||||
//#region 文字列ベースでのinjection用(循環参照対応のため)
|
||||
|
@ -295,6 +296,8 @@ const $ApPersonService: Provider = { provide: 'ApPersonService', useExisting: Ap
|
|||
const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting: ApQuestionService };
|
||||
//#endregion
|
||||
|
||||
const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: SponsorsService };
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
QueueModule,
|
||||
|
@ -443,6 +446,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
|||
ApQuestionService,
|
||||
QueueService,
|
||||
|
||||
SponsorsService,
|
||||
|
||||
//#region 文字列ベースでのinjection用(循環参照対応のため)
|
||||
$LoggerService,
|
||||
$AbuseReportService,
|
||||
|
@ -586,6 +591,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
|||
$ApPersonService,
|
||||
$ApQuestionService,
|
||||
//#endregion
|
||||
|
||||
$SponsorsService,
|
||||
],
|
||||
exports: [
|
||||
QueueModule,
|
||||
|
@ -731,6 +738,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
|||
ApQuestionService,
|
||||
QueueService,
|
||||
|
||||
SponsorsService,
|
||||
|
||||
//#region 文字列ベースでのinjection用(循環参照対応のため)
|
||||
$LoggerService,
|
||||
$AbuseReportService,
|
||||
|
@ -873,6 +882,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
|||
$ApPersonService,
|
||||
$ApQuestionService,
|
||||
//#endregion
|
||||
|
||||
$SponsorsService,
|
||||
],
|
||||
})
|
||||
export class CoreModule { }
|
||||
|
|
88
packages/backend/src/core/SponsorsService.ts
Normal file
88
packages/backend/src/core/SponsorsService.ts
Normal 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();
|
||||
}
|
||||
}
|
|
@ -3,14 +3,13 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import * as Redis from 'ioredis';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { SponsorsService } from '@/core/SponsorsService.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['meta'],
|
||||
description: 'Get Sharkey Sponsors',
|
||||
description: 'Get Sharkey Sponsors or Instance Sponsors',
|
||||
|
||||
requireCredential: false,
|
||||
requireCredentialPrivateMode: false,
|
||||
|
@ -20,6 +19,7 @@ export const paramDef = {
|
|||
type: 'object',
|
||||
properties: {
|
||||
forceUpdate: { type: 'boolean', default: false },
|
||||
instance: { type: 'boolean', default: false },
|
||||
},
|
||||
required: [],
|
||||
} as const;
|
||||
|
@ -27,31 +27,14 @@ export const paramDef = {
|
|||
@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,
|
||||
private sponsorsService: SponsorsService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
let totalSponsors;
|
||||
const cachedSponsors = await this.redisClient.get('sponsors');
|
||||
|
||||
if (!ps.forceUpdate && cachedSponsors) {
|
||||
totalSponsors = JSON.parse(cachedSponsors);
|
||||
if (ps.instance) {
|
||||
return { sponsor_data: await this.sponsorsService.instanceSponsors(ps.forceUpdate) };
|
||||
} else {
|
||||
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
|
||||
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: await this.sponsorsService.sharkeySponsors(ps.forceUpdate) };
|
||||
}
|
||||
return { sponsor_data: totalSponsors };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,6 +116,22 @@ SPDX-License-Identifier: AGPL-3.0-only
|
|||
</FormSection>
|
||||
</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>
|
||||
<template #label>Well-known resources</template>
|
||||
<div class="_gaps_s">
|
||||
|
@ -130,6 +146,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
|||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref } from 'vue';
|
||||
import sanitizeHtml from '@/scripts/sanitize-html.js';
|
||||
import { host, version } from '@/config.js';
|
||||
import { i18n } from '@/i18n.js';
|
||||
|
@ -144,7 +161,10 @@ import MkFolder from '@/components/MkFolder.vue';
|
|||
import MkKeyValue from '@/components/MkKeyValue.vue';
|
||||
import MkLink from '@/components/MkLink.vue';
|
||||
|
||||
const sponsors = ref([]);
|
||||
|
||||
const initStats = () => misskeyApi('stats', {});
|
||||
await misskeyApi('sponsors', { instance: true }).then((res) => sponsors.value.push(res.sponsor_data));
|
||||
</script>
|
||||
|
||||
<style lang="scss" module>
|
||||
|
@ -207,4 +227,37 @@ const initStats = () => misskeyApi('stats', {});
|
|||
.ruleText {
|
||||
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>
|
||||
|
|
|
@ -4269,7 +4269,7 @@ declare module '../api.js' {
|
|||
): Promise<SwitchCaseResponseType<E, P>>;
|
||||
|
||||
/**
|
||||
* Get Sharkey Sponsors
|
||||
* Get Sharkey Sponsors or Instance Sponsors
|
||||
*
|
||||
* **Credential required**: *No*
|
||||
*/
|
||||
|
|
|
@ -3689,7 +3689,7 @@ export type paths = {
|
|||
'/sponsors': {
|
||||
/**
|
||||
* sponsors
|
||||
* @description Get Sharkey Sponsors
|
||||
* @description Get Sharkey Sponsors or Instance Sponsors
|
||||
*
|
||||
* **Credential required**: *No*
|
||||
*/
|
||||
|
@ -28079,7 +28079,7 @@ export type operations = {
|
|||
};
|
||||
/**
|
||||
* sponsors
|
||||
* @description Get Sharkey Sponsors
|
||||
* @description Get Sharkey Sponsors or Instance Sponsors
|
||||
*
|
||||
* **Credential required**: *No*
|
||||
*/
|
||||
|
@ -28089,6 +28089,8 @@ export type operations = {
|
|||
'application/json': {
|
||||
/** @default false */
|
||||
forceUpdate?: boolean;
|
||||
/** @default false */
|
||||
instance?: boolean;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue