class Redd::Models::Subreddit

A subreddit.

Constants

SETTINGS_MAP

A mapping from keys returned by settings to keys required by modify_settings

Public Class Methods

from_id(client, id) click to toggle source

Create a Subreddit from its name. @param client [APIClient] the api client to initialize the object with @param id [String] the subreddit name @return [Subreddit]

# File lib/redd/models/subreddit.rb, line 33
def self.from_id(client, id)
  new(client, display_name: id)
end

Public Instance Methods

accept_moderator_invite() click to toggle source

Accept an invite to become a moderator of this subreddit.

# File lib/redd/models/subreddit.rb, line 331
def accept_moderator_invite
  @client.post("/r/#{get_attribute(:display_name)}/api/accept_moderator_invite")
end
add_contributor(user) click to toggle source

Add a contributor to the subreddit. @param user [User] the user to add

# File lib/redd/models/subreddit.rb, line 348
def add_contributor(user)
  add_relationship(type: 'contributor', name: user.name)
end
add_wiki_contributor(user) click to toggle source

Allow a user to contribute to the wiki. @param user [User] the user to add

# File lib/redd/models/subreddit.rb, line 382
def add_wiki_contributor(user)
  add_relationship(type: 'wikicontributor', name: user.name)
end
ban(user, **params) click to toggle source

Ban a user from a subreddit. @param user [User] the user to ban @param params [Hash] additional options to supply with the request @option params [String] :ban_reason the reason for the ban @option params [String] :ban_message a message sent to the banned user @option params [String] :note a note that only moderators can see @option params [Integer] :duration the number of days to ban the user (if temporary)

# File lib/redd/models/subreddit.rb, line 370
def ban(user, **params)
  add_relationship(type: 'banned', name: user.name, **params)
end
ban_wiki_contributor(user, **params) click to toggle source

Ban a user from contributing to the wiki. @param user [User] the user to ban @param params [Hash] additional options to supply with the request @option params [String] :ban_reason the reason for the ban (not sure this matters) @option params [String] :note a note that only moderators can see @option params [Integer] :duration the number of days to ban the user (if temporary)

# File lib/redd/models/subreddit.rb, line 398
def ban_wiki_contributor(user, **params)
  add_relationship(type: 'wikibanned', name: user.name, **params)
end
comment_stream(**params, &block) click to toggle source

Stream newly submitted comments.

# File lib/redd/models/subreddit.rb, line 165
def comment_stream(**params, &block)
  params[:limit] ||= 100
  stream = Utilities::Stream.new do |previous|
    before = previous ? previous.first.name : nil
    listing(:comments, params.merge(before: before))
  end
  block_given? ? stream.stream(&block) : stream.enum_for(:stream)
end
delete_flair(user) click to toggle source

Remove the flair from a user @param thing [User, String] a User from which to remove flair

# File lib/redd/models/subreddit.rb, line 241
def delete_flair(user)
  name = user.is_a?(User) ? user.name : user
  @client.post("/r/#{get_attribute(:display_name)}/api/deleteflair", name: name)
end
flair_listing(**params) click to toggle source

