Module: DiscourseApi::API::Topics

Included in:
Client
Defined in:
lib/discourse_api/api/topics.rb

Instance Method Summary collapse

Instance Method Details

#change_topic_status(topic_slug, topic_id, params = {}) ⇒ Object

TODO: Deprecated. Remove after 20201231



46
47
48
49
# File 'lib/discourse_api/api/topics.rb', line 46

def change_topic_status(topic_slug, topic_id, params = {})
  deprecated(__method__, 'update_topic_status')
  update_topic_status(topic_id, params)
end

#create_topic(args = {}) ⇒ Object

:category OPTIONAL name of category, not ID :skip_validations OPTIONAL boolean :auto_track OPTIONAL boolean :created_at OPTIONAL seconds since epoch.



9
10
11
12
13
14
# File 'lib/discourse_api/api/topics.rb', line 9

def create_topic(args = {})
  args = API.params(args)
    .required(:title, :raw)
    .optional(:skip_validations, :category, :auto_track, :created_at, :api_username, :tags)
  post("/posts", args.to_h)
end

#create_topic_action(args) ⇒ Object



16
17
18
19
20
# File 'lib/discourse_api/api/topics.rb', line 16

def create_topic_action(args)
  args = API.params(args)
    .required(:id, :post_action_type_id)
  post("/post_actions", args.to_h.merge(flag_topic: true))
end

#delete_topic(id) ⇒ Object



68
69
70
# File 'lib/discourse_api/api/topics.rb', line 68

def delete_topic(id)
  delete("/t/#{id}.json")
end

#edit_topic_timestamp(topic_id, timestamp) ⇒ Object

timestamp is seconds past the epoch.



23
24
25
# File 'lib/discourse_api/api/topics.rb', line 23

def edit_topic_timestamp(topic_id, timestamp)
  put("/t/#{topic_id}/change-timestamp", timestamp: timestamp)
end

#latest_topics(params = {}) ⇒ Object



27
28
29
30
# File 'lib/discourse_api/api/topics.rb', line 27

def latest_topics(params = {})
  response = get('/latest.json', params)
  response[:body]['topic_list']['topics']
end

#new_topics(params = {}) ⇒ Object



32
33
34
35
# File 'lib/discourse_api/api/topics.rb', line 32

def new_topics(params = {})
  response = get("/new.json", params)
  response[:body]['topic_list']['topics']
end

#recategorize_topic(topic_id, category_id) ⇒ Object



41
42
43
# File 'lib/discourse_api/api/topics.rb', line 41

def recategorize_topic(topic_id, category_id)
  put("/t/#{topic_id}.json", topic_id: topic_id, category_id: category_id)
end

#rename_topic(topic_id, title) ⇒ Object



37
38
39
# File 'lib/discourse_api/api/topics.rb', line 37

def rename_topic(topic_id, title)
  put("/t/#{topic_id}.json", topic_id: topic_id, title: title)
end

#topic(id, params = {}) ⇒ Object



58
59
60
61
# File 'lib/discourse_api/api/topics.rb', line 58

def topic(id, params = {})
  response = get("/t/#{id}.json", params)
  response[:body]
end

#topic_posts(topic_id, post_ids = []) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/discourse_api/api/topics.rb', line 72

def topic_posts(topic_id, post_ids = [])
  url = ["/t/#{topic_id}/posts.json"]
  if post_ids.count > 0
    url.push('?')
    url.push(post_ids.map { |id| "post_ids[]=#{id}" }.join('&'))
  end
  response = get(url.join)
  response[:body]
end

#topics_by(username, params = {}) ⇒ Object



63
64
65
66
# File 'lib/discourse_api/api/topics.rb', line 63

def topics_by(username, params = {})
  response = get("/topics/created-by/#{username}.json", params)
  response[:body]['topic_list']['topics']
end

#update_topic_status(topic_id, params = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/discourse_api/api/topics.rb', line 51

def update_topic_status(topic_id, params = {})
  params = API.params(params)
    .required(:status, :enabled)
    .optional(:api_username)
  put("/t/#{topic_id}/status", params)
end