mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
add anonyhash
This commit is contained in:
parent
e231451abd
commit
f21eaa87bb
7 changed files with 63 additions and 1 deletions
|
@ -15,6 +15,7 @@ config :bcrypt_elixir,
|
|||
log_rounds: String.to_integer(System.get_env("BCRYPT_ROUNDS") || "12")
|
||||
|
||||
config :philomena,
|
||||
anonymous_name_salt: System.get_env("ANONYMOUS_NAME_SALT"),
|
||||
password_pepper: System.get_env("PASSWORD_PEPPER"),
|
||||
otp_secret_key: System.get_env("OTP_SECRET_KEY"),
|
||||
image_url_root: System.get_env("IMAGE_URL_ROOT"),
|
||||
|
|
17
lib/philomena/attribution.ex
Normal file
17
lib/philomena/attribution.ex
Normal file
|
@ -0,0 +1,17 @@
|
|||
defprotocol Philomena.Attribution do
|
||||
@doc """
|
||||
Provides the "parent object" identifier for this object. This is so
|
||||
that anonymous posts under the same topic id can return the same hash
|
||||
for the same user.
|
||||
"""
|
||||
@spec object_identifier(struct()) :: String.t()
|
||||
def object_identifier(object)
|
||||
|
||||
@doc """
|
||||
Provides the "best" user identifier for an object. Usually this will be
|
||||
the user_id, but may also be the fingerprint or IP address if other
|
||||
information is unavailable.
|
||||
"""
|
||||
@spec best_user_identifier(struct()) :: String.t()
|
||||
def best_user_identifier(object)
|
||||
end
|
9
lib/philomena/comments/attribution.ex
Normal file
9
lib/philomena/comments/attribution.ex
Normal file
|
@ -0,0 +1,9 @@
|
|||
defimpl Philomena.Attribution, for: Philomena.Comments.Comment do
|
||||
def object_identifier(comment) do
|
||||
to_string(comment.image_id || comment.id)
|
||||
end
|
||||
|
||||
def best_user_identifier(comment) do
|
||||
to_string(comment.user_id || comment.fingerprint || comment.ip)
|
||||
end
|
||||
end
|
9
lib/philomena/images/attribution.ex
Normal file
9
lib/philomena/images/attribution.ex
Normal file
|
@ -0,0 +1,9 @@
|
|||
defimpl Philomena.Attribution, for: Philomena.Images.Image do
|
||||
def object_identifier(image) do
|
||||
to_string(image.id)
|
||||
end
|
||||
|
||||
def best_user_identifier(image) do
|
||||
to_string(image.user_id || image.fingerprint || image.ip)
|
||||
end
|
||||
end
|
9
lib/philomena/posts/attribution.ex
Normal file
9
lib/philomena/posts/attribution.ex
Normal file
|
@ -0,0 +1,9 @@
|
|||
defimpl Philomena.Attribution, for: Philomena.Posts.Post do
|
||||
def object_identifier(post) do
|
||||
to_string(post.topic_id || post.id)
|
||||
end
|
||||
|
||||
def best_user_identifier(post) do
|
||||
to_string(post.user_id || post.fingerprint || post.ip)
|
||||
end
|
||||
end
|
|
@ -2,4 +2,5 @@
|
|||
strong<> = @object.user.name
|
||||
- else
|
||||
strong<>
|
||||
' Background Pony
|
||||
| Background Pony #
|
||||
= anonymous_name(@object)
|
|
@ -1,3 +1,19 @@
|
|||
defmodule PhilomenaWeb.UserAttributionView do
|
||||
alias Philomena.Attribution
|
||||
use Bitwise
|
||||
use PhilomenaWeb, :view
|
||||
|
||||
def anonymous_name(object) do
|
||||
salt = anonymous_name_salt()
|
||||
id = Attribution.object_identifier(object)
|
||||
user_id = Attribution.best_user_identifier(object)
|
||||
|
||||
(:erlang.crc32(salt <> id <> user_id) &&& 0xffff)
|
||||
|> Integer.to_string(16)
|
||||
end
|
||||
|
||||
defp anonymous_name_salt do
|
||||
Application.get_env(:philomena, :anonymous_name_salt)
|
||||
|> to_string()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue