diff --git a/.env.example b/.env.example index 98dcd300..719cdacb 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/app/graphql/mutations/modify_user.rb b/app/graphql/mutations/modify_user.rb index 6eaa81c3..978d955d 100644 --- a/app/graphql/mutations/modify_user.rb +++ b/app/graphql/mutations/modify_user.rb @@ -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 diff --git a/app/graphql/types/user_type.rb b/app/graphql/types/user_type.rb index 34f9d293..4491ff12 100644 --- a/app/graphql/types/user_type.rb +++ b/app/graphql/types/user_type.rb @@ -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? diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 65a06e57..cdd34c48 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -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] diff --git a/app/models/user.rb b/app/models/user.rb index 81a4fda5..80ddd99b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,6 +20,7 @@ # email_verification_token :string(255) # email_verification_sent_at :datetime # name :string(255) +# language :string(255) default("en") # # Indexes # @@ -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') } @@ -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 diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 4eb4d7d5..0159a3bc 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -20,6 +20,7 @@ # email_verification_token :string(255) # email_verification_sent_at :datetime # name :string(255) +# language :string(255) default("en") # # Indexes # diff --git a/app/services/notification/slack.rb b/app/services/notification/slack.rb index 6cf6eaf9..e4308fd6 100644 --- a/app/services/notification/slack.rb +++ b/app/services/notification/slack.rb @@ -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? diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 197c08f9..21eec936 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -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 @@ -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, diff --git a/app/views/user_mailer/submission.html.erb b/app/views/user_mailer/submission.html.erb index 1ec2c3c2..ff4bd088 100644 --- a/app/views/user_mailer/submission.html.erb +++ b/app/views/user_mailer/submission.html.erb @@ -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;} -