Class: Note

Inherits:
Content
  • Object
show all
Includes:
ConfigManager, PublifyGuid
Defined in:
app/models/note.rb

Constant Summary collapse

TWITTER_FTP_URL_LENGTH =
19
TWITTER_HTTP_URL_LENGTH =
20
TWITTER_HTTPS_URL_LENGTH =
21
22

Instance Attribute Summary

Attributes included from ContentBase

#just_changed_published_status

Instance Method Summary collapse

Methods included from ConfigManager

append_features, #canonicalize

Methods included from PublifyGuid

#create_guid

Methods inherited from Content

#author=, #author_name, find_already_published, #rss_description, search_with, #short_url, #shorten_url, #whiteboard

Methods included from ContentBase

#default_text_filter, #excerpt_text, #generate_html, #html, #html_map, #html_postprocess, included, #really_send_notifications, #send_notification_to_user, #text_filter

Instance Method Details

#access_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/models/note.rb', line 108

def access_by?(user)
  user.admin? || user_id == user.id
end

#categoriesObject



38
39
40
# File 'app/models/note.rb', line 38

def categories
  []
end

#html_preprocess(_field, html) ⇒ Object



46
47
48
# File 'app/models/note.rb', line 46

def html_preprocess(_field, html)
  PublifyApp::Textfilter::Twitterfilter.filtertext(html)
end

#password_protected?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/note.rb', line 104

def password_protected?
  false
end


112
113
114
115
116
117
118
119
# File 'app/models/note.rb', line 112

def permalink_url(anchor = nil, only_path = false)
  blog.url_for(
    controller: "/notes",
    action: "show",
    permalink: permalink,
    anchor: anchor,
    only_path: only_path)
end

#prefixObject



126
127
128
# File 'app/models/note.rb', line 126

def prefix
  blog.shortener_url.sub(%r{^https?://}, "")
end

#published?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/note.rb', line 130

def published?
  state == "published"
end

#send_to_twitterObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/note.rb', line 73

def send_to_twitter
  return false unless blog.has_twitter_configured?
  return false unless user.has_twitter_configured?

  twitter = Twitter::REST::Client.new do |config|
    config.consumer_key = blog.twitter_consumer_key
    config.consumer_secret = blog.twitter_consumer_secret
    config.access_token = user.twitter_oauth_token
    config.access_token_secret = user.twitter_oauth_token_secret
  end

  begin
    options = {}
    if in_reply_to_status_id && in_reply_to_status_id != ""
      options = { in_reply_to_status_id: in_reply_to_status_id }
      self.in_reply_to_message = twitter.status(in_reply_to_status_id).to_json
    end
    tweet = twitter.update(twitter_message, options)
    self.twitter_id = tweet.attrs[:id_str]
    save
    user.update_twitter_profile_image(tweet.attrs[:user][:profile_image_url])
    true
  rescue StandardError => e
    Rails.logger.error("Error while sending to twitter: #{e}")
    errors.add(:message, e)
    false
  end
end


33
34
35
36
# File 'app/models/note.rb', line 33

def set_permalink
  self.permalink = "#{id}-#{body.to_permalink[0..79]}" if permalink.blank?
  save
end


121
122
123
124
# File 'app/models/note.rb', line 121

def short_link
  path = redirect.from_path
  "#{prefix} #{path}"
end

#tagsObject



42
43
44
# File 'app/models/note.rb', line 42

def tags
  []
end

#truncate(message, length) ⇒ Object



50
51
52
53
54
55
56
# File 'app/models/note.rb', line 50

def truncate(message, length)
  if message[length + 1] == " "
    message[0..length]
  else
    message[0..(message[0..length].rindex(" ") - 1)]
  end
end

#twitter_messageObject



58
59
60
61
62
63
64
65
66
# File 'app/models/note.rb', line 58

def twitter_message
  base_message = body.strip_html
  if too_long?("#{base_message} (#{short_link})")
    max_length = 140 - "... (#{redirect.from_url})".length - 1
    "#{truncate(base_message, max_length)}... (#{redirect.from_url})"
  else
    "#{base_message} (#{short_link})"
  end
end

#twitter_urlObject

FIXME: This breaks if the user changes or deletes their handle.



69
70
71
# File 'app/models/note.rb', line 69

def twitter_url
  File.join("https://twitter.com", user.twitter, "status", twitter_id)
end