Skip to content

Commit

Permalink
add discord webhook setting
Browse files Browse the repository at this point in the history
  • Loading branch information
nenohi committed May 19, 2024
1 parent f4fbc22 commit b550bed
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 8 deletions.
2 changes: 2 additions & 0 deletions locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,8 @@ loading: "Loading"
surrender: "Cancel"
gameRetry: "Retry"
here: "here"
discordWebhookUrl: "DiscordWebhookURL"
discordWebhookUrlDescription: "send notifications to Discord by setting a Discord webhook URL"
_bubbleGame:
howToPlay: "How to play"
hold: "Hold"
Expand Down
8 changes: 8 additions & 0 deletions locales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5071,6 +5071,14 @@ export interface Locale extends ILocale {
* こちら
*/
"here": string;
/**
* DiscordWebhookURL
*/
"discordWebhookUrl": string;
/**
* DiscordのWebhookURLを設定すると、お知らせをDiscordに送信できます。
*/
"discordWebhookUrlDescription": string;
"_bubbleGame": {
/**
* 遊び方
Expand Down
2 changes: 2 additions & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,8 @@ reportComplete: "通報完了"
blockThisUser: "このユーザーをブロックする"
muteThisUser: "このユーザーをミュートする"
here: "こちら"
discordWebhookUrl: "DiscordWebhookURL"
discordWebhookUrlDescription: "DiscordのWebhookURLを設定すると、お知らせをDiscordに送信できます。"

_bubbleGame:
howToPlay: "遊び方"
Expand Down
16 changes: 16 additions & 0 deletions packages/backend/migration/1716137989170-notifytodiscord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

export class Notifytodiscord1716137989170 {
name = 'Notifytodiscord1716137989170'

async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "meta" ADD "discordWebhookUrl" varchar(1024) default null`);
}

async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "discordWebhookUrl" SET DEFAULT true`);
}
}
6 changes: 6 additions & 0 deletions packages/backend/src/models/Meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ export class MiMeta {
})
public wellKnownWebsites: string[];

@Column('varchar', {
length: 1024,
nullable: true,
})
public discordWebhookUrl: string | null;

@Column('varchar', {
length: 3072, array: true, default: '{}',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { AnnouncementService } from '@/core/AnnouncementService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
import { MetaService } from '@/core/MetaService.js';

export const meta = {
tags: ['admin'],
Expand Down Expand Up @@ -104,6 +106,8 @@ export const paramDef = {
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
private announcementService: AnnouncementService,
private httpRequestService: HttpRequestService,
private metaService: MetaService,
) {
super(meta, paramDef, async (ps, me) => {
const { raw, packed } = await this.announcementService.create({
Expand All @@ -120,7 +124,23 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
silence: ps.silence,
userId: ps.userId,
}, me);

const instance = await this.metaService.fetch(true);
try {
if (instance.discordWebhookUrl !== null && ps.userId === null) {
await this.httpRequestService.send(instance.discordWebhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: `${instance.name} Announcement`,
content: `${raw.title}\n${raw.text}`,
}),
});
}
} catch (e) {
// ignore
}
return packed;
});
}
Expand Down
5 changes: 5 additions & 0 deletions packages/backend/src/server/api/endpoints/admin/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ export const meta = {
type: 'boolean',
optional: false, nullable: false,
},
discordWebhookUrl: {
type: 'string',
optional: false, nullable: true,
},
defaultDarkTheme: {
type: 'string',
optional: false, nullable: true,
Expand Down Expand Up @@ -615,6 +619,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
objectStorageS3ForcePathStyle: instance.objectStorageS3ForcePathStyle,
deeplAuthKey: instance.deeplAuthKey,
deeplIsPro: instance.deeplIsPro,
discordWebhookUrl: instance.discordWebhookUrl,
enableIpLogging: instance.enableIpLogging,
enableActiveEmailValidation: instance.enableActiveEmailValidation,
enableVerifymailApi: instance.enableVerifymailApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const paramDef = {
},
deeplAuthKey: { type: 'string', nullable: true },
deeplIsPro: { type: 'boolean' },
discordWebhookUrl: { type: 'string', nullable: true },
enableEmail: { type: 'boolean' },
email: { type: 'string', nullable: true },
smtpSecure: { type: 'boolean' },
Expand Down Expand Up @@ -526,6 +527,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
}

if (ps.discordWebhookUrl !== undefined) {
if (ps.discordWebhookUrl === '') {
set.discordWebhookUrl = null;
} else {
set.discordWebhookUrl = ps.discordWebhookUrl;
}
}

if (ps.deeplIsPro !== undefined) {
set.deeplIsPro = ps.deeplIsPro;
}
Expand Down
38 changes: 31 additions & 7 deletions packages/frontend/src/pages/admin/external-services.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ SPDX-License-Identifier: AGPL-3.0-only
</MkSwitch>
</div>
</FormSection>
<FormSection>
<template #label>Discord</template>

<div class="_gaps_m">
<MkInput v-model="discordWebhookUrl">
<template #prefix><i class="ti ti-link"></i></template>
<template #label>Webhook URL</template>
</MkInput>
</div>
</formsection>
</FormSuspense>
</MkSpacer>
<template #footer>
Expand All @@ -47,22 +57,36 @@ import { fetchInstance } from '@/instance.js';
import { i18n } from '@/i18n.js';
import { definePageMetadata } from '@/scripts/page-metadata.js';
const deeplAuthKey = ref<string>('');
const deeplAuthKey = ref<string|null>('');
const deeplIsPro = ref<boolean>(false);
const discordWebhookUrl = ref<string|null>('');
async function init() {
const meta = await misskeyApi('admin/meta');
deeplAuthKey.value = meta.deeplAuthKey;
deeplIsPro.value = meta.deeplIsPro;
discordWebhookUrl.value = meta.discordWebhookUrl;
}
function save() {
os.apiWithDialog('admin/update-meta', {
deeplAuthKey: deeplAuthKey.value,
deeplIsPro: deeplIsPro.value,
}).then(() => {
fetchInstance(true);
});
const urlPattern = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name and extension
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?' + // port
'(\\/[-a-z\\d%_.~+]*)*' + // path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
if (discordWebhookUrl.value !== null && !urlPattern.test(discordWebhookUrl.value)) {
alert(i18n.ts.invalidValue);
} else {
os.apiWithDialog('admin/update-meta', {
deeplAuthKey: deeplAuthKey.value,
deeplIsPro: deeplIsPro.value,
discordWebhookUrl: discordWebhookUrl.value,
}).then(() => {
fetchInstance(true);
});
}
}
const headerActions = computed(() => []);
Expand Down
2 changes: 2 additions & 0 deletions packages/misskey-js/src/autogen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5241,6 +5241,7 @@ export type operations = {
backgroundImageUrl: string | null;
deeplAuthKey: string | null;
deeplIsPro: boolean;
discordWebhookUrl: string | null;
defaultDarkTheme: string | null;
defaultLightTheme: string | null;
description: string | null;
Expand Down Expand Up @@ -9803,6 +9804,7 @@ export type operations = {
langs?: string[];
deeplAuthKey?: string | null;
deeplIsPro?: boolean;
discordWebhookUrl?: string | null;

Check failure on line 9807 in packages/misskey-js/src/autogen/types.ts

View workflow job for this annotation

GitHub Actions / lint (misskey-js)

Mixed spaces and tabs
enableEmail?: boolean;
email?: string | null;
smtpSecure?: boolean;
Expand Down

0 comments on commit b550bed

Please sign in to comment.