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.

182 lines
4.6 KiB

  1. module AtomHelper
  2. def stream_updated_at
  3. @account.stream_entries.last ? @account.stream_entries.last.created_at : @account.updated_at
  4. end
  5. def entry(xml, is_root, &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)
  32. end
  33. def title(xml, title)
  34. xml.title title
  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
  53. end
  54. def subtitle(xml, subtitle)
  55. xml.subtitle subtitle
  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
  72. xml['poco'].note account.note
  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 disambiguate_uri(target)
  78. if target.local?
  79. if target.object_type == :person
  80. profile_url(name: target.username)
  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 disambiguate_url(target)
  89. if target.local?
  90. if target.object_type == :person
  91. profile_url(name: target.username)
  92. else
  93. status_url(name: target.stream_entry.account.username, id: target.stream_entry.id)
  94. end
  95. else
  96. target.url
  97. end
  98. end
  99. def link_mention(xml, account)
  100. xml.link(rel: 'mentioned', href: disambiguate_uri(account))
  101. end
  102. def include_author(xml, account)
  103. object_type xml, :person
  104. uri xml, profile_url(name: account.username)
  105. name xml, account.username
  106. summary xml, account.note
  107. link_alternate xml, profile_url(name: account.username)
  108. portable_contact xml, account
  109. end
  110. def include_entry(xml, stream_entry)
  111. unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
  112. published_at xml, stream_entry.activity.created_at
  113. updated_at xml, stream_entry.activity.updated_at
  114. title xml, stream_entry.title
  115. content xml, stream_entry.content
  116. verb xml, stream_entry.verb
  117. link_self xml, atom_entry_url(id: stream_entry.id)
  118. object_type xml, stream_entry.object_type
  119. # Comments need thread element
  120. if stream_entry.threaded?
  121. in_reply_to xml, disambiguate_uri(stream_entry.thread), disambiguate_url(stream_entry.thread)
  122. end
  123. if stream_entry.targeted?
  124. target(xml) do
  125. object_type xml, stream_entry.target.object_type
  126. simple_id xml, disambiguate_uri(stream_entry.target)
  127. title xml, stream_entry.target.title
  128. link_alternate xml, disambiguate_url(stream_entry.target)
  129. # People have summary and portable contacts information
  130. if stream_entry.target.object_type == :person
  131. summary xml, stream_entry.target.content
  132. portable_contact xml, stream_entry.target
  133. end
  134. # Statuses have content
  135. if [:note, :comment].include? stream_entry.target.object_type
  136. content xml, stream_entry.target.content
  137. end
  138. end
  139. end
  140. stream_entry.mentions.each do |mentioned|
  141. link_mention xml, mentioned
  142. end
  143. end
  144. private
  145. def root_tag(xml, tag, &block)
  146. 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'}, &block)
  147. end
  148. end