/posts.json: add is_favorited attribute (#3633)
ref: https://github.com/r888888888/danbooru/issues/3633#issuecomment-384863568.
This commit is contained in:
@@ -922,10 +922,12 @@ class Post < ApplicationRecord
|
|||||||
update_column(:fav_count, fav_count)
|
update_column(:fav_count, fav_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def favorited_by?(user_id)
|
def favorited_by?(user_id = CurrentUser.id)
|
||||||
!!(fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/)
|
!!(fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias_method :is_favorited?, :favorited_by?
|
||||||
|
|
||||||
def append_user_to_fav_string(user_id)
|
def append_user_to_fav_string(user_id)
|
||||||
update_column(:fav_string, (fav_string + " fav:#{user_id}").strip)
|
update_column(:fav_string, (fav_string + " fav:#{user_id}").strip)
|
||||||
clean_fav_string! if clean_fav_string?
|
clean_fav_string! if clean_fav_string?
|
||||||
@@ -1478,7 +1480,7 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def method_attributes
|
def method_attributes
|
||||||
list = super + [:uploader_name, :has_large, :has_visible_children, :children_ids] + TagCategory.categories.map {|x| "tag_string_#{x}".to_sym}
|
list = super + [:uploader_name, :has_large, :has_visible_children, :children_ids, :is_favorited?] + TagCategory.categories.map {|x| "tag_string_#{x}".to_sym}
|
||||||
if visible?
|
if visible?
|
||||||
list += [:file_url, :large_file_url, :preview_file_url]
|
list += [:file_url, :large_file_url, :preview_file_url]
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user