Class: Wikidotrb::Module::ForumThread

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidotrb/module/forum_thread.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, id:, forum:, category: nil, title: nil, description: nil, created_by: nil, created_at: nil, posts_counts: nil, page: nil, pagerno: nil, last_post_id: nil) ⇒ ForumThread

Returns a new instance of ForumThread.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wikidotrb/module/forum_thread.rb', line 78

def initialize(site:, id:, forum:, category: nil, title: nil, description: nil, created_by: nil, created_at: nil,
               posts_counts: nil, page: nil, pagerno: nil, last_post_id: nil)
  @site = site
  @id = id
  @forum = forum
  @category = category
  @title = title
  @description = description
  @created_by = created_by
  @created_at = created_at
  @posts_counts = posts_counts
  @page = page
  @pagerno = pagerno
  @_last_post_id = last_post_id
  @_last = nil
end

Instance Attribute Details

#category

Returns the value of attribute category.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def category
  @category
end

#created_at

Returns the value of attribute created_at.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def created_at
  @created_at
end

#created_by

Returns the value of attribute created_by.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def created_by
  @created_by
end

#description

Returns the value of attribute description.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def description
  @description
end

#forum

Returns the value of attribute forum.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def forum
  @forum
end

#id

Returns the value of attribute id.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def id
  @id
end

#last

最後の投稿の取得



96
97
98
# File 'lib/wikidotrb/module/forum_thread.rb', line 96

def last
  @last
end

#page

Returns the value of attribute page.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def page
  @page
end

#pagerno

Returns the value of attribute pagerno.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def pagerno
  @pagerno
end

#posts_counts

Returns the value of attribute posts_counts.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def posts_counts
  @posts_counts
end

#site

Returns the value of attribute site.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def site
  @site
end

#title

Returns the value of attribute title.



74
75
76
# File 'lib/wikidotrb/module/forum_thread.rb', line 74

def title
  @title
end

Instance Method Details

#edit(title: nil, description: nil)

スレッドの編集

Raises:

  • (Wikidotrb::Common::UnexpectedException)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/wikidotrb/module/forum_thread.rb', line 162

def edit(title: nil, description: nil)
  @site.client.
  raise Wikidotrb::Common::UnexpectedException, "Title can not be left empty." if title == ""

  raise Wikidotrb::Common::UnexpectedException, "Page's discussion can not be edited." if @page

  return self if title.nil? && description.nil?

  @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "title" => @title.nil? ? title : @title,
        "description" => description.nil? ? @description : description,
        "action" => "ForumAction",
        "event" => "saveThreadMeta",
        "moduleName" => "Empty"
      }
    ]
  )

  @title = title.nil? ? @title : title
  @description = description.nil? ? @description : description

  self
end

#get(post_id)

投稿を取得する



341
342
343
# File 'lib/wikidotrb/module/forum_thread.rb', line 341

def get(post_id)
  posts.find { |post| post.id == post_id }
end

#get_url

スレッドのURLを取得する



152
153
154
# File 'lib/wikidotrb/module/forum_thread.rb', line 152

def get_url
  "#{@site.get_url}/forum/t-#{@id}"
end

#lock

スレッドのロック



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/wikidotrb/module/forum_thread.rb', line 206

def lock
  @site.client.
  @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "block" => "true",
        "action" => "ForumAction",
        "event" => "saveBlock",
        "moduleName" => "Empty"
      }
    ]
  )
  self
end

#locked?Boolean

スレッドがロックされているか確認

Returns:

  • (Boolean)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/wikidotrb/module/forum_thread.rb', line 239

def locked?
  @site.client.
  response = @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "moduleName" => "forum/sub/ForumEditThreadBlockModule"
      }
    ]
  ).first

  html = Nokogiri::HTML(response.body.to_s)
  checked = html.at_css("input.checkbox")["checked"]

  !checked.nil?
end

#move_to(category_id)

スレッドのカテゴリ移動



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/wikidotrb/module/forum_thread.rb', line 190

def move_to(category_id)
  @site.client.
  @site.amc_request(
    bodies: [
      {
        "categoryId" => category_id,
        "threadId" => @id,
        "action" => "ForumAction",
        "event" => "moveThread",
        "moduleName" => "Empty"
      }
    ]
  )
