~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
233 lines · 8.8 KB Raw History
import Foundation

extension MediaMappingFixture {
    var quotedFacets: [[String: Any]] {
        [
            [
                "index": ["byteStart": quotedLinkStart, "byteEnd": quotedLinkEnd],
                "features": [["$type": "app.bsky.richtext.facet#link", "uri": "https://quoted.example"]]
            ],
            [
                "index": ["byteStart": quotedMentionStart, "byteEnd": quotedMentionEnd],
                "features": [["$type": "app.bsky.richtext.facet#mention", "did": "did:plc:quoted"]]
            ],
            [
                "index": ["byteStart": quotedTagStart, "byteEnd": quotedTagEnd],
                "features": [["$type": "app.bsky.richtext.facet#tag", "tag": "quote"]]
            ]
        ]
    }

    var nestedImage: [String: Any] {
        post(
            "nested-image",
            embed: [
                "$type": "app.bsky.embed.recordWithMedia#view",
                "record": [
                    "record": [
                        "uri": quotedURI, "cid": "quoted-cid",
                        "author": ["did": "did:plc:quoted", "handle": "quoted.example"],
                        "value": ["text": quotedText, "facets": quotedFacets, "createdAt": "2023-01-02T03:04:05Z"],
                        "embeds": [
                            [
                                "$type": "app.bsky.embed.images#view",
                                "images": [
                                    [
                                        "thumb": "https://cdn.example.test/quoted-thumb.jpg",
                                        "fullsize": "https://cdn.example.test/quoted.jpg", "alt": "Quoted photo"
                                    ]
                                ]
                            ],
                            [
                                "$type": "app.bsky.embed.video#view", "cid": "quoted-video-cid",
                                "playlist": "https://video.example.test/quoted.m3u8", "presentation": "gif"
                            ],
                            [
                                "$type": "app.bsky.embed.external#view",
                                "external": [
                                    "uri": "https://example.test/quoted-article", "title": "Article", "description": ""
                                ]
                            ]
                        ]
                    ]
                ],
                "media": [
                    "$type": "app.bsky.embed.images#view",
                    "images": [
                        [
                            "thumb": "https://cdn.example.test/nested.png",
                            "fullsize": "https://cdn.example.test/nested.png", "alt": "Nested photo"
                        ]
                    ]
                ]
            ])
    }

    var nestedVideo: [String: Any] {
        post(
            "nested-video",
            embed: [
                "$type": "app.bsky.embed.recordWithMedia#view",
                "record": [
                    "record": ["uri": "at://did:plc:quoted/app.bsky.feed.post/video-quote", "cid": "video-quote-cid"]
                ],
                "media": [
                    "$type": "app.bsky.embed.video#view",
                    "cid": "nested-video-cid",
                    "playlist": "https://video.example.test/nested.m3u8"
                ]
            ])
    }

    var nestedGIFVideo: [String: Any] {
        post(
            "nested-gif-video",
            embed: [
                "$type": "app.bsky.embed.recordWithMedia#view",
                "record": [
                    "record": [
                        "uri": "at://did:plc:quoted/app.bsky.feed.post/gif-video-quote", "cid": "gif-video-quote-cid"
                    ]
                ],
                "media": [
                    "$type": "app.bsky.embed.video#view",
                    "cid": "nested-animated-cid",
                    "playlist": "https://video.example.test/nested-animated.m3u8",
                    "presentation": "gif"
                ]
            ])
    }

    var directExternal: [String: Any] {
        post(
            "direct-external",
            embed: [
                "$type": "app.bsky.embed.external#view",
                "external": [
                    "uri": "https://example.test/article", "title": "Article title",
                    "description": "Article description"
                ]
            ])
    }

    var textWithExternal: [String: Any] {
        post(
            "text-with-external", text: "An article",
            embed: [
                "$type": "app.bsky.embed.external#view",
                "external": ["uri": "https://example.test/article", "title": "Article", "description": ""]
            ])
    }

    var duplicateExternal: [String: Any] {
        post(
            "duplicate-external", text: "https://example.test/article",
            embed: [
                "$type": "app.bsky.embed.external#view",
                "external": ["uri": "https://example.test/article", "title": "Article", "description": ""]
            ])
    }

    var invalidExternal: [String: Any] {
        post(
            "invalid-external",
            embed: [
                "$type": "app.bsky.embed.external#view",
                "external": ["uri": "javascript:alert(1)", "title": "Invalid", "description": ""]
            ])
    }

    var facetGIFText: String {
        "see animation"
    }

    var externalGIF: [String: Any] {
        post(
            "external-gif",
            embed: [
                "$type": "app.bsky.embed.recordWithMedia#view",
                "record": [
                    "record": [
                        "uri": quotedURI, "author": ["did": "did:plc:quoted", "handle": "quoted.example"],
                        "value": [
                            "text": facetGIFText,
                            "facets": [
                                [
                                    "index": ["byteStart": 0, "byteEnd": facetGIFText.utf8.count],
                                    "features": [
                                        [
                                            "$type": "app.bsky.richtext.facet#link",
                                            "uri": "https://example.test/facet.gif"
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        "embeds": [
                            [
                                "$type": "app.bsky.embed.external#view",
                                "external": [
                                    "uri": "https://example.test/quoted.gif?size=1", "title": "GIF",
                                    "description": "Quoted GIF"
                                ]
                            ]
                        ]
                    ]
                ],
                "media": [
                    "$type": "app.bsky.embed.external#view",
                    "external": [
                        "uri": "https://static.klipy.com/example.gif?hh=498&mp4=abc", "title": "GIF",
                        "description": "A person nods"
                    ]
                ]
            ])
    }

    var textMedia: [String: Any] {
        post(
            "text-media",
            text:
                "GIF https://cdn.example.test/loop.gif video https://cdn.example.test/clip.mp4 "
                + "site https://example.test/article"
        )
    }

    var duplicateTextImage: [String: Any] {
        post(
            "duplicate-text-image",
            text: "https://cdn.example.test/photo.jpg",
            embed: [
                "$type": "app.bsky.embed.images#view",
                "images": [
                    [
                        "thumb": "https://cdn.example.test/photo-thumb.jpg",
                        "fullsize": "https://cdn.example.test/photo.jpg", "alt": "Photo"
                    ]
                ]
            ]
        )
    }

    var responseData: Data {
        get throws {
            try JSONSerialization.data(withJSONObject: [
                "feed": [
                    attachedImage, attachedVideo, attachedGIFVideo, nestedImage, nestedVideo, nestedGIFVideo,
                    directExternal, textWithExternal, duplicateExternal, invalidExternal, externalGIF, textMedia,
                    duplicateTextImage
                ].map { ["post": $0] }
            ])
        }
    }

    func byteRange(of value: String, in text: String) -> (Int, Int) {
        let range = text.range(of: value)!
        let start = range.lowerBound.samePosition(in: text.utf8)!
        let end = range.upperBound.samePosition(in: text.utf8)!
        return (
            text.utf8.distance(from: text.utf8.startIndex, to: start),
            text.utf8.distance(from: text.utf8.startIndex, to: end)
        )
    }
}