Get a listing of all user flairs. @param params [Hash] a list of params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count the number of items already seen in the listing @option params [String] :name prefer {#get_flair} @option params [:links, :comments] :only the type of objects required

@return [Listing<Hash<Symbol, String>>]

# File lib/redd/models/subreddit.rb, line 224
def flair_listing(**params)
  res = @client.get("/r/#{get_attribute(:display_name)}/api/flairlist", params).body
  Listing.new(@client, children: res[:users], before: res[:prev], after: res[:next])
end
get_flair(user) click to toggle source

Get the user's flair data. @param user [User] the user whose flair to fetch @return [Hash, nil]

# File lib/redd/models/subreddit.rb, line 232
def get_flair(user)
  # We have to do this because reddit returns all flairs if given a nonexistent user
  flair = flair_listing(name: user.name).first
  return flair if flair && flair[:user].casecmp(user.name).zero?
  nil
end
invite_moderator(user, permissions: '+all') click to toggle source

Invite a user to moderate this subreddit. @param user [User] the user to invite @param permissions [String] the permission string to invite the user with

# File lib/redd/models/subreddit.rb, line 320
def invite_moderator(user, permissions: '+all')
  add_relationship(type: 'moderator_invite', name: user.name, permissions: permissions)
end
leave_contributor() click to toggle source

Leave from being a contributor on a subreddit.

# File lib/redd/models/subreddit.rb, line 359
def leave_contributor
  @client.post('/api/leavecontributor', id: get_attribute(:name))
end
leave_moderator() click to toggle source

Leave from being a moderator on a subreddit.

# File lib/redd/models/subreddit.rb, line 342
def leave_moderator
  @client.post('/api/leavemoderator', id: get_attribute(:name))
end
listing(sort, **params) click to toggle source

Get the appropriate listing. @param sort [:hot, :new, :top, :controversial, :comments, :rising, :gilded] the type of

listing

@param params [Hash] a list of params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count the number of items already seen in the listing @option params [1..100] :limit the maximum number of things to return @option params [:hour, :day, :week, :month, :year, :all] :time the time period to consider

when sorting

@note The option :time only applies to the top and controversial sorts. @return [Listing<Submission, Comment>]

# File lib/redd/models/subreddit.rb, line 73
def listing(sort, **params)
  params[:t] = params.delete(:time) if params.key?(:time)
  @client.model(:get, "/r/#{get_attribute(:display_name)}/#{sort}", params)
end
mod_log(**params) click to toggle source

Get the moderation log. @param params [Hash] a list of params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count the number of items already seen in the listing @option params [1..100] :limit the maximum number of things to return @option params [String] :type filter events to a specific type

@return [Listing<ModAction>]

# File lib/redd/models/subreddit.rb, line 313
def mod_log(**params)
  @client.model(:get, "/r/#{get_attribute(:display_name)}/about/log", params)
end
moderator_listing(type, **params) click to toggle source

Get the appropriate moderator listing. @param type [:reports, :spam, :modqueue, :unmoderated, :edited] the type of listing @param params [Hash] a list of params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count the number of items already seen in the listing @option params [1..100] :limit the maximum number of things to return @option params [:links, :comments] :only the type of objects required

@return [Listing<Submission, Comment>]

# File lib/redd/models/subreddit.rb, line 104
def moderator_listing(type, **params)
  @client.model(:get, "/r/#{get_attribute(:display_name)}/about/#{type}", params)
end
modify_settings(**params) click to toggle source

Modify the subreddit's settings. @param params [Hash] the settings to change @see www.reddit.com/dev/api#POST_api_site_admin

# File lib/redd/models/subreddit.rb, line 297
def modify_settings(**params)
  full_params = settings.merge(params)
  full_params[:sr] = get_attribute(:name)
  SETTINGS_MAP.each { |src, dest| full_params[dest] = full_params.delete(src) }
  @client.post('/api/site_admin', full_params)
end
post_stream(**params, &block) click to toggle source

Stream newly submitted posts.

# File lib/redd/models/subreddit.rb, line 155
def post_stream(**params, &block)
  params[:limit] ||= 100
  stream = Utilities::Stream.new do |previous|
    before = previous ? previous.first.name : nil
    listing(:new, params.merge(before: before))
  end
  block_given? ? stream.stream(&block) : stream.enum_for(:stream)
end
relationship_listing(type, **params) click to toggle source

Get the appropriate relationship listing. @param type [:banned, :muted, :wikibanned, :contributors, :wikicontributors, :moderators]

the type of listing

@param params [Hash] a list of params to send with the request @option params [String] :after return results after the given fullname @option params [String] :before return results before the given fullname @option params [Integer] :count the number of items already seen in the listing @option params [1..100] :limit the maximum number of things to return @option params [String] :user find a specific user

@return [Array<Hash>]

# File lib/redd/models/subreddit.rb, line 133
def relationship_listing(type, **params)
  # TODO: add methods to determine if a certain user was banned/muted/etc
  # TODO: return User types?
  user_list = @client.get("/r/#{get_attribute(:display_name)}/about/#{type}", params).body
  user_list[:data][:children]
end
remove_contributor(user) click to toggle source

Remove a contributor from the subreddit. @param user [User] the user to remove

# File lib/redd/models/subreddit.rb, line 354
def remove_contributor(user)
  remove_relationship(type: 'contributor', name: user.name)
end
remove_moderator(user) click to toggle source

Dethrone a moderator. @param user [User] the user to remove

# File lib/redd/models/subreddit.rb, line 337
def remove_moderator(user)
  remove_relationship(type: 'moderator', name: user.name)
end
remove_wiki_contributor(user) click to toggle source

No longer allow a user to contribute to the wiki. @param user [User] the user to remove

# File lib/redd/models/subreddit.rb, line 388
def remove_wiki_contributor(user)
  remove_relationship(type: 'wikicontributor', name: user.name)
end
send_message(subject:, text:, from: nil) click to toggle source

Compose a message to the moderators of a subreddit.

@param subject [String] the subject of the message @param text [String] the message text @param from [Subreddit, nil] the subreddit to send the message on behalf of

Calls superclass method Redd::Models::Messageable#send_message
# File lib/redd/models/subreddit.rb, line 200
def send_message(subject:, text:, from: nil)
  super(to: "/r/#{get_attribute(:display_name)}", subject: subject, text: text, from: from)
end
set_flair(thing, text, css_class: nil) click to toggle source

Set the flair for a link or a user for this subreddit. @param thing [User, Submission] the object whose flair to edit @param text [String] a string no longer than 64 characters @param css_class [String] the css class to assign to the flair

# File lib/redd/models/subreddit.rb, line 208
def set_flair(thing, text, css_class: nil)
  key = thing.is_a?(User) ? :name : :link
  params = { :text => text, key => thing.name }
  params[:css_class] = css_class if css_class
  @client.post("/r/#{get_attribute(:display_name)}/api/flair", params)
end
set_flair_template(thing, template_id, text: nil) click to toggle source

Set a Submission's or User's flair based on a flair template id. @param thing [User, Submission] an object to assign a template to @param template_id [String] the UUID of the flair template to assign @param text [String] optional text for the flair

# File lib/redd/models/subreddit.rb, line 250
def set_flair_template(thing, template_id, text: nil)
  key = thing.is_a?(User) ? :name : :link
  params = { key => thing.name, flair_template_id: template_id, text: text }
  @client.post("/r/#{get_attribute(:display_name)}/api/selectflair", params)
end
settings() click to toggle source

@return [Hash] the subreddit's settings

# File lib/redd/models/subreddit.rb, line 290
def settings
  @client.get("/r/#{get_attribute(:display_name)}/about/edit").body[:data]
end
stylesheet() click to toggle source

Get the subreddit's CSS. @return [String, nil] the stylesheet or nil if no stylesheet exists

# File lib/redd/models/subreddit.rb, line 273
def stylesheet
  url = @client.get("/r/#{get_attribute(:display_name)}/stylesheet").headers['location']
  HTTP.get(url).body.to_s
rescue Redd::NotFound
  nil
end
submit(title, text: nil, url: nil, resubmit: false, sendreplies: true) click to toggle source

Submit a link or a text post to the subreddit. @note If both text and url are provided, url takes precedence.

@param title [String] the title of the submission @param text [String] the text of the self-post @param url [String] the URL of the link @param resubmit [Boolean] whether to post a link to the subreddit despite it having been

posted there before (you monster)

@param sendreplies [Boolean] whether to send the replies to your inbox @return [Submission] The returned object (url, id and name)

# File lib/redd/models/subreddit.rb, line 184
def submit(title, text: nil, url: nil, resubmit: false, sendreplies: true)
  params = {
    title: title, sr: get_attribute(:display_name),
    resubmit: resubmit, sendreplies: sendreplies
  }
  params[:kind] = url ? 'link' : 'self'
  params[:url]  = url  if url
  params[:text] = text if text
  Submission.new(@client, @client.post('/api/submit', params).body[:json][:data])
end
subscribe(action: :sub, skip_initial_defaults: false) click to toggle source

Add the subreddit to the user's subscribed subreddits.

# File lib/redd/models/subreddit.rb, line 257
def subscribe(action: :sub, skip_initial_defaults: false)
  @client.post(
    '/api/subscribe',
    sr_name: get_attribute(:display_name),
    action: action,
    skip_initial_defaults: skip_initial_defaults
  )
end
unban(user) click to toggle source

Remove a ban on a user. @param user [User] the user to unban

# File lib/redd/models/subreddit.rb, line 376
def unban(user)
  remove_relationship(type: 'banned', name: user.name)
end
unban_wiki_contributor(user) click to toggle source

No longer ban a user from contributing to the wiki. @param user [User] the user to unban

# File lib/redd/models/subreddit.rb, line 404
def unban_wiki_contributor(user)
  remove_relationship(type: 'wikibanned', name: user.name)
end
uninvite_moderator(user) click to toggle source

Take back a moderator request. @param user [User] the requested user

# File lib/redd/models/subreddit.rb, line 326
def uninvite_moderator(user)
  remove_relationship(type: 'moderator_invite', name: user.name)
end
unsubscribe() click to toggle source

Remove the subreddit from the user's subscribed subreddits.

# File lib/redd/models/subreddit.rb, line 267
def unsubscribe
  subscribe(action: :unsub)
end
update_stylesheet(text, reason: nil) click to toggle source

Edit the subreddit's stylesheet. @param text [String] the updated CSS @param reason [String] the reason for modifying the stylesheet

# File lib/redd/models/subreddit.rb, line 283
def update_stylesheet(text, reason: nil)
  params = { op: 'save', stylesheet_contents: text }
  params[:reason] = reason if reason
  @client.post("/r/#{get_attribute(:display_name)}/api/subreddit_stylesheet", params)
end
upload_image(file:, image_type:, upload_type:, image_name: nil) click to toggle source

Upload a subreddit-specific image. @param file [String, IO] the image file to upload @param image_type ['jpg', 'png'] the image type @param upload_type ['img', 'header', 'icon', 'banner'] where to upload the image @param image_name [String] the name of the image (if upload_type is 'img') @return [String] the url of the uploaded file

# File lib/redd/models/subreddit.rb, line 414
def upload_image(file:, image_type:, upload_type:, image_name: nil)
  file_data = HTTP::FormData::File.new(file)
  params = { img_type: image_type, upload_type: upload_type, file: file_data }
  params[:name] = image_name if upload_type.to_s == 'img'
  @client.post("/r/#{display_name}/api/upload_sr_img", params).body[:img_src]
end
wiki_page(title) click to toggle source

Get a wiki page by its title. @param title [String] the page's title @return [WikiPage]

# File lib/redd/models/subreddit.rb, line 45
def wiki_page(title)
  WikiPage.new(@client, title: title, subreddit: self)
end
wiki_pages() click to toggle source

@return [Array<String>] the subreddit's wiki pages

# File lib/redd/models/subreddit.rb, line 38
def wiki_pages
  @client.get("/r/#{get_attribute(:display_name)}/wiki/pages").body[:data]
end

Private Instance Methods

add_relationship(**params) click to toggle source
# File lib/redd/models/subreddit.rb, line 427
def add_relationship(**params)
  @client.post("/r/#{get_attribute(:display_name)}/api/friend", params)
end
default_loader() click to toggle source
# File lib/redd/models/subreddit.rb, line 423
def default_loader
  @client.get("/r/#{@attributes.fetch(:display_name)}/about").body[:data]
end
remove_relationship(**params) click to toggle source
# File lib/redd/models/subreddit.rb, line 431
def remove_relationship(**params)
  @client.post("/r/#{get_attribute(:display_name)}/api/unfriend", params)
end