Skip to content

Commit 6e8f755

Browse files
author
Hyunje Jun
authored
Separate common logic between group and room methods (#98)
1 parent c030b3f commit 6e8f755

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

lib/client.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function checkJSON<T>(raw: T): T {
1515
}
1616
}
1717

18+
type ChatType = "group" | "room";
19+
1820
export default class Client {
1921
public config: Types.ClientConfig;
2022
private http: HTTPClient;
@@ -67,28 +69,37 @@ export default class Client {
6769
return this.http.get<Types.Profile>(`/profile/${userId}`).then(checkJSON);
6870
}
6971

70-
public getGroupMemberProfile(
71-
groupId: string,
72+
private getChatMemberProfile(
73+
chatType: ChatType,
74+
chatId: string,
7275
userId: string,
7376
): Promise<Types.Profile> {
7477
return this.http
75-
.get<Types.Profile>(`/group/${groupId}/member/${userId}`)
78+
.get<Types.Profile>(`/${chatType}/${chatId}/member/${userId}`)
7679
.then(checkJSON);
7780
}
7881

82+
public getGroupMemberProfile(
83+
groupId: string,
84+
userId: string,
85+
): Promise<Types.Profile> {
86+
return this.getChatMemberProfile("group", groupId, userId);
87+
}
88+
7989
public getRoomMemberProfile(
8090
roomId: string,
8191
userId: string,
8292
): Promise<Types.Profile> {
83-
return this.http
84-
.get<Types.Profile>(`/room/${roomId}/member/${userId}`)
85-
.then(checkJSON);
93+
return this.getChatMemberProfile("room", roomId, userId);
8694
}
8795

88-
public getGroupMemberIds(groupId: string): Promise<string[]> {
96+
private getChatMemberIds(
97+
chatType: ChatType,
98+
chatId: string,
99+
): Promise<string[]> {
89100
const load = (start?: string): Promise<string[]> =>
90101
this.http
91-
.get(`/group/${groupId}/members/ids`, start ? { start } : null)
102+
.get(`/${chatType}/${chatId}/members/ids`, start ? { start } : null)
92103
.then(checkJSON)
93104
.then((res: { memberIds: string[]; next?: string }) => {
94105
if (!res.next) {
@@ -102,33 +113,28 @@ export default class Client {
102113
return load();
103114
}
104115

105-
public getRoomMemberIds(roomId: string): Promise<string[]> {
106-
const load = (start?: string): Promise<string[]> =>
107-
this.http
108-
.get(`/room/${roomId}/members/ids`, start ? { start } : null)
109-
.then(checkJSON)
110-
.then((res: { memberIds: string[]; next?: string }) => {
111-
if (!res.next) {
112-
return res.memberIds;
113-
}
116+
public getGroupMemberIds(groupId: string): Promise<string[]> {
117+
return this.getChatMemberIds("group", groupId);
118+
}
114119

115-
return load(res.next).then(extraIds =>
116-
res.memberIds.concat(extraIds),
117-
);
118-
});
119-
return load();
120+
public getRoomMemberIds(roomId: string): Promise<string[]> {
121+
return this.getChatMemberIds("room", roomId);
120122
}
121123

122124
public getMessageContent(messageId: string): Promise<Readable> {
123125
return this.http.getStream(`/message/${messageId}/content`);
124126
}
125127

128+
private leaveChat(chatType: ChatType, chatId: string): Promise<any> {
129+
return this.http.post(`/${chatType}/${chatId}/leave`);
130+
}
131+
126132
public leaveGroup(groupId: string): Promise<any> {
127-
return this.http.post(`/group/${groupId}/leave`);
133+
return this.leaveChat("group", groupId);
128134
}
129135

130136
public leaveRoom(roomId: string): Promise<any> {
131-
return this.http.post(`/room/${roomId}/leave`);
137+
return this.leaveChat("room", roomId);
132138
}
133139

134140
public getRichMenu(richMenuId: string): Promise<Types.RichMenuResponse> {

0 commit comments

Comments
 (0)