Skip to content

Commit

Permalink
feat(lark): support getUser (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan authored Oct 11, 2023
1 parent 040162a commit 7d21997
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
5 changes: 5 additions & 0 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export class LarkBot extends Bot<LarkBot.Config> {
const data = await Promise.all(messages.items.reverse().map(data => Utils.decodeMessage(this, data)))
return { data, next: data[0]?.id }
}

async getUser(userId: string, guildId?: string) {
const data = await this.internal.getUserInfo(userId)
return Utils.decodeUser(data.data)
}
}

export namespace LarkBot {
Expand Down
2 changes: 2 additions & 0 deletions adapters/lark/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export namespace Lark {
* @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create
*/
export type ReceiveIdType = UserIdType | 'email' | 'chat_id'

export type DepartmentIdType = 'department_id' | 'open_department_id'
}

export { Lark as Feishu }
91 changes: 91 additions & 0 deletions adapters/lark/src/types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Lark } from '.'
import { Internal } from './internal'

declare module '.' {
export namespace Lark {
export interface User {
union_id: string
user_id?: string
open_id: string
name?: string
en_name?: string
nickname?: string
email?: string
mobile?: string
mobile_visible: boolean
gender?: Gender
avatar?: AvatarInfo
status?: UserStatus
department_ids?: string[]
leader_user_id?: string
city?: string
country?: string
work_station?: string
join_time?: number
is_tenant_manager?: boolean
employee_no?: string
employee_type?: number
orders?: UserOrder[]
custom_attrs?: any // TODO
enterprise_email?: string
job_title?: string
geo?: string
job_level_id?: string
job_family_id?: string
assign_info?: any // TODO
department_path?: DepartmentDetail[]
}

export enum Gender {
SECRET = 0,
MALE = 1,
FEMALE = 2,
}

export interface AvatarInfo {
avatar_72: string
avatar_240: string
avatar_640: string
avatar_origin: string
}

export interface UserStatus {
is_frozen: boolean
is_resigned: boolean
is_activated: boolean
is_exited: boolean
is_unjoin: boolean
}

export interface UserOrder {
department_id: string
user_order: number
department_order: number
is_primary_dept: boolean
}

export interface DepartmentDetail {
dotted_line_leader_user_ids: string[]
}
}
}

export interface GuildMember {
member_id_type: Lark.UserIdType
member_id: string
name: string
tenant_key: string
}

declare module './internal' {
export interface Internal {
/** @see https://open.larksuite.com/document/server-docs/contact-v3/user/get */
getUserInfo(user_id: string, user_id_type?: Lark.UserIdType, department_id_type?: Lark.DepartmentIdType): Promise<BaseResponse & { data: Lark.User }>
}
}

Internal.define({
'contact/v3/users/{user_id}': {
GET: 'getUserInfo',
},
})
11 changes: 10 additions & 1 deletion adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'crypto'
import { Message } from '@satorijs/protocol'
import { Message, User } from '@satorijs/protocol'
import { h, Session, trimSlash } from '@satorijs/satori'
import { FeishuBot, LarkBot } from './bot'
import { AllEvents, Events, Lark, Message as LarkMessage, MessageContentType, MessageType } from './types'
Expand Down Expand Up @@ -146,6 +146,15 @@ export function extractIdType(id: string): Lark.ReceiveIdType {
return 'user_id'
}

export function decodeUser(user: Lark.User): User {
return {
id: user.open_id,
avatar: user.avatar?.avatar_origin,
isBot: false,
name: user.name,
}
}

export class Cipher {
encryptKey: string
key: Buffer
Expand Down

0 comments on commit 7d21997

Please sign in to comment.