-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ⚗️ Add experimental feature for chat pubsubs
- Loading branch information
1 parent
8041df9
commit 31e1ff7
Showing
7 changed files
with
161 additions
and
0 deletions.
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
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
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,63 @@ | ||
import { createModule, gql } from 'graphql-modules' | ||
import { TwitchClients } from '../injections/Twitch-Clients' | ||
import { TwitchId } from '../injections/Twitch-Id' | ||
import { UserId } from '../injections/User-Id' | ||
import asyncify from 'callback-to-async-iterator' | ||
import { ChatClient } from 'twitch-chat-client' | ||
|
||
export interface Chat { | ||
channel: string | ||
displayName: string | ||
message: string | ||
} | ||
|
||
export const ChatPubSubResolvers = { | ||
Subscription: { | ||
newChat: { | ||
subscribe: async ( | ||
_: any, | ||
args: { channel: string }, | ||
{ injector }: GraphQLModules.Context | ||
) => { | ||
const clients = injector.get(TwitchClients) | ||
|
||
const chatClient = new ChatClient(await clients.authProvider(), { | ||
channels: [args.channel], | ||
}) | ||
await chatClient.connect() | ||
|
||
const curriedOnChat = async (cb: any) => | ||
chatClient.onMessage(async (channel, user, message) => { | ||
return cb({ channel, displayName: user, message }) | ||
}) | ||
|
||
const asyncified = asyncify(curriedOnChat) | ||
|
||
return asyncified | ||
}, | ||
resolve: (chat: any) => { | ||
return chat | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
export const ChatPubSubSchema = gql` | ||
type Chat { | ||
message: String | ||
displayName: String | ||
channel: String | ||
} | ||
extend type Subscription { | ||
newChat(channel: String!): Chat | ||
} | ||
` | ||
|
||
export const ChatPubSubModule = createModule({ | ||
id: `chat-pubsub-module`, | ||
dirname: __dirname, | ||
providers: [TwitchClients, TwitchId, UserId], | ||
typeDefs: ChatPubSubSchema, | ||
resolvers: ChatPubSubResolvers, | ||
}) |
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,34 @@ | ||
import { createModule, gql } from 'graphql-modules' | ||
import { TwitchClients } from '../injections/Twitch-Clients' | ||
import { TwitchId } from '../injections/Twitch-Id' | ||
import { UserId } from '../injections/User-Id' | ||
import { Chat } from './chat-pubsub-type-schema' | ||
|
||
export const ChatUserLinkResolvers = { | ||
Chat: { | ||
user: async ( | ||
chat: Chat, | ||
_args: any, | ||
{ injector }: GraphQLModules.Context | ||
) => { | ||
const clients = injector.get(TwitchClients) | ||
const twitchClient = await clients.apiClient() | ||
|
||
return twitchClient.helix.users.getUserByName(chat.displayName) | ||
}, | ||
}, | ||
} | ||
|
||
export const ChatUserLinkSchema = gql` | ||
extend type Chat { | ||
user: User | ||
} | ||
` | ||
|
||
export const ChatUserLinkModule = createModule({ | ||
id: `chat-pubsub-user-link-module`, | ||
dirname: __dirname, | ||
providers: [TwitchClients, TwitchId, UserId], | ||
typeDefs: ChatUserLinkSchema, | ||
resolvers: ChatUserLinkResolvers, | ||
}) |
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