Add support for links to statuses in announcements to be opened in web UI (#13212)

* Add support for links to public statuses in announcements to be opened in WebUI

* Please CodeClimate
master
ThibG 5 years ago committed by GitHub
parent 5e4b649655
commit aa67036b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/javascript/mastodon/features/getting_started/components/announcements.js
  2. 4
      app/models/announcement.rb
  3. 15
      app/models/status.rb
  4. 13
      app/serializers/rest/announcement_serializer.rb

@ -95,6 +95,10 @@ class Content extends ImmutablePureComponent {
} else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) { } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false); link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
} else { } else {
let status = this.props.announcement.get('statuses').find(item => link.href === item.get('url'));
if (status) {
link.addEventListener('click', this.onStatusClick.bind(this, status), false);
}
link.setAttribute('title', link.href); link.setAttribute('title', link.href);
link.classList.add('unhandled-link'); link.classList.add('unhandled-link');
} }
@ -120,6 +124,13 @@ class Content extends ImmutablePureComponent {
} }
} }
onStatusClick = (status, e) => {
if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
e.preventDefault();
this.context.router.history.push(`/statuses/${status.get('id')}`);
}
}
handleEmojiMouseEnter = ({ target }) => { handleEmojiMouseEnter = ({ target }) => {
target.src = target.getAttribute('data-original'); target.src = target.getAttribute('data-original');
} }

@ -48,6 +48,10 @@ class Announcement < ApplicationRecord
@mentions ||= Account.from_text(text) @mentions ||= Account.from_text(text)
end end
def statuses
@statuses ||= Status.from_text(text)
end
def tags def tags
@tags ||= Tag.find_or_create_by_names(Extractor.extract_hashtags(text)) @tags ||= Tag.find_or_create_by_names(Extractor.extract_hashtags(text))
end end

@ -369,6 +369,21 @@ class Status < ApplicationRecord
end end
end end
def from_text(text)
return [] if text.blank?
text.scan(FetchLinkCardService::URL_PATTERN).map(&:first).uniq.map do |url|
status = begin
if TagManager.instance.local_url?(url)
ActivityPub::TagManager.instance.uri_to_resource(url, Status)
else
Status.find_by(uri: url) || Status.find_by(url: url)
end
end
status&.distributable? ? status : nil
end.compact
end
private private
def timeline_scope(local_only = false) def timeline_scope(local_only = false)

@ -7,6 +7,7 @@ class REST::AnnouncementSerializer < ActiveModel::Serializer
attribute :read, if: :current_user? attribute :read, if: :current_user?
has_many :mentions has_many :mentions
has_many :statuses
has_many :tags, serializer: REST::StatusSerializer::TagSerializer has_many :tags, serializer: REST::StatusSerializer::TagSerializer
has_many :emojis, serializer: REST::CustomEmojiSerializer has_many :emojis, serializer: REST::CustomEmojiSerializer
has_many :reactions, serializer: REST::ReactionSerializer has_many :reactions, serializer: REST::ReactionSerializer
@ -46,4 +47,16 @@ class REST::AnnouncementSerializer < ActiveModel::Serializer
object.pretty_acct object.pretty_acct
end end
end end
class StatusSerializer < ActiveModel::Serializer
attributes :id, :url
def id
object.id.to_s
end
def url
ActivityPub::TagManager.instance.url_for(object)
end
end
end end

Loading…
Cancel
Save