diff --git a/app/models/chat_channel.rb b/app/models/chat_channel.rb index 99ac57413..0727e2f4e 100644 --- a/app/models/chat_channel.rb +++ b/app/models/chat_channel.rb @@ -35,6 +35,10 @@ class ChatChannel < ApplicationRecord channel_type != "direct" end + def private_org_channel? + channel_name.to_s.ends_with?(" private group chat") #e.g. @devteam private group chat + end + def clear_channel messages.destroy_all Pusher.trigger(pusher_channels, "channel-cleared", { chat_channel_id: id }.to_json) diff --git a/app/models/organization_membership.rb b/app/models/organization_membership.rb index ff1ef4b4e..98ea1aff3 100644 --- a/app/models/organization_membership.rb +++ b/app/models/organization_membership.rb @@ -8,10 +8,37 @@ class OrganizationMembership < ApplicationRecord validates :user_id, uniqueness: { scope: :organization_id } validates :type_of_user, inclusion: { in: USER_TYPES } - after_create :update_user_organization_info_updated_at + after_save :upsert_chat_channel_membership + after_create :update_user_organization_info_updated_at after_destroy :update_user_organization_info_updated_at def update_user_organization_info_updated_at user.touch(:organization_info_updated_at) end + + private + + def upsert_chat_channel_membership + return if type_of_user == "guest" + + role = type_of_user == "admin" ? "mod" : "member" + name = "@#{organization.slug} private group chat" + channel = ChatChannel.find_by(channel_name: name) + if channel + add_chat_channel_membership(user, channel, role) + else + ChatChannel.create_with_users( + users: [user], + channel_type: "invite_only", + contrived_name: name, + membership_role: role, + ) + end + end + + def add_chat_channel_membership(user, channel, role) + membership = ChatChannelMembership.find_or_initialize_by(user_id: user.id, chat_channel_id: channel.id) + membership.role = role + membership.save + end end diff --git a/app/policies/chat_channel_policy.rb b/app/policies/chat_channel_policy.rb index f2b9c0e30..998bcefef 100644 --- a/app/policies/chat_channel_policy.rb +++ b/app/policies/chat_channel_policy.rb @@ -39,7 +39,8 @@ class ChatChannelPolicy < ApplicationPolicy def user_can_edit_channel record.present? && - (user.has_role?(:super_admin) || record.channel_mod_ids.include?(user.id)) + (user.has_role?(:super_admin) || record.channel_mod_ids.include?(user.id)) && + !record.private_org_channel? end def user_part_of_channel_or_open diff --git a/app/views/chat_channel_memberships/edit.html.erb b/app/views/chat_channel_memberships/edit.html.erb index f74ce32dc..85a13b7f0 100644 --- a/app/views/chat_channel_memberships/edit.html.erb +++ b/app/views/chat_channel_memberships/edit.html.erb @@ -20,7 +20,7 @@
Invite new members by inviting them to this organization.
+ <% else %> + <%= form_for ChatChannelMembership.new do |f| %> +Questions about Connect Channel moderation? Contact <%= SiteConfig.default_site_email %>
<% else %>