Skip to content

Commit

Permalink
feat(message): add download
Browse files Browse the repository at this point in the history
  • Loading branch information
piloking committed Jan 4, 2025
1 parent 5e6a37f commit 75da8cf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/linejs/base/e2ee/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ export class E2EE {
...messageObj.contentMetadata,
...meta,
};
if (messageObj.chunks) messageObj.chunks = undefined as any;
} else if (
(messageObj.contentType === "LOCATION" ||
messageObj.contentType ===
Expand All @@ -671,8 +672,8 @@ export class E2EE {
messageObj.location = await this.decryptE2EELocationMessage(
messageObj,
);
if (messageObj.chunks) messageObj.chunks = undefined as any;
}
if (messageObj.chunks) messageObj.chunks = undefined as any;

return messageObj;
}
Expand Down
48 changes: 44 additions & 4 deletions packages/linejs/client/features/message/square.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type {
StickerMetadata,
} from "./internal-types.ts";
import type { DecorationsData, From, MentionTarget, To } from "./types.ts";
import { InternalError } from "../../../base/core/mod.ts";

const hasContents = ["IMAGE", "VIDEO", "AUDIO", "FILE"];
export interface SquareMessageInit {
client: Client;
raw: Message;
Expand All @@ -28,6 +30,7 @@ export class SquareMessage {

readonly isSquare = true;
readonly isTalk = false;
#authorIsMe?: boolean;

constructor(init: SquareMessageInit) {
this.#client = init.client;
Expand Down Expand Up @@ -97,7 +100,9 @@ export class SquareMessage {
*/
async unsend() {
if (!this.isMyMessage) {
throw new TypeError("Cannot unsend the message which is not yours.");
throw new TypeError(
"Cannot unsend the message which is not yours.",
);
}
await this.#client.base.square.unsendMessage({
messageId: this.#raw.message.id,
Expand Down Expand Up @@ -141,7 +146,8 @@ export class SquareMessage {
throw new TypeError("The message is not text message.");
}
const emojiUrls: string[] = [];
const emojiData = this.#raw.message.contentMetadata as unknown as EmojiMeta;
const emojiData = this.#raw.message
.contentMetadata as unknown as EmojiMeta;
const emojiResources = emojiData?.REPLACE?.sticon?.resources ?? [];
for (const emoji of emojiResources) {
emojiUrls.push(
Expand Down Expand Up @@ -303,8 +309,42 @@ export class SquareMessage {
};
}

get isMyMessage(): boolean {
return this.#client.base.profile?.mid === this.from.id;
/**
* @return {Blob} message data
*/
async getData(preview?: boolean): Promise<Blob> {
if (!hasContents.includes(this.#raw.message.contentType as string)) {
throw new TypeError(
"message have no contents",
);
}
if (this.#raw.message.contentMetadata.DOWNLOAD_URL) {
if (preview) {
const r = await this.#client.base
.fetch(this.#raw.message.contentMetadata.PREVIEW_URL);
return await r.blob();
} else {
const r_1 = await this.#client.base
.fetch(this.#raw.message.contentMetadata.DOWNLOAD_URL);
return await r_1.blob();
}
}
return this.#client.base.obs.getMessageObsData({
messageId: this.#raw.message.id,
isPreview: preview,
isSquare: true,
});
}

public async isMyMessage(): Promise<boolean> {
if (typeof this.#authorIsMe === "boolean") {
return this.#authorIsMe;
}
this.#authorIsMe = this.from.id ===
(await this.#client.base.square.getSquareChat({
squareChatMid: this.to.id,
})).squareChatMember.squareMemberMid;
return this.#authorIsMe;
}

get to(): To {
Expand Down
52 changes: 49 additions & 3 deletions packages/linejs/client/features/message/talk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import type {
StickerMetadata,
} from "./internal-types.ts";
import type { DecorationsData, From, MentionTarget, To } from "./types.ts";
import { InternalError } from "../../../base/core/mod.ts";

export interface TalkMessageInit {
client: Client;
raw: Message;
}

const hasContents = ["IMAGE", "VIDEO", "AUDIO", "FILE"];

export class TalkMessage {
#client: Client;
#raw: Message;
Expand Down Expand Up @@ -107,7 +110,9 @@ export class TalkMessage {
*/
async unsend() {
if (!this.isMyMessage) {
throw new TypeError("Cannot unsend the message which is not yours.");
throw new TypeError(
"Cannot unsend the message which is not yours.",
);
}
await this.#client.base.talk.unsendMessage({
messageId: this.#raw.id,
Expand Down Expand Up @@ -247,7 +252,9 @@ export class TalkMessage {
*/
getSharedContact(): ContactMeta {
if (this.#content.type !== "CONTACT") {
throw new TypeError("The message does not share contact infomation.");
throw new TypeError(
"The message does not share contact infomation.",
);
}
const contactData = this.#content.metadata as unknown as ContactMeta;
return { mid: contactData.mid, displayName: contactData.displayName };
Expand Down Expand Up @@ -313,6 +320,42 @@ export class TalkMessage {
};
}

/**
* @return {Blob} message data
*/
async getData(preview?: boolean): Promise<Blob> {
if (!hasContents.includes(this.#content.type as string)) {
throw new TypeError(
"message have no contents",
);
}
if (this.#raw.contentMetadata.DOWNLOAD_URL) {
if (preview) {
const r = await this.#client.base
.fetch(this.#raw.contentMetadata.PREVIEW_URL);
return await r.blob();
} else {
const r = await this.#client.base
.fetch(this.#raw.contentMetadata.DOWNLOAD_URL);
return await r.blob();
}
}
if (this.#raw.chunks) {
const file = await this.#client.base.obs.downloadMediaByE2EE(
this.#raw,
);
if (!file) {
throw new InternalError("ObsError", "Download failed");
}
return file;
} else {
return await this.#client.base.obs.getMessageObsData({
messageId: this.#raw.id,
isPreview: preview,
isSquare: false,
});
}
}
get isMyMessage(): boolean {
return this.#client.base.profile?.mid === this.from.id;
}
Expand Down Expand Up @@ -347,7 +390,10 @@ export class TalkMessage {
): Promise<TalkMessage> {
return this.fromRawTalk(source.event.message, client);
}
static async fromRawTalk(raw: Message, client: Client): Promise<TalkMessage> {
static async fromRawTalk(
raw: Message,
client: Client,
): Promise<TalkMessage> {
if (raw.contentMetadata.e2eeVersion) {
raw = await client.base.e2ee.decryptE2EEMessage(raw);
}
Expand Down

0 comments on commit 75da8cf

Please sign in to comment.