-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from laminne/feat/163-notification
feat: 通知を送信できるように (#163)
- Loading branch information
Showing
6 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as http from "http"; | ||
import WebSocket, { WebSocketServer } from "ws"; | ||
import { IncomingMessage } from "http"; | ||
import { Snowflake } from "../../helpers/id_generator.js"; | ||
import logger from "../../helpers/logger.js"; | ||
|
||
export interface NotificationSendReceiver { | ||
Send(payload: string, to: string): Promise<void>; | ||
} | ||
|
||
export class DummyNotificationSendReceiver implements NotificationSendReceiver { | ||
async Send(): Promise<void> { | ||
return; | ||
} | ||
} | ||
|
||
export class WebSocketNotificationSendReceiver | ||
implements NotificationSendReceiver | ||
{ | ||
private ws: WebSocket.Server< | ||
typeof WebSocket.WebSocket, | ||
typeof IncomingMessage | ||
>; | ||
private connections: Map<Snowflake, WebSocket.WebSocket>; | ||
|
||
constructor(server: http.Server) { | ||
this.ws = new WebSocketServer({ noServer: true }); | ||
server.on("upgrade", (q, so, head) => { | ||
// fixme: q.headers.authorizationを使って認証とユーザーの検証をする | ||
this.ws.handleUpgrade(q, so, head, (w) => { | ||
this.ws.emit("connection", w, q); | ||
this.connections.set("123" as Snowflake, w); | ||
}); | ||
}); | ||
|
||
this.connections = new Map<Snowflake, WebSocket.WebSocket>(); | ||
|
||
this.ws.on("connection", (w) => { | ||
logger.info("connect"); | ||
w.on("message", () => { | ||
console.log("received!"); | ||
}); | ||
}); | ||
} | ||
|
||
async Send(payload: string, to: string): Promise<void> { | ||
const cli = this.connections.get(to as Snowflake); | ||
if (!cli) { | ||
return; | ||
} | ||
cli.send(payload); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export interface CommonNotificationObject<T, U> { | ||
type: T; | ||
payload: U; | ||
} | ||
|
||
export type FollowedNotification = CommonNotificationObject< | ||
"Followed", | ||
FollowedNotificationPayload | ||
>; | ||
|
||
export interface FollowedNotificationPayload { | ||
id: string; | ||
nickName: string; | ||
iconImageURL: string; | ||
} | ||
|
||
export type FollowSuccessNotification = CommonNotificationObject< | ||
"FollowSuccess", | ||
FollowSuccessNotificationPayload | ||
>; | ||
export interface FollowSuccessNotificationPayload { | ||
id: string; | ||
nickName: string; | ||
iconImageURL: string; | ||
} | ||
|
||
export type ReactionNotification = CommonNotificationObject< | ||
"Reaction", | ||
ReactionNotificationPayload | ||
>; | ||
export interface ReactionNotificationPayload { | ||
id: string; | ||
nickName: string; | ||
iconImageURL: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { NotificationSendReceiver } from "../../server/ws/websocket_handlers.js"; | ||
import { UserData } from "../data/user.js"; | ||
import { Snowflake } from "../../helpers/id_generator.js"; | ||
import { | ||
FollowedNotification, | ||
FollowSuccessNotification, | ||
ReactionNotification, | ||
} from "./@types.js"; | ||
|
||
export class NotificationSenderService { | ||
private sender: NotificationSendReceiver; | ||
constructor(sender: NotificationSendReceiver) { | ||
this.sender = sender; | ||
} | ||
|
||
// フォローされた通知 | ||
async Followed(u: UserData, to: Snowflake) { | ||
const payload: FollowedNotification = { | ||
type: "Followed", | ||
payload: { | ||
id: u.id, | ||
nickName: u.nickName, | ||
iconImageURL: u.iconImageURL, | ||
}, | ||
}; | ||
const serialized = JSON.stringify(payload); | ||
await this.sender.Send(serialized, to); | ||
return; | ||
} | ||
// フォローが成功した通知 | ||
async FollowSuccess(u: UserData, to: Snowflake) { | ||
const payload: FollowSuccessNotification = { | ||
type: "FollowSuccess", | ||
payload: { | ||
id: u.id, | ||
nickName: u.nickName, | ||
iconImageURL: u.iconImageURL, | ||
}, | ||
}; | ||
const serialized = JSON.stringify(payload); | ||
await this.sender.Send(serialized, to); | ||
return; | ||
} | ||
// リアクションされた通知 | ||
async Reaction(u: UserData, to: Snowflake) { | ||
const payload: ReactionNotification = { | ||
type: "Reaction", | ||
payload: { | ||
id: u.id, | ||
nickName: u.nickName, | ||
iconImageURL: u.iconImageURL, | ||
}, | ||
}; | ||
const serialized = JSON.stringify(payload); | ||
await this.sender.Send(serialized, to); | ||
return; | ||
} | ||
} |