Skip to content

Commit a31a8af

Browse files
committed
render claim body
1 parent fd16bef commit a31a8af

File tree

6 files changed

+45
-22
lines changed

6 files changed

+45
-22
lines changed

lib/algora/accounts/accounts.ex

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,15 @@ defmodule Algora.Accounts do
318318
end
319319

320320
def get_random_access_tokens(n) when is_integer(n) and n > 0 do
321-
Identity
322-
|> where([i], i.provider == "github" and not is_nil(i.provider_token))
323-
|> order_by(fragment("RANDOM()"))
324-
|> limit(^n)
325-
|> select([i], i.provider_token)
326-
|> Repo.all()
321+
case Identity
322+
|> where([i], i.provider == "github" and not is_nil(i.provider_token))
323+
|> order_by(fragment("RANDOM()"))
324+
|> limit(^n)
325+
|> select([i], i.provider_token)
326+
|> Repo.all() do
327+
[""] -> []
328+
tokens -> tokens
329+
end
327330
end
328331

329332
defp update_github_token(%User{} = user, new_token) do

lib/algora/integrations/github/behaviour.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ defmodule Algora.Github.Behaviour do
2020
@callback list_repository_events(token(), String.t(), String.t(), keyword()) :: response
2121
@callback list_repository_comments(token(), String.t(), String.t(), keyword()) :: response
2222
@callback add_labels(token(), String.t(), String.t(), integer(), [String.t()]) :: response
23+
@callback render_markdown(token(), String.t()) :: response
2324
end

lib/algora/integrations/github/client.ex

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ defmodule Algora.Github.Client do
77
@type token :: String.t()
88

99
# TODO: move to a separate module and use only for data migration between databases
10-
def http_cached(host, method, path, headers, body) do
10+
def http_cached(host, method, path, headers, body, opts \\ []) do
1111
cache_path = ".local/github/#{path}.json"
1212

1313
with :error <- read_from_cache(cache_path),
14-
{:ok, response_body} <- do_http_request(host, method, path, headers, body) do
14+
{:ok, response_body} <- do_http_request(host, method, path, headers, body, opts) do
1515
write_to_cache(cache_path, response_body)
1616
{:ok, response_body}
1717
else
@@ -20,18 +20,18 @@ defmodule Algora.Github.Client do
2020
end
2121
end
2222

23-
def http(host, method, path, headers, body) do
24-
do_http_request(host, method, path, headers, body)
23+
def http(host, method, path, headers, body, opts \\ []) do
24+
do_http_request(host, method, path, headers, body, opts)
2525
end
2626

27-
defp do_http_request(host, method, path, headers, body) do
27+
defp do_http_request(host, method, path, headers, body, opts) do
2828
url = "https://#{host}#{path}"
2929
headers = [{"Content-Type", "application/json"} | headers]
3030

3131
with {:ok, encoded_body} <- Jason.encode(body),
3232
request = Finch.build(method, url, headers, encoded_body),
3333
{:ok, response} <- Finch.request(request, Algora.Finch) do
34-
handle_response(response)
34+
if opts[:skip_decoding], do: {:ok, response.body}, else: handle_response(response)
3535
end
3636
end
3737

@@ -67,17 +67,19 @@ defmodule Algora.Github.Client do
6767
File.write!(cache_path, Jason.encode!(data))
6868
end
6969

70-
def fetch(access_token, url, method \\ "GET", body \\ nil)
70+
def fetch(access_token, url, method \\ "GET", body \\ nil, opts \\ [])
7171

72-
def fetch(access_token, "https://api.github.com" <> path, method, body), do: fetch(access_token, path, method, body)
72+
def fetch(access_token, "https://api.github.com" <> path, method, body, opts),
73+
do: fetch(access_token, path, method, body, opts)
7374

74-
def fetch(access_token, path, method, body) do
75+
def fetch(access_token, path, method, body, opts) do
7576
http(
7677
"api.github.com",
7778
method,
7879
path,
7980
[{"accept", "application/vnd.github.v3+json"}, {"Authorization", "Bearer #{access_token}"}],
80-
body
81+
body,
82+
opts
8183
)
8284
end
8385

@@ -185,4 +187,9 @@ defmodule Algora.Github.Client do
185187
def add_labels(access_token, owner, repo, number, labels) do
186188
fetch(access_token, "/repos/#{owner}/#{repo}/issues/#{number}/labels", "POST", %{labels: labels})
187189
end
190+
191+
@impl true
192+
def render_markdown(access_token, markdown) do
193+
fetch(access_token, "/markdown", "POST", %{text: markdown}, skip_decoding: true)
194+
end
188195
end

lib/algora/integrations/github/github.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,7 @@ defmodule Algora.Github do
9494

9595
@impl true
9696
def add_labels(token, owner, repo, number, labels), do: client().add_labels(token, owner, repo, number, labels)
97+
98+
@impl true
99+
def render_markdown(token, markdown), do: client().render_markdown(token, markdown)
97100
end

lib/algora/integrations/github/token_pool.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Algora.Github.TokenPool do
33
use GenServer
44

55
alias Algora.Accounts
6+
alias Algora.Github
67

78
require Logger
89

@@ -38,7 +39,7 @@ defmodule Algora.Github.TokenPool do
3839
token = Enum.at(tokens, index)
3940

4041
if token == nil do
41-
{:reply, nil, state}
42+
{:reply, Github.pat(), state}
4243
else
4344
next_index = rem(index + 1, length(tokens))
4445
if next_index == 0, do: refresh_tokens()

lib/algora_web/live/claim_live.ex

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ defmodule AlgoraWeb.ClaimLive do
33
use AlgoraWeb, :live_view
44

55
alias Algora.Bounties.Claim
6+
alias Algora.Github
67
alias Algora.Repo
78

89
@impl true
910
def mount(%{"id" => id}, _session, socket) do
1011
{:ok, claim} = Repo.fetch(Claim, id)
1112

12-
claim = Repo.preload(claim, [:user, source: [repository: [:user]], target: [repository: [:user], bounties: [:owner]]])
13+
claim =
14+
Repo.preload(claim, [
15+
:user,
16+
source: [repository: [:user]],
17+
target: [repository: [:user], bounties: [:owner]]
18+
])
1319

14-
dbg(claim)
1520
{:ok, prize_pool} = claim.target.bounties |> Enum.map(& &1.amount) |> Money.sum()
21+
token = Github.TokenPool.get_token()
22+
{:ok, source_body_html} = Github.render_markdown(token, claim.source.description)
1623

1724
{:ok,
1825
socket
@@ -22,7 +29,8 @@ defmodule AlgoraWeb.ClaimLive do
2229
|> assign(:source, claim.source)
2330
|> assign(:user, claim.user)
2431
|> assign(:bounties, claim.target.bounties)
25-
|> assign(:prize_pool, prize_pool)}
32+
|> assign(:prize_pool, prize_pool)
33+
|> assign(:source_body_html, source_body_html)}
2634
end
2735

2836
@impl true
@@ -133,8 +141,8 @@ defmodule AlgoraWeb.ClaimLive do
133141
<div class="text-sm text-muted-foreground">
134142
{@source.repository.user.provider_login}/{@source.repository.name}#{@source.number}
135143
</div>
136-
<div class="mt-4">
137-
{@source.description}
144+
<div class="mt-4 prose dark:prose-invert">
145+
{Phoenix.HTML.raw(@source_body_html)}
138146
</div>
139147
</div>
140148
</.card_content>

0 commit comments

Comments
 (0)