Skip to content

Commit

Permalink
Add TalkBlockSession. This implement #85
Browse files Browse the repository at this point in the history
Add blockUser to OpenChannelSession.
  • Loading branch information
storycraft committed Feb 1, 2021
1 parent 2c2932a commit 17e4734
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-kakao",
"version": "4.0.0-alpha.4",
"version": "4.0.0-alpha.5",
"description": "Loco protocol compatible library",
"main": "./dist/index.js",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions src/openlink/open-channel-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export interface OpenChannelSession {
*/
kickUser(user: ChannelUser): AsyncCommandResult;

/**
* Block open user from the this channel permanently in client. This cannot be undone.
*
* @param user
*/
blockUser(user: ChannelUser): AsyncCommandResult;

/**
* Get latest channel openlink
*/
Expand Down
96 changes: 96 additions & 0 deletions src/talk/block/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Created on Mon Feb 01 2021
*
* Copyright (c) storycraft. Licensed under the MIT Licence.
*/

import { AsyncCommandResult, KnownDataStatusCode } from "../../request";
import { ChannelUser } from "../../user";
import { TalkSession } from "../client";

/**
* Provide user block / unblock api.
* Note: To get block list use web api.
*/
export class TalkBlockSession {

constructor(private _session: TalkSession) {

}

/**
* Block normal user
*
* @param user
* @param type
*/
async blockUser(user: ChannelUser, type: TalkBlockType = TalkBlockType.BLOCK): AsyncCommandResult {
const res = await this._session.request(
'BLADDITEM',
{
'l': [user.userId],
'ts': [type]
}
);

return { success: res.status === KnownDataStatusCode.SUCCESS, status: res.status };
}

/**
* Block plus user
*
* @param plusUser
* @param type
*/
async blockPlusUser(plusUser: ChannelUser, type: TalkBlockType = TalkBlockType.BLOCK): AsyncCommandResult {
const res = await this._session.request(
'BLADDITEM',
{
'pl': [plusUser.userId],
'pts': [type]
}
);

return { success: res.status === KnownDataStatusCode.SUCCESS, status: res.status };
}

/**
* Unblock normal user.
*
* @param user
*/
async unblockUser(user: ChannelUser) {
const res = await this._session.request(
'BLDELITEM',
{
'l': [user.userId],
}
);

return { success: res.status === KnownDataStatusCode.SUCCESS, status: res.status };
}

/**
* Unblock plus user.
*
* @param plusUser
*/
async unblockPlusUser(plusUser: ChannelUser) {
const res = await this._session.request(
'BLDELITEM',
{
'pl': [plusUser.userId],
}
);

return { success: res.status === KnownDataStatusCode.SUCCESS, status: res.status };
}

}

export enum TalkBlockType {

BLOCK = 0,
BLOCK_HIDE_PROFILE = 1

}
9 changes: 9 additions & 0 deletions src/talk/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TalkChannelList } from "../talk-channel-list";
import { ClientEvents } from "../event";
import { Long } from "bson";
import { TypedEmitter } from "../../event";
import { TalkBlockSession } from "../block";

export * from "./talk-client-session";

Expand Down Expand Up @@ -53,6 +54,7 @@ export class TalkClient extends TypedEmitter<ClientEvents> implements CommandSes
private _clientSession: TalkClientSession;

private _cilentUser: ChannelUser;
private _blockList: TalkBlockSession;

private _channelList: TalkChannelList;

Expand All @@ -70,6 +72,7 @@ export class TalkClient extends TypedEmitter<ClientEvents> implements CommandSes
this._channelList = new TalkChannelList(this.createSessionProxy());

this._cilentUser = { userId: Long.ZERO };
this._blockList = new TalkBlockSession(this.createSessionProxy());

this._openLink = new OpenLinkService(this.createSessionProxy());
}
Expand All @@ -94,6 +97,12 @@ export class TalkClient extends TypedEmitter<ClientEvents> implements CommandSes
return this._cilentUser;
}

get blockList() {
if (!this.logon) throw new Error('Cannot access without logging in');

return this._blockList;
}

get openLink() {
if (!this.logon) throw new Error('Cannot access without logging in');

Expand Down
1 change: 1 addition & 0 deletions src/talk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright (c) storycraft. Licensed under the MIT Licence.
*/

export * from "./block";
export * from "./chat";
export * from "./channel";
export * from "./talk-channel-list";
Expand Down
15 changes: 15 additions & 0 deletions src/talk/openlink/talk-open-channel-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ export class TalkOpenChannelSession implements OpenChannelSession {
return { status: res.status, success: res.status === KnownDataStatusCode.SUCCESS };
}

async blockUser(user: ChannelUser): AsyncCommandResult {
const res = await this._session.request(
'BLIND',
{
'c': this._channel.channelId,
'li': this._channel.linkId,
'mid': user.userId,
// Reporting user is not supported.
'r': false
}
);

return { status: res.status, success: res.status === KnownDataStatusCode.SUCCESS };
}

async changeProfile(profile: OpenLinkProfiles): AsyncCommandResult<Readonly<OpenLinkChannelUserInfo> | null> {
const res = await this._session.request(
'UPLINKPROF',
Expand Down
4 changes: 4 additions & 0 deletions src/talk/openlink/talk-open-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ export class TalkOpenChannel extends TypedEmitter<OpenChannelEvents> implements
return res;
}

async blockUser(user: ChannelUser) {
return this._openChannelSession.blockUser(user);
}

react(flag: boolean) {
return this._openChannelSession.react(flag);
}
Expand Down
6 changes: 4 additions & 2 deletions src/talk/talk-channel-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ export class TalkChannelList extends TypedEmitter<TalkChannelListEvents> impleme
}
});

await TalkNormalChannelList.initialize(talkChannelList._normalList, normalList);
await TalkOpenChannelList.initialize(talkChannelList._openList, openList);
await Promise.all([
TalkNormalChannelList.initialize(talkChannelList._normalList, normalList),
TalkOpenChannelList.initialize(talkChannelList._openList, openList)
]);

return talkChannelList;
}
Expand Down
3 changes: 1 addition & 2 deletions src/user/channel-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import { Long } from "bson";
import { OpenLinkComponent } from "../openlink";
import { OpenChannelUserPerm } from "../openlink/open-link-type";

/**
* Channel user
* Any user that can talk via channel.
*/
export interface ChannelUser {

Expand Down

0 comments on commit 17e4734

Please sign in to comment.