Skip to content

Commit

Permalink
Merge branch 'main' into fix-profile-bug-for-stories
Browse files Browse the repository at this point in the history
  • Loading branch information
wintermeyer authored Nov 12, 2024
2 parents cb646c6 + d8bb957 commit d26dc21
Show file tree
Hide file tree
Showing 34 changed files with 2,842 additions and 180 deletions.
5 changes: 5 additions & 0 deletions lib/animina/accounts/resources/credit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ defmodule Animina.Accounts.Credit do
reference :user, on_delete: :delete
reference :donor, on_delete: :delete
end

custom_indexes do
index [:donor_id]
index [:user_id]
end
end

code_interface do
Expand Down
92 changes: 90 additions & 2 deletions lib/animina/accounts/resources/fast_user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,79 @@ defmodule Animina.Accounts.FastUser do
domain Animina.Accounts
define :by_id_email_or_username
define :list
define :public_users_who_created_an_account_in_the_last_60_days
end

actions do
defaults []

action :public_users_who_created_an_account_in_the_last_60_days, :map do
argument :limit, :integer, default: 10
argument :page, :integer, default: 1, constraints: [min: 1]
argument :gender, :string, allow_nil?: false

run fn input, _ ->
date = DateTime.add(DateTime.utc_now(), -60, :day)

# we dont need to filter by id, username or email. So we pass true as filter
# but pass extra filters in the options keyword list

query =
build_query(true, true, true,
filter_by_gender: dynamic([u], u.gender == ^input.arguments.gender),
filter_by_private_profile: dynamic([u], u.is_private == ^false),
filter_by_created_at: dynamic([u], u.created_at >= ^date),
filter_by_state: dynamic([u], u.state == ^:normal or u.state == ^:validated),
filter_by_registration_completed_at:
dynamic([u], is_nil(u.registration_completed_at) == ^false)
)

# query pagination
limit = input.arguments.limit
offset = (input.arguments.page - 1) * input.arguments.limit

# total users query
total_count_query =
from u in user_query(),
where: u.is_private == ^false,
where: u.gender == ^input.arguments.gender,
where: u.state == ^:normal or u.state == ^:validated,
where: u.created_at >= ^date,
where: is_nil(u.registration_completed_at) == ^false,
select: count(u.id)

# merge pagination and count to query
query =
from u in query,
limit: ^limit,
offset: ^offset,
order_by: fragment("RANDOM()"),
select_merge: %{count: subquery(total_count_query)}

# load the results
results =
Repo.all(query)

# get count of total results
count = Enum.at(results, 0, %{}) |> Map.get(:count, 0)

# map results to user struct
results = Enum.map(results, fn user -> to_user_struct(user) end)

results =
struct(Ash.Page.Offset, %{
count: count,
results: results,
offset: offset,
more?: count > input.arguments.page * input.arguments.limit,
page: input.arguments.page,
limit: input.arguments.limit
})

{:ok, results}
end
end

action :list, :map do
argument :limit, :integer, default: 10
argument :page, :integer, default: 1, constraints: [min: 1]
Expand Down Expand Up @@ -63,7 +131,7 @@ defmodule Animina.Accounts.FastUser do
Repo.all(query)

# get count of total results
count = Enum.at(results, 0, %{}) |> Map.get(:count)
count = Enum.at(results, 0, %{}) |> Map.get(:count, 0)

# map results to user struct
results = Enum.map(results, fn user -> to_user_struct(user) end)
Expand Down Expand Up @@ -165,7 +233,7 @@ defmodule Animina.Accounts.FastUser do
update_timestamp :updated_at
end

defp build_query(filter_by_id, filter_by_username, filter_by_email) do
defp build_query(filter_by_id, filter_by_username, filter_by_email, opts \\ []) do
query = user_query()
user_roles_query = user_roles_query()
roles_query = roles_query()
Expand All @@ -174,10 +242,30 @@ defmodule Animina.Accounts.FastUser do
optimized_photo_query = optimized_photo_query()
credit_points_query = credit_points_query()

filter_by_private_profile =
Keyword.get(opts, :filter_by_private_profile, true)

