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.

367 lines
14 KiB

  1. # frozen_string_literal: true
  2. class AtomSerializer
  3. include RoutingHelper
  4. include ActionView::Helpers::SanitizeHelper
  5. class << self
  6. def render(element)
  7. document = Ox::Document.new(version: '1.0')
  8. document << element
  9. ('<?xml version="1.0"?>' + Ox.dump(element, effort: :tolerant)).force_encoding('UTF-8')
  10. end
  11. end
  12. def author(account)
  13. author = Ox::Element.new('author')
  14. uri = TagManager.instance.uri_for(account)
  15. append_element(author, 'id', uri)
  16. append_element(author, 'activity:object-type', TagManager::TYPES[:person])
  17. append_element(author, 'uri', uri)
  18. append_element(author, 'name', account.username)
  19. append_element(author, 'email', account.local? ? account.local_username_and_domain : account.acct)
  20. append_element(author, 'summary', Formatter.instance.simplified_format(account).to_str, type: :html) if account.note?
  21. append_element(author, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(account))
  22. append_element(author, 'link', nil, rel: :avatar, type: account.avatar_content_type, 'media:width': 120, 'media:height': 120, href: full_asset_url(account.avatar.url(:original))) if account.avatar?
  23. append_element(author, 'link', nil, rel: :header, type: account.header_content_type, 'media:width': 700, 'media:height': 335, href: full_asset_url(account.header.url(:original))) if account.header?
  24. append_element(author, 'poco:preferredUsername', account.username)
  25. append_element(author, 'poco:displayName', account.display_name) if account.display_name?
  26. append_element(author, 'poco:note', account.local? ? account.note : strip_tags(account.note)) if account.note?
  27. append_element(author, 'mastodon:scope', account.locked? ? :private : :public)
  28. author
  29. end
  30. def feed(account, stream_entries)
  31. feed = Ox::Element.new('feed')
  32. add_namespaces(feed)
  33. append_element(feed, 'id', account_url(account, format: 'atom'))
  34. append_element(feed, 'title', account.display_name.presence || account.username)
  35. append_element(feed, 'subtitle', account.note)
  36. append_element(feed, 'updated', account.updated_at.iso8601)
  37. append_element(feed, 'logo', full_asset_url(account.avatar.url(:original)))
  38. feed << author(account)
  39. append_element(feed, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(account))
  40. append_element(feed, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_url(account, format: 'atom'))
  41. append_element(feed, 'link', nil, rel: :next, type: 'application/atom+xml', href: account_url(account, format: 'atom', max_id: stream_entries.last.id)) if stream_entries.size == 20
  42. append_element(feed, 'link', nil, rel: :hub, href: api_push_url)
  43. append_element(feed, 'link', nil, rel: :salmon, href: api_salmon_url(account.id))
  44. stream_entries.each do |stream_entry|
  45. feed << entry(stream_entry)
  46. end
  47. feed
  48. end
  49. def entry(stream_entry, root = false)
  50. entry = Ox::Element.new('entry')
  51. add_namespaces(entry) if root
  52. append_element(entry, 'id', TagManager.instance.unique_tag(stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type))
  53. append_element(entry, 'published', stream_entry.created_at.iso8601)
  54. append_element(entry, 'updated', stream_entry.updated_at.iso8601)
  55. append_element(entry, 'title', stream_entry&.status&.title || "#{stream_entry.account.acct} deleted status")
  56. entry << author(stream_entry.account) if root
  57. append_element(entry, 'activity:object-type', TagManager::TYPES[stream_entry.object_type])
  58. append_element(entry, 'activity:verb', TagManager::VERBS[stream_entry.verb])
  59. entry << object(stream_entry.target) if stream_entry.targeted?
  60. if stream_entry.status.nil?
  61. append_element(entry, 'content', 'Deleted status')
  62. else
  63. serialize_status_attributes(entry, stream_entry.status)
  64. end
  65. append_element(entry, 'link', nil, rel: :alternate, type: 'text/html', href: account_stream_entry_url(stream_entry.account, stream_entry))
  66. append_element(entry, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom'))
  67. append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(stream_entry.thread), href: TagManager.instance.url_for(stream_entry.thread)) if stream_entry.threaded?
  68. append_element(entry, 'ostatus:conversation', nil, ref: conversation_uri(stream_entry.status.conversation)) unless stream_entry&.status&.conversation_id.nil?
  69. entry
  70. end
  71. def object(status)
  72. object = Ox::Element.new('activity:object')
  73. append_element(object, 'id', TagManager.instance.uri_for(status))
  74. append_element(object, 'published', status.created_at.iso8601)
  75. append_element(object, 'updated', status.updated_at.iso8601)
  76. append_element(object, 'title', status.title)
  77. object << author(status.account)
  78. append_element(object, 'activity:object-type', TagManager::TYPES[status.object_type])
  79. append_element(object, 'activity:verb', TagManager::VERBS[status.verb])
  80. serialize_status_attributes(object, status)
  81. append_element(object, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(status))
  82. append_element(object, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(status.thread), href: TagManager.instance.url_for(status.thread)) unless status.thread.nil?
  83. append_element(object, 'ostatus:conversation', nil, ref: conversation_uri(status.conversation)) unless status.conversation_id.nil?
  84. object
  85. end
  86. def follow_salmon(follow)
  87. entry = Ox::Element.new('entry')
  88. add_namespaces(entry)
  89. description = "#{follow.account.acct} started following #{follow.target_account.acct}"
  90. append_element(entry, 'id', TagManager.instance.unique_tag(follow.created_at, follow.id, 'Follow'))
  91. append_element(entry, 'title', description)
  92. append_element(entry, 'content', description, type: :html)
  93. entry << author(follow.account)
  94. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  95. append_element(entry, 'activity:verb', TagManager::VERBS[:follow])
  96. object = author(follow.target_account)
  97. object.value = 'activity:object'
  98. entry << object
  99. entry
  100. end
  101. def follow_request_salmon(follow_request)
  102. entry = Ox::Element.new('entry')
  103. add_namespaces(entry)
  104. append_element(entry, 'id', TagManager.instance.unique_tag(follow_request.created_at, follow_request.id, 'FollowRequest'))
  105. append_element(entry, 'title', "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}")
  106. entry << author(follow_request.account)
  107. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  108. append_element(entry, 'activity:verb', TagManager::VERBS[:request_friend])
  109. object = author(follow_request.target_account)
  110. object.value = 'activity:object'
  111. entry << object
  112. entry
  113. end
  114. def authorize_follow_request_salmon(follow_request)
  115. entry = Ox::Element.new('entry')
  116. add_namespaces(entry)
  117. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest'))
  118. append_element(entry, 'title', "#{follow_request.target_account.acct} authorizes follow request by #{follow_request.account.acct}")
  119. entry << author(follow_request.target_account)
  120. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  121. append_element(entry, 'activity:verb', TagManager::VERBS[:authorize])
  122. object = Ox::Element.new('activity:object')
  123. object << author(follow_request.account)
  124. append_element(object, 'activity:object-type', TagManager::TYPES[:activity])
  125. append_element(object, 'activity:verb', TagManager::VERBS[:request_friend])
  126. inner_object = author(follow_request.target_account)
  127. inner_object.value = 'activity:object'
  128. object << inner_object
  129. entry << object
  130. entry
  131. end
  132. def reject_follow_request_salmon(follow_request)
  133. entry = Ox::Element.new('entry')
  134. add_namespaces(entry)
  135. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest'))
  136. append_element(entry, 'title', "#{follow_request.target_account.acct} rejects follow request by #{follow_request.account.acct}")
  137. entry << author(follow_request.target_account)
  138. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  139. append_element(entry, 'activity:verb', TagManager::VERBS[:reject])
  140. object = Ox::Element.new('activity:object')
  141. object << author(follow_request.account)
  142. append_element(object, 'activity:object-type', TagManager::TYPES[:activity])
  143. append_element(object, 'activity:verb', TagManager::VERBS[:request_friend])
  144. inner_object = author(follow_request.target_account)
  145. inner_object.value = 'activity:object'
  146. object << inner_object
  147. entry << object
  148. entry
  149. end
  150. def unfollow_salmon(follow)
  151. entry = Ox::Element.new('entry')
  152. add_namespaces(entry)
  153. description = "#{follow.account.acct} is no longer following #{follow.target_account.acct}"
  154. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow.id, 'Follow'))
  155. append_element(entry, 'title', description)
  156. append_element(entry, 'content', description, type: :html)
  157. entry << author(follow.account)
  158. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  159. append_element(entry, 'activity:verb', TagManager::VERBS[:unfollow])
  160. object = author(follow.target_account)
  161. object.value = 'activity:object'
  162. entry << object
  163. entry
  164. end
  165. def block_salmon(block)
  166. entry = Ox::Element.new('entry')
  167. add_namespaces(entry)
  168. description = "#{block.account.acct} no longer wishes to interact with #{block.target_account.acct}"
  169. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block'))
  170. append_element(entry, 'title', description)
  171. entry << author(block.account)
  172. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  173. append_element(entry, 'activity:verb', TagManager::VERBS[:block])
  174. object = author(block.target_account)
  175. object.value = 'activity:object'
  176. entry << object
  177. entry
  178. end
  179. def unblock_salmon(block)
  180. entry = Ox::Element.new('entry')
  181. add_namespaces(entry)
  182. description = "#{block.account.acct} no longer blocks #{block.target_account.acct}"
  183. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block'))
  184. append_element(entry, 'title', description)
  185. entry << author(block.account)
  186. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  187. append_element(entry, 'activity:verb', TagManager::VERBS[:unblock])
  188. object = author(block.target_account)
  189. object.value = 'activity:object'
  190. entry << object
  191. entry
  192. end
  193. def favourite_salmon(favourite)
  194. entry = Ox::Element.new('entry')
  195. add_namespaces(entry)
  196. description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}"
  197. append_element(entry, 'id', TagManager.instance.unique_tag(favourite.created_at, favourite.id, 'Favourite'))
  198. append_element(entry, 'title', description)
  199. append_element(entry, 'content', description, type: :html)
  200. entry << author(favourite.account)
  201. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  202. append_element(entry, 'activity:verb', TagManager::VERBS[:favorite])
  203. entry << object(favourite.status)
  204. append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status))
  205. entry
  206. end
  207. def unfavourite_salmon(favourite)
  208. entry = Ox::Element.new('entry')
  209. add_namespaces(entry)
  210. description = "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}"
  211. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, favourite.id, 'Favourite'))
  212. append_element(entry, 'title', description)
  213. append_element(entry, 'content', description, type: :html)
  214. entry << author(favourite.account)
  215. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  216. append_element(entry, 'activity:verb', TagManager::VERBS[:unfavorite])
  217. entry << object(favourite.status)
  218. append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status))
  219. entry
  220. end
  221. private
  222. def append_element(parent, name, content = nil, attributes = {})
  223. element = Ox::Element.new(name)
  224. attributes.each { |k, v| element[k] = sanitize_str(v) }
  225. element << sanitize_str(content) unless content.nil?
  226. parent << element
  227. end
  228. def sanitize_str(raw_str)
  229. raw_str.to_s
  230. end
  231. def conversation_uri(conversation)
  232. return conversation.uri if conversation.uri?
  233. TagManager.instance.unique_tag(conversation.created_at, conversation.id, 'Conversation')
  234. end
  235. def add_namespaces(parent)
  236. parent['xmlns'] = TagManager::XMLNS
  237. parent['xmlns:thr'] = TagManager::THR_XMLNS
  238. parent['xmlns:activity'] = TagManager::AS_XMLNS
  239. parent['xmlns:poco'] = TagManager::POCO_XMLNS
  240. parent['xmlns:media'] = TagManager::MEDIA_XMLNS
  241. parent['xmlns:ostatus'] = TagManager::OS_XMLNS
  242. parent['xmlns:mastodon'] = TagManager::MTDN_XMLNS
  243. end
  244. def serialize_status_attributes(entry, status)
  245. append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text?
  246. append_element(entry, 'content', Formatter.instance.format(status).to_str, type: 'html', 'xml:lang': status.language)
  247. status.mentions.each do |mentioned|
  248. append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:person], href: TagManager.instance.uri_for(mentioned.account))
  249. end
  250. append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:collection], href: TagManager::COLLECTIONS[:public]) if status.public_visibility?
  251. status.tags.each do |tag|
  252. append_element(entry, 'category', nil, term: tag.name)
  253. end
  254. append_element(entry, 'category', nil, term: 'nsfw') if status.sensitive?
  255. status.media_attachments.each do |media|
  256. append_element(entry, 'link', nil, rel: :enclosure, type: media.file_content_type, length: media.file_file_size, href: full_asset_url(media.file.url(:original, false)))
  257. end
  258. append_element(entry, 'mastodon:scope', status.visibility)
  259. end
  260. end