diff --git a/Changelog.md b/Changelog.md index 8f2f0dab1..012b710ec 100644 --- a/Changelog.md +++ b/Changelog.md @@ -37,6 +37,7 @@ * Add settings web mobile. [#3701](https://github.com/diaspora/diaspora/pull/3701) * Stream form on profile page [#3910](https://github.com/diaspora/diaspora/issues/3910). * Add Getting_Started page mobile. [#3949](https://github.com/diaspora/diaspora/issues/3949). +* Autoscroll to the first unread message in conversations [#3216](https://github.com/diaspora/diaspora/issues/3216) ## Bug Fixes diff --git a/app/assets/javascripts/inbox.js b/app/assets/javascripts/inbox.js index 2809bea60..899fb27d0 100644 --- a/app/assets/javascripts/inbox.js +++ b/app/assets/javascripts/inbox.js @@ -6,6 +6,10 @@ $(document).ready(function(){ + if ($('#first_unread').length > 0) { + $("html").scrollTop($('#first_unread').offset().top-45); + } + $('a.conversation').live('click', function(){ $.getScript(this.href, function() { Diaspora.page.directionDetector.updateBinds(); @@ -13,7 +17,7 @@ $(document).ready(function(){ history.pushState(null, "", this.href); var conv = $(this).children('.stream_element'), - cBadge = $("#message_inbox_badge").children(".badge_count"); + cBadge = $("#message_inbox_badge .badge_count"); if(conv.hasClass('unread') ){ conv.removeClass('unread'); } diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index acf91a70e..75ef768bd 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -10,16 +10,18 @@ class ConversationsController < ApplicationController @visibilities = ConversationVisibility.where(:person_id => current_user.person_id).paginate( :page => params[:page], :per_page => 15, :order => 'updated_at DESC') + + @conversation = Conversation.joins(:conversation_visibilities).where( + :conversation_visibilities => {:person_id => current_user.person_id, :conversation_id => params[:conversation_id]}).first @unread_counts = {} @visibilities.each { |v| @unread_counts[v.conversation_id] = v.unread } + + @first_unread_message_id = @conversation.try(:first_unread_message, current_user).try(:id) @authors = {} @conversations.each { |c| @authors[c.id] = c.last_author } - @conversation = Conversation.joins(:conversation_visibilities).where( - :conversation_visibilities => {:person_id => current_user.person_id, :conversation_id => params[:conversation_id]}).first - respond_with do |format| format.html format.json { render :json => @conversations, :status => 200 } @@ -56,6 +58,8 @@ class ConversationsController < ApplicationController def show if @conversation = Conversation.joins(:conversation_visibilities).where(:id => params[:id], :conversation_visibilities => {:person_id => current_user.person_id}).first + + @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id) if @visibility = ConversationVisibility.where(:conversation_id => params[:id], :person_id => current_user.person.id).first @visibility.unread = 0 @visibility.save diff --git a/app/models/conversation.rb b/app/models/conversation.rb index bdc3e123a..8d2f25c25 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -33,6 +33,12 @@ class Conversation < ActiveRecord::Base def diaspora_handle= nh self.author = Webfinger.new(nh).fetch end + + def first_unread_message(user) + if visibility = self.conversation_visibilities.where(:person_id => user.person.id).where('unread > 0').first + self.messages.all[-visibility.unread] + end + end def public? false diff --git a/app/views/conversations/show.js.erb b/app/views/conversations/show.js.erb index 767b22ad1..e0009ea26 100644 --- a/app/views/conversations/show.js.erb +++ b/app/views/conversations/show.js.erb @@ -4,4 +4,6 @@ $(".stream_element", "#conversation_inbox").removeClass('selected'); $(".stream_element[data-guid='<%= @conversation.id %>']", "#conversation_inbox").addClass('selected'); $(".stream_element[data-guid='<%= @conversation.id %>']", "#conversation_inbox").find(".unread_message_count").remove() +$("html").scrollTop($('#first_unread').offset().top-45); + Diaspora.page.timeAgo.updateTimeAgo(); diff --git a/app/views/messages/_message.haml b/app/views/messages/_message.haml index f471c3830..12607e024 100644 --- a/app/views/messages/_message.haml +++ b/app/views/messages/_message.haml @@ -2,7 +2,7 @@ -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -.stream_element{:data=>{:guid=>message.id}} +.stream_element{:data=>{:guid=>message.id}, :id => ('first_unread' if @first_unread_message_id == message.id)} .media .img = person_image_link(message.author, :size => :thumb_small) @@ -12,4 +12,4 @@ = t('ago', :time => time_ago_in_words(message.created_at)) %div{ :class => direction_for(message.text) } - = markdownify(message, :oembed => true) \ No newline at end of file + = markdownify(message, :oembed => true) diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 5c5b0f40c..90a852aa0 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -32,6 +32,22 @@ describe Conversation do end end + describe '#first_unread_message' do + before do + @cnv = Conversation.create(@create_hash) + @message = Message.create(:author => @user2.person, :created_at => Time.now + 100, :text => "last", :conversation_id => @cnv.id) + @message.increase_unread(@user1) + end + + it 'returns the first unread message if there are unread messages in a conversation' do + @cnv.first_unread_message(@user1) == @message + end + + it 'returns nil if there are no unread messages in a conversation' do + @cnv.conversation_visibilities.where(:person_id => @user1.person.id).first.tap { |cv| cv.unread = 0 }.save + @cnv.first_unread_message(@user1).should be_nil + end + end context 'transport' do before do