Skip to content

Commit

Permalink
change: remove enum and add const,union type
Browse files Browse the repository at this point in the history
  • Loading branch information
piloking committed Nov 16, 2024
1 parent 9d20c15 commit fa42426
Show file tree
Hide file tree
Showing 10 changed files with 4,895 additions and 3,434 deletions.
45 changes: 16 additions & 29 deletions packages/linejs/client/clients/base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
for (const event of myEvents.events) {
this.emit("square:event", event);

if (
event.type === LINETypes.SquareEventType._NOTIFICATION_MESSAGE
) {
if (event.type === "NOTIFICATION_MESSAGE") {
const payload = event.payload.notificationMessage;

if (!payload) {
Expand Down Expand Up @@ -414,10 +412,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
await this.getMessageObsData(message.id, preview)),
message,
});
} else if (
event.type ===
LINETypes.SquareEventType._NOTIFIED_UPDATE_SQUARE_CHAT_STATUS
) {
} else if (event.type === "NOTIFIED_UPDATE_SQUARE_CHAT_STATUS") {
const payload = event.payload.notifiedUpdateSquareChatStatus;

if (!payload) {
Expand Down Expand Up @@ -481,7 +476,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
this.emit("v2_square_event", event);

if (
event.type === LINETypes.SquareEventType._NOTIFICATION_MESSAGE &&
event.type === "NOTIFICATION_MESSAGE" &&
event.payload.notificationMessage
) {
const squareEventNotificationMessage =
Expand Down Expand Up @@ -535,19 +530,16 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
for (const operation of myEvents.operationResponse?.operations) {
revision = operation.revision;
if (
operation.type === LINETypes.OpType._RECEIVE_MESSAGE ||
operation.type === LINETypes.OpType._SEND_MESSAGE ||
operation.type === LINETypes.OpType._SEND_CONTENT
operation.type === "RECEIVE_MESSAGE" ||
operation.type === "SEND_MESSAGE" ||
operation.type === "SEND_CONTENT"
) {
const message = await this.decryptE2EEMessage(operation.message);
if (
this.hasData(message) &&
operation.type == LINETypes.OpType._SEND_MESSAGE
) {
if (this.hasData(message) && operation.type == "SEND_MESSAGE") {
//continue;
}
let sendIn = "";
if (message.toType === LINETypes.MIDType._USER) {
if (message.toType === "USER") {
if (message._from === this.user?.mid) {
sendIn = message.to;
} else {
Expand Down Expand Up @@ -608,14 +600,14 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
};

const chat =
message.toType === LINETypes.MIDType._USER
message.toType === "USER"
? () => {
return this.getContact({ mid: sendIn });
}
: undefined;

const group =
message.toType !== LINETypes.MIDType._USER
message.toType !== "USER"
? async () => {
return (await this.getChats({ mids: [sendIn] })).chats[0];
}
Expand All @@ -631,9 +623,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {

this.emit("message", {
...operation,
type: (message.toType === LINETypes.MIDType._USER
? "chat"
: "group") as LooseType,
type: (message.toType === "USER" ? "chat" : "group") as LooseType,
opType: operation.type,
content: typeof message.text === "string" ? message.text : "",
contentMetadata: message.contentMetadata,
Expand Down Expand Up @@ -713,15 +703,12 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
new LINEClass.Operation(operation, this as LooseType),
);
if (
operation.type === LINETypes.OpType._RECEIVE_MESSAGE ||
operation.type === LINETypes.OpType._SEND_MESSAGE ||
operation.type === LINETypes.OpType._SEND_CONTENT
operation.type === "RECEIVE_MESSAGE" ||
operation.type === "SEND_MESSAGE" ||
operation.type === "SEND_CONTENT"
) {
const message = await this.decryptE2EEMessage(operation.message);
if (
this.hasData(message) &&
operation.type == LINETypes.OpType._SEND_MESSAGE
) {
if (this.hasData(message) && operation.type == "SEND_MESSAGE") {
continue;
}
this.emit(
Expand Down Expand Up @@ -777,7 +764,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
public async sendMessage(_options: {
to: string;
text?: string;
contentType?: LINETypes.ContentType;
contentType?: LINETypes.ContentType & number;
contentMetadata?: LooseType;
relatedMessageId?: string;
location?: LINETypes.Location;
Expand Down
26 changes: 10 additions & 16 deletions packages/linejs/client/clients/e2ee/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ import { Buffer } from "node:buffer";
import { TalkClient } from "../internal/talk-client.ts";
import type { LooseType } from "../../entities/common.ts";
import { rawReadStruct as readStruct } from "../../libs/thrift/read.ts";
import {
ContentType,
type Location,
type Message,
MIDType,
} from "@evex/linejs-types";
import type { Location, Message } from "@evex/linejs-types";
import nacl from "tweetnacl";
import { InternalError } from "../../entities/errors.ts";
import * as LINETypes from "@evex/linejs-types";

class E2EE extends TalkClient {
public async getE2EESelfKeyData(mid: string): Promise<LooseType> {
try {
Expand Down Expand Up @@ -90,7 +84,7 @@ class E2EE extends TalkClient {
let key: LooseType = undefined;
let fd: LooseType, fn: LooseType;

if (toType === LINETypes.MIDType.USER) {
if (toType === LINETypes.enums.MIDType.USER) {
fd = "e2eePublicKeys";
fn = `:${keyId}`;
if (keyId !== undefined) {
Expand Down Expand Up @@ -427,7 +421,7 @@ class E2EE extends TalkClient {
const senderKeyId = selfKeyData.keyId;
let receiverKeyId, keyData;

if (this.getToType(to) === LINETypes.MIDType.USER) {
if (this.getToType(to) === LINETypes.enums.MIDType.USER) {
const privateKey = Buffer.from(selfKeyData.privKey, "base64");
const receiverKeyData = await this.negotiateE2EEPublicKey({ mid: to });
specVersion = receiverKeyData.specVersion;
Expand All @@ -449,7 +443,7 @@ class E2EE extends TalkClient {
}

let chunks;
if (contentType === LINETypes.ContentType.LOCATION) {
if (contentType === LINETypes.enums.ContentType.LOCATION) {
chunks = this.encryptE2EELocationMessage(
senderKeyId,
receiverKeyId,
Expand Down Expand Up @@ -566,14 +560,14 @@ class E2EE extends TalkClient {

override async decryptE2EEMessage(messageObj: Message): Promise<Message> {
if (
(messageObj.contentType === LINETypes.ContentType._NONE ||
messageObj.contentType === ContentType.NONE) &&
(messageObj.contentType === "NONE" ||
messageObj.contentType === LINETypes.enums.ContentType.NONE) &&
messageObj.chunks
) {
messageObj.text = await this.decryptE2EETextMessage(messageObj);
} else if (
(messageObj.contentType === LINETypes.ContentType._LOCATION ||
messageObj.contentType === ContentType.LOCATION) &&
(messageObj.contentType === "LOCATION" ||
messageObj.contentType === LINETypes.enums.ContentType.LOCATION) &&
messageObj.chunks
) {
messageObj.location = await this.decryptE2EELocationMessage(messageObj);
Expand Down Expand Up @@ -608,7 +602,7 @@ class E2EE extends TalkClient {
let privK = Buffer.from(selfKey.privKey, "base64");
let pubK;

if (toType === MIDType.USER || toType === MIDType._USER) {
if (toType === LINETypes.enums.MIDType.USER || toType === "USER") {
pubK = await this.getE2EELocalPublicKey(
isSelf ? to : _from,
isSelf ? receiverKeyId : senderKeyId,
Expand Down Expand Up @@ -663,7 +657,7 @@ class E2EE extends TalkClient {
let privK = Buffer.from(selfKey.privKey, "base64");
let pubK;

if (toType === MIDType.USER || toType === MIDType._USER) {
if (toType === LINETypes.enums.MIDType.USER || toType === "USER") {
pubK = await this.getE2EELocalPublicKey(
to,
isSelf ? receiverKeyId : senderKeyId,
Expand Down
2 changes: 1 addition & 1 deletion packages/linejs/client/clients/internal/square-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ export class SquareClient extends LiffClient {
...options,
};
const UPDATE_PREF_ATTRS: number[] = [];
const UPDATE_ATTRS = [5];
const UPDATE_ATTRS: (LINETypes.SquareMemberAttribute & number)[] = [5];
const MEMBERSHIP_STATE = allowRejoin ? 5 : 6;
const getSquareMemberResp = await this.getSquareMember({ squareMemberMid });
const squareMember = getSquareMemberResp.squareMember;
Expand Down
12 changes: 8 additions & 4 deletions packages/linejs/client/entities/message-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,12 @@ export class TalkMessage extends ClientMessage {
type: LINETypes.MessageReactionType,
): Promise<LINETypes.ReactToMessageResponse> {
if (typeof type === "string") {
type = LINETypes.MessageReactionType[type];
type = LINETypes.enums.MessageReactionType[
type
] as LINETypes.MessageReactionType & number;
}
return this.client.reactToMessage({
reactionType: type as LINETypes.MessageReactionType & number,
reactionType: type,
messageId: BigInt(this.id),
});
}
Expand Down Expand Up @@ -867,11 +869,13 @@ export class SquareMessage extends ClientMessage {
type: LINETypes.MessageReactionType,
): Promise<LINETypes.ReactToMessageResponse> {
if (typeof type === "string") {
type = LINETypes.MessageReactionType[type];
type = LINETypes.enums.MessageReactionType[
type
] as LINETypes.MessageReactionType & number;
}
return this.client.reactToSquareMessage({
squareChatMid: this.to,
reactionType: type as LINETypes.MessageReactionType & number,
reactionType: type,
squareMessageId: this.id,
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/linejs/client/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type SquareMessage = Omit<
export type MessageReplyOptions =
| {
text?: string;
contentType?: LINETypes.ContentType;
contentType?: LINETypes.ContentType & number;
contentMetadata?: LooseType;
}
| string;
Expand Down
4 changes: 2 additions & 2 deletions packages/linejs/client/entities/square-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
this.invitationUrl = squareChat.invitationUrl;
this.messageVisibility = squareChat.messageVisibility;
this.ableToSearchMessage = [null, false, true][
LINETypes.BooleanState[squareChat.ableToSearchMessage] as number
LINETypes.enums.BooleanState[squareChat.ableToSearchMessage]
];
this.mymid = squareChatMember.squareMemberMid;
this.memberCount = squareChatStatus.otherStatus.memberCount;
Expand Down Expand Up @@ -276,7 +276,7 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
this.invitationUrl = squareChat.invitationUrl;
this.messageVisibility = squareChat.messageVisibility;
this.ableToSearchMessage = [null, false, true][
LINETypes.BooleanState[squareChat.ableToSearchMessage] as number
LINETypes.enums.BooleanState[squareChat.ableToSearchMessage]
];
this.emit("update", squareChat);
}
Expand Down
98 changes: 70 additions & 28 deletions packages/linejs/client/entities/talk-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ function getMidType(mid: string): LINETypes.MIDType | null {
}
}

function toBit(num: number): number[] {
let i = 0;
const nums = [];
while (1 << i <= num) {
nums.push(((1 << i) & num) >> i);
i++;
}
return nums;
}

export class Note {
constructor(
public mid: string,
Expand Down Expand Up @@ -747,20 +757,16 @@ export class NotifiedUpdateProfileContent {
attr as LooseType as LINETypes.ProfileAttribute;
} else {
const arr: LINETypes.ProfileAttribute[] = [];
parseInt(op.param[2])
.toString(2)
.split("")
.reverse()
.forEach((e, i) => {
if (e === "1") {
arr.push(
parseEnum(
"ProfileAttribute",
2 ** i,
) as LooseType as LINETypes.ProfileAttribute,
);
}
});
toBit(parseInt(op.param[2])).forEach((e, i) => {
if (e === 1) {
arr.push(
parseEnum(
"ProfileAttribute",
2 ** i,
) as LooseType as LINETypes.ProfileAttribute,
);
}
});
this.profileAttributes = arr;
}
}
Expand Down Expand Up @@ -793,26 +799,62 @@ export class NotifiedUpdateProfile {
attr as LooseType as LINETypes.ProfileAttribute;
} else {
const arr: LINETypes.ProfileAttribute[] = [];
parseInt(op.param[2])
.toString(2)
.split("")
.reverse()
.forEach((e, i) => {
if (e === "1") {
arr.push(
parseEnum(
"ProfileAttribute",
2 ** i,
) as LooseType as LINETypes.ProfileAttribute,
);
}
});
toBit(parseInt(op.param[2])).forEach((e, i) => {
if (e === 1) {
arr.push(
parseEnum(
"ProfileAttribute",
2 ** i,
) as LooseType as LINETypes.ProfileAttribute,
);
}
});
this.profileAttributes = arr;
}
this.info = JSON.parse(op.param[3]);
}
}

/**
* @description the profile was updated by you
*/
export class UpdateProfile {
public readonly name: string = "UpdateProfile";
public profileAttributes: (LINETypes.ProfileAttribute | null)[] = [];
public info: Record<string, LooseType> = {};

constructor(op: Operation) {
if (op.type !== "UPDATE_PROFILE") {
throw new TypeError("Wrong operation type");
}
if (
typeof op.param[1] === "undefined" ||
typeof op.param[2] === "undefined"
) {
throw new TypeError("Wrong param");
}
const attr = parseEnum("ProfileAttribute", op.param[1]);
if (attr !== null) {
this.profileAttributes[0] =
attr as LooseType as LINETypes.ProfileAttribute;
} else {
const arr: LINETypes.ProfileAttribute[] = [];
toBit(parseInt(op.param[1])).forEach((e, i) => {
if (e === 1) {
arr.push(
parseEnum(
"ProfileAttribute",
2 ** i,
) as LooseType as LINETypes.ProfileAttribute,
);
}
});
this.profileAttributes = arr;
}
this.info = JSON.parse(op.param[2]);
}
}

/**
* @description the message was reacted by ypu
*/
Expand Down
Loading

0 comments on commit fa42426

Please sign in to comment.