diff --git a/Changelog.md b/Changelog.md index 30bd4a812d0142ae45df1f7365ab0be382717cd6..1a1114d587bf8c02d450fb563184e842bb26f723 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 ccdeada87a892529567d43681be8d2d4b1b325e3..611b1cf94905fac12afd8b5d0ea53e31f7c0fe47 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 a4053a2121d2b31a7a8c5bcac0de83f9dae6fb15..5c0778df5a36c3d5c81666ecd2602b75527e0e06 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 bdcc1bc74c33f70a7c79a8f896065fa13d5ec963..a2bbf55d0444cc9ec813242945ad63e141f1ee6a 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 d462389bb8e2965ad28c70b60fc984928f975cf8..c7dcc37894b89d4d754fc99e386020ec7aac66d3 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 3a99fe8ee7cdbdb7a6b08e590014046354be77f3..269834c9a051e5dfab26f57920806aa71137bd98 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 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' + ## Welcome Message settings + welcome_message: ##Section + + ## 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. + #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 378380b8579daceecf0e70acc717729ff77037ea..b29898df51ae024fdfbf60af16789e8ed2491549 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 78425bc463b144c2aba4ca512c058f436ec00abc..690df1bba290cd9601d93027a3a3acc7c02113e2 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 517cacd856c21c9e80b88e59f60f4c71de01a648..5c001abe1021686bcf00c82dcd96192804365793 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