闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.2 KiB

8 years ago
8 years ago
8 years ago
  1. module AtomBuilderHelper
  2. def stream_updated_at
  3. @account.stream_entries.last ? (@account.updated_at > @account.stream_entries.last.created_at ? @account.updated_at : @account.stream_entries.last.created_at) : @account.updated_at
  4. end
  5. def entry(xml, is_root = false, &block)
  6. if is_root
  7. root_tag(xml, :entry, &block)
  8. else
  9. xml.entry(&block)
  10. end
  11. end
  12. def feed(xml, &block)
  13. root_tag(xml, :feed, &block)
  14. end
  15. def unique_id(xml, date, id, type)
  16. xml.id_ TagManager.instance.unique_tag(date, id, type)
  17. end
  18. def simple_id(xml, id)
  19. xml.id_ id
  20. end
  21. def published_at(xml, date)
  22. xml.published date.iso8601
  23. end
  24. def updated_at(xml, date)
  25. xml.updated date.iso8601
  26. end
  27. def verb(xml, verb)
  28. xml['activity'].send('verb', "http://activitystrea.ms/schema/1.0/#{verb}")
  29. end
  30. def content(xml, content)
  31. xml.content({ type: 'html' }, content) unless content.blank?
  32. end
  33. def title(xml, title)
  34. xml.title strip_tags(title || '').truncate(80)
  35. end
  36. def author(xml, &block)
  37. xml.author(&block)
  38. end
  39. def target(xml, &block)
  40. xml['activity'].object(&block)
  41. end
  42. def object_type(xml, type)
  43. xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{type}")
  44. end
  45. def uri(xml, uri)
  46. xml.uri uri
  47. end
  48. def name(xml, name)
  49. xml.name name
  50. end
  51. def summary(xml, summary)
  52. xml.summary(summary) unless summary.blank?
  53. end
  54. def subtitle(xml, subtitle)
  55. xml.subtitle(subtitle) unless subtitle.blank?
  56. end
  57. def link_alternate(xml, url)
  58. xml.link(rel: 'alternate', type: 'text/html', href: url)
  59. end
  60. def link_self(xml, url)
  61. xml.link(rel: 'self', type: 'application/atom+xml', href: url)
  62. end
  63. def link_hub(xml, url)
  64. xml.link(rel: 'hub', href: url)
  65. end
  66. def link_salmon(xml, url)
  67. xml.link(rel: 'salmon', href: url)
  68. end
  69. def portable_contact(xml, account)
  70. xml['poco'].preferredUsername account.username
  71. xml['poco'].displayName(account.display_name) unless account.display_name.blank?
  72. xml['poco'].note(account.note) unless account.note.blank?
  73. end
  74. def in_reply_to(xml, uri, url)
  75. xml['thr'].send('in-reply-to', { ref: uri, href: url, type: 'text/html' })
  76. end
  77. def link_mention(xml, account)
  78. xml.link(rel: 'mentioned', href: TagManager.instance.uri_for(account))
  79. end
  80. def link_enclosure(xml, media)
  81. xml.link(rel: 'enclosure', href: full_asset_url(media.file.url), type: media.file_content_type, length: media.file_file_size)
  82. end
  83. def link_avatar(xml, account)
  84. single_link_avatar(xml, account, :large, 300)
  85. single_link_avatar(xml, account, :medium, 96)
  86. single_link_avatar(xml, account, :small, 48)
  87. end
  88. def logo(xml, url)
  89. xml.logo url
  90. end
  91. def email(xml, email)
  92. xml.email email
  93. end
  94. def conditionally_formatted(activity)
  95. if activity.is_a?(Status)
  96. Formatter.instance.format(activity.reblog? ? activity.reblog : activity)
  97. elsif activity.nil?
  98. nil
  99. else
  100. activity.content
  101. end
  102. end
  103. def include_author(xml, account)
  104. object_type xml, :person
  105. uri xml, TagManager.instance.uri_for(account)
  106. name xml, account.username
  107. email xml, account.local? ? "#{account.acct}@#{Rails.configuration.x.local_domain}" : account.acct
  108. summary xml, account.note
  109. link_alternate xml, TagManager.instance.url_for(account)
  110. link_avatar xml, account
  111. portable_contact xml, account
  112. end
  113. def include_entry(xml, stream_entry)
  114. unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
  115. published_at xml, stream_entry.created_at
  116. updated_at xml, stream_entry.updated_at
  117. title xml, stream_entry.title
  118. content xml, conditionally_formatted(stream_entry.activity)
  119. verb xml, stream_entry.verb
  120. link_self xml, account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')
  121. link_alternate xml, account_stream_entry_url(stream_entry.account, stream_entry)
  122. object_type xml, stream_entry.object_type
  123. # Comments need thread element
  124. if stream_entry.threaded?
  125. in_reply_to xml, TagManager.instance.uri_for(stream_entry.thread), TagManager.instance.url_for(stream_entry.thread)
  126. end
  127. if stream_entry.targeted?
  128. target(xml) do
  129. if stream_entry.target.object_type == :person
  130. include_author xml, stream_entry.target
  131. else
  132. object_type xml, stream_entry.target.object_type
  133. simple_id xml, TagManager.instance.uri_for(stream_entry.target)
  134. title xml, stream_entry.target.title
  135. link_alternate xml, TagManager.instance.url_for(stream_entry.target)
  136. end
  137. # Statuses have content and author
  138. if stream_entry.target.is_a?(Status)
  139. content xml, conditionally_formatted(stream_entry.target)
  140. verb xml, stream_entry.target.verb
  141. published_at xml, stream_entry.target.created_at
  142. updated_at xml, stream_entry.target.updated_at
  143. author(xml) do
  144. include_author xml, stream_entry.target.account
  145. end
  146. stream_entry.target.mentions.each do |mention|
  147. link_mention xml, mention.account
  148. end
  149. stream_entry.target.media_attachments.each do |media|
  150. link_enclosure xml, media
  151. end
  152. end
  153. end
  154. end
  155. stream_entry.mentions.each do |mentioned|
  156. link_mention xml, mentioned
  157. end
  158. if stream_entry.activity.is_a?(Status)
  159. stream_entry.activity.media_attachments.each do |media|
  160. link_enclosure xml, media
  161. end
  162. end
  163. end
  164. private
  165. def root_tag(xml, tag, &block)
  166. xml.send(tag, { :xmlns => 'http://www.w3.org/2005/Atom', 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0', 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/', 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0', 'xmlns:media' => 'http://purl.org/syndication/atommedia' }, &block)
  167. end
  168. def single_link_avatar(xml, account, size, px)
  169. xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => px, 'media:height' => px, 'href' => full_asset_url(account.avatar.url(size, false)))
  170. end
  171. end