class Hive::Post
Tracks all content including posts, comments (replies).
To find a post, you can do a lookup using the {Hive::Post#find_by_slug} scope:
post = Hive::Post.find_by_slug '@steemit/firstpost'
A post has many children, which are direct replies to that post.
children = post.children
Each child post has a parent that links back to the post it is a reply to:
parent = children.first.parent
A post also has an account_record, which is the ActiveRecord version of the account string:
account = post.account_record
A post has many followers, which are the accounts that follow the author:
followers = post.followers
A post belongs to a community, which can be accessed as community_record, the ActiveRecord version of the community string:
communit = post.community_record
A post has many flaggers, which are the accounts that have flagged this post:
flaggers = post.flaggers
A post has many rebloggers, which are the accounts that have reblogged this post:
rebloggers = post.rebloggers
A post has many promoters, which are the accounts that have promoted this post:
promoters = post.promoters
Scopes
¶ ↑
We can use these scopes to perform a lookup on posts:
posts = Hive::Post.author 'alice' posts = Hive::Post.community 'steemit' posts = Hive::Post.category 'steemit' posts = Hive::Post.depth 0 # only returns root posts posts = Hive::Post.root_posts # same as depth 0
Replies can be queried by specifying depth greater than zero:
replies = Hive::Post.depth 1..10 # only returns replies up to 10 in depth replies = Hive::Post.depth 1..255 # only returns replies replies = Hive::Post.replies # same as depth 1..255
We can also specify replies for a particular author, which is analogous to all replies to an author on steemit.com (i.e.: steemit.com/@alice/recent-replies):
replies = Hive::Post.replies(parent_author: 'alice')
We can query for posts that were reblogged by someone:
posts = Hive::Post.rebloggers('alice')
If we want to grab all of the posts that have all of these tags:
posts = Hive::Post.tagged(all: %w(steemit steem video youtube))
Or we can grab all of the posts with any of these tags:
posts = Hive::Post.tagged(any: %w(steemit steem video youtube))
Or, which is the same as using any:
posts = Hive::Post.tagged(%w(steemit steem video youtube))
Here, we can find all of the posts that mention all of these accounts:
posts = Hive::Post.mentioned(all: %w(alice bob))
Or find all of the posts that mention any of these accounts:
posts = Hive::Post.mentioned(any: %w(alice bob))
We can order by (which automatically joins {Hive::PostsCache}):
posts = Hive::Post.order_by_payout(:asc) posts = Hive::Post.order_by_payout # same as order_by_payout(:asc) posts = Hive::Post.order_by_payout(:desc) posts = Hive::Post.order_by_children posts = Hive::Post.order_by_author_rep posts = Hive::Post.order_by_total_votes posts = Hive::Post.order_by_up_votes posts = Hive::Post.order_by_promoted posts = Hive::Post.order_by_created_at posts = Hive::Post.order_by_payout_at posts = Hive::Post.order_by_updated_at posts = Hive::Post.order_by_rshares # first result is the most flagged post!
Other scopes:
posts = Hive::Post.deleted posts = Hive::Post.deleted(false) posts = Hive::Post.pinned posts = Hive::Post.pinned(false) posts = Hive::Post.muted posts = Hive::Post.muted(false) posts = Hive::Post.valid posts = Hive::Post.valid(false) posts = Hive::Post.promoted posts = Hive::Post.promoted(false) posts = Hive::Post.reblogged posts = Hive::Post.reblogged(false) posts = Hive::Post.after(7.days.ago) posts = Hive::Post.after(7.days.ago, invert: true) posts = Hive::Post.before(7.days.ago) posts = Hive::Post.before(7.days.ago, invert: true) posts = Hive::Post.updated_after(7.days.ago) posts = Hive::Post.updated_after(7.days.ago, invert: true) posts = Hive::Post.updated_before(7.days.ago) posts = Hive::Post.updated_before(7.days.ago, invert: true) posts = Hive::Post.payout_after(7.days.ago) posts = Hive::Post.payout_after(7.days.ago, invert: true) posts = Hive::Post.payout_before(7.days.ago) posts = Hive::Post.payout_before(7.days.ago, invert: true) posts = Hive::Post.payout_zero posts = Hive::Post.payout_zero(false) posts = Hive::Post.app('busy') posts = Hive::Post.app('busy', version: '1.0') posts = Hive::Post.app('busy', version: '1.0', invert: true) posts = Hive::Post.format('markdown') posts = Hive::Post.format('markdown', invert: false)
All scopes can be strung together, e.g.:
posts = Hive::Post.root_posts.author('alice').promoted posts = Hive::Post.replies(parent_author: 'alice').community('steemit') posts = Hive::Post.category('steemit').depth(255).valid posts = Hive::Post.tagged(all: %w(radiator ruby)).where.not(author: 'inertia') posts = Hive::Post.mentioned(all: %w(inertia whatsup)).tagged(any: %w(community shoutout))
Most interesting queries (once your node is fully synchronized):
Hive::Post.order_by_rshares.deleted.first # o_O mfw
Constants
- MAX_HARD_DEPTH
Blockchain limit on depth.
- MAX_SOFT_DEPTH
Witness limit on depth.
Public Class Methods
Finds a post by slug (or even URL).
@param slug String A composite of author and permlink that uniquely identifies a post. E.g.: “@steemit/firstpost” @return <Hive::Post>
# File lib/hive/models/post.rb, line 373 def self.find_by_slug(slug) slug = slug.split('@').last slug = slug.split('/') author = slug[0] permlink = slug[1..-1].join('/') Post.where(author: author, permlink: permlink).first end
Public Instance Methods
The entire discussion related to this post including all children and grandchildren replies (the result also includes this post).
@return {ActiveRecord::Relation}
# File lib/hive/models/post.rb, line 391 def discussion clause = <<-DONE hive_posts.community = ? AND hive_posts.category = ? AND hive_posts.id >= ? AND hive_posts.id IN(?) DONE Post.deleted(false). where(clause, community, category, id, discussion_ids) end
Returns the unique string containing the author and permlink, useful in restful development as param_id, if desired.
@return String
# File lib/hive/models/post.rb, line 407 def slug "@#{author}/#{permlink}" end
Private Instance Methods
@private
# File lib/hive/models/post.rb, line 412 def discussion_ids(ids = [id]) new_ids = Post.deleted(false).depth(1..MAX_SOFT_DEPTH). where(community: community, category: category). where(parent_id: ids).pluck(:id) ids += new_ids + discussion_ids(new_ids) if new_ids.any? ids.uniq.compact end