filter_by_gender =
Keyword.get(opts, :filter_by_gender, true)

filter_by_state =
Keyword.get(opts, :filter_by_state, true)

filter_by_created_at =
Keyword.get(opts, :filter_by_created_at, true)

filter_by_registration_completed_at =
Keyword.get(opts, :filter_by_registration_completed_at, true)

from u in query,
where: ^filter_by_username,
where: ^filter_by_id,
where: ^filter_by_email,
where: ^filter_by_private_profile,
where: ^filter_by_state,
where: ^filter_by_gender,
where: ^filter_by_created_at,
where: ^filter_by_registration_completed_at,
left_join: ur in subquery(user_roles_query),
on: u.id == ur.user_id,
left_join: r in subquery(roles_query),
Expand Down
5 changes: 5 additions & 0 deletions lib/animina/accounts/resources/optimized_photo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ defmodule Animina.Accounts.OptimizedPhoto do
reference :user, on_delete: :delete
reference :photo, on_delete: :delete
end

custom_indexes do
index [:photo_id]
index [:user_id]
end
end

code_interface do
Expand Down
5 changes: 5 additions & 0 deletions lib/animina/accounts/resources/photo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ defmodule Animina.Accounts.Photo do
references do
reference :user, on_delete: :delete
end

custom_indexes do
index [:story_id]
index [:user_id]
end
end

state_machine do
Expand Down
6 changes: 6 additions & 0 deletions lib/animina/accounts/resources/photo_flags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ defmodule Animina.Accounts.PhotoFlags do
reference :photo, on_delete: :delete
reference :flag, on_delete: :delete
end

custom_indexes do
index [:photo_id]
index [:user_id]
index [:flag_id]
end
end

code_interface do
Expand Down
5 changes: 5 additions & 0 deletions lib/animina/accounts/resources/reaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ defmodule Animina.Accounts.Reaction do
reference :sender, on_delete: :delete
reference :receiver, on_delete: :delete
end

custom_indexes do
index [:sender_id]
index [:receiver_id]
end
end

code_interface do
Expand Down
18 changes: 14 additions & 4 deletions lib/animina/accounts/resources/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ defmodule Animina.Accounts.User do
table "users"

repo Animina.Repo

custom_indexes do
index [:registration_completed_at]
index [:state]
index [:gender]
index [:created_at]
index [:is_private]
index [:zip_code]
end
end

authentication do
Expand Down Expand Up @@ -413,6 +422,7 @@ defmodule Animina.Accounts.User do
generate_pin_and_email_it(
changeset.attributes.name,
changeset.attributes.email,
changeset.attributes.language,
"create"
)

Expand Down Expand Up @@ -659,19 +669,19 @@ defmodule Animina.Accounts.User do
:ok
end

def generate_pin_and_email_it(name, email, "create") do
def generate_pin_and_email_it(name, email, language, "create") do
new_pin =
generate_pin()

hashed_pin =
hash_pin(email, new_pin)

spawn(fn -> UserEmail.send_pin(name, email, new_pin) end)
spawn(fn -> UserEmail.send_pin(name, email, new_pin, language) end)

hashed_pin
end

def generate_pin_and_email_it(user, "update") do
def generate_pin_and_email_it(user, language, "update") do
new_pin =
generate_pin()

Expand All @@ -684,7 +694,7 @@ defmodule Animina.Accounts.User do
confirmation_pin_attempts: 0
})

spawn(fn -> UserEmail.send_pin(user.name, user.email, new_pin) end)
spawn(fn -> UserEmail.send_pin(user.name, user.email, new_pin, language) end)

user
end
Expand Down
5 changes: 5 additions & 0 deletions lib/animina/accounts/resources/user_role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ defmodule Animina.Accounts.UserRole do
reference :user, on_delete: :delete
reference :role, on_delete: :delete
end

custom_indexes do
index [:role_id]
index [:user_id]
end
end

