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
This commit is contained in:
fap 2015-06-20 22:40:23 +02:00 committed by Jonne Haß
parent 073f028f88
commit 1061e101fd
9 changed files with 124 additions and 30 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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