Skip to content

Commit

Permalink
通知システムの不具合修正 (#22)
Browse files Browse the repository at this point in the history
* modify: Remove unused interface

* fix: Add NotifyTask and FIx cancel

* refactor: Move notificationQuery into another file

* refactor: Add typing
  • Loading branch information
MikuroXina authored Feb 24, 2021
1 parent 0f8336d commit ba006da
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/bot/play/notify.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Database, notify } from "./notify";
import { Database, Query, notify } from "./notify";
import {
GitHubNotifications,
NotificationId,
Expand All @@ -20,7 +20,7 @@ const db: Database = {
},
};

const query = {
const query: Query = {
fetchNotification: () =>
Promise.resolve([
{
Expand Down
23 changes: 23 additions & 0 deletions src/bot/skin/github-notification-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GitHubNotifications } from "../exp/github-notification";
import { GitHubUser } from "../exp/github-user";
import { Query } from "../play/notify";
import fetch from "node-fetch";

export const notificationQuery: Query = {
async fetchNotification({
userName,
notificationToken,
}: GitHubUser): Promise<GitHubNotifications> {
const rawRes = await fetch("https://api.github.com/notifications", {
headers: {
Authorization: `Basic ${Buffer.from(
`${userName}:${notificationToken}`,
).toString("base64")}`,
},
});
if (!rawRes.ok) {
throw new Error("fail to fetch notifications");
}
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
},
};
46 changes: 11 additions & 35 deletions src/bot/skin/notifier.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import type {
GitHubNotifications,
NotificationId,
} from "../exp/github-notification";
import { MessageEmbed, User } from "discord.js";
import { Database as NotifyController, Query, notify } from "../play/notify";
import { Database as NotifyController, notify } from "../play/notify";
import type {
SubscriptionDatabase,
UpdateHandler,
} from "../abst/user-database";
import type { Analecta } from "../exp/analecta";
import type { DiscordId } from "../exp/discord-id";
import type { GitHubUser } from "../exp/github-user";
import fetch from "node-fetch";

export interface Database {
update: (id: DiscordId, notificationIds: NotificationId[]) => Promise<void>;
}
import type { NotificationId } from "../exp/github-notification";
import { notificationQuery } from "./github-notification-query";

const safeParseDecimal = (str: string): number => {
const val = parseInt(str, 10);
Expand All @@ -33,27 +26,12 @@ export type UserDic = {
fetch: (userId: string) => Promise<User>;
};

const notificationQuery: Query = {
async fetchNotification({
userName,
notificationToken,
}: GitHubUser): Promise<GitHubNotifications> {
const rawRes = await fetch("https://api.github.com/notifications", {
headers: {
Authorization: `Basic ${Buffer.from(
`${userName}:${notificationToken}`,
).toString("base64")}`,
},
});
if (!rawRes.ok) {
throw new Error("fail to fetch notifications");
}
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
},
};
interface NotifyTask {
timer: NodeJS.Timeout;
}

export class SubscriptionNotifier implements UpdateHandler {
private notifyTasks: Map<DiscordId, () => void> = new Map();
private notifyTasks: Map<DiscordId, NotifyTask> = new Map();

constructor(
private analecta: Analecta,
Expand All @@ -68,7 +46,7 @@ export class SubscriptionNotifier implements UpdateHandler {
return Promise.resolve();
}

private makeNotifyTask(userId: DiscordId, sub: GitHubUser): () => void {
private makeNotifyTask(userId: DiscordId, sub: GitHubUser): NotifyTask {
const notifyHandler = notify(
this.notifyController(sub, userId),
notificationQuery,
Expand All @@ -77,13 +55,11 @@ export class SubscriptionNotifier implements UpdateHandler {
() =>
notifyHandler(this.analecta, this.sendMessage(userId)).catch((err) => {
console.error(err);
clearInterval(timer);
this.stop(userId);
}),
NOTIFY_INTERVAL,
);
return (): void => {
clearInterval(timer);
};
return { timer };
}

private sendMessage(userId: string): (mes: MessageEmbed) => Promise<void> {
Expand All @@ -108,7 +84,7 @@ export class SubscriptionNotifier implements UpdateHandler {
private stop(id: DiscordId): void {
const task = this.notifyTasks.get(id);
if (task) {
task();
clearInterval(task.timer);
this.notifyTasks.delete(id);
}
}
Expand Down

0 comments on commit ba006da

Please sign in to comment.