code_interface do
Expand Down
40 changes: 17 additions & 23 deletions lib/animina/chat_completion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,33 @@ Make sure to always return three messages.
"""

prompt = """
You are a highly skilled language model trained to rewrite and improve user-generated stories by closely following the provided writing style and vocabulary list. Your task is to help users create engaging and personalized stories that they can share with potential partners.
You are a skilled language model trained to rewrite and improve user-generated stories based on the provided writing style and vocabulary list. Your task is to help users create engaging stories while keeping the content concise.
Below is the user's selected headline and their original story content, followed by the reasons they would like the content to be improved.
Below is the user's selected headline, their original story content, and reasons they want improvements:
Headline: #{headline}
Content: #{content}
Reasons for Improvement: #{reasons}
Instructions:
### Instructions:
1. **Maintain Style**: Follow the original story's tone, style, and vocabulary.
2. **Preserve Meaning**: Keep the original intent and key details intact. Do not introduce new ideas or remove essential elements.
3. **Apply Adjustments**: Make improvements based on the user's reasons:
- Fix grammatical, spelling, and punctuation errors.
- Add humor or excitement as requested, while staying appropriate.
- Condense or expand content as specified without changing the core message.
4. **Consistency**: Ensure the story reads smoothly and has no redundancies.
1. Maintain Style: The revised story must follow the provided writing style and vocabulary list. This includes tone, style, sentence structure, and word choice.
2. Preserve Meaning: Keep the original meaning, intent, and key details of the user's story intact. DO NOT introduce new ideas or remove essential information.
3. Reason-Specific Adjustments: Apply changes based on the provided reasons for improvement. For example:
- Correct Errors: Identify and fix any grammatical, spelling, or punctuation mistakes.
- Make Funnier: Add humor while keeping it appropriate and aligned with the original style.
- Make More Exciting: Increase the story's engagement level by enhancing descriptions, adding suspense, or amplifying emotions.
- Lengthen Story: Expand the content with relevant details, anecdotes, or dialogue without deviating from the core message.
- Shorten Story: Condense the content by removing unnecessary details or redundancies while preserving the essence of the story.
4. Consistency: Ensure that the final output reads smoothly, with coherent transitions and consistent pacing throughout.
### Output Constraints:
- The revised story must be **at least 50 characters** and **no more than 1024 characters**, including spaces.
- Return **only the revised story content**.
- Do not include any introductory text, explanations, or extra phrases like "This is the updated story."
- **Strictly limit the response to 1024 characters maximum**, including spaces.
Your response should reflect the improvements made based on the reasons provided by the user and return the updated story. If you are not able to improve the story, please return the original story as it is.
### Generate Output:
Return only the revised story content, ensuring it does not exceed 1024 characters.
You should not return the errors or reasons for improvement in your response, instead focus on enhancing the story content and return the revised version.
Use the same language as the story provided.
Should have a minimum of 50 characters, and a maximum of 1024 characters
You should not include the headline in the response.
You should only return the content of the story.
"""

prompt
Expand Down
4 changes: 4 additions & 0 deletions lib/animina/geo_data/resources/city.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule Animina.GeoData.City do
postgres do
table "geo_data_cities"
repo Animina.Repo

custom_indexes do
index [:zip_code]
end
end

code_interface do
Expand Down
2 changes: 1 addition & 1 deletion lib/animina/narratives/resources/fast_story.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ defmodule Animina.Narratives.FastStory do
Repo.all(query)

# get count of total results
count = Enum.at(results, 0, %{}) |> Map.get(:count)
count = Enum.at(results, 0, %{}) |> Map.get(:count, 0)

# cast photo, user to struct
results =
Expand Down
5 changes: 5 additions & 0 deletions lib/animina/narratives/resources/story.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ defmodule Animina.Narratives.Story do
references do
reference :user, on_delete: :delete
end

custom_indexes do
index [:headline_id]
index [:user_id]
end
end

code_interface do
Expand Down
4 changes: 4 additions & 0 deletions lib/animina/traits/resources/flag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule Animina.Traits.Flag do
postgres do
table "traits_flags"
repo Animina.Repo

custom_indexes do
index [:category_id]
end
end

code_interface do
Expand Down
Loading

0 comments on commit d26dc21

Please sign in to comment.