Module: TurboChat::ApplicationHelper::ParticipantSupport

Included in:
TurboChat::ApplicationHelper
Defined in:
app/helpers/turbo_chat/application_helper/participant_support.rb

Instance Method Summary collapse

Instance Method Details

#can_edit_chat_message?(chat_message, participant: nil) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/turbo_chat/application_helper/participant_support.rb', line 39

def can_edit_chat_message?(chat_message, participant: nil)
  return false if chat_message.nil?

  participant ||= current_chat_participant_for_view
  if participant.nil?
    return true unless respond_to?(:current_chat_participant, true)

    return false
  end

  adapter = TurboChat.configuration.permission_adapter
  return false unless adapter.respond_to?(:new)

  permission = adapter.new(participant, chat_message.chat)
  if permission.respond_to?(:can_edit_message?)
    return permission.can_edit_message?(chat_message)
  end

  return false if permission.respond_to?(:can_post_message?) && !permission.can_post_message?

  own_chat_message?(chat_message, participant: participant)
rescue NoMethodError, TypeError
  false
end

#chat_participant_email(participant) ⇒ Object



8
9
10
# File 'app/helpers/turbo_chat/application_helper/participant_support.rb', line 8

def chat_participant_email(participant)
  TurboChat::ParticipantIdentity.email(participant)
end

#chat_participant_invite_option_label(participant) ⇒ Object



12
13
14
15
16
17
18
19
# File 'app/helpers/turbo_chat/application_helper/participant_support.rb', line 12

def chat_participant_invite_option_label(participant)
  participant_name = chat_participant_name(participant)
  participant_email = chat_participant_email(participant)
  participant_id_label = participant.respond_to?(:id) && participant.id.present? ? "##{participant.id}" : "unknown"
  return "#{participant_name} (#{participant_id_label})" unless participant_email.present? && participant_email != participant_name

  "#{participant_name} - #{participant_email} (#{participant_id_label})"
end

#chat_participant_name(participant) ⇒ Object



4
5
6
# File 'app/helpers/turbo_chat/application_helper/participant_support.rb', line 4

def chat_participant_name(participant)
  TurboChat::ParticipantIdentity.display_name(participant)
end

#chat_participant_search_text(participant) ⇒ Object



21
22
23
24
25
26
27
# File 'app/helpers/turbo_chat/application_helper/participant_support.rb', line 21

def chat_participant_search_text(participant)
  participant_name = chat_participant_name(participant)
  participant_email = chat_participant_email(participant)
  participant_id = participant.respond_to?(:id) ? participant.id : nil

  [participant_name, participant_email, participant_id].compact_blank.join(" ")
end

#own_chat_message?(chat_message, participant: nil) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'app/helpers/turbo_chat/application_helper/participant_support.rb', line 29

def own_chat_message?(chat_message, participant: nil)
  return false if chat_message.nil?

  participant ||= current_chat_participant_for_view
  participant_identity = participant_identity_for(participant)
  return false if participant_identity.nil?

  [chat_message.participant_type.to_s, chat_message.participant_id.to_s] == participant_identity
end