Skip to content

Commit

Permalink
リファクタ (#26)
Browse files Browse the repository at this point in the history
* refactor: Apply RSAPE

* refactor: Move procs into run

* refactor: Rename MockDB into MockUserDB

* refactor: Tidy up mock classes

* refactor: Move messageHandler into procs

* refactor: Rename notify modules

* fix: Apply renaming runner

* lint: Remove disable-eslint

* fix: Fix typo

* refactor: Extract to local variable

* fix: Use console.dir

* fix: Refactor omit
  • Loading branch information
MikuroXina committed Aug 2, 2021
1 parent 9d7fddd commit e5c480a
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 89 deletions.
52 changes: 0 additions & 52 deletions exe/bot.ts

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"dev:bot": "ts-node ./exe/bot.ts",
"build:bot": "esbuild --outfile=dist/bundle.js --sourcemap --bundle exe/bot.ts --platform=node --target=es2019 --external:discord.js --external:node-fetch",
"dev:bot": "ts-node src/bot/run/release.ts",
"build:bot": "esbuild --outfile=dist/bundle.js --sourcemap --bundle src/bot/run/release.ts --platform=node --target=es2019 --external:discord.js --external:node-fetch",
"start:bot": "node dist/bundle.js",
"dev:web": "next ./src/web",
"build:web": "next build ./src/web",
Expand Down
3 changes: 2 additions & 1 deletion src/bot/exp/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export const omitBody = (body: string): string => {
if (body.length < bodyWidth) {
return body;
}
return `${body.slice(0, bodyWidth)}...`;
const omitted = body.slice(0, bodyWidth);
return `${omitted}...`;
};
2 changes: 1 addition & 1 deletion src/bot/play/bring/branch.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageEmbed } from "discord.js";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { analectaForTest } from "../../skin/test-analecta";
import { bringBranch } from "./branch";
import { colorFromState } from "../../exp/state-color";
Expand Down
2 changes: 1 addition & 1 deletion src/bot/play/bring/issue.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageEmbed } from "discord.js";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { analectaForTest } from "../../skin/test-analecta";
import { bringIssue } from "./issue";
import { colorFromState } from "../../exp/state-color";
Expand Down
2 changes: 1 addition & 1 deletion src/bot/play/bring/pr.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageEmbed } from "discord.js";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { analectaForTest } from "../../skin/test-analecta";
import { bringPR } from "./pr";
import { colorFromState } from "../../exp/state-color";
Expand Down
2 changes: 1 addition & 1 deletion src/bot/play/bring/repo.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageEmbed } from "discord.js";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { analectaForTest } from "../../skin/test-analecta";
import { bringRepo } from "./repo";

Expand Down
2 changes: 1 addition & 1 deletion src/bot/play/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MockMessage } from "../skin/mock-message";
import { MockMessage } from "../skin/mock/message";
import { analectaForTest } from "../skin/test-analecta";
import { error } from "./error";

Expand Down
2 changes: 1 addition & 1 deletion src/bot/play/flavor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MockMessage } from "../skin/mock-message";
import { MockMessage } from "../skin/mock/message";
import { analectaForTest } from "../skin/test-analecta";
import { flavor } from "./flavor";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { DiscordId } from "../../exp/discord-id";
import { GitHubUser } from "../../exp/github-user";
import { MockDB } from "../../skin/mock-db";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { MockUserDB } from "../../skin/mock/user-db";
import { NotificationId } from "../../exp/github-notification";
import { analectaForTest } from "../../skin/test-analecta";
import { markAsRead } from "./mark-as-read";

