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.

229 lines
6.4 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_ 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 uri_for_target(target)
  78. if target.local?
  79. if target.object_type == :person
  80. account_url(target)
  81. else
  82. unique_tag(target.stream_entry.created_at, target.stream_entry.activity_id, target.stream_entry.activity_type)
  83. end
  84. else
  85. target.uri
  86. end
  87. end
  88. def url_for_target(target)
  89. if target.local?
  90. if target.object_type == :person
  91. account_url(target)
  92. else
  93. account_stream_entry_url(target.account, target.stream_entry)
  94. end
  95. else
  96. target.url
  97. end
  98. end
  99. def link_mention(xml, account)
  100. xml.link(rel: 'mentioned', href: uri_for_target(account))
  101. end
  102. def link_enclosure(xml, media)
  103. xml.link(rel: 'enclosure', href: full_asset_url(media.file.url), type: media.file_content_type, length: media.file_size)
  104. end
  105. def link_avatar(xml, account)
  106. single_link_avatar(xml, account, :large, 300)
  107. single_link_avatar(xml, account, :medium, 96)
  108. single_link_avatar(xml, account, :small, 48)
  109. end
  110. def logo(xml, url)
  111. xml.logo url
  112. end
  113. def email(xml, email)
  114. xml.email email
  115. end
  116. def conditionally_formatted(activity)
  117. if activity.is_a?(Status)
  118. content_for_status(activity.reblog? ? activity.reblog : activity)
  119. elsif activity.nil?
  120. nil
  121. else
  122. activity.content
  123. end
  124. end
  125. def include_author(xml, account)
  126. object_type xml, :person
  127. uri xml, url_for_target(account)
  128. name xml, account.username
  129. email xml, account.local? ? "#{account.acct}@#{Rails.configuration.x.local_domain}" : account.acct
  130. summary xml, account.note
  131. link_alternate xml, url_for_target(account)
  132. link_avatar xml, account
  133. portable_contact xml, account
  134. end
  135. def include_entry(xml, stream_entry)
  136. unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
  137. published_at xml, stream_entry.created_at
  138. updated_at xml, stream_entry.updated_at
  139. title xml, stream_entry.title
  140. content xml, conditionally_formatted(stream_entry.activity)
  141. verb xml, stream_entry.verb
  142. link_self xml, account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')
  143. link_alternate xml, account_stream_entry_url(stream_entry.account, stream_entry)
  144. # Comments need thread element
  145. if stream_entry.threaded?
  146. in_reply_to xml, uri_for_target(stream_entry.thread), url_for_target(stream_entry.thread)
  147. end
  148. if stream_entry.targeted?
  149. target(xml) do
  150. if stream_entry.target.object_type == :person
  151. include_author xml, stream_entry.target
  152. else
  153. object_type xml, stream_entry.target.object_type
  154. simple_id xml, uri_for_target(stream_entry.target)
  155. title xml, stream_entry.target.title
  156. link_alternate xml, url_for_target(stream_entry.target)
  157. end
  158. # Statuses have content and author
  159. if [:note, :comment].include? stream_entry.target.object_type
  160. content xml, conditionally_formatted(stream_entry.target)
  161. verb xml, stream_entry.target.verb
  162. published_at xml, stream_entry.target.created_at
  163. updated_at xml, stream_entry.target.updated_at
  164. author(xml) do
  165. include_author xml, stream_entry.target.account
  166. end
  167. end
  168. end
  169. else
  170. object_type xml, stream_entry.object_type
  171. end
  172. stream_entry.mentions.each do |mentioned|
  173. link_mention xml, mentioned
  174. end
  175. if stream_entry.activity.is_a?(Status)
  176. stream_entry.activity.media_attachments.each do |media|
  177. link_enclosure xml, media
  178. end
  179. end
  180. end
  181. private
  182. def root_tag(xml, tag, &block)
  183. 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)
  184. end
  185. def single_link_avatar(xml, account, size, px)
  186. xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => px, 'media:height' =>px, 'href' => full_asset_url(account.avatar.url(size, false)))
  187. end
  188. end