diff --git a/app/assets/stylesheets/chat.scss b/app/assets/stylesheets/chat.scss
index 20f93e461..719df0d4b 100644
--- a/app/assets/stylesheets/chat.scss
+++ b/app/assets/stylesheets/chat.scss
@@ -1214,3 +1214,54 @@
}
}
}
+.joining-message {
+ display: grid;
+ margin-top: 30px;
+ justify-content: center;
+ h3,
+ h2 {
+ width: 90%;
+ margin-top: 10px;
+ text-align: center;
+ justify-self: center;
+ }
+}
+.user-picture,
+.send-request {
+ display: flex;
+ justify-content: center;
+ margin-top: 10vh;
+}
+.user-picture {
+ .chatmessage__profilepic {
+ .chatmessagebody__profileimage {
+ width: 50px;
+ height: 50px;
+ border: 1px solid var(--base-30);
+ }
+ }
+}
+.send-request {
+ .cta {
+ margin: 4px 6px 4px 0;
+ border-radius: 3px;
+ font-weight: 600px;
+ font-family: 'HelveticaNeue-CondensedBold', 'HelveticaNeueBoldCondensed',
+ 'HelveticaNeue-Bold-Condensed', 'Helvetica Neue Bold Condensed',
+ 'HelveticaNeueBold', 'HelveticaNeue-Bold', 'Helvetica Neue Bold',
+ 'HelveticaNeue', 'Helvetica Neue', 'TeXGyreHerosCnBold', 'Helvetica',
+ 'Tahoma', 'Geneva', 'Arial Narrow', 'Arial', sans-serif;
+ font-size: 18px;
+ border: 0px;
+ height: 50px;
+ padding: 0 30px;
+ }
+}
+.loading-user {
+ height: '210px';
+ width: '210px';
+ margin: ' 15px auto';
+ display: 'block';
+ border-radius: '500px';
+ background-color: '#f5f6f7';
+}
diff --git a/app/controllers/chat_channel_memberships_controller.rb b/app/controllers/chat_channel_memberships_controller.rb
index 6f80921c8..66c6647e6 100644
--- a/app/controllers/chat_channel_memberships_controller.rb
+++ b/app/controllers/chat_channel_memberships_controller.rb
@@ -1,5 +1,5 @@
class ChatChannelMembershipsController < ApplicationController
- after_action :verify_authorized
+ after_action :verify_authorized, except: :join_channel
include MessagesHelper
def index
@@ -39,6 +39,23 @@ class ChatChannelMembershipsController < ApplicationController
redirect_to edit_chat_channel_membership_path(membership)
end
+ def join_channel
+ membership_params = params[:chat_channel_membership]
+ chat_channel = ChatChannel.find(membership_params[:chat_channel_id])
+ existing_membership = ChatChannelMembership.find_by(user_id: current_user.id, chat_channel_id: chat_channel.id)
+ if existing_membership.present? && %w[active joining_request].exclude?(existing_membership.status)
+ status = existing_membership.update(status: "joining_request", role: "member")
+ else
+ membership = ChatChannelMembership.new(user_id: current_user.id, chat_channel_id: chat_channel.id, role: "member", status: "joining_request")
+ status = membership.save
+ end
+ if status
+ render json: { status: "success", message: "Request Sent" }
+ else
+ render json: { status: 400, message: "Unable to join channel" }, status: :bad_request
+ end
+ end
+
def remove_membership
@chat_channel = ChatChannel.find(params[:chat_channel_id])
authorize @chat_channel, :update?
@@ -55,11 +72,18 @@ class ChatChannelMembershipsController < ApplicationController
redirect_to edit_chat_channel_membership_path(membership)
end
+ def add_membership
+ @chat_channel = ChatChannel.find(params[:chat_channel_id])
+ authorize @chat_channel, :update?
+ @chat_channel_membership = @chat_channel.chat_channel_memberships.find(params[:membership_id])
+ respond_to_invitation(@chat_channel_membership.status) if permitted_params[:user_action].present? && @chat_channel_membership.status == "joining_request"
+ end
+
def update
@chat_channel_membership = ChatChannelMembership.find(params[:id])
authorize @chat_channel_membership
if permitted_params[:user_action].present?
- respond_to_invitation
+ respond_to_invitation(@chat_channel_membership.status)
else
@chat_channel_membership.update(permitted_params)
flash[:settings_notice] = "Personal settings updated."
@@ -84,12 +108,20 @@ class ChatChannelMembershipsController < ApplicationController
params.require(:chat_channel_membership).permit(:user_action, :show_global_badge_notification)
end
- def respond_to_invitation
+ def respond_to_invitation(previous_status)
if permitted_params[:user_action] == "accept"
@chat_channel_membership.update(status: "active")
channel_name = @chat_channel_membership.chat_channel.channel_name
- send_chat_action_message("@#{current_user.username} joined #{@chat_channel_membership.channel_name}", current_user, @chat_channel_membership.chat_channel_id, "joined")
- flash[:settings_notice] = "Invitation to #{channel_name} accepted. It may take a moment to show up in your list."
+ if previous_status == "pending"
+ send_chat_action_message("@#{current_user.username} joined #{@chat_channel_membership.channel_name}", current_user, @chat_channel_membership.chat_channel_id, "joined")
+ flash[:settings_notice] = "Invitation to #{channel_name} accepted. It may take a moment to show up in your list."
+ else
+ send_chat_action_message("@#{current_user.username} added @#{@chat_channel_membership.user.username}", current_user, @chat_channel_membership.chat_channel_id, "joined")
+ NotifyMailer.channel_invite_email(@chat_channel_membership, @chat_channel_membership.user).deliver_later
+ flash[:settings_notice] = "Accepted request of #{@chat_channel_membership.user.username} to join #{channel_name}."
+ membership = ChatChannelMembership.find_by!(chat_channel_id: @chat_channel_membership.chat_channel.id, user: current_user)
+ redirect_to(edit_chat_channel_membership_path(membership)) && return
+ end
else
@chat_channel_membership.update(status: "rejected")
flash[:settings_notice] = "Invitation rejected."
diff --git a/app/controllers/chat_channels_controller.rb b/app/controllers/chat_channels_controller.rb
index 638284a40..1780212bf 100644
--- a/app/controllers/chat_channels_controller.rb
+++ b/app/controllers/chat_channels_controller.rb
@@ -13,6 +13,9 @@ class ChatChannelsController < ApplicationController
elsif params[:state] == "pending"
authorize ChatChannel
render_pending_json_response
+ elsif params[:state] == "joining_request"
+ authorize ChatChannel
+ render_joining_request_json_response
else
skip_authorization
render_channels_html
@@ -31,6 +34,11 @@ class ChatChannelsController < ApplicationController
def update
if ChatChannelUpdateService.new(@chat_channel, chat_channel_params).update
+ if !chat_channel_params[:discoverable].to_i.zero?
+ ChatChannelMembership.create(user_id: SiteConfig.mascot_user_id, chat_channel_id: @chat_channel.id, role: "member", status: "active")
+ else
+ ChatChannelMembership.find_by(user_id: SiteConfig.mascot_user_id)&.destroy
+ end
flash[:settings_notice] = "Channel settings updated."
else
default_error_message = "Channel settings updation failed. Try again later."
@@ -140,9 +148,9 @@ class ChatChannelsController < ApplicationController
def render_unopened_json_response
@chat_channels_memberships = if session_current_user_id
ChatChannelMembership.where(user_id: session_current_user_id).includes(:chat_channel).
- where("has_unopened_messages = ? OR status = ?",
- true, "pending").
+ where(has_unopened_messages: true).
where(show_global_badge_notification: true).
+ where.not(status: %w[removed_from_channel left_channel]).
order("chat_channel_memberships.updated_at DESC")
else
[]
@@ -164,10 +172,16 @@ class ChatChannelsController < ApplicationController
def render_unopened_ids_response
@unopened_ids = ChatChannelMembership.where(user_id: session_current_user_id).includes(:chat_channel).
- where(has_unopened_messages: true).pluck(:chat_channel_id)
+ where(has_unopened_messages: true).where.not(status: %w[removed_from_channel left_channel]).pluck(:chat_channel_id)
render json: { unopened_ids: @unopened_ids }
end
+ def render_joining_request_json_response
+ requested_memberships = current_user.chat_channel_memberships.includes(:chat_channel).
+ where(chat_channels: { discoverable: true }, role: "mod").pluck(:chat_channel_id).map { |membership_id| ChatChannel.find_by(id: membership_id).requested_memberships }
+ render json: { joining_requests: requested_memberships.flatten }
+ end
+
def render_channels_html
return unless current_user && params[:slug]
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index c96a6fd82..eb30c0b00 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -57,8 +57,9 @@ class SearchController < ApplicationController
end
def chat_channels
+ search_user_id = chat_channel_params[:user_id].present? ? [current_user.id, SiteConfig.mascot_user_id] : [current_user.id]
ccm_docs = Search::ChatChannelMembership.search_documents(
- params: chat_channel_params.merge(user_id: current_user.id).to_h,
+ params: chat_channel_params.merge(user_id: search_user_id).to_h,
)
render json: { result: ccm_docs }
@@ -118,6 +119,7 @@ class SearchController < ApplicationController
channel_type
channel_status
status
+ user_id
]
params.permit(accessible)
diff --git a/app/javascript/chat/__tests__/__snapshots__/channelButton.test.jsx.snap b/app/javascript/chat/__tests__/__snapshots__/channelButton.test.jsx.snap
new file mode 100644
index 000000000..15795f518
--- /dev/null
+++ b/app/javascript/chat/__tests__/__snapshots__/channelButton.test.jsx.snap
@@ -0,0 +1,41 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[` should render and test snapshot 1`] = `
+
+`;
diff --git a/app/javascript/chat/__tests__/__snapshots__/channelRequest.test.jsx.snap b/app/javascript/chat/__tests__/__snapshots__/channelRequest.test.jsx.snap
new file mode 100644
index 000000000..da10f9660
--- /dev/null
+++ b/app/javascript/chat/__tests__/__snapshots__/channelRequest.test.jsx.snap
@@ -0,0 +1,47 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[` should render and test snapshot 1`] = `
+
+
+
+ Hey Sarthak !
+
+
+ You are not a member of this group yet. Send request to join.
+
+
+
+
+
![undefined profile]()
+

+
+
+
+
+
+
+`;
diff --git a/app/javascript/chat/__tests__/__snapshots__/channels.test.jsx.snap b/app/javascript/chat/__tests__/__snapshots__/channels.test.jsx.snap
index 3b4f3d8b4..053dd1a86 100644
--- a/app/javascript/chat/__tests__/__snapshots__/channels.test.jsx.snap
+++ b/app/javascript/chat/__tests__/__snapshots__/channels.test.jsx.snap
@@ -9,14 +9,20 @@ exports[` expanded with chat channels should render and test snapsho
id="chatchannels__channelslist"
>