lib/redd/objects/subreddit.rb in redd-0.7.3 vs lib/redd/objects/subreddit.rb in redd-0.7.4

- old
+ new

@@ -1,5 +1,6 @@ +require "fastimage" require_relative "thing" module Redd module Objects # A comment made on links. @@ -12,10 +13,53 @@ alias_property :nsfw?, :over18 alias_property :users_online, :accounts_active alias_property :type, :subreddit_type alias_property :times_gilded, :gilded + # @!group Submissions + + # Submit a link or a text post to the subreddit. + # + # @param [String] title The title of the submission. + # @param [String] captcha A possible captcha result to send if one + # is required. + # @param [String] identifier The identifier for the captcha if one + # is required. + # @param [String] text The text of the self-post. + # @param [String] url The URL of the link. + # @param [Boolean] resubmit Whether to post a link to the subreddit + # despite it having been posted there before (you monster). + # @param [Boolean] sendreplies Whether to send the replies to your + # inbox. + # @return [Objects::Thing] The returned result (url, id and name). + def submit( + title, captcha = nil, identifier = nil, text: nil, url: nil, + resubmit: false, sendreplies: true + ) + + params = { + extension: "json", title: title, sr: display_name, + resubmit: resubmit, sendreplies: sendreplies + } + + params << {captcha: captcha, iden: identifier} if captcha + params[:kind], params[:text] = :self, text if text + params[:kind], params[:url] = :link, url if url + + response = post("/api/submit", params) + Objects::Thing.new(self, response.body[:json][:data]) + end + + # Add a comment to the submission. + # @param text [String] The text to comment. + # @return [Objects::Comment] The reply. + def add_comment(text) + client.add_comment(self, text) + end + + # @!endgroup + # @!group Stylesheets # @return [String] The url for the subreddit's stylesheet. def stylesheet_url get("/r/#{display_name}/stylesheet").headers["location"] @@ -170,10 +214,14 @@ # @return [Objects::Listing<Objects::Thing>] def search(query, **params) client.search(query, self, **params) end + # @!endgroup + + # @!group Moderation + # @!method get_reports(**params) # @!method get_spam(**params) # @!method get_modqueue(**params) # @!method get_unmoderated(**params) # @!method get_edited(**params) @@ -199,45 +247,70 @@ :get, "/r/#{display_name}/about/#{sort}", params ) end end - # @!endgroup - - # @!group Moderator Settings - # @return [Objects::Base] The current settings of a subreddit. def admin_about client.request_object(:get, "/r/#{display_name}/about/edit.json") end # Edit the subreddit's settings # @param [Hash] attributes The subreddit's new settings. - # @author Takashi M (@beatak) and Avinash Dwarapu (@avidw) # @note This method may make additional requests if not all of the - # required attributes are provided. + # required attributes are provided. Take a look at the source for the + # required attributes required to avoid making the additional request. # @see https://github.com/alaycock/MeetCal-bot/blob/master/serverInfo.conf def admin_edit(attributes) - params = {sr: fullname} - required_attributes = %i( + params = { + # Subreddit name + sr: fullname, + # Apparently useless options + show_cname_sidebar: true, + :"header-title" => title + } + + required = %i( allow_top collapse_deleted_comments comment_score_hide_mins - css_on_cname description exclude_banned_modqueue lang name over_18 - public_description public_traffic show_cname_sidebar show_media - spam_comments spam_links spam_selfposts submit_link_label - submit_text submit_text_label title type wiki_edit_age - wiki_edit_karma wikimode header-title + css_on_cname description exclude_banned_modqueue lang link_type name + over_18 public_description public_traffic show_media spam_comments + spam_links spam_selfposts submit_link_label submit_text + submit_text_label title type wiki_edit_age wiki_edit_karma wikimode ) - if required_attributes.all? { |key| attributes.key?(key) } + if required.all? { |key| attributes.key?(key) } params.merge!(attributes) else - current = admin_about - current.delete(:kind) - complete = current.merge(attributes) - params.merge!(complete) + about = admin_about + final = about + .select { |k, _| required.include?(k) } + .merge( + name: display_name, + type: about[:subreddit_type], + lang: about[:language], + link_type: about[:content_options], + allow_top: true, + css_on_cname: true + ) + .merge(attributes) + params.merge!(final) end post("/api/site_admin", params) + end + + # Add or replace the subreddit image or header logo. + # @param [String, IO] file The path/url to the file or the file itself. + # @param [String] name The name of the uploaded file. + # @return [String] The url of the image on reddit's CDN. + def upload_image(file, name = nil) + io = (file.is_a?(IO) ? file : File.open(file, "r")) + type = FastImage.type(io) + payload = Faraday::UploadIO.new(io, "image/#{type}") + + params = {file: payload, header: (name ? 0 : 1), img_type: type} + params[:name] = name if name + post("/r/#{display_name}/api/upload_sr_img", params).body[:img_src] end # @!endgroup end end