Skip to content

Commit

Permalink
refactor(user-find): move manipulation of user object into user_json …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
akinsey committed Nov 14, 2024
1 parent dfd0a30 commit 0068cf4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
89 changes: 38 additions & 51 deletions lib/epochtalk_server_web/helpers/proxy_conversion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,57 +83,44 @@ defmodule EpochtalkServerWeb.Helpers.ProxyConversion do
end

def build_user(user_id) do
user =
from(u in "smf_members", where: u.id_member == ^user_id)
|> join(:left, [u], a in "smf_attachments",
on: u.id_member == a.id_member and a.attachmentType == 1
)
|> join(:left, [u], m in "smf_membergroups",
on: u.id_group != 0 and u.id_group == m.id_group
)
|> join(:left, [u], g in "smf_membergroups",
on: u.id_post_group == g.id_group
)
|> select([u, a, m, g], %{
activity: u.activity,
created_at: u.dateRegistered * 1000,
dob: u.birthdate,
gender: u.gender,
id: u.id_member,
language: nil,
location: u.location,
merit: u.merit,
id_group: u.id_group,
id_post_group: u.id_post_group,
signature: u.signature,
post_count: u.posts,
name: u.realName,
username: u.realName,
title: u.usertitle,
website: u.websiteUrl,
last_login: u.lastLogin * 1000,
show_online: u.showOnline,
group_name: m.groupName,
group_name_2: g.groupName,
group_color: m.onlineColor,
group_color_2: g.onlineColor,
avatar:
fragment(
"if(? <>'',concat('https://bitcointalk.org/avatars/',?),ifnull(concat('https://bitcointalk.org/useravatars/',?),''))",
u.avatar,
u.avatar,
a.filename
)
})
|> SmfRepo.one()

user
|> Map.put(:position, user.group_name || user.group_name_2)
|> Map.put(:position_color, user.group_color || user.group_color_2)
|> Map.delete(:group_name)
|> Map.delete(:group_name_2)
|> Map.delete(:group_color)
|> Map.delete(:group_color_2)
from(u in "smf_members", where: u.id_member == ^user_id)
|> join(:left, [u], a in "smf_attachments",
on: u.id_member == a.id_member and a.attachmentType == 1
)
|> join(:left, [u], m in "smf_membergroups", on: u.id_group != 0 and u.id_group == m.id_group)
|> join(:left, [u], g in "smf_membergroups", on: u.id_post_group == g.id_group)
|> select([u, a, m, g], %{
activity: u.activity,
created_at: u.dateRegistered * 1000,
dob: u.birthdate,
gender: u.gender,
id: u.id_member,
language: nil,
location: u.location,
merit: u.merit,
id_group: u.id_group,
id_post_group: u.id_post_group,
signature: u.signature,
post_count: u.posts,
name: u.realName,
username: u.realName,
title: u.usertitle,
website: u.websiteUrl,
last_login: u.lastLogin * 1000,
show_online: u.showOnline,
group_name: m.groupName,
group_name_2: g.groupName,
group_color: m.onlineColor,
group_color_2: g.onlineColor,
avatar:
fragment(
"if(? <>'',concat('https://bitcointalk.org/avatars/',?),ifnull(concat('https://bitcointalk.org/useravatars/',?),''))",
u.avatar,
u.avatar,
a.filename
)
})
|> SmfRepo.one()
end

def build_poll(thread_id) do
Expand Down
15 changes: 11 additions & 4 deletions lib/epochtalk_server_web/json/user_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,26 @@ defmodule EpochtalkServerWeb.Controllers.UserJSON do
end

{:ok, last_login} = DateTime.from_unix(user.last_login, :millisecond)
last_login_past_72_hours = DateTime.diff(DateTime.utc_now, last_login, :hour) > 72
last_login_past_72_hours = DateTime.diff(DateTime.utc_now(), last_login, :hour) > 72

last_active = if user.show_online == 1 or last_login_past_72_hours,
do: user.last_login,
else: nil
last_active =
if user.show_online == 1 or last_login_past_72_hours,
do: user.last_login,
else: nil

user
|> Map.put(:signature, parsed_signature)
|> Map.put(:gender, gender)
|> Map.put(:dob, dob)
|> Map.put(:last_active, last_active)
|> Map.put(:position, user.group_name || user.group_name_2)
|> Map.put(:position_color, user.group_color || user.group_color_2)
|> Map.delete(:last_login)
|> Map.delete(:show_online)
|> Map.delete(:group_name)
|> Map.delete(:group_name_2)
|> Map.delete(:group_color)
|> Map.delete(:group_color_2)
end

@doc """
Expand Down

0 comments on commit 0068cf4

Please sign in to comment.