Skip to content

Commit e89ab7e

Browse files
authored
Merge pull request #226 from bettaku/override-robot-txt
feat: adding robots.txt override via admin control panel
2 parents 18057c1 + 8da55ee commit e89ab7e

File tree

12 files changed

+83
-1
lines changed

12 files changed

+83
-1
lines changed

locales/en-US.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,8 @@ trustedLinkUrlPatterns: "Link to external site warning exclusion list"
14721472
trustedLinkUrlPatternsDescription: "Separate with spaces for an AND condition or with line breaks for an OR condition. Using surrounding keywords with slashes will turn them into a regular expression. If you write only the domain name, it will be a backward match."
14731473
open: "Open"
14741474
keyboardShortcuts: "Keyboard shortcuts"
1475+
overrideRobotsTxt: "Override robots.txt"
1476+
overrideRobotsTxtDescription: "Override the default robots.txt. Maximum 2048 characters."
14751477
_nsfwOpenBehavior:
14761478
click: "Click to open"
14771479
doubleClick: "Double click to open"

locales/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -5951,6 +5951,14 @@ export interface Locale extends ILocale {
59515951
* キーボードショートカット
59525952
*/
59535953
"keyboardShortcuts": string;
5954+
/**
5955+
* robots.txtの上書き
5956+
*/
5957+
"overrideRobotsTxt": string;
5958+
/**
5959+
* robots.txtの内容を上書きします。最大2048文字です。
5960+
*/
5961+
"overrideRobotsTxtDescription": string;
59545962
"_nsfwOpenBehavior": {
59555963
/**
59565964
* タップして開く

locales/ja-JP.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,8 @@ trustedLinkUrlPatterns: "外部サイトへのリンク警告 除外リスト"
14801480
trustedLinkUrlPatternsDescription: "スペースで区切るとAND指定になり、改行で区切るとOR指定になります。スラッシュで囲むと正規表現になります。ドメイン名だけ書くと後方一致になります。"
14811481
open: "開く"
14821482
keyboardShortcuts: "キーボードショートカット"
1483+
overrideRobotsTxt: "robots.txtの上書き"
1484+
overrideRobotsTxtDescription: "robots.txtの内容を上書きします。最大2048文字です。"
14831485

14841486
_nsfwOpenBehavior:
14851487
click: "タップして開く"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-FileCopyrightText: marie and other Sharkey contributors
3+
* SPDX-License-Identifier: AGPL-3.0-only
4+
*/
5+
6+
export class RobotsTxt1738098171990 {
7+
name = 'RobotsTxt1738098171990'
8+
9+
async up(queryRunner) {
10+
await queryRunner.query(`ALTER TABLE "meta" ADD "robotsTxt" character varying(2048)`);
11+
}
12+
13+
async down(queryRunner) {
14+
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "robotsTxt"`);
15+
}
16+
}

packages/backend/src/core/entities/MetaEntityService.ts

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class MetaEntityService {
9797
mcaptchaSiteKey: instance.mcaptchaSitekey,
9898
mcaptchaInstanceUrl: instance.mcaptchaInstanceUrl,
9999
enableRecaptcha: instance.enableRecaptcha,
100+
robotsTxt: instance.robotsTxt,
100101
recaptchaSiteKey: instance.recaptchaSiteKey,
101102
enableTurnstile: instance.enableTurnstile,
102103
turnstileSiteKey: instance.turnstileSiteKey,

packages/backend/src/models/Meta.ts

+5
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ export class MiMeta {
659659
})
660660
public enableIdenticonGeneration: boolean;
661661

662+
@Column('varchar', {
663+
length: 2048, nullable: true,
664+
})
665+
public robotsTxt: string | null;
666+
662667
@Column('jsonb', {
663668
default: { },
664669
})

packages/backend/src/models/json-schema/meta.ts

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ export const packedMetaLiteSchema = {
119119
type: 'string',
120120
optional: false, nullable: true,
121121
},
122+
robotsTxt: {
123+
type: 'string',
124+
optional: false, nullable: true,
125+
},
122126
enableTestcaptcha: {
123127
type: 'boolean',
124128
optional: false, nullable: false,

packages/backend/src/server/api/endpoints/admin/meta.ts

+5
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ export const meta = {
396396
type: 'boolean',
397397
optional: false, nullable: false,
398398
},
399+
robotsTxt: {
400+
type: 'string',
401+
optional: false, nullable: true,
402+
},
399403
enableIdenticonGeneration: {
400404
type: 'boolean',
401405
optional: false, nullable: false,
@@ -774,6 +778,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
774778
enableChartsForFederatedInstances: instance.enableChartsForFederatedInstances,
775779
enableStatsForFederatedInstances: instance.enableStatsForFederatedInstances,
776780
enableServerMachineStats: instance.enableServerMachineStats,
781+
robotsTxt: instance.robotsTxt,
777782
enableIdenticonGeneration: instance.enableIdenticonGeneration,
778783
bannedEmailDomains: instance.bannedEmailDomains,
779784
policies: { ...DEFAULT_POLICIES, ...instance.policies },

packages/backend/src/server/api/endpoints/admin/update-meta.ts

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export const paramDef = {
159159
enableChartsForFederatedInstances: { type: 'boolean' },
160160
enableStatsForFederatedInstances: { type: 'boolean' },
161161
enableServerMachineStats: { type: 'boolean' },
162+
robotsTxt: { type: 'string', nullable: true },
162163
enableIdenticonGeneration: { type: 'boolean' },
163164
serverRules: { type: 'array', items: { type: 'string' } },
164165
bannedEmailDomains: { type: 'array', items: { type: 'string' } },
@@ -709,6 +710,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
709710
set.enableServerMachineStats = ps.enableServerMachineStats;
710711
}
711712

713+
if (ps.robotsTxt !== undefined) {
714+
set.robotsTxt = ps.robotsTxt;
715+
}
716+
712717
if (ps.enableIdenticonGeneration !== undefined) {
713718
set.enableIdenticonGeneration = ps.enableIdenticonGeneration;
714719
}

packages/backend/src/server/web/ClientServerService.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,14 @@ export class ClientServerService {
471471
});
472472

473473
fastify.get('/robots.txt', async (request, reply) => {
474-
return await reply.sendFile('/robots.txt', staticAssets);
474+
if (this.meta.robotsTxt) {
475+
let content = '';
476+
content += this.meta.robotsTxt;
477+
reply.header('Content-Type', 'text/plain');
478+
return await reply.send(content);
479+
} else {
480+
return await reply.sendFile('/robots.txt', staticAssets);
481+
}
475482
});
476483

477484
// OpenSearch XML

packages/cherrypick-js/src/autogen/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5249,6 +5249,7 @@ export type components = {
52495249
recaptchaSiteKey: string | null;
52505250
enableTurnstile: boolean;
52515251
turnstileSiteKey: string | null;
5252+
robotsTxt: string | null;
52525253
enableTestcaptcha: boolean;
52535254
swPublickey: string | null;
52545255
/** @default /assets/ai.png */
@@ -9036,6 +9037,7 @@ export type operations = {
90369037
enableChartsForFederatedInstances: boolean;
90379038
enableStatsForFederatedInstances: boolean;
90389039
enableServerMachineStats: boolean;
9040+
robotsTxt: string | null;
90399041
enableIdenticonGeneration: boolean;
90409042
manifestJsonOverride: string;
90419043
policies: Record<string, never>;
@@ -11537,6 +11539,7 @@ export type operations = {
1153711539
enableChartsForFederatedInstances?: boolean;
1153811540
enableStatsForFederatedInstances?: boolean;
1153911541
enableServerMachineStats?: boolean;
11542+
robotsTxt?: string | null;
1154011543
enableIdenticonGeneration?: boolean;
1154111544
serverRules?: string[];
1154211545
bannedEmailDomains?: string[];

packages/frontend/src/pages/admin/settings.vue

+24
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,21 @@ SPDX-License-Identifier: AGPL-3.0-only
271271
</div>
272272
</MkFolder>
273273

274+
<MkFolder>
275+
<template #icon><i class="ti ti-adjustments"></i></template>
276+
<template #label>{{ i18n.ts.otherSettings }}</template>
277+
<template v-if="otherSettingsForm.modified.value" #footer>
278+
<MkFormFooter :form="otherSettingsForm"/>
279+
</template>
280+
281+
<div class="_gaps_m">
282+
<MkTextarea v-model="otherSettingsForm.state.robotTxt">
283+
<template #label>{{ i18n.ts.overrideRobotsTxt }}<span v-if="otherSettingsForm.modifiedStates.robotTxt" class="_modified">{{ i18n.ts.modified }}</span></template>
284+
<template #caption>{{ i18n.ts.overrideRobotsTxtDescription }}</template>
285+
</MkTextarea>
286+
</div>
287+
</MkFolder>
288+
274289
<MkFolder>
275290
<template #icon><i class="ti ti-file-search"></i></template>
276291
<template #label>{{ i18n.ts.search }}</template>
@@ -419,6 +434,15 @@ const emailToReceiveAbuseReportForm = useForm({
419434
fetchInstance(true);
420435
});
421436

437+
const otherSettingsForm = useForm({
438+
robotTxt: meta.robotsTxt,
439+
}, async (state) => {
440+
await os.apiWithDialog('admin/update-meta', {
441+
robotsTxt: state.robotTxt,
442+
});
443+
fetchInstance(true);
444+
});
445+
422446
function chooseProxyAccount() {
423447
os.selectUser({ localOnly: true }).then(user => {
424448
proxyAccount.value = user;

0 commit comments

Comments
 (0)