Skip to content

Commit

Permalink
feat(last-active): implement last active, respecting flag showOnline …
Browse files Browse the repository at this point in the history
…if user doesnt want to be shown as active within the past 72 hours
  • Loading branch information
akinsey committed Nov 14, 2024
1 parent e09340f commit b9cefea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/epochtalk_server_web/helpers/proxy_conversion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,18 @@ defmodule EpochtalkServerWeb.Helpers.ProxyConversion do
end

def build_user(user_id) do
last_active =
from m in "smf_messages",
where: m.id_member == ^user_id,
order_by: [desc: m.posterTime],
limit: 1,
select: %{last_active: m.posterTime * 1000}

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
)
|> select([u, a], %{
|> 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,
Expand All @@ -112,6 +111,12 @@ defmodule EpochtalkServerWeb.Helpers.ProxyConversion do
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/',?),''))",
Expand All @@ -122,9 +127,13 @@ defmodule EpochtalkServerWeb.Helpers.ProxyConversion do
})
|> SmfRepo.one()

if user.post_count > 0,
do: Map.merge(user, SmfRepo.one(last_active)),
else: Map.put(user, :last_active, user.created_at)
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)
end

def build_poll(thread_id) do
Expand Down
14 changes: 14 additions & 0 deletions lib/epochtalk_server_web/json/user_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ defmodule EpochtalkServerWeb.Controllers.UserJSON do
_ -> d
end

{:ok, last_login} = DateTime.from_unix(1731427963000, :millisecond)
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

user = if user.title == "",
do: user |> Map.delete(:title),
else: user

user
|> Map.put(:signature, parsed_signature)
|> Map.put(:gender, gender)
|> Map.put(:dob, dob)
|> Map.put(:last_active, last_active)
|> Map.delete(:last_login)
|> Map.delete(:show_online)
end

@doc """
Expand Down

0 comments on commit b9cefea

Please sign in to comment.