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.

231 lines
6.4 KiB

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