Skip to content

Commit

Permalink
feat: add external URL to reddit posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Massolari committed Jan 25, 2024
1 parent 7cdc527 commit b010037
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 17 deletions.
28 changes: 27 additions & 1 deletion src/reddit.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub type Post {
text: String,
score: Int,
media: Result(Media, Nil),
external_url: Result(String, Nil),
)
}

Expand Down Expand Up @@ -122,13 +123,14 @@ fn posts_decoder() -> dynamic.Decoder(List(Post)) {
}

fn post_decoder() -> dynamic.Decoder(Post) {
dynamic.decode5(
dynamic.decode6(
Post,
dynamic.field(named: "id", of: dynamic.string),
dynamic.field(named: "title", of: dynamic.string),
dynamic.field(named: "selftext", of: dynamic.string),
dynamic.field(named: "score", of: dynamic.int),
media_decoder(),
external_url_decoder(),
)
}

Expand Down Expand Up @@ -254,3 +256,27 @@ fn is_url_image(url: String) -> Bool {
fn is_url_gif(url: String) -> Bool {
string.ends_with(url, ".gif") || string.ends_with(url, ".gifv")
}

fn external_url_decoder() -> dynamic.Decoder(Result(String, Nil)) {
fn(json) {
dynamic.field(named: "is_self", of: fn(dynamic_is_self) {
dynamic.bool(dynamic_is_self)
|> result.try(fn(is_self) {
case is_self {
True -> Ok(Error(Nil))
False -> {
dynamic.field(named: "url", of: fn(dynamic_url) {
dynamic.string(dynamic_url)
|> result.map(fn(url) {
case string.contains(url, "reddit") {
True -> Error(Nil)
_ -> Ok(url)
}
})
})(json)
}
}
})
})(json)
}
}
21 changes: 14 additions & 7 deletions src/telegram.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,26 @@ fn video_encode(url: String, post: reddit.Post, chat_id: String) {
}

fn text_encode(post: reddit.Post, chat_id: String) {
let external_url = case post.external_url {
Ok(url) -> "\n" <> url
Error(_) -> ""
}

let text = case post.text {
"" -> ""
_ -> "\n\n" <> post.text
}

json.object([
#(
"text",
json.string(
post.title
<> "\n"
<> "\n"
<> post.text
<> "\n"
<> "\n"
<> external_url
<> text
<> "\n\n"
<> reddit.short_link(post)
<> "\n"
<> "\n"
<> "\n\n"
<> chat_id_as_link(chat_id),
),
),
Expand Down
1 change: 1 addition & 0 deletions test/reddit_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const post = Post(
text: "bar",
score: 10,
media: Error(Nil),
external_url: Error(Nil),
)

pub fn short_link_test() {
Expand Down
82 changes: 73 additions & 9 deletions test/reddit_to_telegram_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,85 @@ import reddit_to_telegram
import reddit

const test_posts = [
reddit.Post(id: "1", title: "a", text: "b", score: 10, media: Error(Nil)),
reddit.Post(id: "2", title: "a2", text: "b2", score: 20, media: Error(Nil)),
reddit.Post(id: "3", title: "a3", text: "b3", score: 30, media: Error(Nil)),
reddit.Post(id: "4", title: "a4", text: "b4", score: 40, media: Error(Nil)),
reddit.Post(id: "5", title: "a5", text: "b5", score: 50, media: Error(Nil)),
reddit.Post(id: "6", title: "a6", text: "b6", score: 60, media: Error(Nil)),
reddit.Post(id: "7", title: "a7", text: "b7", score: 70, media: Error(Nil)),
reddit.Post(id: "8", title: "a8", text: "b8", score: 80, media: Error(Nil)),
reddit.Post(id: "9", title: "a9", text: "b9", score: 90, media: Error(Nil)),
reddit.Post(
id: "1",
title: "a",
text: "b",
score: 10,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "2",
title: "a2",
text: "b2",
score: 20,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "3",
title: "a3",
text: "b3",
score: 30,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "4",
title: "a4",
text: "b4",
score: 40,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "5",
title: "a5",
text: "b5",
score: 50,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "6",
title: "a6",
text: "b6",
score: 60,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "7",
title: "a7",
text: "b7",
score: 70,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "8",
title: "a8",
text: "b8",
score: 80,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "9",
title: "a9",
text: "b9",
score: 90,
media: Error(Nil),
external_url: Error(Nil),
),
reddit.Post(
id: "10",
title: "a10",
text: "b10",
score: 100,
media: Error(Nil),
external_url: Error(Nil),
),
]

Expand Down
1 change: 1 addition & 0 deletions test/telegram_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const reddit_post = reddit.Post(
text: "Post text",
score: 10,
media: Error(Nil),
external_url: Error(Nil),
)

pub fn chat_id_as_link_test() {
Expand Down

0 comments on commit b010037

Please sign in to comment.