All files / servicesExternal BlueSkyService.js

90.61% Statements 193/213
73.52% Branches 25/34
100% Functions 9/9
90.61% Lines 193/213

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 2131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x     10x 10x 10x     10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 5x 5x 5x 5x 5x 5x                 5x 5x 1x 1x 2x 2x 2x 2x 2x 2x     2x     2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
import {BskyAgent, RichText} from '@atproto/api'
import {descriptionOfPostAuthor, didOfPostAuthor, postLinkOf, postsFilterSearchResults} from "../domain/post.js";
import {getEncodingBufferAndBase64FromUri, isSet, nowISO8601, nowMinusHoursUTCISO} from "../lib/Common.js";
 
export default class BlueSkyService {
    constructor(config, loggerService) {
        this.config = config;
        this.logger = loggerService.getLogger().child({label: 'BlueSkyService'});
        const {identifier, password, service} = config.bluesky
        this.agent = new BskyAgent({service})
        this.api = this.agent.api;// inspired from https://github.com/skyware-js/bot/blob/main/src/bot/Bot.ts#L324
        this.agentConfig = {identifier, password};
    }
 
    login() {
        const bs = this;
        const {identifier, service} = this.config.bluesky
        this.logger.info(`login ${identifier}@${service}`);
        return new Promise((resolve, reject) => {
            bs.agent.login(this.agentConfig)
                .then(loginResponse => {
                    bs.agent.getProfile({"actor": loginResponse.data.did})
                        .then(profileResponse => {
                            bs.profile = profileResponse;
                            resolve();
                        })
                        .catch(e => {
                            bs.logger.error(e);
                            reject(new Error("Failed to fetch bot profile. cf. logs."));
                        });
                })
                .catch(e => {
                    bs.logger.error(e);
                    reject(new Error("Failed to log in — double check your credentials and try again."));
                });
        });
    }
 
    /**
     * search on bluesky :
     * - official doc: https://docs.bsky.app/docs/api/app-bsky-feed-search-posts
     * - advanced search term : https://github.com/bluesky-social/social-app/issues/3378
     * @returns {Promise<void>}
     */
    async searchPosts(options = {}) {
        const {
            searchQuery = "boly38",
            limit = 20,
            sort = "latest",// recent first
            hasImages = false,// does post include embed image
            hasNoReply = false,// does post has 0 reply
            isNotMuted = true,// is post muted by bot
            maxHoursOld = 1// restrict search time window "since" limit
        } = options;
        const since = isSet(maxHoursOld) ? nowMinusHoursUTCISO(maxHoursOld) : null;
        await this.login();
        let params = {q: searchQuery, sort, limit};
        if (isSet(since)) {
            params["since"] = since;
            params["until"] = nowMinusHoursUTCISO(0);
        }
        this.logger.info(`searchPosts ${JSON.stringify(params)}`);
        const response = await this.api.app.bsky.feed.searchPosts(params, {});
        const posts = postsFilterSearchResults(response.data.posts, hasImages, hasNoReply, isNotMuted);
        this.logger.debug(`posts`, JSON.stringify(posts, null, 2));
        return posts;
    }
 
    /**
     * reply to a given POST with a given TEXT
     * bluesky replyTo doc : https://docs.bsky.app/docs/tutorials/creating-a-post#replies
     * To create facets, we may use dedicated tooling.
     *  - https://docs.bsky.app/docs/advanced-guides/posts#mentions-and-links
     *  - https://docs.bsky.app/docs/advanced-guides/post-richtext
     * @param post
     * @param text
     * @param doSimulate
     * @param embed
     * @returns {Promise<unknown>}
     */
    replyTo(post, text, doSimulate, embed = null) {
        const bs = this;
        return new Promise(async (resolve, reject) => {
            const {"uri": parentUri, "cid": parentCid} = post;
            const rootUri = post?.record?.reply?.root?.uri || parentUri;
            const rootCid = post?.record?.reply?.root?.cid || parentCid;
 
            //~ rich format
            const rt = new RichText({text})
            try {
                await rt.detectFacets(bs.agent) // automatically detects mentions and links
            } catch (err) {
                bs.logger.error(`detectFacets error ${err.message}`);
            }
            const replyPost = {
                "reply": {
                    "root": {"uri": rootUri, "cid": rootCid},
                    "parent": {"uri": parentUri, "cid": parentCid}
                },
                "$type": "app.bsky.feed.post",
                text: rt.text,
                facets: rt.facets,
                "createdAt": nowISO8601(), // ex. "2023-08-07T05:49:40.501974Z" OR new Date().toISOString()
            };
            if (embed !== null) {
                replyPost["embed"] = embed;
            }
 
            if (doSimulate) {
                const embedDesc = embed !== null ? `\n[${embed["$type"]}|alt:${embed?.images[0]?.alt}]` : '';
                bs.logger.info(`SIMULATE REPLY TO ${postLinkOf(post)} : ${text}${embedDesc}`);
                return resolve({"uri": "simulated_reply_uri", "cid": "simulated_reply_cid"});
            }
            bs.logger.debug("POST SENT:\n" + JSON.stringify(replyPost, null, 2))
            bs.agent.post(replyPost)
                .then(postReplyResponse => {
                    bs.logger.info(`replyTo response : ${JSON.stringify(postReplyResponse)}`);
                    bs.logger.info(`replyTo ${postLinkOf(post)} : ${text}`);
                    resolve(postReplyResponse);
                })
                .catch(reject)
        });
    }
 
