<commit_before>defmodule Liquio.HtmlDelegationController do
	use Liquio.Web, :controller

	with_params(%{
		:from_identity => {Plugs.CurrentUser, [require: true]},
		:to_identity => {Plugs.ItemParam, [name: "html_identity_id", schema: Identity]},
		:weight => {Plugs.NumberParam, [name: "weight", maybe: true, error: "Delegation weight must be a number"]},
		:topics => {Plugs.ListParam, [name: "topics", maybe: true, item: {Plugs.StringParam, [downcase: true]}]}
	},
	def create(conn, %{:from_identity => from_identity, :to_identity => to_identity, :weight => weight, :topics => topics}) do
		if weight == 0 do
			Delegation.unset(from_identity, to_identity)
			conn
			|> put_flash(:info, "You no longer delegate your votes to this identity.")
			|> redirect(to: default_redirect(conn))
		else
			current_delegation = Delegation.get_by(from_identity, to_identity)
			current_weight = if current_delegation != nil and current_delegation.data != nil do current_delegation.data.weight else nil end

			weight = weight || 1.0
			changeset = Delegation.changeset(%Delegation{}, %{
				from_identity_id: from_identity.id,
				to_identity_id: to_identity.id,
				weight: weight,
				topics: topics
			})
			
			query = from(d in Delegation, where: d.from_identity_id == ^from_identity.id and d.is_last == true and not is_nil(d.data))
			total_weights = query
			|> Repo.all
			|> Enum.map(& &1.data.weight)
			|> Enum.sum
			next_total = (total_weights - (current_weight || 0) + weight)
			ratio = weight / next_total

			changeset
			|> Delegation.set
			|> handle_errors(conn, fn(_delegation) ->
				conn
				|> put_flash(:info, "You now delegate #{round(100 * ratio)}% of your power for those topics.")
				|> redirect(to: default_redirect(conn))
			end)
		end
	end)
end<commit_msg>text fix, cleanup<commit_after>defmodule Liquio.HtmlDelegationController do
	use Liquio.Web, :controller

	with_params(%{
		:from_identity => {Plugs.CurrentUser, [require: true]},
		:to_identity => {Plugs.ItemParam, [name: "html_identity_id", schema: Identity]},
		:weight => {Plugs.NumberParam, [name: "weight", maybe: true, error: "Delegation weight must be a number"]},
		:topics => {Plugs.ListParam, [name: "topics", maybe: true, item: {Plugs.StringParam, [downcase: true]}]}
	},
	def create(conn, %{:from_identity => from_identity, :to_identity => to_identity, :weight => weight, :topics => topics}) do
		if weight == 0 do
			Delegation.unset(from_identity, to_identity)
			conn
			|> put_flash(:info, "You no longer delegate your votes to this identity.")
			|> redirect(to: default_redirect(conn))
		else
			current_delegation = Delegation.get_by(from_identity, to_identity)
			current_weight = if current_delegation != nil and current_delegation.data != nil do current_delegation.data.weight else 0 end

			weight = weight || 1.0
			changeset = Delegation.changeset(%Delegation{}, %{
				from_identity_id: from_identity.id,
				to_identity_id: to_identity.id,
				weight: weight,
				topics: topics
			})
			
			query = from(d in Delegation, where: d.from_identity_id == ^from_identity.id and d.is_last == true and not is_nil(d.data))
			total_weights = query
			|> Repo.all
			|> Enum.map(& &1.data.weight)
			|> Enum.sum
			next_total = (total_weights - current_weight + weight)
			ratio = weight / next_total

			changeset
			|> Delegation.set
			|> handle_errors(conn, fn(_delegation) ->
				conn
				|> put_flash(:info, "You now delegate #{round(100 * ratio)}% of your power to this identity.")
				|> redirect(to: default_redirect(conn))
			end)
		end
	end)
end
{'fraction_non_alphanumeric': 0.13361462728551335, 'fraction_numerical': 0.00450070323488045, 'mean_word_length': 4.331334332833583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5006324', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1181, 'n_words': 298}