From e6c605d92b3a48f954f66451af139682878b2264 Mon Sep 17 00:00:00 2001 From: margori <ing@marcelobriones.com.ar> Date: Mon, 15 Dec 2014 02:01:52 -0300 Subject: [PATCH] Destroy conversation when all participants leave it --- app/models/conversation_visibility.rb | 10 +++++++ ...141216213423_purge_orphan_conversations.rb | 9 ++++++ db/schema.rb | 2 +- spec/models/conversation_visibilities_spec.rb | 28 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20141216213423_purge_orphan_conversations.rb create mode 100644 spec/models/conversation_visibilities_spec.rb diff --git a/app/models/conversation_visibility.rb b/app/models/conversation_visibility.rb index da398c7244..9c0afb2bde 100644 --- a/app/models/conversation_visibility.rb +++ b/app/models/conversation_visibility.rb @@ -3,4 +3,14 @@ class ConversationVisibility < ActiveRecord::Base belongs_to :conversation belongs_to :person + after_destroy :check_orphan_conversation + + private + + def check_orphan_conversation + conversation = Conversation.find_by_id(self.conversation.id) + if conversation + conversation.destroy if conversation.participants.count == 0 + end + end end diff --git a/db/migrate/20141216213423_purge_orphan_conversations.rb b/db/migrate/20141216213423_purge_orphan_conversations.rb new file mode 100644 index 0000000000..df6f157dc5 --- /dev/null +++ b/db/migrate/20141216213423_purge_orphan_conversations.rb @@ -0,0 +1,9 @@ +class PurgeOrphanConversations < ActiveRecord::Migration + def up + Conversation.joins("LEFT JOIN conversation_visibilities ON conversation_visibilities.conversation_id = conversations.id").group('conversations.id').having("COUNT(conversation_visibilities.id) = 0").delete_all + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/db/schema.rb b/db/schema.rb index 497151fd56..b5112c5e88 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141209041241) do +ActiveRecord::Schema.define(version: 20141216213423) do create_table "account_deletions", force: true do |t| t.string "diaspora_handle" diff --git a/spec/models/conversation_visibilities_spec.rb b/spec/models/conversation_visibilities_spec.rb new file mode 100644 index 0000000000..da15b8743d --- /dev/null +++ b/spec/models/conversation_visibilities_spec.rb @@ -0,0 +1,28 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe ConversationVisibility, :type => :model do + before do + @user1 = alice + @participant_ids = [@user1.contacts.first.person.id, @user1.person.id] + + @create_hash = { + :author => @user1.person, + :participant_ids => @participant_ids, + :subject => "cool stuff", + :messages_attributes => [ {:author => @user1.person, :text => 'hey'} ] + } + @conversation = Conversation.create(@create_hash) + end + + it 'destroy conversation when no participant' do + @conversation.conversation_visibilities.each do |visibility| + visibility.destroy + end + + expect(Conversation).not_to exist(@conversation.id) + end +end -- GitLab