Skip to content

Commit

Permalink
format & fix type & improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Oct 10, 2024
1 parent a75cfbe commit 89c479b
Show file tree
Hide file tree
Showing 4 changed files with 783 additions and 2,001 deletions.
27 changes: 13 additions & 14 deletions packages/linejs/client/clients/base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,13 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
this.emit("square:event", event);

if (event.type === "NOTIFICATION_MESSAGE") {
const message =
event.payload.notificationMessage.squareMessage.message;
const notificationMessage = event.payload.notificationMessage;

if (!notificationMessage) {
continue;
}

const message = notificationMessage.squareMessage.message;

if (previousMessageId === message.id) {
continue;
Expand Down Expand Up @@ -323,15 +328,13 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
const react = async (options: SquareMessageReactionOptions) => {
if (typeof options === "number") {
return await this.reactToSquareMessage({
squareChatMid:
event.payload.notificationMessage.squareChatMid,
squareChatMid: notificationMessage.squareChatMid,
reactionType: options as LINETypes.MessageReactionType,
squareMessageId: message.id,
});
} else {
return await this.reactToSquareMessage({
squareChatMid:
event.payload.notificationMessage.squareChatMid,
squareChatMid: notificationMessage.squareChatMid,
reactionType: (
options as Exclude<
SquareMessageReactionOptions,
Expand All @@ -353,7 +356,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
});

this.emit("square:message", {
...event.payload.notificationMessage,
...notificationMessage,
type: "square",
content: typeof message.text === "string" ? message.text : "",
contentMetadata: message.contentMetadata,
Expand All @@ -367,7 +370,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
mid: message._from,
get displayName() {
return (
event.payload.notificationMessage.senderDisplayName ||
notificationMessage.senderDisplayName ||
getMyProfile().then((myProfile) => myProfile.displayName)
);
},
Expand All @@ -384,8 +387,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
getMyProfile,
square: async () =>
await this.getSquareChat({
squareChatMid:
event.payload.notificationMessage.squareChatMid,
squareChatMid: notificationMessage.squareChatMid,
}),
data:
this.hasData(message) &&
Expand Down Expand Up @@ -976,10 +978,7 @@ export class BaseClient extends TypedEventEmitter<ClientEvents> {
Buffer.from(secret),
Buffer.from(e2eeInfo.metadata.encryptedKeyChain, "base64"),
);
const e2eeLogin = await this.confirmE2EELogin(
response[3],
deviceSecret,
);
const e2eeLogin = await this.confirmE2EELogin(response[3], deviceSecret);
response = await this.loginV2(
keynm,
encryptedMessage,
Expand Down
46 changes: 30 additions & 16 deletions packages/linejs/client/clients/internal/talk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,16 @@ export class TalkClient extends ChannelClient {
useCache: boolean = this.useTalkCache,
): Promise<LINETypes.GetContactsV2Response> {
const { mids } = { ...options };
if (useCache && mids.length === 1 && this.cache.getCache("getContactV2", { mid: mids[0] })) {
const res: { contacts: Record<string, LooseType> } = { contacts: {} }
res.contacts[mids[0]] = this.cache.getCache("getContactV2", { mid: mids[0] })
return res
if (
useCache &&
mids.length === 1 &&
this.cache.getCache("getContactV2", { mid: mids[0] })
) {
const res: { contacts: Record<string, LooseType> } = { contacts: {} };
res.contacts[mids[0]] = this.cache.getCache("getContactV2", {
mid: mids[0],
});
return res;
}
const response = (await this.request(
[[15, 1, [11, mids]]],
Expand All @@ -465,8 +471,16 @@ export class TalkClient extends ChannelClient {
for (const key in response.contacts) {
if (Object.prototype.hasOwnProperty.call(response.contacts, key)) {
const contact = response.contacts[key];
this.cache.setCache("getContact", { mid: contact.contact.mid }, contact.contact);
this.cache.setCache("getContactV2", { mid: contact.contact.mid }, contact);
this.cache.setCache(
"getContact",
{ mid: contact.contact.mid },
contact.contact,
);
this.cache.setCache(
"getContactV2",
{ mid: contact.contact.mid },
contact,
);
}
}
return response;
Expand Down Expand Up @@ -764,20 +778,20 @@ export class TalkClient extends ChannelClient {
chatSet.picturePath ? [11, 7, chatSet.picturePath] : null,
chatSet.extra?.groupExtra
? [
12,
8,
[
12,
8,
[
12,
1,
[
[2, 2, chatSet.extra.groupExtra.preventedJoinByTicket],
[2, 6, chatSet.extra.groupExtra.addFriendDisabled],
[2, 7, chatSet.extra.groupExtra.ticketDisabled],
12,
1,
[
[2, 2, chatSet.extra.groupExtra.preventedJoinByTicket],
[2, 6, chatSet.extra.groupExtra.addFriendDisabled],
[2, 7, chatSet.extra.groupExtra.ticketDisabled],
],
],
],
],
]
]
: null,
],
],
Expand Down
Loading

0 comments on commit 89c479b

Please sign in to comment.