Class: TurboChat::ChatsController

Inherits:
ApplicationController show all
Includes:
EventPayloadSupport, InvitationSupport
Defined in:
app/controllers/turbo_chat/chats_controller.rb,
app/controllers/turbo_chat/chats_controller/invitation_support.rb,
app/controllers/turbo_chat/chats_controller/event_payload_support.rb

Defined Under Namespace

Modules: EventPayloadSupport, InvitationSupport

Instance Method Summary collapse

Instance Method Details

#acceptObject



23
24
25
26
27
28
29
30
31
# File 'app/controllers/turbo_chat/chats_controller.rb', line 23

def accept
  participant = current_chat_participant
  membership = pending_invitation_membership_for(participant, action: "accept")
  return if membership.nil?

  membership.accept_invitation!
  set_invitation_accepted_flash(membership)
  complete_membership_transition(participant: participant, membership: membership, system_event: :accepted, lifecycle_action: :joined, notice: "Invitation accepted")
end

#closeObject



90
# File 'app/controllers/turbo_chat/chats_controller.rb', line 90

def close = transition_chat_state!(permission_method: :can_close_chat?, mutation: :close!, action: :closed, notice: "Chat closed")

#createObject



44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/turbo_chat/chats_controller.rb', line 44

def create
  @chat = TurboChat::Chat.new(chat_params)

  if @chat.save
    membership = @chat.chat_memberships.create!(participant: current_chat_participant, role: :admin)
    set_chat_lifecycle_event(action: :joined, chat: @chat, membership: membership)
    redirect_to chat_path(@chat), notice: "Chat created"
  else
    render :new, status: :unprocessable_entity
  end
end

#declineObject



33
34
35
36
37
38
39
40
# File 'app/controllers/turbo_chat/chats_controller.rb', line 33

def decline
  participant = current_chat_participant
  membership = pending_invitation_membership_for(participant, action: "decline")
  return if membership.nil?

  remove_membership!(membership, invitation_accepted: false)
  complete_membership_transition(participant: participant, membership: membership, system_event: :declined, lifecycle_action: :declined, notice: "Invitation declined")
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/turbo_chat/chats_controller.rb', line 9

def index
  participant = current_chat_participant
  @chats = TurboChat::Chat.for_participant(participant).order(created_at: :desc, id: :desc)
  @chat_member_counts = if @chats.exists?
                          chat_ids = @chats.except(:order).select(:id)
                          TurboChat::ChatMembership.active.where(chat_id: chat_ids).group(:chat_id).count
                        else
                          {}
                        end
  @pending_invitations = pending_invitations_for(participant)
  @invitation_accepted_event = invitation_accepted_payload
  @chat_lifecycle_event = chat_lifecycle_event_payload
end

#leaveObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/turbo_chat/chats_controller.rb', line 78

def leave
  authorize_view_chat!(@chat)
  return if performed?

  participant = current_chat_participant
  membership = @chat.chat_memberships.active.find_by(participant: participant)
  return redirect_to(chats_path, alert: "You are no longer a participant in this chat", status: :see_other) if membership.nil?

  remove_membership!(membership)
  complete_membership_transition(participant: participant, membership: membership, system_event: :left, lifecycle_action: :left, notice: "You left the chat")
end

#newObject



42
# File 'app/controllers/turbo_chat/chats_controller.rb', line 42

def new = @chat = TurboChat::Chat.new

#reopenObject



92
# File 'app/controllers/turbo_chat/chats_controller.rb', line 92

def reopen = transition_chat_state!(permission_method: :can_reopen_chat?, mutation: :reopen!, action: :reopened, notice: "Chat reopened")

#showObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/turbo_chat/chats_controller.rb', line 56

def show
  authorize_view_chat!(@chat)
  return if performed?

  @chat_lifecycle_event = chat_lifecycle_event_payload
  @chat_permission = permission_for(@chat)
  @chat_messages = @chat.visible_messages
  @can_post_message = @chat_permission.can_post_message? && !chat_input_disabled?(@chat)
  @show_members = show_members_enabled?(@chat)
  @can_invite_member = permission_allows?(@chat_permission, :can_invite_member?)
  @can_grant_member_permissions = permission_allows?(@chat_permission, :can_grant_member_permissions?)
  @can_mute_member = permission_allows?(@chat_permission, :can_mute_member?)
  @can_ban_member = permission_allows?(@chat_permission, :can_ban_member?)
  @can_manage_member_permissions = @can_grant_member_permissions || @can_mute_member || @can_ban_member
  @can_close_chat = @chat_permission.can_close_chat?
  @can_reopen_chat = @chat_permission.can_reopen_chat?
  @can_edit_own_messages = permission_allows?(@chat_permission, :can_edit_message?, fallback: @can_post_message)
  @chat_message = @chat.chat_messages.build if @can_post_message
  @invitable_participants = build_invitable_participants if @can_invite_member
  @invite_participant_type = current_chat_participant.class.base_class.name if @can_invite_member
end