end

#new_post(title: "", source: "", parent_id: "")

新しい投稿を作成する

Raises:

  • (Wikidotrb::Common::UnexpectedException)


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/wikidotrb/module/forum_thread.rb', line 308

def new_post(title: "", source: "", parent_id: "")
  client = @site.client
  client.
  raise Wikidotrb::Common::UnexpectedException, "Post body can not be left empty." if source == ""

  response = @site.amc_request(
    bodies: [
      {
        "parentId" => parent_id,
        "title" => title,
        "source" => source,
        "action" => "ForumAction",
        "event" => "savePost"
      }
    ]
  ).first

  body = JSON.parse(response.body.to_s)

  ForumPost.new(
    site: @site,
    id: body["postId"].to_i,
    forum: @forum,
    title: title,
    source: source,
    thread: self,
    parent_id: parent_id.empty? ? nil : parent_id.to_i,
    created_by: client.user.get(client.username),
    created_at: DateTime.parse(body["CURRENT_TIMESTAMP"])
  )
end

#postsForumPostCollection

投稿のコレクションを取得する

Returns:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/wikidotrb/module/forum_thread.rb', line 111

def posts
  client = @site.client
  responses = @site.amc_request(
    bodies: (1..@pagerno).map do |no|
      { "pagerNo" => no, "t" => @id, "order" => "", "moduleName" => "forum/ForumViewThreadPostsModule" }
    end
  )

  posts = []

  responses.each do |response|
    html = Nokogiri::HTML(response.body.to_s)
    html.css("div.post").each do |post|
      cuser = post.at_css("div.info span.printuser")
      codate = post.at_css("div.info span.odate")
      parent = post.parent["id"]
      parent_id = parent == "thread-container-posts" ? nil : parent.match(/fpc-(\d+)/)[1].to_i
      euser = post.at_css("div.changes span.printuser")
      eodate = post.at_css("div.changes span.odate a")

      posts << ForumPost.new(
        site: @site,
        id: post["id"].match(/post-(\d+)/)[1].to_i,
        forum: @forum,
        thread: self,
        _title: post.at_css("div.title").text.strip,
        parent_id: parent_id,
        created_by: user_parser(client, cuser),
        created_at: odate_parser(codate),
        edited_by: euser.nil? ? nil : client.user.get(euser.text),
        edited_at: eodate.nil? ? nil : odate_parser(eodate),
        source_ele: post.at_css("div.content"),
        source_text: post.at_css("div.content").text.strip
      )
    end
  end

  ForumPostCollection.new(thread: self, posts: posts)
end

#stick

スレッドを固定する



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/wikidotrb/module/forum_thread.rb', line 257

def stick
  @site.client.
  @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "sticky" => "true",
        "action" => "ForumAction",
        "event" => "saveSticky",
        "moduleName" => "Empty"
      }
    ]
  )
  self
end

#sticked?Boolean

スレッドが固定されているか確認

Returns:

  • (Boolean)


290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/wikidotrb/module/forum_thread.rb', line 290

def sticked?
  @site.client.
  response = @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "moduleName" => "forum/sub/ForumEditThreadStickinessModule"
      }
    ]
  ).first

  html = Nokogiri::HTML(response.body.to_s)
  checked = html.at_css("input.checkbox")["checked"]

  !checked.nil?
end

#unlock

スレッドのアンロック



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/wikidotrb/module/forum_thread.rb', line 223

def unlock
  @site.client.
  @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "action" => "ForumAction",
        "event" => "saveBlock",
        "moduleName" => "Empty"
      }
    ]
  )
  self
end

#unstick

スレッドの固定を解除する



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/wikidotrb/module/forum_thread.rb', line 274

def unstick
  @site.client.
  @site.amc_request(
    bodies: [
      {
        "threadId" => @id,
        "action" => "ForumAction",
        "event" => "saveSticky",
        "moduleName" => "Empty"
      }
    ]
  )
  self
end

#update

スレッドを更新する



157
158
159
# File 'lib/wikidotrb/module/forum_thread.rb', line 157

def update
  ForumThreadCollection.new(forum: @forum, threads: [self]).update.first
end