Class: Channel

Inherits:
Base
  • Object
show all
Defined in:
lib/mattermost/models/channel.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attributes, #first, #initialize, #last, #to_json

Constructor Details

This class inherits a constructor from Base

Class Method Details

.default_attributesObject



102
103
104
# File 'lib/mattermost/models/channel.rb', line 102

def self.default_attributes
  {:display_name => String, :team_id => String, :type => Integer, :purpose => String}
end

Instance Method Details

#add_member(user) ⇒ Object Also known as: add_user

Add a user to a channel Send a user object or a user_id



67
68
69
70
# File 'lib/mattermost/models/channel.rb', line 67

def add_member(user)
  user_id = user.is_a? User ? user.id : user
  Mattermost.post("/channels/#{self.id}/add", :body => {:user_id => user_id})
end

#deleteObject Also known as: destroy

Delete a channel



12
13
14
# File 'lib/mattermost/models/channel.rb', line 12

def delete
  Mattermost.post("/channels/#{self.id}/delete")
end

#get_channel_extra_info(member_limit = nil) ⇒ Object Also known as: users

Get the users in a channel



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mattermost/models/channel.rb', line 38

def get_channel_extra_info(member_limit = nil)
  uri = "/channels/#{self.id}/extra_info"
  uri += "/#{member_limit}" if member_limit
  request = Mattermost.get(uri)
  response = {}
  response['id'] = request.parsed_response['id']
  response['member_count'] = request.parsed_response['member_count']
  response['members'] = []
  request.parsed_response['members'].each do |user|
    response['members'] << User.new(user)
  end
  response
end

#infoObject

Get channel info



18
19
20
# File 'lib/mattermost/models/channel.rb', line 18

def info
  Mattermost.get("/channels/#{self.id}/")
end

#joinObject

Join the channel with your authenticated user If you’re looking to add a user to a channel, see #add_member



55
56
57
# File 'lib/mattermost/models/channel.rb', line 55

def join
  Mattermost.post("/channels/#{self.id}/join")
end

#leaveObject

Leave the channel with your authenticated user If you’re looking to remove a user from a channel, see #remove_member



61
62
63
# File 'lib/mattermost/models/channel.rb', line 61

def leave
  Mattermost.post("/channels/#{self.id}/leave")
end

#nameObject



7
8
9
# File 'lib/mattermost/models/channel.rb', line 7

def name 
  @name ||= self.display_name.gsub(" ", '-')
end

#posts(before = nil) ⇒ Object

Returns posts for the channel before a given post.id

Parameters:

  • before (string) (defaults to: nil)

    to get posts before a given post id



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mattermost/models/channel.rb', line 25

def posts(before = nil)
  uri = "/channels/#{self.id}/posts"
  uri += "/#{before}" if before 
  uri += "/0/60?_=#{Time.now.to_i}"
  request = Mattermost.get("/channels/#{self.id}/posts/0/60?_=#{Time.now.to_i}")
  response = {}
  request.parsed_response['posts'].each do |_, post|
    response[k] = Post.new(post)
  end
  response
end

#remove_member(user) ⇒ Object Also known as: remove_user

Add a user to a channel Send a user object or a user_id



75
76
77
78
# File 'lib/mattermost/models/channel.rb', line 75

def remove_member(user)
  user_id = user.is_a? User ? user.id : user
  Mattermost.post("/channels/#{self.id}/remove", :body => {:user_id => user_id})
end

#saveObject



3
4
5
# File 'lib/mattermost/models/channel.rb', line 3

def save
  defined?(self.id) ? update : create_channel
end

#update_attributes(attributes = {}) ⇒ Object

Raises:

  • (NotImplementedError)


85
86
87
88
# File 'lib/mattermost/models/channel.rb', line 85

def update_attributes(attributes = {})
  raise NotImplementedError
  Mattermost.post("/channels/update")
end

#update_header(header) ⇒ Object

Update the channel’s header. I really hate that they have a specific method for this.



92
93
94
# File 'lib/mattermost/models/channel.rb', line 92

def update_header(header)
  Mattermost.post("/teams/#{Mattermost.team.id}/channels/#{self.id}/update_header", :body => {:channel_id => self.id, :channel_header => header}.to_json)
end

#update_last_viewed_atObject



81
82
83
# File 'lib/mattermost/models/channel.rb', line 81

def update_last_viewed_at
  Mattermost.post("/channels/#{self.id}/update_last_viewed_at")
end

#update_purpose(purpose) ⇒ Object

Update the channel’s purpose. I really hate that they have a specific method for this.



98
99
100
# File 'lib/mattermost/models/channel.rb', line 98

def update_purpose(purpose)
  Mattermost.post("/teams/#{Mattermost.team.id}/channels/#{self.id}/update_purpose", :body => {:channel_id => self.id, :channel_purpose => purpose}.to_json)
end