mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:04:11 +08:00
FEATURE: backend for inviting a user to a group
This commit is contained in:
parent
77d68ccf08
commit
34d1668f9f
|
@ -22,6 +22,5 @@ end
|
|||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_badges_on_badge_type_id (badge_type_id)
|
||||
# index_badges_on_name (name) UNIQUE
|
||||
# index_badges_on_name (name) UNIQUE
|
||||
#
|
||||
|
|
|
@ -6,12 +6,12 @@ end
|
|||
#
|
||||
# Table name: category_custom_fields
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# category_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# category_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -49,3 +49,16 @@ class ColorScheme < ActiveRecord::Base
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: color_schemes
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string(255) not null
|
||||
# enabled :boolean default(FALSE), not null
|
||||
# versioned_id :integer
|
||||
# version :integer default(1), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
|
|
|
@ -97,3 +97,20 @@ class ColorSchemeColor < ActiveRecord::Base
|
|||
bookmarkColor: "00aaff"
|
||||
}
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: color_scheme_colors
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string(255) not null
|
||||
# hex :string(255) not null
|
||||
# opacity :integer default(100), not null
|
||||
# color_scheme_id :integer not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_color_scheme_colors_on_color_scheme_id (color_scheme_id)
|
||||
#
|
||||
|
|
|
@ -243,6 +243,7 @@ end
|
|||
# automatic :boolean default(FALSE), not null
|
||||
# user_count :integer default(0), not null
|
||||
# alias_level :integer default(0)
|
||||
# visible :boolean default(TRUE), not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -6,12 +6,12 @@ end
|
|||
#
|
||||
# Table name: group_custom_fields
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# group_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# group_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -5,6 +5,8 @@ class Invite < ActiveRecord::Base
|
|||
belongs_to :topic
|
||||
belongs_to :invited_by, class_name: 'User'
|
||||
|
||||
has_many :invited_groups
|
||||
has_many :groups, through: :invited_groups
|
||||
has_many :topic_invites
|
||||
has_many :topics, through: :topic_invites, source: :topic
|
||||
validates_presence_of :email
|
||||
|
|
|
@ -34,6 +34,7 @@ InviteRedeemer = Struct.new(:invite) do
|
|||
def process_invitation
|
||||
add_to_private_topics_if_invited
|
||||
add_user_to_invited_topics
|
||||
add_user_to_groups
|
||||
send_welcome_message
|
||||
approve_account_if_needed
|
||||
notify_invitee
|
||||
|
@ -75,6 +76,12 @@ InviteRedeemer = Struct.new(:invite) do
|
|||
end
|
||||
end
|
||||
|
||||
def add_user_to_groups
|
||||
invite.groups.each do |g|
|
||||
invited_user.group_users.create(group_id: g.id)
|
||||
end
|
||||
end
|
||||
|
||||
def send_welcome_message
|
||||
if Invite.where(['email = ?', invite.email]).update_all(['user_id = ?', invited_user.id]) == 1
|
||||
invited_user.send_welcome_message = true
|
||||
|
|
15
app/models/invited_group.rb
Normal file
15
app/models/invited_group.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
class InvitedGroup < ActiveRecord::Base
|
||||
belongs_to :group
|
||||
belongs_to :invite
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: invited_groups
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# group_id :integer
|
||||
# invite_id :integer
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
|
@ -6,12 +6,12 @@ end
|
|||
#
|
||||
# Table name: post_custom_fields
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# post_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# post_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -6,12 +6,12 @@ end
|
|||
#
|
||||
# Table name: topic_custom_fields
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# topic_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# topic_id :integer not null
|
||||
# name :string(256) not null
|
||||
# value :text
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -777,6 +777,9 @@ end
|
|||
# primary_group_id :integer
|
||||
# locale :string(10)
|
||||
# profile_background :string(255)
|
||||
# email_hash :string(255)
|
||||
# registration_ip_address :inet
|
||||
# last_redirected_to_top_at :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
9
db/migrate/20140508053815_add_invited_groups.rb
Normal file
9
db/migrate/20140508053815_add_invited_groups.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class AddInvitedGroups < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :invited_groups do |t|
|
||||
t.integer :group_id
|
||||
t.integer :invite_id
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,10 +2,6 @@ require 'spec_helper'
|
|||
|
||||
describe Invite do
|
||||
|
||||
it { should belong_to :user }
|
||||
it { should have_many :topic_invites }
|
||||
it { should belong_to :invited_by }
|
||||
it { should have_many :topics }
|
||||
it { should validate_presence_of :email }
|
||||
it { should validate_presence_of :invited_by_id }
|
||||
|
||||
|
@ -65,9 +61,6 @@ describe Invite do
|
|||
|
||||
it 'belongs to the topic' do
|
||||
topic.invites.should == [@invite]
|
||||
end
|
||||
|
||||
it 'has a topic' do
|
||||
@invite.topics.should == [topic]
|
||||
end
|
||||
|
||||
|
@ -77,27 +70,16 @@ describe Invite do
|
|||
|
||||
it 'returns a different invite' do
|
||||
new_invite.should_not == @invite
|
||||
end
|
||||
|
||||
it 'has a different key' do
|
||||
new_invite.invite_key.should_not == @invite.invite_key
|
||||
end
|
||||
|
||||
it 'has the topic relationship' do
|
||||
new_invite.topics.should == [topic]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'when adding a duplicate' do
|
||||
it 'returns the original invite' do
|
||||
topic.invite_by_email(inviter, 'iceking@adventuretime.ooo').should == @invite
|
||||
end
|
||||
|
||||
it 'matches case insensitively for the domain part' do
|
||||
topic.invite_by_email(inviter, 'iceking@ADVENTURETIME.ooo').should == @invite
|
||||
end
|
||||
|
||||
it 'matches case sensitively for the local part' do
|
||||
topic.invite_by_email(inviter, 'ICEKING@adventuretime.ooo').should_not == @invite
|
||||
end
|
||||
|
||||
|
@ -114,21 +96,13 @@ describe Invite do
|
|||
context 'when adding to another topic' do
|
||||
let!(:another_topic) { Fabricate(:topic, user: topic.user) }
|
||||
|
||||
before do
|
||||
@new_invite = another_topic.invite_by_email(inviter, iceking)
|
||||
end
|
||||
|
||||
it 'should be the same invite' do
|
||||
@new_invite = another_topic.invite_by_email(inviter, iceking)
|
||||
@new_invite.should == @invite
|
||||
end
|
||||
|
||||
it 'belongs to the new topic' do
|
||||
another_topic.invites.should == [@invite]
|
||||
end
|
||||
|
||||
it 'has references to both topics' do
|
||||
@invite.topics.should =~ [topic, another_topic]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -172,7 +146,18 @@ describe Invite do
|
|||
invite.redeem.should be_blank
|
||||
end
|
||||
|
||||
context 'invite trust levels' do
|
||||
context "when inviting to groups" do
|
||||
it "add the user to the correct groups" do
|
||||
group = Fabricate(:group)
|
||||
invite.invited_groups.build(group_id: group.id)
|
||||
invite.save
|
||||
|
||||
user = invite.redeem
|
||||
user.groups.count.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
context "invite trust levels" do
|
||||
it "returns the trust level in default_invitee_trust_level" do
|
||||
SiteSetting.stubs(:default_invitee_trust_level).returns(TrustLevel.levels[:leader])
|
||||
invite.redeem.trust_level.should == TrustLevel.levels[:leader]
|
||||
|
@ -183,7 +168,6 @@ describe Invite do
|
|||
it 'correctly activates accounts' do
|
||||
SiteSetting.stubs(:must_approve_users).returns(true)
|
||||
user = invite.redeem
|
||||
|
||||
user.approved?.should == true
|
||||
end
|
||||
end
|
||||
|
@ -257,13 +241,7 @@ describe Invite do
|
|||
|
||||
it 'adds the user to the topic_users of the first topic' do
|
||||
topic.allowed_users.include?(user).should be_true
|
||||
end
|
||||
|
||||
it 'adds the user to the topic_users of the second topic' do
|
||||
another_topic.allowed_users.include?(user).should be_true
|
||||
end
|
||||
|
||||
it 'does not redeem the second invite' do
|
||||
another_invite.reload
|
||||
another_invite.should_not be_redeemed
|
||||
end
|
||||
|
@ -276,9 +254,6 @@ describe Invite do
|
|||
|
||||
it 'returns the same user' do
|
||||
@result.should == user
|
||||
end
|
||||
|
||||
it 'marks the second invite as redeemed' do
|
||||
another_invite.reload
|
||||
another_invite.should be_redeemed
|
||||
end
|
||||
|
@ -303,7 +278,7 @@ describe Invite do
|
|||
context 'with user that has not invited' do
|
||||
it 'does not return invites' do
|
||||
user = Fabricate(:user)
|
||||
invite = Fabricate(:invite)
|
||||
Fabricate(:invite)
|
||||
|
||||
invites = Invite.find_all_invites_from(user)
|
||||
|
||||
|
@ -315,12 +290,13 @@ describe Invite do
|
|||
describe '.find_redeemed_invites_from' do
|
||||
it 'returns redeemed invites only' do
|
||||
inviter = Fabricate(:user)
|
||||
pending_invite = Fabricate(
|
||||
Fabricate(
|
||||
:invite,
|
||||
invited_by: inviter,
|
||||
user_id: nil,
|
||||
email: 'pending@example.com'
|
||||
)
|
||||
|
||||
redeemed_invite = Fabricate(
|
||||
:invite,
|
||||
invited_by: inviter,
|
||||
|
|
Loading…
Reference in New Issue
Block a user