From b814ebcdfb0c1189ba7c61f6d20d6e9ca7e91342 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Wed, 22 Nov 2023 23:23:46 +0100 Subject: [PATCH] Add setting for an autofollowed account on signup --- .../1700686908916-add-autofollowed-account.ts | 17 +++++++++++++++++ packages/backend/src/models/entities/meta.ts | 6 ++++++ .../backend/src/server/api/common/signup.ts | 16 ++++++++++++++++ .../src/server/api/endpoints/admin/meta.ts | 6 ++++++ .../server/api/endpoints/admin/update-meta.ts | 11 +++++++++++ packages/client/src/pages/admin/settings.vue | 19 +++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 packages/backend/src/migration/1700686908916-add-autofollowed-account.ts diff --git a/packages/backend/src/migration/1700686908916-add-autofollowed-account.ts b/packages/backend/src/migration/1700686908916-add-autofollowed-account.ts new file mode 100644 index 000000000..24b7659f7 --- /dev/null +++ b/packages/backend/src/migration/1700686908916-add-autofollowed-account.ts @@ -0,0 +1,17 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddAutofollowedAccount1700686908916 implements MigrationInterface { + name = "AddAutofollowedAccount1700686908916"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "meta" ADD "autofollowedAccount" character varying(128)`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "meta" DROP COLUMN "autofollowedAccount"`, + ); + } +} diff --git a/packages/backend/src/models/entities/meta.ts b/packages/backend/src/models/entities/meta.ts index 44152ba64..23cd629e3 100644 --- a/packages/backend/src/models/entities/meta.ts +++ b/packages/backend/src/models/entities/meta.ts @@ -518,4 +518,10 @@ export class Meta { nullable: true, }) public donationLink: string | null; + + @Column("varchar", { + length: 64, + nullable: true, + }) + public autofollowedAccount: string | null; } diff --git a/packages/backend/src/server/api/common/signup.ts b/packages/backend/src/server/api/common/signup.ts index 19d4f17aa..b41846062 100644 --- a/packages/backend/src/server/api/common/signup.ts +++ b/packages/backend/src/server/api/common/signup.ts @@ -12,6 +12,8 @@ import { UsedUsername } from "@/models/entities/used-username.js"; import { db } from "@/db/postgre.js"; import config from "@/config/index.js"; import { hashPassword } from "@/misc/password.js"; +import { fetchMeta } from "@/misc/fetch-meta.js"; +import follow from "@/services/following/create.js"; export async function signup(opts: { username: User["username"]; @@ -133,6 +135,20 @@ export async function signup(opts: { const account = await Users.findOneByOrFail({ id: user.id }); + const meta = await fetchMeta(); + + // If an autofollow account exists, follow it + if (meta.autofollowedAccount) { + const autofollowedAccount = await Users.findOneByOrFail({ + usernameLower: meta.autofollowedAccount.toLowerCase(), + host: IsNull(), + }); + + if (autofollowedAccount) { + await follow(account, autofollowedAccount) + } + } + usersChart.update(account, true); return { account, secret }; } diff --git a/packages/backend/src/server/api/endpoints/admin/meta.ts b/packages/backend/src/server/api/endpoints/admin/meta.ts index 8e193380c..c3177fc1c 100644 --- a/packages/backend/src/server/api/endpoints/admin/meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/meta.ts @@ -430,6 +430,11 @@ export const meta = { optional: true, nullable: true, }, + autofollowedAccount: { + type: "string", + optional: true, + nullable: true, + }, }, }, } as const; @@ -536,5 +541,6 @@ export default define(meta, paramDef, async (ps, me) => { enableServerMachineStats: instance.enableServerMachineStats, enableIdenticonGeneration: instance.enableIdenticonGeneration, donationLink: instance.donationLink, + autofollowedAccount: instance.autofollowedAccount, }; }); diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index e4f9b51a7..7f0c279ce 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -3,6 +3,8 @@ import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { db } from "@/db/postgre.js"; import define from "../../define.js"; import { Metas } from "@/models/index.js"; +import { Users } from "@/models/index.js"; + export const meta = { tags: ["admin"], @@ -166,6 +168,7 @@ export const paramDef = { enableServerMachineStats: { type: "boolean" }, enableIdenticonGeneration: { type: "boolean" }, donationLink: { type: "string", nullable: true }, + autofollowedAccount: { type: "string", nullable: true }, }, required: [], } as const; @@ -547,6 +550,14 @@ export default define(meta, paramDef, async (ps, me) => { } } + if (ps.autofollowedAccount !== undefined) { + // Verify account exists and is a local account + const user = await Users.findOneBy({ username: ps.autofollowedAccount, host: null }); + if (user) { + set.autofollowedAccount = user.username; + } + } + const meta = await Metas.findOne({ where: {}, order: { diff --git a/packages/client/src/pages/admin/settings.vue b/packages/client/src/pages/admin/settings.vue index 8875c2723..8062b6955 100644 --- a/packages/client/src/pages/admin/settings.vue +++ b/packages/client/src/pages/admin/settings.vue @@ -382,6 +382,22 @@ > + + + + + + + + + @@ -437,6 +453,7 @@ let defaultReaction: string = $ref(""); let defaultReactionCustom: string = $ref(""); let enableServerMachineStats: boolean = $ref(false); let enableIdenticonGeneration: boolean = $ref(false); +let autofollowedAccount: string | null = $ref(null); async function init() { const meta = await os.api("admin/meta"); @@ -478,6 +495,7 @@ async function init() { : meta.defaultReaction; enableServerMachineStats = meta.enableServerMachineStats; enableIdenticonGeneration = meta.enableIdenticonGeneration; + autofollowedAccount = meta.autofollowedAccount; } function save() { @@ -517,6 +535,7 @@ function save() { defaultReaction, enableServerMachineStats, enableIdenticonGeneration, + autofollowedAccount, }).then(() => { fetchInstance(); });