Skip to content

Commit

Permalink
Support for language preferences (#34)
Browse files Browse the repository at this point in the history
* feat: Support for language preferences
---------

Signed-off-by: 魏宏斌 <[email protected]>
  • Loading branch information
weihongbin1 authored Jun 6, 2023
1 parent b570c2b commit 913b397
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 96 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export MAX_EMAIL_CHANGE_COUNT=3
export MAX_SEND_EMAIL_COUNT=3

# Notification
export NOTIFICATION_URL=https://compass.gitee.com
export NOTIFICATION_URL=https://oss-compass.org
export NOTIFICATION_ZH_URL=https://compass.gitee.com
export NOTIFICATION_ANALYZE_URL=/analyze
export NOTIFICATION_ABOUT_URL=/docs/community
export NOTIFICATION_SUBSCRIPTION_URL=/settings/subscribe
Expand Down
11 changes: 7 additions & 4 deletions app/graphql/mutations/modify_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ class ModifyUser < BaseMutation

argument :name, String, required: true, description: 'user name'
argument :email, String, required: true, description: 'user email'
argument :language, String, required: false, description: 'user language'

def resolve(name:, email:)
def resolve(name:, email:, language:)
current_user = context[:current_user]
raise GraphQL::ExecutionError.new I18n.t('users.require_login') if current_user.blank?

current_user.update!(name: name, email: email)
update_attrs = { name: name, email: email }
update_attrs[:language] = language if language.present?
current_user.update!(update_attrs)

OpenStruct.new({ status: true, message: '' })
{ status: true, message: '' }
rescue => ex
OpenStruct.new({ status: false, message: ex.message })
{ status: false, message: ex.message }
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class UserType < Types::BaseObject
field :email, String, null: false
field :email_verified, Boolean, null: false
field :subscriptions, Types::Subscription::SubscriptionPageType, null: false, resolver: Queries::SubscriptionsQuery
field :language, String, null: false

def email_verified
object.email_verified?
Expand Down
9 changes: 5 additions & 4 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ def email_verification

def subscription_update
submission_params
mail(to: @user.email, subject: "OSS Compass project subscription update - #{@subject_name}")
mail(to: @user.email, subject: "#{I18n.t('notification.email.project_subscription_update', locale: @user.language)} - #{@subject_name}")
end

def submission
submission_params
mail(to: @user.email, subject: 'OSS Compass project subscription')
mail(to: @user.email, subject: I18n.t('notification.email.project_subscription', locale: @user.language))
end

def subscription_create
submission_params
mail(to: @user.email, subject: 'OSS Compass project subscription')
mail(to: @user.email, subject: I18n.t('notification.email.project_subscription', locale: @user.language))
end

def subscription_delete
submission_params
mail(to: @user.email, subject: 'OSS Compass project unsubscription')
mail(to: @user.email, subject: I18n.t('notification.email.project_unsubscription', locale: @user.language))
end

def submission_params
@locale = params[:user].language
@user = params[:user]
@subject_name = params[:subject_name]
@subject_url = params[:subject_url]
Expand Down
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# email_verification_token :string(255)
# email_verification_sent_at :datetime
# name :string(255)
# language :string(255) default("en")
#
# Indexes
#
Expand All @@ -37,6 +38,7 @@ class User < ApplicationRecord
has_many :subscriptions, dependent: :destroy

validate :check_email_change_limit
after_initialize :set_default_language, if: :new_record?

after_update :send_email_verification, if: -> { saved_changes.keys.include?('email') }

Expand Down Expand Up @@ -171,4 +173,9 @@ def check_email_change_limit
max_count = ENV.fetch('MAX_EMAIL_CHANGE_COUNT') { 3 }.to_i
errors.add(:base, I18n.t("users.email_change_limit", count: max_count)) if count > max_count
end

def set_default_language
self.language = I18n.locale
self.language ||= I18n.default_locale
end
end
1 change: 1 addition & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# email_verification_token :string(255)
# email_verification_sent_at :datetime
# name :string(255)
# language :string(255) default("en")
#
# Indexes
#
Expand Down
60 changes: 9 additions & 51 deletions app/services/notification/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,25 @@ def execute
client = Slack::Web::Client.new
client.chat_postMessage(
channel: uid,
text: send("#{notification_type}_context"),
text: send("#{notification_type}_content"),
mrkdwn: true
)
end

def subscription_update_context
"## OSS Compass project subscription update - #{subject_name}
Hi #{user.name},
There has been a recent update to the project report you subscribed to on OSS Compass, as follows:
- [#{subject_name}](#{subject_url})
Click the link above to view the updated report. If you need to manage your OSS Compass project subscription, [please click here](#{subscription_url}).
For more insight into open source software project analysis, visit: [OSS Compass](#{explore_url}).
[OSS Compass team](#{about_url})
"
def subscription_update_content
I18n.t('notification.slack.subscription_update_content', locale: user.language, user_name: user.name, subject_name: subject_name, subject_url: subject_url, subscription_url: subscription_url, explore_url: explore_url, about_url: about_url)
end

def submission_context
"## OSS Compass project subscription
Hi #{user.name},
Your analysis request for [#{subject_name}](#{subject_url}) on the OSS Compass website has been submitted and subscribed. We will synchronize the relevant report information with you after we confirm and complete the analysis.
To manage OSS Compass project subscriptions, [please click here](#{subscription_url}).
For more insight into open source software project analysis, visit: [OSS Compass](#{explore_url}).
[OSS Compass team](#{about_url})
"
def submission_content
I18n.t('notification.slack.submission_content', locale: user.language, user_name: user.name, subject_name: subject_name, subject_url: subject_url, subscription_url: subscription_url, explore_url: explore_url, about_url: about_url)
end

def subscription_create_context
"## OSS Compass project subscription
Hi #{user.name},
You have successfully subscribed to the analysis report of the [#{subject_name}](#{subject_url}). We will update the report information with you when the project report is updated.
To manage OSS Compass project subscriptions, [please click here](#{subscription_url}).
For more insight into open source software project analysis, visit: [OSS Compass](#{explore_url}).
[OSS Compass team](#{about_url})
"
def subscription_create_content
I18n.t('notification.slack.subscription_create_content', locale: user.language, user_name: user.name, subject_name: subject_name, subject_url: subject_url, subscription_url: subscription_url, explore_url: explore_url, about_url: about_url)
end

def subscription_delete_context
"## OSS Compass project unsubscription
Hi #{user.name},
You have successfully unsubscribed the analysis report for the [#{subject_name}](#{subject_url}).
To manage OSS Compass project subscriptions, [please click here](#{subscription_url}).
For more insight into open source software project analysis, visit: [OSS Compass](#{explore_url}).
[OSS Compass team](#{about_url})
"
def subscription_delete_content
I18n.t('notification.slack.subscription_delete_content', locale: user.language, user_name: user.name, subject_name: subject_name, subject_url: subject_url, subscription_url: subscription_url, explore_url: explore_url, about_url: about_url)
end

def enabled?
Expand Down
12 changes: 8 additions & 4 deletions app/services/notification_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ def enabled?
false
end

def notification_url
user.language == 'zh-CN' ? ENV['NOTIFICATION_ZH_URL'] : ENV['NOTIFICATION_URL']
end

def explore_url
"#{ENV['NOTIFICATION_URL']}#{ENV['NOTIFICATION_EXPLORE_URL']}"
"#{notification_url}#{ENV['NOTIFICATION_EXPLORE_URL']}"
end

def subscription_url
"#{ENV['NOTIFICATION_URL']}#{ENV['NOTIFICATION_SUBSCRIPTION_URL']}"
"#{notification_url}#{ENV['NOTIFICATION_SUBSCRIPTION_URL']}"
end

def about_url
"#{ENV['NOTIFICATION_URL']}#{ENV['NOTIFICATION_ABOUT_URL']}"
"#{notification_url}#{ENV['NOTIFICATION_ABOUT_URL']}"
end

def subject_name
Expand All @@ -53,7 +57,7 @@ def subject_name
end

def subject_url
compass_analyze_url = "#{ENV['NOTIFICATION_URL']}#{ENV['NOTIFICATION_ANALYZE_URL']}"
compass_analyze_url = "#{notification_url}#{ENV['NOTIFICATION_ANALYZE_URL']}"
compass_analyze_uri = Addressable::URI.parse(compass_analyze_url)
compass_analyze_uri.query_values = {
label: params[:subject].label,
Expand Down
15 changes: 8 additions & 7 deletions app/views/user_mailer/submission.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
td,th,div,p,a,h1,h2,h3,h4,h5,h6 {font-family: "Segoe UI", sans-serif; mso-line-height-rule: exactly;}
</style>
<![endif]-->
<title>OSS Compass project subscription</title>
<title><%= I18n.t('notification.email.project_subscription') %></title>
<style>
img {
max-width: 100%;
Expand Down Expand Up @@ -63,27 +63,28 @@
<tr>
<td class="sm-px-6" style="border: 1px solid #cfcfcf; background-color: #fff; padding: 32px 40px; font-size: 16px; color: #334155; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05)">
<h1 class="sm-leading-8" style="margin: 0 0 20px; font-size: 20px; font-weight: 600; color: #000">
OSS Compass project subscription
<%= I18n.t('notification.email.project_subscription') %>
</h1>

<div>
Hi <%= @user.name %>,
</div>

<div style="padding-top: 20px;">
&nbsp;&nbsp;&nbsp;&nbsp; You analysis request for <a href="<%= @subject_url %>"><%= @subject_name %></a> on the OSS Compass website has been submitted and subscribed. We will synchronize the relevant report information with you after we confirm and complete the analysis.
<%= raw I18n.t('notification.email.submission_description', subject_url: @subject_url, subject_name: @subject_name) %>
</div>

<div style="padding-top: 20px;">
Click the link above to view the updated report. If you need to manage your OSS Compass project subscription, <a href="<%= @subscription_url %>">please click here</a>.
<%= raw I18n.t('notification.email.manage_subscription',subscription_url: @subscription_url) %>
</div>

<div style="padding-top: 20px;font-size: 14px;">
For more insight into open source software project analysis, visit: <a href="<%= @explore_url %>">OSS Compass</a>.
<%= I18n.t('notification.email.more_info') %>
<a href="<%= @explore_url %>">OSS Compass</a>.
</div>

<div style="text-align: right; padding-top: 25px;">
<a href="<%= @about_url %>">OSS Compass team</a>
<div style="text-align: right;padding-top: 20px;">
<a href="<%= @about_url %>">OSS Compass <%= I18n.t('notification.email.team') %></a>
</div>

</td>
Expand Down
15 changes: 6 additions & 9 deletions app/views/user_mailer/subscription_create.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
</style>
<![endif]-->
<title>OSS Compass project subscription</title>
<title><%= I18n.t('notification.email.project_subscription') %></title>
<style>
img {
max-width: 100%;
Expand Down Expand Up @@ -70,31 +70,28 @@
<tr>
<td class="sm-px-6" style="border: 1px solid #cfcfcf; background-color: #fff; padding: 32px 40px; font-size: 16px; color: #334155; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05)">
<h1 class="sm-leading-8" style="margin: 0 0 20px; font-size: 20px; font-weight: 600; color: #000">
OSS Compass project subscription
<%= I18n.t('notification.email.project_subscription') %>
</h1>

<div>
Hi <%= @user.name %>,
</div>

<div style="padding-top: 20px;">
&nbsp;&nbsp;&nbsp;&nbsp; You have successfully subscribed to the analysis report of the
<a href="<%= @subject_url %>"><%= @subject_name %></a>. We will update the report information with you
when the project report is updated.
&nbsp;<%= raw I18n.t('notification.email.subscription_create_description', subject_url: @subject_url, subject_name: @subject_name) %>
</div>

<div style="padding-top: 20px;">
Click the link above to view the updated report. If you need to manage your OSS Compass project
subscription, <a href="<%= @subscription_url %>">please click here</a>.
<%= raw I18n.t('notification.email.manage_subscription',subscription_url: @subscription_url) %>
</div>

<div style="padding-top: 20px;font-size: 14px;">
For more insight into open source software project analysis, visit:
<%= I18n.t('notification.email.more_info') %>
<a href="<%= @explore_url %>">OSS Compass</a>.
</div>

<div style="text-align: right;padding-top: 20px;">
<a href="<%= @about_url %>">OSS Compass team</a>
<a href="<%= @about_url %>">OSS Compass <%= I18n.t('notification.email.team') %></a>
</div>

</td>
Expand Down
14 changes: 6 additions & 8 deletions app/views/user_mailer/subscription_delete.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
</style>
<![endif]-->
<title>OSS Compass project unsubscription</title>
<title><%= I18n.t('notification.email.project_unsubscription') %></title>
<style>
img {
max-width: 100%;
Expand Down Expand Up @@ -70,30 +70,28 @@
<tr>
<td class="sm-px-6" style="border: 1px solid #cfcfcf; background-color: #fff; padding: 32px 40px; font-size: 16px; color: #334155; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05)">
<h1 class="sm-leading-8" style="margin: 0 0 20px; font-size: 20px; font-weight: 600; color: #000">OSS
Compass project unsubscription
<%= I18n.t('notification.email.project_unsubscription') %>
</h1>

<div>
Hi <%= @user.name %>,
</div>

<div style="padding-top: 20px;">
&nbsp;&nbsp;&nbsp;&nbsp; You have successfully unsubscribed the analysis report for the
<a href="<%= @subject_url %>"><%= @subject_name %></a>.
<%= raw I18n.t('notification.email.subscription_delete_description', subject_url: @subject_url, subject_name: @subject_name) %>
</div>

<div style="padding-top: 20px;">
Click the link above to view the updated report. If you need to manage your OSS Compass project
subscription, <a href="<%= @subscription_url %>">please click here</a>.
<%= raw I18n.t('notification.email.manage_subscription',subscription_url: @subscription_url) %>
</div>

<div style="padding-top: 20px;font-size: 14px;">
For more insight into open source software project analysis, visit:
<%= I18n.t('notification.email.more_info') %>
<a href="<%= @explore_url %>">OSS Compass</a>.
</div>

<div style="text-align: right;padding-top: 20px;">
<a href="<%= @about_url %>">OSS Compass team</a>
<a href="<%= @about_url %>">OSS Compass <%= I18n.t('notification.email.team') %></a>
</div>

</td>
Expand Down
Loading

0 comments on commit 913b397

Please sign in to comment.