The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

248 lines
6.9 KiB

  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)
  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', TagManager::TYPES[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), 'ostatus:object-type' => TagManager::TYPES[:person])
  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 link_visibility(xml, item)
  112. return unless item.respond_to?(:visibility) && item.public_visibility?
  113. xml.link(:rel => 'mentioned', :href => TagManager::COLLECTIONS[:public], 'ostatus:object-type' => TagManager::TYPES[:collection])
  114. end
  115. def include_author(xml, account)
  116. object_type xml, :person
  117. uri xml, TagManager.instance.uri_for(account)
  118. name xml, account.username
  119. email xml, account.local? ? "#{account.acct}@#{Rails.configuration.x.local_domain}" : account.acct
  120. summary xml, account.note
  121. link_alternate xml, TagManager.instance.url_for(account)
  122. link_avatar xml, account
  123. portable_contact xml, account
  124. end
  125. def include_entry(xml, stream_entry)
  126. unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
  127. published_at xml, stream_entry.created_at
  128. updated_at xml, stream_entry.updated_at
  129. title xml, stream_entry.title
  130. content xml, conditionally_formatted(stream_entry.activity)
  131. verb xml, stream_entry.verb
  132. link_self xml, account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')
  133. link_alternate xml, account_stream_entry_url(stream_entry.account, stream_entry)
  134. object_type xml, stream_entry.object_type
  135. # Comments need thread element
  136. if stream_entry.threaded?
  137. in_reply_to xml, TagManager.instance.uri_for(stream_entry.thread), TagManager.instance.url_for(stream_entry.thread)
  138. end
  139. if stream_entry.targeted?
  140. target(xml) do
  141. simple_id xml, TagManager.instance.uri_for(stream_entry.target)
  142. if stream_entry.target.object_type == :person
  143. include_author xml, stream_entry.target
  144. else
  145. object_type xml, stream_entry.target.object_type
  146. title xml, stream_entry.target.title
  147. link_alternate xml, TagManager.instance.url_for(stream_entry.target)
  148. end
  149. # Statuses have content and author
  150. if stream_entry.target.is_a?(Status)
  151. content xml, conditionally_formatted(stream_entry.target)
  152. verb xml, stream_entry.target.verb
  153. published_at xml, stream_entry.target.created_at
  154. updated_at xml, stream_entry.target.updated_at
  155. author(xml) do
  156. include_author xml, stream_entry.target.account
  157. end
  158. link_visibility xml, stream_entry.target
  159. stream_entry.target.mentions.each do |mention|
  160. link_mention xml, mention.account
  161. end
  162. stream_entry.target.media_attachments.each do |media|
  163. link_enclosure xml, media
  164. end
  165. stream_entry.target.tags.each do |tag|
  166. category xml, tag
  167. end
  168. end
  169. end
  170. end
  171. link_visibility xml, stream_entry.activity
  172. stream_entry.mentions.each do |mentioned|
  173. link_mention xml, mentioned
  174. end
  175. return unless stream_entry.activity.is_a?(Status)
  176. stream_entry.activity.media_attachments.each do |media|
  177. link_enclosure xml, media
  178. end
  179. stream_entry.activity.tags.each do |tag|
  180. category xml, tag
  181. end
  182. end
  183. private
  184. def root_tag(xml, tag, &block)
  185. xml.send(tag, {
  186. 'xmlns' => TagManager::XMLNS,
  187. 'xmlns:thr' => TagManager::THR_XMLNS,
  188. 'xmlns:activity' => TagManager::AS_XMLNS,
  189. 'xmlns:poco' => TagManager::POCO_XMLNS,
  190. 'xmlns:media' => TagManager::MEDIA_XMLNS,
  191. 'xmlns:ostatus' => TagManager::OS_XMLNS,
  192. }, &block)
  193. end
  194. def single_link_avatar(xml, account, size, px)
  195. xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => px, 'media:height' => px, 'href' => full_asset_url(account.avatar.url(size, false)))
  196. end
  197. end