From 073f028f88d1989f4149706c7c852bd71966e12e Mon Sep 17 00:00:00 2001 From: fap Date: Sat, 20 Jun 2015 22:39:33 +0200 Subject: [PATCH 1/2] Adds welcome message options to config --- config/defaults.yml | 3 +++ config/diaspora.yml.example | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/config/defaults.yml b/config/defaults.yml index f48bea6fa..d462389bb 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -100,6 +100,9 @@ defaults: enable_registrations: true autofollow_on_join: true autofollow_on_join_user: 'diasporahq@joindiaspora.com' + welcome_message: false + welcome_message_subject: 'Welcome Message' + welcome_message_text: 'Hello [USERNAME], welcome to diaspora*.' invitations: open: true count: 25 diff --git a/config/diaspora.yml.example b/config/diaspora.yml.example index 3fe5f2ca1..3a99fe8ee 100644 --- a/config/diaspora.yml.example +++ b/config/diaspora.yml.example @@ -414,6 +414,23 @@ configuration: ## Section ## please consider resharing diaspora* HQ's posts for your pod's users! #autofollow_on_join_user: 'diasporahq@joindiaspora.com' + ## Welcome Message on registration (default=false) + ## Send a message to new users after registration + ## to tell them about your pod and how things + ## are handled on it. + #welcome_message=false + + ## Welcome Message text (default='Hello [USERNAME], welcome to diaspora.') + ## The content of your welcome message. + ## The placeholder '[USERNAME]' will be replaced by the username + ## of the new user. + #welcome_message_text='Hello [USERNAME], welcome to diaspora.' + + ## Welcome Message subject (default='Welcome Message') + ## The subject of the conversation that is started + ## by your welcome message. + #welcome_message_subject='Welcome Message' + ## Invitation settings invitations: ## Section From 1061e101fde21f7acb7659ff701a4cf6f9c3d0f3 Mon Sep 17 00:00:00 2001 From: fap Date: Sat, 20 Jun 2015 22:40:23 +0200 Subject: [PATCH 2/2] Adds welcome message on registration Welcome message is only send out if podmin account is defined and welcome message is enabled in the config. This also enables the podmin account to send messages to any local user. closes #6128 --- Changelog.md | 1 + app/controllers/registrations_controller.rb | 1 + app/models/conversation.rb | 3 +- app/models/user.rb | 17 ++++++++ config/defaults.yml | 7 +-- config/diaspora.yml.example | 31 +++++++------ lib/postzord/receiver/private.rb | 2 +- spec/models/conversation_spec.rb | 44 ++++++++++++++----- spec/models/user_spec.rb | 48 +++++++++++++++++++++ 9 files changed, 124 insertions(+), 30 deletions(-) diff --git a/Changelog.md b/Changelog.md index 30bd4a812..1a1114d58 100644 --- a/Changelog.md +++ b/Changelog.md @@ -12,6 +12,7 @@ ## Features * Add configuration options for some debug logs [#6090](https://github.com/diaspora/diaspora/pull/6090) +* Send new users a welcome message from the podmin [#6128](https://github.com/diaspora/diaspora/pull/6128) # 0.5.1.1 diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index ccdeada87..611b1cf94 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -14,6 +14,7 @@ class RegistrationsController < Devise::RegistrationsController if @user.sign_up flash[:notice] = I18n.t 'registrations.create.success' @user.seed_aspects + @user.send_welcome_message sign_in_and_redirect(:user, @user) logger.info "event=registration status=successful user=#{@user.diaspora_handle}" else diff --git a/app/models/conversation.rb b/app/models/conversation.rb index a4053a212..5c0778df5 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -24,7 +24,8 @@ class Conversation < ActiveRecord::Base def local_recipients recipients.each do |recipient| if recipient.local? - if recipient.owner.contacts.where(:person_id => self.author.id).count == 0 + unless recipient.owner.contacts.where(person_id: author.id).any? || + (author.owner && author.owner.podmin_account?) errors.add(:all_recipients, "recipient not allowed") end end diff --git a/app/models/user.rb b/app/models/user.rb index bdcc1bc74..a2bbf55d0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -460,6 +460,19 @@ class User < ActiveRecord::Base aq end + def send_welcome_message + return unless AppConfig.settings.welcome_message.enabled? && AppConfig.admins.account? + sender_username = AppConfig.admins.account.get + sender = User.find_by(username: sender_username) + conversation = sender.build_conversation( + participant_ids: [sender.person.id, person.id], + subject: AppConfig.settings.welcome_message.subject.get, + message: {text: AppConfig.settings.welcome_message.text.get % {username: username}}) + if conversation.save + Postzord::Dispatcher.build(sender, conversation).post + end + end + def encryption_key OpenSSL::PKey::RSA.new(serialized_private_key) end @@ -468,6 +481,10 @@ class User < ActiveRecord::Base Role.is_admin?(self.person) end + def podmin_account? + username == AppConfig.admins.account + end + def mine?(target) if target.present? && target.respond_to?(:user_id) return self.id == target.user_id diff --git a/config/defaults.yml b/config/defaults.yml index d462389bb..c7dcc3789 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -100,9 +100,10 @@ defaults: enable_registrations: true autofollow_on_join: true autofollow_on_join_user: 'diasporahq@joindiaspora.com' - welcome_message: false - welcome_message_subject: 'Welcome Message' - welcome_message_text: 'Hello [USERNAME], welcome to diaspora*.' + welcome_message: + enabled: false + subject: 'Welcome Message' + text: 'Hello %{username}, welcome to diaspora*.' invitations: open: true count: 25 diff --git a/config/diaspora.yml.example b/config/diaspora.yml.example index 3a99fe8ee..269834c9a 100644 --- a/config/diaspora.yml.example +++ b/config/diaspora.yml.example @@ -414,22 +414,25 @@ configuration: ## Section ## please consider resharing diaspora* HQ's posts for your pod's users! #autofollow_on_join_user: 'diasporahq@joindiaspora.com' - ## Welcome Message on registration (default=false) - ## Send a message to new users after registration - ## to tell them about your pod and how things - ## are handled on it. - #welcome_message=false + ## Welcome Message settings + welcome_message: ##Section - ## Welcome Message text (default='Hello [USERNAME], welcome to diaspora.') - ## The content of your welcome message. - ## The placeholder '[USERNAME]' will be replaced by the username - ## of the new user. - #welcome_message_text='Hello [USERNAME], welcome to diaspora.' + ## Welcome Message on registration (default=false) + ## Send a message to new users after registration + ## to tell them about your pod and how things + ## are handled on it. + #enabled: false - ## Welcome Message subject (default='Welcome Message') - ## The subject of the conversation that is started - ## by your welcome message. - #welcome_message_subject='Welcome Message' + ## Welcome Message subject (default='Welcome Message') + ## The subject of the conversation that is started + ## by your welcome message. + #subject: "Welcome Message" + + ## Welcome Message text (default='Hello %{username}, welcome to diaspora.') + ## The content of your welcome message. + ## The placeholder "%{username}" will be replaced by the username + ## of the new user. + #text: "Hello %{username}, welcome to diaspora." ## Invitation settings invitations: ## Section diff --git a/lib/postzord/receiver/private.rb b/lib/postzord/receiver/private.rb index 378380b85..b29898df5 100644 --- a/lib/postzord/receiver/private.rb +++ b/lib/postzord/receiver/private.rb @@ -85,7 +85,7 @@ class Postzord::Receiver::Private < Postzord::Receiver end def contact_required_unless_request - unless @object.is_a?(Request) || @user.contact_for(@author) + unless @object.is_a?(Request) || @user.contact_for(@author) || (@author.owner && @author.owner.podmin_account?) logger.error "event=receive status=abort reason='sender not connected to recipient' type=#{@object.class} " \ "recipient=#{@user_person.diaspora_handle} sender=#{@author.diaspora_handle}" return true diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 78425bc46..690df1bba 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -134,19 +134,41 @@ describe Conversation, :type => :model do end end - describe '#invalid parameters' do - before do - @invalid_hash = { - :author => peter.person, - :participant_ids => [peter.person.id, @user1.person.id], - :subject => "cool stuff", - :messages_attributes => [ {:author => peter.person, :text => 'hey'} ] - } + describe "#invalid parameters" do + context "local author" do + before do + @invalid_hash = { + author: peter.person, + participant_ids: [peter.person.id, @user1.person.id], + subject: "cool stuff", + messages_attributes: [{author: peter.person, text: "hey"}] + } + end + + it "is invalid with invalid recipient" do + conversation = Conversation.create(@invalid_hash) + expect(conversation).to be_invalid + end end - it 'with invalid recipient' do - conversation = Conversation.create(@invalid_hash) - expect(conversation).to be_invalid + context "remote author" do + before do + @remote_person = remote_raphael + @local_user = alice + @participant_ids = [@remote_person.id, @local_user.person.id] + + @invalid_hash_remote = { + author: @remote_person, + participant_ids: @participant_ids, + subject: "cool stuff", + messages_attributes: [{author: @remote_person, text: "hey"}] + } + end + + it "is invalid with invalid recipient" do + conversation = Conversation.create(@invalid_hash_remote) + expect(conversation).to be_invalid + end end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 517cacd85..5c001abe1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -927,6 +927,54 @@ describe User, :type => :model do end end + describe "#send_welcome_message" do + let(:user) { FactoryGirl.create(:user) } + let(:podmin) { FactoryGirl.create(:user) } + + context "with welcome message enabled" do + before do + AppConfig.settings.welcome_message.enabled = true + end + + it "should send welcome message from podmin account" do + AppConfig.admins.account = podmin.username + expect { + user.send_welcome_message + }.to change(user.conversations, :count).by(1) + expect(user.conversations.first.author.owner.username).to eq podmin.username + end + + it "should send welcome message text from config" do + AppConfig.admins.account = podmin.username + AppConfig.settings.welcome_message.text = "Hello %{username}, welcome!" + user.send_welcome_message + expect(user.conversations.first.messages.first.text).to eq "Hello #{user.username}, welcome!" + end + + it "should use subject from config" do + AppConfig.settings.welcome_message.subject = "Welcome Message" + AppConfig.admins.account = podmin.username + user.send_welcome_message + expect(user.conversations.first.subject).to eq "Welcome Message" + end + + it "should send no welcome message if no podmin is specified" do + AppConfig.admins.account = "" + user.send_welcome_message + expect(user.conversations.count).to eq 0 + end + end + + context "with welcome message disabled" do + it "shouldn't send a welcome message" do + AppConfig.settings.welcome_message.enabled = false + AppConfig.admins.account = podmin.username + user.send_welcome_message + expect(user.conversations.count).to eq 0 + end + end + end + context "close account" do before do @user = bob