Class: StreamChat::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/stream-chat/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, channel_type, channel_id = nil, custom_data = nil) ⇒ Channel

Returns a new instance of Channel.



10
11
12
13
14
15
16
17
18
# File 'lib/stream-chat/channel.rb', line 10

def initialize(client, channel_type, channel_id=nil, custom_data=nil)
  @channel_type = channel_type
  @id = channel_id
  @client = client
  @custom_data = custom_data
  if @custom_data == nil
    @custom_data = {}
  end
end

Instance Attribute Details

#channel_typeObject (readonly)

Returns the value of attribute channel_type.



7
8
9
# File 'lib/stream-chat/channel.rb', line 7

def channel_type
  @channel_type
end

#custom_dataObject (readonly)

Returns the value of attribute custom_data.



8
9
10
# File 'lib/stream-chat/channel.rb', line 8

def custom_data
  @custom_data
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/stream-chat/channel.rb', line 6

def id
  @id
end

Instance Method Details

#add_members(user_ids) ⇒ Object



81
82
83
# File 'lib/stream-chat/channel.rb', line 81

def add_members(user_ids)
  @client.post(url, data: {"add_members": user_ids})
end

#add_moderators(user_ids) ⇒ Object



85
86
87
# File 'lib/stream-chat/channel.rb', line 85

def add_moderators(user_ids)
  @client.post(url, data: {"add_moderators": user_ids})
end

#ban_user(user_id, **options) ⇒ Object



110
111
112
# File 'lib/stream-chat/channel.rb', line 110

def ban_user(user_id, **options)
  @client.ban_user(user_id, type: @channel_type, id: @id, **options)
end

#create(user_id) ⇒ Object



49
50
51
52
# File 'lib/stream-chat/channel.rb', line 49

def create(user_id)
  @custom_data["created_by"] = {"id": user_id}
  query(watch: false, state: false, presence: false)
end

#deleteObject



73
74
75
# File 'lib/stream-chat/channel.rb', line 73

def delete
  @client.delete(url)
end

#delete_reaction(message_id, reaction_type, user_id) ⇒ Object



42
43
44
45
46
47
# File 'lib/stream-chat/channel.rb', line 42

def delete_reaction(message_id, reaction_type, user_id)
  @client.delete(
    "messages/#{message_id}/reaction/#{reaction_type}",
    params: {"user_id": user_id}
  )
end

#demote_moderators(user_ids) ⇒ Object



93
94
95
# File 'lib/stream-chat/channel.rb', line 93

def demote_moderators(user_ids)
  @client.post(url, data: {"demote_moderators": user_ids})
end

#get_reactions(message_id, **options) ⇒ Object



106
107
108
# File 'lib/stream-chat/channel.rb', line 106

def get_reactions(message_id, **options)
  @client.get("messages/#{message_id}/reactions", params: options)
end

#get_replies(parent_id, **options) ⇒ Object



102
103
104
# File 'lib/stream-chat/channel.rb', line 102

def get_replies(parent_id, **options)
  @client.get("messages/#{parent_id}/replies", params: options)
end

#mark_read(user_id, **options) ⇒ Object



97
98
99
100
# File 'lib/stream-chat/channel.rb', line 97

def mark_read(user_id, **options)
  payload = add_user_id(options, user_id)
  @client.post("#{url}/read", data: payload)
end

#query(**options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stream-chat/channel.rb', line 54

def query(**options)
  payload = {"state": true, "data": @custom_data}.merge(options)
  url = "channels/#{@channel_type}"
  if @id != nil
    url = "#{url}/#{@id}"
  end

  state = @client.post("#{url}/query", data: payload)
  if @id == nil
    @id = state["channel"]["id"]
  end
  state
end

#remove_members(user_ids) ⇒ Object



89
90
91
# File 'lib/stream-chat/channel.rb', line 89

def remove_members(user_ids)
  @client.post(url, data: {"remove_members": user_ids})
end

#send_event(event, user_id) ⇒ Object



32
33
34
35
# File 'lib/stream-chat/channel.rb', line 32

def send_event(event, user_id)
  payload = {'event' => add_user_id(event, user_id)}
  @client.post("#{url}/event", data: payload)
end

#send_message(message, user_id) ⇒ Object



27
28
29
30
# File 'lib/stream-chat/channel.rb', line 27

def send_message(message, user_id)
  payload = {"message": add_user_id(message, user_id)}
  @client.post("#{url}/message", data: payload)
end

#send_reaction(message_id, reaction, user_id) ⇒ Object



37
38
39
40
# File 'lib/stream-chat/channel.rb', line 37

def send_reaction(message_id, reaction, user_id)
  payload = {"reaction": add_user_id(reaction, user_id)}
  @client.post("messages/#{message_id}/reaction", data: payload)
end

#truncateObject



77
78
79
# File 'lib/stream-chat/channel.rb', line 77

def truncate
  @client.post("#{url}/truncate")
end

#unban_user(user_id) ⇒ Object



114
115
116
# File 'lib/stream-chat/channel.rb', line 114

def unban_user(user_id)
  @client.unban_user(user_id, type: @channel_type, id: @id)
end

#update(channel_data, update_message = nil) ⇒ Object



68
69
70
71
# File 'lib/stream-chat/channel.rb', line 68

def update(channel_data, update_message=nil)
  payload = {"data": channel_data, "message": update_message}
  @client.post(url, data: payload)
end

#urlObject



20
21
22
23
24
25
# File 'lib/stream-chat/channel.rb', line 20

def url
  if @id == nil
    raise StreamChannelException "channel does not have an id"
  end
  "channels/#{@channel_type}/#{@id}"
end