Current section

Files

Jump to
inlay src inlay@mastodon.erl
Raw

src/inlay@mastodon.erl

-module(inlay@mastodon).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/inlay/mastodon.gleam").
-export([detect/2, render/2]).
-file("src/inlay/mastodon.gleam", 58).
-spec detect_mastodon(binary(), gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect_mastodon(Server, Url) ->
case gleam@uri:path_segments(erlang:element(6, Url)) of
[User_with_at, Id] ->
case gleam_stdlib:string_starts_with(User_with_at, <<"@"/utf8>>) of
true ->
User = gleam@string:drop_start(User_with_at, 1),
{some, {mastodon_post, Server, User, Id}};
false ->
none
end;
_ ->
none
end.
-file("src/inlay/mastodon.gleam", 10).
-spec detect(gleam@uri:uri(), inlay@embed:mastodon_config()) -> gleam@option:option(inlay@embed:embed()).
detect(Url, Config) ->
case erlang:element(4, Url) of
{some, Host} ->
case gleam@list:contains(erlang:element(2, Config), Host) of
true ->
detect_mastodon(Host, Url);
false ->
none
end;
none ->
none
end.
-file("src/inlay/mastodon.gleam", 21).
-spec render(inlay@embed:embed(), inlay@embed:config()) -> {ok,
lustre@vdom@vnode:element(any())} |
{error, nil}.
render(Embed, Config) ->
case Embed of
{mastodon_post, Server, User, Id} ->
Width = case erlang:element(13, Config) of
{some, {mastodon_config, _, W, _}} ->
gleam@option:unwrap(W, 400);
none ->
400
end,
Src = <<<<<<<<<<<<"https://"/utf8, Server/binary>>/binary,
"/@"/utf8>>/binary,
User/binary>>/binary,
"/"/utf8>>/binary,
Id/binary>>/binary,
"/embed"/utf8>>,
{ok,
lustre@element@html:'div'(
[],
[lustre@element@html:iframe(
[lustre@attribute:src(Src),
lustre@attribute:class(
<<"mastodon-embed"/utf8>>
),
lustre@attribute:styles(
[{<<"max-width"/utf8>>, <<"100%"/utf8>>},
{<<"border"/utf8>>, <<"0"/utf8>>}]
),
lustre@attribute:width(Width),
lustre@attribute:attribute(
<<"allowfullscreen"/utf8>>,
<<"true"/utf8>>
),
lustre@attribute:attribute(
<<"loading"/utf8>>,
<<"lazy"/utf8>>
),
lustre@attribute:attribute(
<<"sandbox"/utf8>>,
<<"allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"/utf8>>
)]
),
lustre@element@html:script(
[lustre@attribute:src(
<<<<"https://"/utf8, Server/binary>>/binary,
"/embed.js"/utf8>>
),
lustre@attribute:attribute(
<<"async"/utf8>>,
<<"true"/utf8>>
),
lustre@attribute:attribute(
<<"defer"/utf8>>,
<<"true"/utf8>>
)],
<<""/utf8>>
)]
)};
_ ->
{error, nil}
end.