Skip to content

Commit

Permalink
Merge pull request #108 from epochtalk/fix-move-thread
Browse files Browse the repository at this point in the history
Fix move thread
  • Loading branch information
unenglishable authored Sep 10, 2024
2 parents 2bd1345 + 071fcb5 commit b3b0bcc
Show file tree
Hide file tree
Showing 29 changed files with 228 additions and 137 deletions.
6 changes: 3 additions & 3 deletions lib/epochtalk_server/models/auto_moderation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ defmodule EpochtalkServer.Models.AutoModeration do
field :conditions, {:array, :map}
field :actions, {:array, :string}
field :options, :map
field :created_at, :naive_datetime
field :updated_at, :naive_datetime
field :created_at, :naive_datetime_usec
field :updated_at, :naive_datetime_usec
end

## === Changesets Functions ===
Expand Down Expand Up @@ -85,7 +85,7 @@ defmodule EpochtalkServer.Models.AutoModeration do
"""
@spec update_changeset(auto_moderation :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def update_changeset(auto_moderation, attrs \\ %{}) do
updated_at = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
updated_at = NaiveDateTime.utc_now()

auto_moderation =
auto_moderation
Expand Down
10 changes: 5 additions & 5 deletions lib/epochtalk_server/models/ban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ defmodule EpochtalkServer.Models.Ban do
@derive {Jason.Encoder, only: [:user_id, :expiration, :created_at, :updated_at]}
schema "bans" do
belongs_to :user, User, primary_key: true
field :expiration, :naive_datetime
field :created_at, :naive_datetime
field :updated_at, :naive_datetime
field :expiration, :naive_datetime_usec
field :created_at, :naive_datetime_usec
field :updated_at, :naive_datetime_usec
end

## === Changesets Functions ===
Expand All @@ -48,7 +48,7 @@ defmodule EpochtalkServer.Models.Ban do
"""
@spec ban_changeset(ban :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def ban_changeset(ban, attrs \\ %{}) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

attrs =
attrs
Expand All @@ -69,7 +69,7 @@ defmodule EpochtalkServer.Models.Ban do
"""
@spec unban_changeset(ban :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def unban_changeset(ban, attrs \\ %{}) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

# set ban expiration to now when unbanning
attrs =
Expand Down
8 changes: 4 additions & 4 deletions lib/epochtalk_server/models/banned_address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ defmodule EpochtalkServer.Models.BannedAddress do
field :ip4, :integer
field :weight, :decimal
field :decay, :boolean, default: false
field :imported_at, :naive_datetime
field :created_at, :naive_datetime
field :updates, {:array, :naive_datetime}
field :imported_at, :naive_datetime_usec
field :created_at, :naive_datetime_usec
field :updates, {:array, :naive_datetime_usec}
end

## === Changesets Functions ===
Expand All @@ -49,7 +49,7 @@ defmodule EpochtalkServer.Models.BannedAddress do
"""
@spec upsert_changeset(banned_address :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def upsert_changeset(banned_address, attrs \\ %{}) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

attrs =
attrs
Expand Down
8 changes: 4 additions & 4 deletions lib/epochtalk_server/models/board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ defmodule EpochtalkServer.Models.Board do
field :viewable_by, :integer
field :postable_by, :integer
field :right_to_left, :boolean, default: false
field :created_at, :naive_datetime
field :imported_at, :naive_datetime
field :updated_at, :naive_datetime
field :created_at, :naive_datetime_usec
field :imported_at, :naive_datetime_usec
field :updated_at, :naive_datetime_usec
field :meta, :map
many_to_many :category, Category, join_through: BoardMapping
end
Expand Down Expand Up @@ -95,7 +95,7 @@ defmodule EpochtalkServer.Models.Board do
"""
@spec create_changeset(board :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(board, attrs) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

attrs =
attrs
Expand Down
2 changes: 1 addition & 1 deletion lib/epochtalk_server/models/category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule EpochtalkServer.Models.Category do
"""
@spec create_changeset(category :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(category, attrs) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

attrs =
attrs
Expand Down
8 changes: 4 additions & 4 deletions lib/epochtalk_server/models/image_reference.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ defmodule EpochtalkServer.Models.ImageReference do
field :length, :integer
field :type, :string
field :checksum, :string
field :expiration, :naive_datetime
field :created_at, :naive_datetime
field :expiration, :naive_datetime_usec
field :created_at, :naive_datetime_usec
many_to_many :posts, Post, join_through: PostImageReference
# many_to_many :messages, Message, join_through: MessageImageReference
many_to_many :profiles, Profile, join_through: ProfileImageReference
Expand Down Expand Up @@ -76,7 +76,7 @@ defmodule EpochtalkServer.Models.ImageReference do
"""
@spec create_changeset(image_reference :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(image_reference, attrs) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()
uuid = Ecto.UUID.generate()

attrs =
Expand Down Expand Up @@ -174,7 +174,7 @@ defmodule EpochtalkServer.Models.ImageReference do
end

defp query_expired() do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

from i in ImageReference,
where: i.expiration < ^now and i.posts == [] and i.messages == [] and i.profiles == []
Expand Down
4 changes: 2 additions & 2 deletions lib/epochtalk_server/models/invitation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule EpochtalkServer.Models.Invitation do
schema "invitations" do
field :email, :string
field :hash, :string
field :created_at, :naive_datetime
field :created_at, :naive_datetime_usec
end

## === Changesets Functions ===
Expand All @@ -28,7 +28,7 @@ defmodule EpochtalkServer.Models.Invitation do
"""
@spec create_changeset(invitation :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(invitation, attrs \\ %{}) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

attrs =
attrs
Expand Down
4 changes: 2 additions & 2 deletions lib/epochtalk_server/models/mention.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule EpochtalkServer.Models.Mention do
belongs_to :post, Post
belongs_to :mentioner, User
belongs_to :mentionee, User
field :created_at, :naive_datetime
field :created_at, :naive_datetime_usec
field :viewed, :boolean, virtual: true
field :notification_id, :integer, virtual: true
end
Expand All @@ -44,7 +44,7 @@ defmodule EpochtalkServer.Models.Mention do
"""
@spec create_changeset(mention :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(mention, attrs) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

mention =
mention
Expand Down
50 changes: 22 additions & 28 deletions lib/epochtalk_server/models/metadata_board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule EpochtalkServer.Models.MetadataBoard do
field :total_post, :integer
field :total_thread_count, :integer
field :last_post_username, :string
field :last_post_created_at, :naive_datetime
field :last_post_created_at, :naive_datetime_usec
field :last_thread_id, :integer
field :last_thread_title, :string
field :last_post_position, :integer
Expand Down Expand Up @@ -96,60 +96,54 @@ defmodule EpochtalkServer.Models.MetadataBoard do
@spec update_last_post_info(metadata_board :: t(), board_id :: non_neg_integer) :: t()
def update_last_post_info(metadata_board, board_id) do
# query most recent post in thread and it's authoring user's data
last_post_subquery =
last_post_query =
from t in Thread,
left_join: p in Post,
on: t.id == p.thread_id,
left_join: u in User,
on: u.id == p.user_id,
where: t.board_id == ^board_id,
order_by: [desc: p.created_at],
limit: 1,
select: %{
thread_id: p.thread_id,
created_at: p.created_at,
username: u.username,
position: p.position
}

# query most recent thread in board, join last post subquery
last_post_query =
from t in Thread,
left_join: p in Post,
on: p.thread_id == t.id,
left_join: lp in subquery(last_post_subquery),
on: p.thread_id == lp.thread_id,
where: t.board_id == ^board_id,
order_by: [desc: t.created_at],
limit: 1,
select: %{
board_id: t.board_id,
thread_id: t.id,
title: p.content["title"],
username: lp.username,
created_at: lp.created_at,
position: lp.position
}

# update board metadata using queried data
updated_metadata_board =
# query most recent thread's, in board, title
last_post_info =
if lp = Repo.one(last_post_query) do
change(metadata_board,
last_thread_title_query =
from p in Post,
where: p.thread_id == ^lp.thread_id,
order_by: [asc: p.created_at],
limit: 1,
select: p.content["title"]

last_thread_title = Repo.one(last_thread_title_query)

%{
last_post_username: lp.username,
last_post_created_at: lp.created_at,
last_thread_id: lp.thread_id,
last_thread_title: lp.title,
last_thread_title: last_thread_title,
last_post_position: lp.position
)
}
else
change(metadata_board,
%{
last_post_username: nil,
last_post_created_at: nil,
last_thread_id: nil,
last_thread_title: nil,
last_post_position: nil
)
}
end

# update board metadata using queried data
updated_metadata_board = change(metadata_board, last_post_info)

Repo.update!(updated_metadata_board)
end
end
4 changes: 2 additions & 2 deletions lib/epochtalk_server/models/moderation_log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule EpochtalkServer.Models.ModerationLog do
field :action_api_url, :string
field :action_api_method, :string
field :action_obj, :map
field :action_taken_at, :naive_datetime
field :action_taken_at, :naive_datetime_usec
field :action_type, :string
field :action_display_text, :string
field :action_display_url, :string
Expand All @@ -57,7 +57,7 @@ defmodule EpochtalkServer.Models.ModerationLog do
"""
@spec create_changeset(moderation_log :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(moderation_log, attrs) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

display_data = ModerationLogHelper.get_display_data(get_in(attrs, [:action, :type]))

Expand Down
2 changes: 1 addition & 1 deletion lib/epochtalk_server/models/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule EpochtalkServer.Models.Notification do
field :data, :map
field :viewed, :boolean
field :type, :string
field :created_at, :naive_datetime
field :created_at, :naive_datetime_usec
end

## === Changesets Functions ===
Expand Down
2 changes: 1 addition & 1 deletion lib/epochtalk_server/models/poll.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule EpochtalkServer.Models.Poll do
field :question, :string
field :locked, :boolean
field :max_answers, :integer
field :expiration, :naive_datetime
field :expiration, :naive_datetime_usec
field :change_vote, :boolean
field :display_mode, Ecto.Enum, values: [:always, :voted, :expired]
field :has_voted, :boolean, virtual: true
Expand Down
10 changes: 4 additions & 6 deletions lib/epochtalk_server/models/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ defmodule EpochtalkServer.Models.Post do
field :position, :integer
field :content, :map
field :metadata, :map
field :created_at, :naive_datetime
field :updated_at, :naive_datetime
field :imported_at, :naive_datetime
field :created_at, :naive_datetime_usec
field :updated_at, :naive_datetime_usec
field :imported_at, :naive_datetime_usec
# field :smf_message, :map, virtual: true
end

Expand All @@ -62,7 +62,7 @@ defmodule EpochtalkServer.Models.Post do
"""
@spec create_changeset(post :: t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def create_changeset(post, attrs) do
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
now = NaiveDateTime.utc_now()

# set default values and timestamps
post =
Expand Down Expand Up @@ -479,8 +479,6 @@ defmodule EpochtalkServer.Models.Post do

# update updated_at field if outside of 10 min grace period,
# or if a moderator is editing a user's post
now = NaiveDateTime.truncate(now, :second)

attrs =
if (attrs.metadata != nil && Map.keys(attrs.metadata) != []) || outside_edit_window,
do: Map.put(attrs, :updated_at, now),
Expand Down
4 changes: 2 additions & 2 deletions lib/epochtalk_server/models/post_draft.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule EpochtalkServer.Models.PostDraft do
schema "user_drafts" do
belongs_to :user, User
field :draft, :string
field :updated_at, :naive_datetime
field :updated_at, :naive_datetime_usec
end

## === Changeset Functions ===
Expand All @@ -35,7 +35,7 @@ defmodule EpochtalkServer.Models.PostDraft do
"""
@spec upsert_changeset(draft :: PostDraft.t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def upsert_changeset(draft, attrs \\ %{}) do
updated_at = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
updated_at = NaiveDateTime.utc_now()

draft =
draft
Expand Down
6 changes: 3 additions & 3 deletions lib/epochtalk_server/models/profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule EpochtalkServer.Models.Profile do
field :raw_signature, :string
field :post_count, :integer, default: 0
field :fields, :map
field :last_active, :naive_datetime
field :last_active, :naive_datetime_usec
end

## === Changesets Functions ===
Expand Down Expand Up @@ -117,13 +117,13 @@ defmodule EpochtalkServer.Models.Profile do
from(p in Profile, where: p.user_id == ^user_id)
|> Repo.update_all(
set: [
last_active: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
last_active: NaiveDateTime.utc_now()
]
)
end

defp update_if_more_than_one_minute_has_passed(user_id, last_active) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
now = NaiveDateTime.utc_now()
last_active_plus_one_minute = NaiveDateTime.add(last_active, 1, :minute)

at_least_one_minute_has_passed =
Expand Down
6 changes: 3 additions & 3 deletions lib/epochtalk_server/models/role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ defmodule EpochtalkServer.Models.Role do
field :permissions, :map
field :priority_restrictions, {:array, :integer}

field :created_at, :naive_datetime
field :updated_at, :naive_datetime
field :created_at, :naive_datetime_usec
field :updated_at, :naive_datetime_usec
end

## === Changesets Functions ===
Expand All @@ -71,7 +71,7 @@ defmodule EpochtalkServer.Models.Role do
"""
@spec update_changeset(role :: Role.t(), attrs :: map() | nil) :: Ecto.Changeset.t()
def update_changeset(role, attrs \\ %{}) do
updated_at = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
updated_at = NaiveDateTime.utc_now()

role =
role
Expand Down
Loading

0 comments on commit b3b0bcc

Please sign in to comment.