Module: Snoo::Moderation

Defined in:
lib/snoo/moderation.rb

Overview

Methods for moderating on reddit, including tasks such as removing, approving, and distinguishing

Author:

Instance Method Summary collapse

Instance Method Details

#approve(id) ⇒ Object

Approve a thing

Parameters:

  • id (String)

    Thing targeted



11
12
13
14
# File 'lib/snoo/moderation.rb', line 11

def approve id
  logged_in?
  post('/api/approve', body: {id: id, uh: @modhash, api_type: 'json'})
end

#distinguish(id, how = "yes") ⇒ Object

Distinguish a thing

Parameters:

  • how (yes, no, admin, special) (defaults to: "yes")

    (yes) Determines how to distinguish something. Only works for the permissions you have.

  • id (String)

    Thing targeted



21
22
23
24
25
# File 'lib/snoo/moderation.rb', line 21

def distinguish id, how = "yes"
  logged_in?
  hows = %w{yes no admin special}
  post('/api/distinguish', body: {id: id, how: how, uh: @modhash, api_type: 'json'})
end

#get_modlog(subreddit, opts = {}) ⇒ Hash

Gets a moderation log This is a tricky function, and may break a lot. Blame the lack of a real api

Parameters:

  • subreddit (String)

    The subreddit to fetch from

  • opts (Hash) (defaults to: {})

    Options to pass to reddit

Options Hash (opts):

  • :limit (Fixnum) — default: 100

    The number to get. Can't be higher than 100

  • :before (String)

    The "fullname" to fetch before.

  • :after (String)

    The "fullname" to fetch after (older than).

  • :type (String)
  • :mod (String)

    The moderator to get. Name, not ID

Returns:

  • (Hash)

    A hash consisting of the data, first fullname, last fullname, first date, and last date



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/snoo/moderation.rb', line 69

def get_modlog subreddit, opts = {}
  logged_in?
  options = {
    limit: 100
  }.merge opts
  data = Nokogiri::HTML.parse(get("/r/#{subreddit}/about/log", query: options).body).css('.modactionlisting tr')
  processed = {
    data:       [],
    first:      data[0]['data-fullname'],
    first_date: Time.parse(data[0].children[0].child['datetime']),
    last:       data[-1]['data-fullname'],
    last_date:  Time.parse(data[-1].children[0].child['datetime']),
  }
  data.each do |tr|
    processed[:data] << {
      fullname:     tr['data-fullname'],
      time:         Time.parse(tr.children[0].child['datetime']),
      author:       tr.children[1].child.content,
      action:       tr.children[2].child['class'].split[1],
      description:  tr.children[3].content,
      href:         tr.children[3].css('a').count == 0 ? nil : tr.children[3].css('a')[0]['href']
    }
  end
  return processed
end

#get_modqueue(subreddit, opts = {}) ⇒ Object

Get the modqueue, or a subset of it (dear god)

Parameters:

  • subreddit (String)

    The subreddit to fetch from

  • opts (Hash) (defaults to: {})

    The options hash to pass to reddit

Options Hash (opts):

  • :limit (Fixnum) — default: 100

    The total items to return. Can't go higher than 100

  • :before (String)

    The thing_id to fetch before (newer than). Can be either a link (t3_) or comment (t1_)

  • :after (String)

    The thing_id to fetch after (older than). Can be either a link (t3_) or comment (t1_)



102
103
104
105
106
107
108
109
# File 'lib/snoo/moderation.rb', line 102

def get_modqueue subreddit, opts={}
  logged_in?
  options = {
    limit: 100
  }.merge opts

  get("/r/#{subreddit}/about/modqueue.json", query: options)
end

#leave_contributor(id) ⇒ Object

Removes you from a subreddits list of contributors

Parameters:

  • id (String)

    The subreddit id



32
33
34
35
# File 'lib/snoo/moderation.rb', line 32

def leave_contributor id
  logged_in?
  post('/api/leavecontributor', body: {id: id, uh: @modhash, api_type: 'json'})
end

#leave_moderator(id) ⇒ Object

Removes you from a subreddits moderators

Parameters:

  • id (String)

    The subreddit id



42
43
44
45
# File 'lib/snoo/moderation.rb', line 42

def leave_moderator id
  logged_in?
  post('/api/leavemoderator', body: {id: id, uh: @modhash, api_type: 'json'})
end

#remove(id, spam = false) ⇒ Object

Removes a thing

Parameters:

  • spam (true, false) (defaults to: false)

    Mark this removal as a spam removal (and train the spamfilter)

  • id (String)

    Thing targeted



52
53
54
55
# File 'lib/snoo/moderation.rb', line 52

def remove id, spam = false
  logged_in?
  post('/api/remove', body: {id: id, spam: spam, uh: @modhash, api_type: 'json'})
end