Skip to content

Commit

Permalink
feat(zulip): add internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Aug 19, 2023
1 parent 01f842a commit a41603f
Show file tree
Hide file tree
Showing 9 changed files with 2,648 additions and 39 deletions.
26 changes: 25 additions & 1 deletion adapters/zulip/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Bot, Context, Logger, Quester, Schema, Universal } from '@satorijs/satori'

Check failure on line 1 in adapters/zulip/src/bot.ts

View workflow job for this annotation

GitHub Actions / lint

'Universal' is defined but never used
import { HttpPolling } from './polling'
import { Internal } from './internal'
import { Internal } from './types'
import { ZulipMessageEncoder } from './message'
// @ts-ignore
import { version } from '../package.json'
import { decodeGuild } from './utils'

export class ZulipBot extends Bot<ZulipBot.Config> {
static MessageEncoder = ZulipMessageEncoder
Expand Down Expand Up @@ -33,6 +34,29 @@ export class ZulipBot extends Bot<ZulipBot.Config> {
this.username = full_name
this.avatar = avatar_url
}

async getUser(userId: string, guildId?: string) {
const { user } = await this.internal.getUser(userId)

Check failure on line 39 in adapters/zulip/src/bot.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'string' is not assignable to parameter of type 'GetUserParam'.
return {
userId,
username: user?.full_name,
}
}

async getGuildList() {
const { streams } = await this.internal.getStreams()
return streams.map(decodeGuild)
}

async getGuild(guildId: string) {
const { stream } = await this.internal.getStreamById(guildId)

Check failure on line 52 in adapters/zulip/src/bot.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'string' is not assignable to parameter of type 'GetStreamByIdParam'.
return decodeGuild(stream)
}

async getChannelList(guildId: string) {
const { topics } = await this.internal.getStreamTopics(guildId)

Check failure on line 57 in adapters/zulip/src/bot.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'string' is not assignable to parameter of type 'GetStreamTopicsParam'.
return topics.map(({ name }) => ({ channelId: name }))
}
}

export namespace ZulipBot {
Expand Down
1 change: 1 addition & 0 deletions adapters/zulip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { ZulipBot } from './bot'
export * from './bot'
export * from './utils'
export * from './polling'
export * from './types'

export default ZulipBot
32 changes: 0 additions & 32 deletions adapters/zulip/src/internal.ts

This file was deleted.

2 changes: 1 addition & 1 deletion adapters/zulip/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ZulipMessageEncoder extends MessageEncoder<ZulipBot> {
form.append('content', this.buffer)
if (!this.session.isDirect) form.append('topic', this.session.channelId)

await this.bot.http.post('/api/v1/messages', form, {
await this.bot.http.post('/messages', form, {
headers: form.getHeaders(),
})
}
Expand Down
6 changes: 4 additions & 2 deletions adapters/zulip/src/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { decodeMessage } from './utils'
export class HttpPolling extends Adapter.Client<ZulipBot> {
async start(bot: ZulipBot) {
await bot.initliaze()
const r = await bot.internal.register()
const r = await bot.internal.registerQueue({
event_types: `["message"]`,
})
let last = -1
const polling = async () => {
if (bot.status === 'disconnect') {
return bot.offline()
}
try {
const updates = await bot.internal.events({
const updates = await bot.internal.getEvents({
queue_id: r.queue_id,
last_event_id: last,
})
Expand Down
Loading

0 comments on commit a41603f

Please sign in to comment.