Add polls
Some checks failed
ci/woodpecker/pr/woodpecker Pipeline failed

This commit is contained in:
Sol Fisher Romanoff 2022-12-03 16:38:52 +02:00
parent 3108e32fa5
commit 7063631787
No known key found for this signature in database
GPG key ID: 9D3F2B64F2341B62
4 changed files with 62 additions and 8 deletions

View file

@ -190,6 +190,13 @@ defp represent(%Activity{object: %Object{data: data}} = activity, selected) do
nil nil
end end
total_votes =
if data["oneOf"] do
Enum.sum(for option <- data["oneOf"], do: option["replies"]["totalItems"])
else
0
end
%{ %{
user: User.sanitize_html(user), user: User.sanitize_html(user),
title: get_title(activity.object), title: get_title(activity.object),
@ -204,7 +211,9 @@ defp represent(%Activity{object: %Object{data: data}} = activity, selected) do
visibility: Visibility.get_visibility(activity.object), visibility: Visibility.get_visibility(activity.object),
reply_to: data["inReplyTo"], reply_to: data["inReplyTo"],
reply_to_user: reply_to_user, reply_to_user: reply_to_user,
edited_at: data["updated"] edited_at: data["updated"],
poll: data["oneOf"],
total_votes: total_votes
} }
end end

View file

@ -45,4 +45,14 @@ def open_content? do
def get_attachment_name(%{"name" => name}), do: name def get_attachment_name(%{"name" => name}), do: name
def get_attachment_name(_), do: "" def get_attachment_name(_), do: ""
def poll_percentage(count, total_votes) do
case count do
0 ->
"0%"
_ ->
Integer.to_string(trunc(count / total_votes * 100)) <> "%"
end
end
end end

View file

@ -66,6 +66,17 @@
<% end %> <% end %>
<div class="status-body"> <div class="status-body">
<%= raw @content %> <%= raw @content %>
<%= if @poll && length(@poll) > 0 do %>
<div class="poll">
<%= for %{"name" => option, "replies" => %{"totalItems" => count}} <- @poll do %>
<div class="poll-option" title="<%= count %>/<%= @total_votes %>">
<span class="percentage"><%= poll_percentage(count, @total_votes) %></span>
<span><%= raw option %></span>
<div class="fill" style="width: <%= poll_percentage(count, @total_votes) %>"></div>
</div>
<% end %>
</div>
<% end %>
<%= if length(@attachment) > 0 do %> <%= if length(@attachment) > 0 do %>
<div class="attachments"> <div class="attachments">
<%= for attachment = %{"url" => [url | _]} <- @attachment do %> <%= for attachment = %{"url" => [url | _]} <- @attachment do %>

View file

@ -26,6 +26,7 @@ :root {
--link: rgba(226, 177, 136, 1); --link: rgba(226, 177, 136, 1);
--text: rgba(185, 185, 186, 1); --text: rgba(185, 185, 186, 1);
--border: rgba(26, 37, 53, 1); --border: rgba(26, 37, 53, 1);
--poll: rgba(99, 84, 72, 1);
} }
@media (prefers-color-scheme: light) { @media (prefers-color-scheme: light) {
:root { :root {
@ -55,6 +56,7 @@ @media (prefers-color-scheme: light) {
--link: rgba(245, 91, 27, 1); --link: rgba(245, 91, 27, 1);
--text: rgba(48, 64, 85, 1); --text: rgba(48, 64, 85, 1);
--border: rgba(216, 230, 249, 1); --border: rgba(216, 230, 249, 1);
--poll: rgba(243, 184, 160, 1);
} }
} }
@ -332,6 +334,28 @@ .nsfw-banner:hover div {
display: none; display: none;
} }
.poll-option {
position: relative;
display: flex;
margin: 0.75em 0.5em;
padding: 0.1em 0.25em;
word-break: break-word;
z-index: 1;
}
.poll-option .percentage {
width: 3.5em;
flex-shrink: 0;
}
.poll-option .fill {
height: 100%;
position: absolute;
background-color: var(--poll);
border-radius: 3px;
top: 0;
left: 0;
z-index: -1;
}
.status-actions { .status-actions {
position: relative; position: relative;
width: 100%; width: 100%;