Skip to content

Commit

Permalink
fix!: Refactor with RAS Architecture (#42)
Browse files Browse the repository at this point in the history
* Reform with RAS

* Fix tests

* Remove condition of not replying

* Fix incorrect handling

* Short catch callback

* Bump version

* Move SayingLoader

* Add Scheduler

* Move SubscriptionNotifier

* Refactor around notification system

* Fix tests

* Fix and Add kilAll

* Fix to kill
  • Loading branch information
MikuroXina authored Nov 18, 2021
1 parent 0782204 commit 9c28dae
Show file tree
Hide file tree
Showing 80 changed files with 1,497 additions and 1,443 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-undefined": "error",
"no-underscore-dangle": "error",
"no-unmodified-loop-condition": "error",
"no-unneeded-ternary": "error",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "github-secretary",
"version": "1.1.0",
"version": "1.2.0",
"main": "index.js",
"repository": "https://github.com/MikuroXina/github-secretary",
"author": "MikuroXina <ryosukadnak@gmail.com>",
"license": "Apache-2.0",
"private": true,
"scripts": {
"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",
"dev:bot": "ts-node src/bot/apps/release.ts",
"build:bot": "esbuild --outfile=dist/bundle.js --sourcemap --bundle src/bot/apps/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
15 changes: 0 additions & 15 deletions src/bot/abst/connector.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/bot/abst/github/branch.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/bot/abst/github/issue.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/bot/abst/github/pr.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/bot/abst/github/repo.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/bot/abst/message.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/bot/abst/reply-failure.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/bot/abst/user-database.ts

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MessageEmbed, Message as RawMessage } from "discord.js";
import type { DiscordId } from "../exp/discord-id";
import type { Message } from "../abst/message";
import type { EmbedMessage, Message } from "../model/message";
import type { DiscordId } from "../model/discord-id";
import { Message as RawMessage } from "discord.js";
import { intoMessageEmbed } from "./message-convert";

export class DiscordMessage implements Message {
constructor(private raw: RawMessage) {}
Expand All @@ -25,18 +26,18 @@ export class DiscordMessage implements Message {
await this.raw.reply(message);
}

async sendEmbed(embed: MessageEmbed): Promise<void> {
await this.raw.channel.send({ embeds: [embed] });
async sendEmbed(embed: EmbedMessage): Promise<void> {
const messageEmbed = intoMessageEmbed(embed);
await this.raw.channel.send({ embeds: [messageEmbed] });
}

panic(reason: unknown): never {
const yellow = 0xffc208;
this.sendEmbed(
new MessageEmbed()
.setColor(yellow)
.setTitle("エラー発生, リトライはされません")
.setDescription(`${reason}`),
);
this.sendEmbed({
color: yellow,
title: "エラー発生, リトライはされません",
description: `${reason}`,
});
throw reason;
}
}
56 changes: 56 additions & 0 deletions src/bot/adaptors/fauna-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable new-cap */

import fauna, { query as q } from "faunadb";
import type { DiscordId } from "../model/discord-id";
import type { GitHubUser } from "../model/github-user";
import type { NotificationId } from "../model/github-notification";
import type { SubscriberRepository } from "../services/notify";

export class FaunaDB implements SubscriberRepository {
private client: fauna.Client;

constructor(secret: string) {
this.client = new fauna.Client({ secret });
}

async updateNotifications(
id: DiscordId,
notificationIds: readonly NotificationId[],
): Promise<void> {
await this.client.query(
q.Update(q.Ref(q.Collection("users"), id), {
data: { currentNotificationIds: notificationIds },
}),
);
}

async register(id: DiscordId, user: GitHubUser): Promise<void> {
try {
await this.client.query(
q.Create(q.Ref(q.Collection("users"), id), { data: { ...user } }),
);
} catch (ignore) {
// Ignore
}
}

async unregister(id: DiscordId): Promise<boolean> {
try {
await this.client.query(q.Delete(q.Ref(q.Collection("users"), id)));
} catch (_err) {
return false;
}
return true;
}

async user(discordId: DiscordId): Promise<GitHubUser | null> {
try {
const { data } = (await this.client.query(
q.Get(q.Ref(q.Collection("users"), discordId)),
)) as { data: GitHubUser };
return data;
} catch (err) {
return null;
}
}
}
52 changes: 35 additions & 17 deletions src/bot/skin/github-api.ts → src/bot/adaptors/github-api.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {
Api,
BranchApi,
IssueApi,
NotificationApi,
PullApi,
UserApi,
} from "../abst/api";
import type { Branch, PartialBranch } from "../abst/github/branch";
import type { Issue, PartialIssue } from "../abst/github/issue";
import type { PartialPullRequest, PullRequest } from "../abst/github/pr";
import type { GitHubUser } from "../exp/github-user";
import type { NotificationId } from "../exp/github-notification";
import type { Repository } from "../abst/github/repo";
import type {
Branch,
Issue,
PartialBranch,
PartialIssue,
PartialPullRequest,
PullRequest,
Repository,
} from "../services/command/bring";
import type {
GitHubNotifications,
NotificationId,
} from "../model/github-notification";
import type { AllApi } from "../services/command/api";
import type { GitHubUser } from "../model/github-user";
import type { NotificationRepository } from "../services/notify";
import fetch from "node-fetch";

const apiRoot = "https://api.github.com";

export class GitHubApi
// eslint-disable-next-line prettier/prettier
implements Api, IssueApi, PullApi, BranchApi, NotificationApi, UserApi {
export class GitHubApi implements AllApi, NotificationRepository {
async fetchRepo(owner: string, repoName: string): Promise<Repository> {
const repoInfoApiUrl = `${apiRoot}/repos/${owner}/${repoName}`;
const infoRes: unknown = await (await fetch(repoInfoApiUrl)).json();
Expand Down Expand Up @@ -129,6 +129,24 @@ export class GitHubApi
currentNotificationIds: [] as NotificationId[],
} as GitHubUser;
}

async notifications({
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 ${base64}`,
},
});
if (!rawRes.ok) {
return [];
}
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
}
}

const checkNotFound = (infoRes: unknown) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {
ApplicationCommand,
ApplicationCommandOption,
commandOptionTypeMap,
} from "../abst/command";
import type { Client, CommandInteraction } from "discord.js";
import type { DiscordId } from "../exp/discord-id";
import type { Message } from "../abst/message";
} from "./discord-command";
import { Client, CommandInteraction } from "discord.js";
import type { DiscordId } from "../model/discord-id";
import type { Message } from "../model/message";
import { intoMessageEmbed } from "./message-convert";

const ownerOption: ApplicationCommandOption = {
name: "owner",
Expand Down Expand Up @@ -122,7 +123,7 @@ export class InteractionsCommandReceiver {
return Promise.resolve();
},
sendEmbed: (embed) => {
interaction.reply({ embeds: [embed] });
interaction.reply({ embeds: [intoMessageEmbed(embed)] });
return Promise.resolve();
},
panic: (reason) => {
Expand Down
36 changes: 36 additions & 0 deletions src/bot/adaptors/message-convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { EmbedMessage } from "../model/message";
import { MessageEmbed } from "discord.js";

export const intoMessageEmbed = ({
author,
color,
description,
fields,
footer,
title,
url,
}: EmbedMessage): MessageEmbed => {
const embed = new MessageEmbed();
if (author) {
embed.setAuthor(author.name, author.iconUrl, author.url);
}
if (color) {
embed.setColor(color);
}
if (description) {
embed.setDescription(description);
}
if (fields) {
embed.setFields(fields);
}
if (footer) {
embed.setFooter(footer);
}
if (title) {
embed.setTitle(title);
}
if (url) {
embed.setURL(url);
}
return embed;
};
Loading

0 comments on commit 9c28dae

Please sign in to comment.