Skip to content

Commit

Permalink
fix: some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
piloking committed Dec 30, 2024
1 parent 91d5f82 commit 82ab02f
Show file tree
Hide file tree
Showing 19 changed files with 58,105 additions and 58,649 deletions.
3 changes: 3 additions & 0 deletions migrate_to_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@ await client.login({ qr: true });

// login with authToken
await client.login({ authToken: ... });

// start polling
client.polling(["talk", "square"]);
```
21 changes: 19 additions & 2 deletions packages/linejs/src/core/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ import {
TalkService,
} from "../service/mod.ts";

import { Login, LoginOption } from "../login/mod.ts";
import { Login, type LoginOption } from "../login/mod.ts";
import { Thrift } from "../thrift/mod.ts";
import { RequestClient } from "../request/mod.ts";
import { E2EE } from "../e2ee/mod.ts";
import { LineObs } from "../obs/mod.ts";
import { Timeline } from "../timeline/mod.ts";
import { Polling } from "../polling/mod.ts";

import { Thrift as def } from "@evex/linejs-types/thrift";
import type * as LINETypes from "@evex/linejs-types";

type PollingOption = "talk" | "square";

export interface ClientInit {
/**
* version which LINE App to emurating
Expand Down Expand Up @@ -99,6 +102,7 @@ export class Client extends TypedEventEmitter<ClientEvents> {
readonly e2ee: E2EE;
readonly obs: LineObs;
readonly timeline: Timeline;
readonly pollingProcess: Polling;

readonly auth: AuthService;
readonly call: CallService;
Expand All @@ -117,6 +121,7 @@ export class Client extends TypedEventEmitter<ClientEvents> {
if (!deviceDetails) {
throw new Error(`Unsupported device: ${init.device}.`);
}

this.storage = init.storage ?? new MemoryStorage();
this.request = new RequestClient({
endpoint: init.endpoint ?? "legy.line-apps.com",
Expand All @@ -128,6 +133,8 @@ export class Client extends TypedEventEmitter<ClientEvents> {
this.e2ee = new E2EE({ client: this });
this.obs = new LineObs({ client: this });
this.timeline = new Timeline({ client: this });
this.pollingProcess = new Polling({ client: this });

this.thrift.def = def;
this.device = init.device;
this.fetch = init.fetch ?? fetch;
Expand All @@ -146,7 +153,7 @@ export class Client extends TypedEventEmitter<ClientEvents> {
}

log(type: string, data: Record<string, any>) {
console.log(type, data);
this.emit("log", { type, data });
}
getToType(mid: string): number | null {
const typeMapping: { [key: string]: number } = {
Expand Down Expand Up @@ -179,4 +186,14 @@ export class Client extends TypedEventEmitter<ClientEvents> {
async login(options?: LoginOption): Promise<void> {
return await this.loginProcess.login(options);
}
polling(options: PollingOption[]): Promise<void[]> {
const promise: Promise<void>[] = [];
if (options.includes("talk")) {
promise.push(this.pollingProcess.talk());
}
if (options.includes("square")) {
promise.push(this.pollingProcess.square());
}
return Promise.all(promise);
}
}
14 changes: 9 additions & 5 deletions packages/linejs/src/core/utils/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type * as LINETypes from "@evex/linejs-types";
import type {
Operation,
SquareMessage,
TalkMessage,
} from "../../polling/events/class.ts";
type LogType = "login" | "request" | "response" | (string & {});

export interface Log {
Expand All @@ -16,10 +21,9 @@ export type ClientEvents = {
"update:cert": (cert: string) => void;
"update:qrcert": (qrCert: string) => void;
log: (data: Log) => void;
//"square:message": (squareMessage: SquareMessage) => void;
//"square:status": (squareStatus: SquaerStatus) => void;
"square:event": (squareEvent: LINETypes.SquareEvent) => void;
//message: (message: Message) => void;
"square:message": (message: SquareMessage) => void;
"square:event": (event: LINETypes.SquareEvent) => void;
message: (message: TalkMessage) => void;
event: (event: Operation) => void;
// TODO: Add more as square
//event: (talkEvent: LINETypes.Operation) => void;
};
27 changes: 23 additions & 4 deletions packages/linejs/src/e2ee/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,12 @@ export class E2EE {
messageObj.contentType === LINETypes.enums.ContentType.NONE) &&
messageObj.chunks
) {
messageObj.text = await this.decryptE2EETextMessage(messageObj);
const [text, meta] = await this.decryptE2EETextMessage(messageObj);
messageObj.text = text;
messageObj.contentMetadata = {
...messageObj.contentMetadata,
...meta,
};
} else if (
(messageObj.contentType === "LOCATION" ||
messageObj.contentType ===
Expand All @@ -623,7 +628,7 @@ export class E2EE {
public async decryptE2EETextMessage(
messageObj: Message,
isSelf = false,
): Promise<string> {
): Promise<[string, Record<string, string>]> {
const _from = messageObj.from;
const to = messageObj.to;
if (_from === this.client.profile?.mid) {
Expand Down Expand Up @@ -676,8 +681,22 @@ export class E2EE {
} else {
decrypted = this.decryptE2EEMessageV1(chunks, privK, pubK);
}

return decrypted.text || "";
const text = decrypted.text || "";
const meta: Record<string, string> = {};
for (const key in decrypted) {
if (key === "text") {
continue;
}
if (Object.prototype.hasOwnProperty.call(decrypted, key)) {
const val = decrypted[key];
if (typeof val === "string") {
meta[key] = val;
} else {
meta[key] = JSON.stringify(val);
}
}
}
return [text, meta];
}

public async decryptE2EELocationMessage(
Expand Down
4 changes: 2 additions & 2 deletions packages/linejs/src/login/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class Login {
if (!EMAIL_REGEX.test(options.email)) {
throw new InternalError("RegExpUnmatch", "invalid email");
}
if (PASSWORD_REGEX.test(options.password)) {
if (!PASSWORD_REGEX.test(options.password)) {
throw new InternalError("RegExpUnmatch", "invalid password");
}

Expand Down Expand Up @@ -289,7 +289,7 @@ export class Login {
email: string,
password: string,
constantPincode: string = "114514",
enableE2EE: boolean = false,
enableE2EE: boolean = true,
): Promise<string> {
if (constantPincode.length !== 6) {
throw new InternalError(
Expand Down
29 changes: 20 additions & 9 deletions packages/linejs/src/polling/events/message-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ export class Message {

constructor(options: {
operation?: LINETypes.Operation;
squareEventNotificationMessage?: LINETypes.SquareEventNotificationMessage;
squareEventNotificationMessage?:
LINETypes.SquareEventNotificationMessage;
squareEventReceiveMessage?: LINETypes.SquareEventReceiveMessage;
squareEventSendMessage?: LINETypes.SquareEventSendMessage;
message?: LINETypes.Message;
Expand Down Expand Up @@ -211,14 +212,16 @@ export class Message {
this.sourceType = 0;
} else if (squareEventNotificationMessage) {
this.rawSource = squareEventNotificationMessage;
this.rawMessage = squareEventNotificationMessage.squareMessage.message;
this.rawMessage =
squareEventNotificationMessage.squareMessage.message;
this._senderDisplayName =
squareEventNotificationMessage.senderDisplayName;
this.sourceType = 1;
} else if (squareEventReceiveMessage) {
this.rawSource = squareEventReceiveMessage;
this.rawMessage = squareEventReceiveMessage.squareMessage.message;
this._senderDisplayName = squareEventReceiveMessage.senderDisplayName;
this._senderDisplayName =
squareEventReceiveMessage.senderDisplayName;
this.sourceType = 2;
} else if (squareEventSendMessage) {
this.rawSource = squareEventSendMessage;
Expand Down Expand Up @@ -356,9 +359,14 @@ export class Message {
splits
.sort((a, b) => a.start - b.start)
.forEach((e) => {
texts.push({
text: this.content?.substring(lastSplit, e.start) as string,
});
if (lastSplit - e.start) {
texts.push({
text: this.content?.substring(
lastSplit,
e.start,
) as string,
});
}
const content: decorationText = {
text: this.content?.substring(e.start, e.end) as string,
};
Expand Down Expand Up @@ -542,7 +550,8 @@ export class ClientMessage extends Message {
constructor(
options: {
operation?: LINETypes.Operation;
squareEventNotificationMessage?: LINETypes.SquareEventNotificationMessage;
squareEventNotificationMessage?:
LINETypes.SquareEventNotificationMessage;
squareEventReceiveMessage?: LINETypes.SquareEventReceiveMessage;
squareEventSendMessage?: LINETypes.SquareEventSendMessage;
message?: LINETypes.Message;
Expand Down Expand Up @@ -735,7 +744,8 @@ export class TalkMessage extends ClientMessage {
type: "MESSAGE",
contents: {
text: this.text,
link: `line://nv/chatMsg?chatId=${this.to}&messageId=${this.id}`,
link:
`line://nv/chatMsg?chatId=${this.to}&messageId=${this.id}`,
},
});
}
Expand All @@ -759,7 +769,8 @@ export class TalkMessage extends ClientMessage {
export class SquareMessage extends ClientMessage {
constructor(
options: {
squareEventNotificationMessage?: LINETypes.SquareEventNotificationMessage;
squareEventNotificationMessage?:
LINETypes.SquareEventNotificationMessage;
squareEventReceiveMessage?: LINETypes.SquareEventReceiveMessage;
squareEventSendMessage?: LINETypes.SquareEventSendMessage;
message?: LINETypes.Message;
Expand Down
21 changes: 14 additions & 7 deletions packages/linejs/src/polling/events/square-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export class Square extends TypedEventEmitter<SquareEvents> {
event.payload.notifiedUpdateSquareStatus.squareMid ===
this.mid
) {
this.status = event.payload.notifiedUpdateSquareStatus.squareStatus;
this.status =
event.payload.notifiedUpdateSquareStatus.squareStatus;
this.emit("update", this);
this.emit("update:status", this.status);
} else if (
Expand Down Expand Up @@ -230,6 +231,7 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
public status: LINETypes.SquareChatStatusWithoutMessage;
public syncToken?: string;
public note: Note;
public polling_delay = 1000;
constructor(
public rawSouce: LINETypes.GetSquareChatResponse,
private client: Client,
Expand Down Expand Up @@ -260,7 +262,7 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
this.status = squareChatStatus.otherStatus;
this.note = new Note(this.squareMid, this.client);
if (polling) {
this.startPolling();
this.polling();
}
if (autoUpdate) {
client.on("square:event", (event) => {
Expand All @@ -280,7 +282,8 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
event.payload.notifiedUpdateSquareChat.squareChatMid ===
this.mid
) {
const { squareChat } = event.payload.notifiedUpdateSquareChat;
const { squareChat } =
event.payload.notifiedUpdateSquareChat;
this.mid = squareChat.squareChatMid;
this.squareMid = squareChat.squareMid;
this.type = squareChat.type;
Expand Down Expand Up @@ -365,7 +368,7 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
/**
* @description start listen (fetchSquareChatEvents)
*/
public async startPolling(): Promise<void> {
public async polling(): Promise<void> {
if (!this.syncToken) {
while (true) {
const noneEvent = await this.client.square
Expand Down Expand Up @@ -401,7 +404,8 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
) {
const message = new SquareMessage(
{
squareEventSendMessage: event.payload.sendMessage,
squareEventSendMessage:
event.payload.sendMessage,
},
this.client,
);
Expand All @@ -412,14 +416,17 @@ export class SquareChat extends TypedEventEmitter<SquareChatEvents> {
) {
const message = new SquareMessage(
{
squareEventReceiveMessage: event.payload.receiveMessage,
squareEventReceiveMessage:
event.payload.receiveMessage,
},
this.client,
);
this.emit("message", message);
}
}
await new Promise<void>((resolve) => setTimeout(resolve, 1000));
await new Promise<void>((resolve) =>
setTimeout(resolve, this.polling_delay)
);
} catch (error) {
this.client.log("SquareChatPollingError", { error });
await new Promise<void>((resolve) => setTimeout(resolve, 2000));
Expand Down
22 changes: 7 additions & 15 deletions packages/linejs/src/polling/events/talk-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class Group extends TypedEventEmitter<GroupEvents> {
export class Operation {
public rawSource: LINETypes.Operation;
protected client?: Client;
public message?: Message | TalkMessage;
public message?: TalkMessage;
public revision: number;
public createdTime: Date;
public type: LINETypes.OpType;
Expand All @@ -502,8 +502,7 @@ export class Operation {

constructor(
source: LINETypes.Operation,
client?: Client,
emit: boolean = false,
client: Client,
) {
this.rawSource = source;
this.client = client;
Expand All @@ -514,7 +513,7 @@ export class Operation {
source.type;
this.reqSeq = source.reqSeq;
this.status = (parseEnum(
"OpStatus",
"Pb1_EnumC13127p6",
source.status,
) as LINETypes.Pb1_EnumC13127p6) ||
source.status;
Expand All @@ -528,14 +527,10 @@ export class Operation {
source.type === "SEND_MESSAGE" ||
source.type === "SEND_CONTENT"
) {
if (client) {
this.message = new TalkMessage(
{ message: source.message },
client,
);
} else {
this.message = new Message({ message: source.message });
}
this.message = new TalkMessage(
{ message: source.message },
client,
);
}
if (source.type == "SEND_CHAT_REMOVED") {
this.event = new SendChatRemoved(this);
Expand Down Expand Up @@ -570,9 +565,6 @@ export class Operation {
} else if (source.type == "NOTIFIED_DELETE_OTHER_FROM_CHAT") {
this.event = new DeleteOtherFromChat(this);
}
if (emit && client) {
// TODO: client.emit("event", source);
}
}
}

Expand Down
Loading

0 comments on commit 82ab02f

Please sign in to comment.