    prepareImageUrlAsBlueskyEmbed(imageUri, imageAltText) {
        const bs = this;
        return new Promise((resolve, reject) => {
            getEncodingBufferAndBase64FromUri(imageUri)
                .then(result => {
                    const {encoding, buffer, base64} = result;
                    if (encoding === undefined) {
                        throw new Error("encoding is undefined");
                    }
                    if (base64?.length < 1) {
                        throw new Error("image is empty");
                    }
                    if (base64?.length > 1000000) {
                        throw new Error(`image file size too large (${base64?.length}). 1000000 bytes maximum`);
                    }
                    bs.logger.debug(`base64.length=${base64?.length} encoding=${encoding}`)
                    // create blueSky blob of image
                    bs.agent.uploadBlob(buffer, {encoding})
                        .then(upBlobResponse => {
                            const {data} = upBlobResponse;
                            const embed = {
                                $type: 'app.bsky.embed.images',
                                images: [ // can be an array up to 4 values
                                    {"alt": imageAltText, "image": data.blob}
                                ]
                            };
                            resolve(embed);
                        })
                        .catch(reject);
                })
                .catch(reject);
        });
    }
 
    async getMutes() {
        await this.login();
        const response = await this.api.app.bsky.graph.getMutes({/* limit: 50 is default */}, {});
        const {mutes} = response?.data || [];
        return mutes;
    }
 
    async safeMuteCandidateAuthor(postAuthor, reason, context) {
        const bs = this;
        const actorDid = didOfPostAuthor(postAuthor);
        const author = descriptionOfPostAuthor(postAuthor);
        await this.login();
        await this.api.app.bsky.graph.muteActor({"actor": actorDid})
            .then(response => bs.logger.info(`mute ${author} (${reason}) - data:${response.data} - success:${response.success}`, context))
            .catch(err => bs.logger.warn(`cant mute ${author} did:${actorDid} (${reason}) - err:${err.message}`, context));
    }
 
    async safeUnMuteMuted(mutedEntry, context) {
        const bs = this;
        const actorDid = didOfPostAuthor(mutedEntry);
        const author = descriptionOfPostAuthor(mutedEntry);
        await this.api.app.bsky.graph.unmuteActor({"actor": actorDid})
            .then(response => bs.logger.info(`un-mute ${author} - data:${response.data} - success:${response.success}`, context))
            .catch(err => bs.logger.warn(`cant un-mute ${author} did:${actorDid}- err:${err.message}`, context));
    }
 
    // https://docs.bsky.app/docs/api/app-bsky-feed-get-post-thread
    async getParentPostOf(uri/* Reference (AT-URI) to post record.*/) {
        const response = await this.api.app.bsky.feed.getPostThread({uri}, {})
        const {data} = response;
        const parent = data?.thread?.parent;
        return parent && parent["$type"] === "app.bsky.feed.defs#threadViewPost" && isSet(parent["post"]) ?
            parent["post"] : null;
    }
 
    /*
    no usage for now
 
    // doc: https://docs.bsky.app/docs/api/app-bsky-actor-get-preferences
    async preferences() {
        await this.login();
        const response = await this.api.app.bsky.actor.getPreferences();
        this.logger.info(`response`, JSON.stringify(response, null, 2));
    }
 
 
    // doc : https://docs.bsky.app/docs/tutorials/viewing-feeds
    async timeline(cursor = "", limit = 5) {
        await this.login();
        const {data} = await this.agent.getTimeline({limit, cursor});
        const {feed: postsArray, cursor: nextPage} = data;
        this.logger.info(`postsArray`, JSON.stringify(postsArray, null, 2));
    }
    */
}