All files / domain post.js

92.81% Statements 142/153
76.92% Branches 40/52
100% Functions 27/27
92.81% Lines 142/153

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 1531x 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 1x 1x 45x 45x 45x 1x 1x 1x 1x 1x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 47x 47x 47x 47x 47x 55x 55x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 1x 1x 27x 27x 27x       27x 1x 1x 6x 6x 6x     6x 6x 1x 6x 6x     6x 6x 1x 1x 1x 1x 1x 1x 21x 21x     21x 21x 21x 21x 21x 12x 21x 9x 9x     21x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x
/////////////////////////////////////////
// post
/////////////////////////////////////////
// author.avatar       : "https://cdn.bsky.app/img/avatar/plain/did:plc:vqyhh3ajdlni3fl25lfbp7sn/bafkreia6xdt4c6gv3fyoxnftvjcxz5jcyen7rchse5
// jkehrcifgrdpgbbm@jpeg"
// author.displayName       : "Arbrav"
// author.handle            : "arbrav.bsky.social",
 
// embed.$type              : "app.bsky.embed.images#view"
// embed.images[0].alt      : "Petit grenadier en fleur sur fonds de terres cuites, de ciel bleu avec un nuage blanc",
// embed.images[0].fullsize : "https://cdn.bsky.app/img/feed_fullsize/plain/did:plc:vqyhh3ajdlni3fl25lfbp7sn/bafkreigb5bhfekvjfbicriwljvtfc\r\nw4ozfe447solasynlelb5qitnm4de@jpeg"
 
// indexedAt                : "2024-05-18T12:17:34.561Z"
// likeCount                : 1
// replyCount               : 0
// repostCount              : 0
// uri                      : "at://did:plc:vqyhh3ajdlni3fl25lfbp7sn/app.bsky.feed.post/3ksr7u22p6b2m",
//// public bsky.app link   : https://bsky.app/profile/arbrav.bsky.social/post/3ksr7u22p6b2m
// record.$type             : "app.bsky.feed.post",
// record.createdAt         : "2024-05-18T12:17:34.561Z",
//// record.embed.images...  skipped
// record.langs             : ["fr"],
// record.text              : "De la couleur"
 
import {BES_DATE_FORMAT, isSet} from "../lib/Common.js";
import dayjs from "dayjs";
import console from "node:console";
 
const bskyAppPostUriBuilder = (authorHandle, postId) => `https://bsky.app/profile/${authorHandle}/post/${postId}`;
const bskyAppHAndleUriBuilder = authorHandle => `https://bsky.app/profile/${authorHandle}`;
 
export const fromBlueskyPostImage = img => {
    const {alt, fullsize} = img;
    return {alt, fullsize};
}
export const fromBlueskyPostImages = images => images?.map(fromBlueskyPostImage)
/**
 * mapper for post entry
 */
export const fromBlueskyPost = post => {
    const {
        "author": {did, avatar, displayName, handle, "viewer": {muted}},
        embed,
        indexedAt, likeCount, replyCount, repostCount, uri, cid,
        record, viewer
    } = post;
    const result = {
        "author": {did, avatar, displayName, handle, "viewer": {muted}},
        indexedAt, likeCount, replyCount, repostCount, uri, cid,
        "record": {
            "$type": record["$type"],
            "createdAt": record.createdAt,
            "langs": record.langs,
            "text": record.text,
            "reply": record.reply
        },
        viewer
    };
    if (isSet(embed)) {
        result["embed"] = {
            "$type": embed["$type"],
            "images": fromBlueskyPostImages(embed.images)
        };
    }
    return result;
}
/**
 * mapper for posts list
 */
export const fromBlueskyPosts = posts => posts?.map(fromBlueskyPost)
 
export const firstImageOf = post => !isSet(post?.embed) ? null : post?.embed.images[0];
export const postLinkOf = post => {
    const {uri, author: {handle}} = post;
    const id = uri.split("/app.bsky.feed.post/")[1];
    return isSet(id) && isSet(handle) ? bskyAppPostUriBuilder(handle, id) : null;
}
 
export const toLocaleDate = blueskyDate => {
    try {
        return dayjs(blueskyDate).format(BES_DATE_FORMAT);
    } catch (err) {
        console.log(err);// should not happen
        return blueskyDate;
    }
}
 
export const postInfoOf = post => {
    const {author: {handle, displayName}, record: {text, createdAt}} = post;
    const recordCreatedAt = createdAt ? toLocaleDate(createdAt) : "";
    if (!handle || !text || !recordCreatedAt) {
        return "";
    }
    return `${recordCreatedAt} --- ${displayName} (@${handle}): ${text}`;
}
export const postImageOf = postImage => {
    const {fullsize, alt} = postImage;
    if (!fullsize) {
        return "";
    }
    return alt ? `${fullsize} --- ${alt}` : fullsize;
}
 
export const postHtmlOf = post => postFormatterOf(post, "html");
export const postTextOf = post => postFormatterOf(post, "text");
 
export const htmlLink = (label, href) => `<a href="${href}">${label}</a>`;
export const postFormatterOf = (post, format = "text") => {
    const {author: {handle, displayName}, record: {text, createdAt}} = post;
    if (!post || !handle || !text || !createdAt) {
        return "";
    }
    const username = isSet(displayName) ? displayName : handle;
    const postDate = createdAt ? toLocaleDate(createdAt) : "";
    const postLink = postLinkOf(post);
    const handleLink = bskyAppHAndleUriBuilder(handle);
    if (format === "text") {
        return `${postLink} ${postDate} by @${username} (${handleLink}) --- ${text}`;
    } else if (format === "html") {
        return `${htmlLink(postDate, postLink)} --- ${htmlLink(username, handleLink)}: ${text}`;
    } else {
        throw new Error(`unsupported format ${format}`);
    }
}
 
export const postAuthorOf = post => post?.author;
export const displayNameOfPostAuthor = postAuthor => postAuthor?.displayName;
export const handleOfPostAuthor = postAuthor => postAuthor?.handle;
export const didOfPostAuthor = postAuthor => postAuthor?.did;
 
export const descriptionOfPostAuthor = postAuthor => {
    const displayName = displayNameOfPostAuthor(postAuthor);
    const handle = handleOfPostAuthor(postAuthor);
    return handle === displayName ? `@${handle}` : `${displayName}(@${handle})`;
}
 
export const filterWithEmbedImageView = p => p?.embed?.$type === "app.bsky.embed.images#view"
export const fiterWithNoReply = p => p?.replyCount === 0
export const fiterWithNotMuted = p => p?.author?.viewer?.muted === false
export const fiterWithNoReplyDisabled = p => p?.viewer?.replyDisabled !== true
 
export const postsFilterSearchResults = (responsePosts, hasImages, hasNoReply, isNotMuted) => {
    let posts = fromBlueskyPosts(responsePosts);
    posts = hasImages ? posts?.filter(filterWithEmbedImageView) : posts;
    posts = hasNoReply ? posts?.filter(fiterWithNoReply) : posts;
    posts = isNotMuted ? posts?.filter(fiterWithNotMuted) : posts;
    // undertakes public viewer replyDisabled rule // https://github.com/bluesky-social/atproto/discussions/2576
    posts = posts?.filter(fiterWithNoReplyDisabled);
    return posts;
}
 
export const txtOfPosts = (posts, max = 5) => posts.slice(0,max).map(postTextOf).join("\n\n")
export const htmlOfPosts = (posts, max = 5) => posts.slice(0,max).map(postHtmlOf).join("<br/><br/>")