summaryrefslogtreecommitdiff
path: root/src/classes/DM.ts
blob: 51830c737f619de6d652346a4cf17135962507f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {Channel} from "./Channel";
import {CLIDispatcher} from "./CLIDispatcher";
import {Client} from "./Client";
import {IChatMessageOptions} from "../types/IChatMessageOptions";
import {SentDataMessage} from "./SentDataMessage";
import {Sticker} from "./Sticker";
import {MessageFormatting} from "./MessageFormatting";

/**
 * A Signal 1-to-1 chat
 */
export class DM extends Channel {
    /**
     * Whether the channel is a group chat
     */
    public group: boolean = false;

    /**
     * Recipient's phone number
     */
    public number: string;
    private client: Client;

    /**
     * @param userId - The ID of the user
     * @param number - The user's phone number
     * @param client
     * @internal
     */
    constructor(userId: string | null, number: string, client: Client) {
        super();
        this.id = userId;
        this.number = number;
        this.client = client;
    }

    /**
     * Send a message to the channel
     * @param text - The text of the message, or an empty string
     * @param options - The optional options used to build the message
     */
    public async send(text: string, options?: IChatMessageOptions): Promise<SentDataMessage> {
        let originalText = text;

        if (options?.markdown) {
            text = MessageFormatting.plainFromMarkdown(text);
            options.formatting = MessageFormatting.fromMarkdown(originalText);
        }

        let data = await CLIDispatcher.dispatch("send", {
            recipient: [this.number],
            message: text,
            attachment: options?.attachments?.map(i => i.uri) ?? [],
            quoteTimestamp: options?.quote?.build().createdTimestamp,
            quoteAuthor: options?.quote?.build().author.number,
            quoteMessage: options?.quote?.build().content,
            editTimestamp: options?.original,
            previewUrl: options?.preview?.url,
            previewTitle: options?.preview?.title,
            previewDescription: options?.preview?.description,
            previewImage: options?.preview?.image,
            textStyle: options?.formatting?.rules.map(rule => rule.toCLIFormat()),
            mention: options?.mentions?.map(rule => rule.toCLIFormat())
        }, this.client.process);

        let timestamp = data.result?.timestamp;
        return new SentDataMessage(timestamp, this, this.client, text, options);
    }

    /**
     * Set the client's typing status for this channel
     * @param typing - Whether the client should be typing or not
     */
    public async setTyping(typing: boolean): Promise<void> {
        await CLIDispatcher.dispatch("sendTyping", {
            recipient: [this.number], stop: !typing
        }, this.client.process);
    }

    /**
     * Send a sticker to this channel
     * @param sticker - The sticker to send
     */
    public async sendSticker(sticker: Sticker): Promise<void> {
        await CLIDispatcher.dispatch("send", {
            recipient: [this.number], sticker: sticker.pack.id + ":" + sticker.id
        }, this.client.process);
    }
}