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.

258 lines
7.2 KiB

8 years ago
8 years ago
8 years ago
8 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', TagManager::VERBS[verb])
  34. end
  35. def content(xml, content, warning = nil)
  36. xml.summary(warning) unless warning.blank?
  37. xml.content({ type: 'html' }, content) unless content.blank?
  38. end
  39. def title(xml, title)
  40. xml.title strip_tags(title || '').truncate(80)
  41. end
  42. def author(xml, &block)
  43. xml.author(&block)
  44. end
  45. def category(xml, term)
  46. xml.category(term: term)
  47. end
  48. def target(xml, &block)
  49. xml['activity'].object(&block)
  50. end
  51. def object_type(xml, type)
  52. xml['activity'].send('object-type', TagManager::TYPES[type])
  53. end
  54. def uri(xml, uri)
  55. xml.uri uri
  56. end
  57. def name(xml, name)
  58. xml.name name
  59. end
  60. def summary(xml, summary)
  61. xml.summary(summary) unless summary.blank?
  62. end
  63. def subtitle(xml, subtitle)
  64. xml.subtitle(subtitle) unless subtitle.blank?
  65. end
  66. def link_alternate(xml, url)
  67. xml.link(rel: 'alternate', type: 'text/html', href: url)
  68. end
  69. def link_self(xml, url)
  70. xml.link(rel: 'self', type: 'application/atom+xml', href: url)
  71. end
  72. def link_hub(xml, url)
  73. xml.link(rel: 'hub', href: url)
  74. end
  75. def link_salmon(xml, url)
  76. xml.link(rel: 'salmon', href: url)
  77. end
  78. def portable_contact(xml, account)
  79. xml['poco'].preferredUsername account.username
  80. xml['poco'].displayName(account.display_name) unless account.display_name.blank?
  81. xml['poco'].note(Formatter.instance.simplified_format(account)) unless account.note.blank?
  82. end
  83. def in_reply_to(xml, uri, url)
  84. xml['thr'].send('in-reply-to', ref: uri, href: url, type: 'text/html')
  85. end
  86. def link_mention(xml, account)
  87. xml.link(:rel => 'mentioned', :href => TagManager.instance.uri_for(account), 'ostatus:object-type' => TagManager::TYPES[:person])
  88. end
  89. def link_enclosure(xml, media)
  90. xml.link(rel: 'enclosure', href: full_asset_url(media.file.url(:original, false)), type: media.file_content_type, length: media.file_file_size)
  91. end
  92. def link_avatar(xml, account)
  93. single_link_avatar(xml, account, :original, 120)
  94. end
  95. def logo(xml, url)
  96. xml.logo url
  97. end
  98. def email(xml, email)
  99. xml.email email
  100. end
  101. def conditionally_formatted(activity)
  102. if activity.is_a?(Status)
  103. Formatter.instance.format(activity.reblog? ? activity.reblog : activity)
  104. elsif activity.nil?
  105. nil
  106. else
  107. activity.content
  108. end
  109. end
  110. def link_visibility(xml, item)
  111. return unless item.respond_to?(:visibility) && item.public_visibility?
  112. xml.link(:rel => 'mentioned', :href => TagManager::COLLECTIONS[:public], 'ostatus:object-type' => TagManager::TYPES[:collection])
  113. end
  114. def include_author(xml, account)
  115. object_type xml, :person
  116. uri xml, TagManager.instance.uri_for(account)
  117. name xml, account.username
  118. email xml, account.local? ? "#{account.acct}@#{Rails.configuration.x.local_domain}" : account.acct
  119. summary xml, account.note
  120. link_alternate xml, TagManager.instance.url_for(account)
  121. link_avatar xml, account
  122. portable_contact xml, account
  123. end
  124. def rich_content(xml, activity)
  125. if activity.is_a?(Status)
  126. content xml, conditionally_formatted(activity), activity.spoiler_text
  127. else
  128. content xml, conditionally_formatted(activity)
  129. end
  130. end
  131. def include_entry(xml, stream_entry)
  132. unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
  133. published_at xml, stream_entry.created_at
  134. updated_at xml, stream_entry.updated_at
  135. title xml, stream_entry.title
  136. rich_content xml, stream_entry.activity
  137. verb xml, stream_entry.verb
  138. link_self xml, account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')
  139. link_alternate xml, account_stream_entry_url(stream_entry.account, stream_entry)
  140. object_type xml, stream_entry.object_type
  141. # Comments need thread element
  142. if stream_entry.threaded?
  143. in_reply_to xml, TagManager.instance.uri_for(stream_entry.thread), TagManager.instance.url_for(stream_entry.thread)
  144. end
  145. if stream_entry.targeted?
  146. target(xml) do
  147. simple_id xml, TagManager.instance.uri_for(stream_entry.target)
  148. if stream_entry.target.object_type == :person
  149. include_author xml, stream_entry.target
  150. else
  151. object_type xml, stream_entry.target.object_type
  152. title xml, stream_entry.target.title
  153. link_alternate xml, TagManager.instance.url_for(stream_entry.target)
  154. end
  155. # Statuses have content and author
  156. if stream_entry.target.is_a?(Status)
  157. content xml, conditionally_formatted(stream_entry.target)
  158. verb xml, stream_entry.target.verb
  159. published_at xml, stream_entry.target.created_at
  160. updated_at xml, stream_entry.target.updated_at
  161. author(xml) do
  162. include_author xml, stream_entry.target.account
  163. end
  164. link_visibility xml, stream_entry.target
  165. stream_entry.target.mentions.each do |mention|
  166. link_mention xml, mention.account
  167. end
  168. stream_entry.target.media_attachments.each do |media|
  169. link_enclosure xml, media
  170. end
  171. stream_entry.target.tags.each do |tag|
  172. category xml, tag.name
  173. end
  174. category(xml, 'nsfw') if stream_entry.target.sensitive?
  175. end
  176. end
  177. end
  178. link_visibility xml, stream_entry.activity
  179. stream_entry.mentions.each do |mentioned|
  180. link_mention xml, mentioned
  181. end
  182. return unless stream_entry.activity.is_a?(Status)
  183. stream_entry.activity.media_attachments.each do |media|
  184. link_enclosure xml, media
  185. end
  186. stream_entry.activity.tags.each do |tag|
  187. category xml, tag.name
  188. end
  189. category(xml, 'nsfw') if stream_entry.activity.sensitive?
  190. end
  191. private
  192. def root_tag(xml, tag, &block)
  193. xml.send(tag, {
  194. 'xmlns' => TagManager::XMLNS,
  195. 'xmlns:thr' => TagManager::THR_XMLNS,
  196. 'xmlns:activity' => TagManager::AS_XMLNS,
  197. 'xmlns:poco' => TagManager::POCO_XMLNS,
  198. 'xmlns:media' => TagManager::MEDIA_XMLNS,
  199. 'xmlns:ostatus' => TagManager::OS_XMLNS,
  200. }, &block)
  201. end
  202. def single_link_avatar(xml, account, size, px)
  203. xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => px, 'media:height' => px, 'href' => full_asset_url(account.avatar.url(size, false)))
  204. end
  205. end