test("mark a notification as read", async () => {
const analecta = await analectaForTest();
const db = new MockDB({
const db = new MockUserDB({
userName: "Alice",
notificationToken: "TEST_TOKEN",
currentNotificationIds: ["0123456789" as NotificationId],
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { MockDB, placeholder } from "../../skin/mock-db";
import { MockUserDB, placeholder } from "../../skin/mock/user-db";
import { DiscordId } from "../../exp/discord-id";
import { GitHubUser } from "../../exp/github-user";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { NotificationId } from "../../exp/github-notification";
import { analectaForTest } from "../../skin/test-analecta";
import { subscribeNotification } from "./subscribe-notification";
import { subscribeNotification } from "./subscribe";

test("subscribe a member", async () => {
const analecta = await analectaForTest();
const db = new MockDB({
const db = new MockUserDB({
userName: "Alice",
notificationToken: "TEST_TOKEN",
currentNotificationIds: [] as NotificationId[],
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MockDB, placeholder } from "../../skin/mock-db";
import { MockUserDB, placeholder } from "../../skin/mock/user-db";
import { DiscordId } from "../../exp/discord-id";
import { MockMessage } from "../../skin/mock-message";
import { MockMessage } from "../../skin/mock/message";
import { analectaForTest } from "../../skin/test-analecta";
import { unsubNotification } from "./unsubscribe-notification";
import { unsubNotification } from "./unsubscribe";

test("subscribe a member", async () => {
const analecta = await analectaForTest();
const db = new MockDB();
const db = new MockUserDB();
const unregisterDone = new Promise<void>((resolve) => {
db.onUnregister.on(placeholder, (id) => {
expect(id).toStrictEqual("alice_discord");
Expand Down
File renamed without changes.
24 changes: 20 additions & 4 deletions src/bot/skin/procs.ts → src/bot/run/procs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { CommandProcessor, connectProcessors } from "../abst/connector";
import type { AllApi } from "../abst/api";
import type { Analecta } from "../exp/analecta";
import { DiscordMessage } from "../skin/discord-message";
import type { Message } from "discord.js";
import type { UserDatabase } from "../abst/user-database";
import { bringBranch } from "../play/bring/branch";
import { bringIssue } from "../play/bring/issue";
import { bringPR } from "../play/bring/pr";
import { bringRepo } from "../play/bring/repo";
import { error } from "../play/error";
import { flavor } from "../play/flavor";
import { markAsRead } from "../play/subscribe/mark-as-read";
// eslint-disable-next-line max-len
import { subscribeNotification } from "../play/subscribe/subscribe-notification";
import { unsubNotification } from "../play/subscribe/unsubscribe-notification";
import { markAsRead } from "../play/notify/mark-as-read";
import { subscribeNotification } from "../play/notify/subscribe";
import { unsubNotification } from "../play/notify/unsubscribe";

export const procs = (
analecta: Analecta,
Expand All @@ -32,3 +33,18 @@ export const procs = (
markAsRead(db, query),
error,
]);

export const messageHandler =
(analecta: Analecta, builtProcs: CommandProcessor) =>
async (msg: Message): Promise<void> => {
if (msg.author.bot) {
return;
}
if (msg.content.startsWith("/gh?")) {
const dm = await msg.author.createDM();
dm.send(analecta.HelpMessage);
return;
}
const discordMessage = new DiscordMessage(msg);
await builtProcs(analecta, discordMessage);
};
32 changes: 32 additions & 0 deletions src/bot/run/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { messageHandler, procs } from "./procs";
import { Client } from "discord.js";
import { FaunaDB } from "../skin/fauna-db";
import { GitHubApi } from "../skin/github-api";
import { SubscriptionNotifier } from "../skin/notifier";
import { TomlLoader } from "../skin/toml-loader";
import dotenv from "dotenv";

dotenv.config();

(async () => {
const loader = new TomlLoader(
process.env.TOML_PATH || "./analecta/laffey.toml",
);
const db = new FaunaDB(process.env.FAUNA_SECRET || "UNSET");
const analecta = await loader.load();

const client = new Client();
const notifier = new SubscriptionNotifier(analecta, client.users, db);
db.onUpdate(notifier);
const query = new GitHubApi();

const builtProcs = procs(analecta, db, query);

client.on("ready", () => {
console.log("I got ready.");
});

client.on("message", messageHandler(analecta, builtProcs));

client.login(process.env.DISCORD_TOKEN);
})().catch((err) => console.error(err));
6 changes: 3 additions & 3 deletions src/bot/skin/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class GitHubApi
const repoInfoApiUrl = `${apiRoot}/repos/${owner}/${repoName}`;
const infoRes: unknown = await (await fetch(repoInfoApiUrl)).json();
if (checkNotFound(infoRes)) {
throw new Error("not found the repositpory");
throw new Error("not found the repository");
}
return infoRes as Repository;
}
Expand All @@ -32,7 +32,7 @@ export class GitHubApi
const apiUrl = `${apiRoot}/repos/${owner}/${repoName}/issues`;
const res: unknown = await (await fetch(apiUrl)).json();
if (checkNotFound(res)) {
throw new Error("not found the repositpory");
throw new Error("not found the repository");
}
return res as PartialIssue[];
}
Expand All @@ -57,7 +57,7 @@ export class GitHubApi
const apiUrl = `${apiRoot}/repos/${owner}/${repoName}/pulls`;
const res: unknown = await (await fetch(apiUrl)).json();
if (checkNotFound(res)) {
throw new Error("not found the repositpory");
throw new Error("not found the repository");
}
return res as PartialPullRequest[];
}
Expand Down
7 changes: 4 additions & 3 deletions src/bot/skin/github-notification-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ export const notificationQuery: Query = {
userName,
notificationToken,
}: GitHubUser): Promise<GitHubNotifications> {
const base64 = Buffer.from(`${userName}:${notificationToken}`).toString(
"base64",
);
const rawRes = await fetch("https://api.github.com/notifications", {
headers: {
Authorization: `Basic ${Buffer.from(
`${userName}:${notificationToken}`,
).toString("base64")}`,
Authorization: `Basic ${base64}`,
},
});
if (!rawRes.ok) {
Expand Down
4 changes: 2 additions & 2 deletions src/bot/skin/mock-message.ts → src/bot/skin/mock/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DiscordId } from "../exp/discord-id";
import type { DiscordId } from "../../exp/discord-id";
import { EventEmitter } from "events";
import type { Message } from "../abst/message";
import type { Message } from "../../abst/message";
import type { MessageEmbed } from "discord.js";

export class MockMessage implements Message {
Expand Down
8 changes: 4 additions & 4 deletions src/bot/skin/mock-db.ts → src/bot/skin/mock/user-db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { DiscordId } from "../exp/discord-id";
import type { DiscordId } from "../../exp/discord-id";
import { EventEmitter } from "events";
import type { GitHubUser } from "../exp/github-user";
import type { UserDatabase } from "../abst/user-database";
import type { GitHubUser } from "../../exp/github-user";
import type { UserDatabase } from "../../abst/user-database";

export const placeholder = Symbol("placeholder for MockDB");

export class MockDB implements UserDatabase {
export class MockUserDB implements UserDatabase {
constructor(private readonly passed?: GitHubUser) {}

readonly onRegister = new EventEmitter();
Expand Down
2 changes: 1 addition & 1 deletion src/bot/skin/toml-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TomlLoader implements SayingLoader {

const analecta: unknown = toml.parse(tomlStr);
if (!validateAnalecta(analecta)) {
console.log({ analecta });
console.dir(analecta);
throw new Error("invalid toml");
}
return analecta;
Expand Down

0 comments on commit e5c480a

Please sign in to comment.