diff options
author | Benjamin Neff <benjamin@coding4coffee.ch> | 2016-03-06 17:26:05 +0300 |
---|---|---|
committer | Benjamin Neff <benjamin@coding4coffee.ch> | 2016-03-07 07:52:54 +0300 |
commit | d94eae0d45b6a081813271f3d889cf6c430cd8ec (patch) | |
tree | 09a260f7e25623a958944fd115f7fa1ebbd13034 /app/services | |
parent | d872c6436910dd27b59c3232f881b704765f06a0 (diff) |
refactoring PostService
* move presenters back to controllers, this is view-logic
* use PostService in CommentService
* remove iframe route, this is not used anymore
* id/guid limit at 16 chars, hex(8) is 16 chars long
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/comment_service.rb | 18 | ||||
-rw-r--r-- | app/services/post_service.rb | 69 |
2 files changed, 36 insertions, 51 deletions
diff --git a/app/services/comment_service.rb b/app/services/comment_service.rb index b417933a9..3f11208cc 100644 --- a/app/services/comment_service.rb +++ b/app/services/comment_service.rb @@ -4,7 +4,7 @@ class CommentService end def create(post_id, text) - post = find_post!(post_id) + post = post_service.find(post_id) user.comment!(post, text) end @@ -19,24 +19,14 @@ class CommentService end def find_for_post(post_id) - find_post!(post_id).comments.for_a_stream + post_service.find(post_id).comments.for_a_stream end private attr_reader :user - def find_post!(post_id) - find_post(post_id).tap do |post| - raise ActiveRecord::RecordNotFound unless post - end - end - - def find_post(post_id) - if user - user.find_visible_shareable_by_id(Post, post_id) - else - Post.find_by_id_and_public(post_id, true) - end + def post_service + @post_service ||= PostService.new(user) end end diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 1408d0642..928375e29 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -1,64 +1,59 @@ class PostService - attr_reader :post - - def initialize(params) - @id = params[:id] - @user = params[:user] - @oembed = params[:oembed] || {} - assign_post + def initialize(user=nil) + @user = user end - def assign_post + def find(id_or_guid) if user - @post = Post.find_non_public_by_guid_or_id_with_user(id, user) + find_non_public_by_guid_or_id_with_user(id_or_guid) else - @post = Post.find_public(id) + find_public(id_or_guid) end end - def present_json - PostPresenter.new(post, user) + def mark_user_notifications(post_id) + return unless user + mark_comment_reshare_like_notifications_read(post_id) + mark_mention_notifications_read(post_id) end - def present_interactions_json - PostInteractionPresenter.new(post, user) + def destroy(post_id) + post = find(post_id) + raise Diaspora::NotMine unless post.author == user.person + user.retract(post) end - def present_oembed - OEmbedPresenter.new(post, oembed) - end + private - def mark_user_notifications - mark_corresponding_notifications_read if user - end + attr_reader :user - def retract_post - raise Diaspora::NotMine unless user_owns_post? - user.retract(@post) + def find_public(id_or_guid) + Post.where(post_key(id_or_guid) => id_or_guid).first.tap do |post| + raise ActiveRecord::RecordNotFound, "could not find a post with id #{id_or_guid}" unless post + raise Diaspora::NonPublic unless post.public? + end end - private - - attr_reader :user, :id, :oembed - - def user_owns_post? - post.author == user.person + def find_non_public_by_guid_or_id_with_user(id_or_guid) + user.find_visible_shareable_by_id(Post, id_or_guid, key: post_key(id_or_guid)).tap do |post| + raise ActiveRecord::RecordNotFound, "could not find a post with id #{id_or_guid} for user #{user.id}" unless post + end end - def mark_corresponding_notifications_read - mark_comment_reshare_like_notifications_read - mark_mention_notifications_read + # We can assume a guid is at least 16 characters long as we have guids set to hex(8) since we started using them. + def post_key(id_or_guid) + id_or_guid.to_s.length < 16 ? :id : :guid end - def mark_comment_reshare_like_notifications_read - notification = Notification.where(recipient_id: user.id, target_type: "Post", target_id: post.id, unread: true) - notification.each do |notification| + def mark_comment_reshare_like_notifications_read(post_id) + notifications = Notification.where(recipient_id: user.id, target_type: "Post", target_id: post_id, unread: true) + notifications.each do |notification| notification.set_read_state(true) end end - def mark_mention_notifications_read - mention = post.mentions.where(person_id: user.person_id).first + def mark_mention_notifications_read(post_id) + mention = find(post_id).mentions.where(person_id: user.person_id).first Notification.where(recipient_id: user.id, target_type: "Mention", target_id: mention.id, unread: true) .first.try(:set_read_state, true) if mention end |