闭社主体 forked from https://github.com/tootsuite/mastodon
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.

356 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)))
  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)))
  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 || 'Delete')
  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. serialize_status_attributes(entry, stream_entry.status) unless stream_entry.status.nil?
  61. append_element(entry, 'link', nil, rel: :alternate, type: 'text/html', href: account_stream_entry_url(stream_entry.account, stream_entry))
  62. append_element(entry, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom'))
  63. 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?
  64. entry
  65. end
  66. def object(status)
  67. object = Ox::Element.new('activity:object')
  68. append_element(object, 'id', TagManager.instance.uri_for(status))
  69. append_element(object, 'published', status.created_at.iso8601)
  70. append_element(object, 'updated', status.updated_at.iso8601)
  71. append_element(object, 'title', status.title)
  72. object << author(status.account)
  73. append_element(object, 'activity:object-type', TagManager::TYPES[status.object_type])
  74. append_element(object, 'activity:verb', TagManager::VERBS[status.verb])
  75. serialize_status_attributes(object, status)
  76. append_element(object, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(status))
  77. append_element(object, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(status.thread), href: TagManager.instance.url_for(status.thread)) if status.reply? && !status.thread.nil?
  78. object
  79. end
  80. def follow_salmon(follow)
  81. entry = Ox::Element.new('entry')
  82. add_namespaces(entry)
  83. description = "#{follow.account.acct} started following #{follow.target_account.acct}"
  84. append_element(entry, 'id', TagManager.instance.unique_tag(follow.created_at, follow.id, 'Follow'))
  85. append_element(entry, 'title', description)
  86. append_element(entry, 'content', description, type: :html)
  87. entry << author(follow.account)
  88. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  89. append_element(entry, 'activity:verb', TagManager::VERBS[:follow])
  90. object = author(follow.target_account)
  91. object.value = 'activity:object'
  92. entry << object
  93. entry
  94. end
  95. def follow_request_salmon(follow_request)
  96. entry = Ox::Element.new('entry')
  97. add_namespaces(entry)
  98. append_element(entry, 'id', TagManager.instance.unique_tag(follow_request.created_at, follow_request.id, 'FollowRequest'))
  99. append_element(entry, 'title', "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}")
  100. entry << author(follow_request.account)
  101. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  102. append_element(entry, 'activity:verb', TagManager::VERBS[:request_friend])
  103. object = author(follow_request.target_account)
  104. object.value = 'activity:object'
  105. entry << object
  106. entry
  107. end
  108. def authorize_follow_request_salmon(follow_request)
  109. entry = Ox::Element.new('entry')
  110. add_namespaces(entry)
  111. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest'))
  112. append_element(entry, 'title', "#{follow_request.target_account.acct} authorizes follow request by #{follow_request.account.acct}")
  113. entry << author(follow_request.target_account)
  114. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  115. append_element(entry, 'activity:verb', TagManager::VERBS[:authorize])
  116. object = Ox::Element.new('activity:object')
  117. object << author(follow_request.account)
  118. append_element(object, 'activity:object-type', TagManager::TYPES[:activity])
  119. append_element(object, 'activity:verb', TagManager::VERBS[:request_friend])
  120. inner_object = author(follow_request.target_account)
  121. inner_object.value = 'activity:object'
  122. object << inner_object
  123. entry << object
  124. entry
  125. end
  126. def reject_follow_request_salmon(follow_request)
  127. entry = Ox::Element.new('entry')
  128. add_namespaces(entry)
  129. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest'))
  130. append_element(entry, 'title', "#{follow_request.target_account.acct} rejects follow request by #{follow_request.account.acct}")
  131. entry << author(follow_request.target_account)
  132. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  133. append_element(entry, 'activity:verb', TagManager::VERBS[:reject])
  134. object = Ox::Element.new('activity:object')
  135. object << author(follow_request.account)
  136. append_element(object, 'activity:object-type', TagManager::TYPES[:activity])
  137. append_element(object, 'activity:verb', TagManager::VERBS[:request_friend])
  138. inner_object = author(follow_request.target_account)
  139. inner_object.value = 'activity:object'
  140. object << inner_object
  141. entry << object
  142. entry
  143. end
  144. def unfollow_salmon(follow)
  145. entry = Ox::Element.new('entry')
  146. add_namespaces(entry)
  147. description = "#{follow.account.acct} is no longer following #{follow.target_account.acct}"
  148. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow.id, 'Follow'))
  149. append_element(entry, 'title', description)
  150. append_element(entry, 'content', description, type: :html)
  151. entry << author(follow.account)
  152. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  153. append_element(entry, 'activity:verb', TagManager::VERBS[:unfollow])
  154. object = author(follow.target_account)
  155. object.value = 'activity:object'
  156. entry << object
  157. entry
  158. end
  159. def block_salmon(block)
  160. entry = Ox::Element.new('entry')
  161. add_namespaces(entry)
  162. description = "#{block.account.acct} no longer wishes to interact with #{block.target_account.acct}"
  163. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block'))
  164. append_element(entry, 'title', description)
  165. entry << author(block.account)
  166. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  167. append_element(entry, 'activity:verb', TagManager::VERBS[:block])
  168. object = author(block.target_account)
  169. object.value = 'activity:object'
  170. entry << object
  171. entry
  172. end
  173. def unblock_salmon(block)
  174. entry = Ox::Element.new('entry')
  175. add_namespaces(entry)
  176. description = "#{block.account.acct} no longer blocks #{block.target_account.acct}"
  177. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block'))
  178. append_element(entry, 'title', description)
  179. entry << author(block.account)
  180. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  181. append_element(entry, 'activity:verb', TagManager::VERBS[:unblock])
  182. object = author(block.target_account)
  183. object.value = 'activity:object'
  184. entry << object
  185. entry
  186. end
  187. def favourite_salmon(favourite)
  188. entry = Ox::Element.new('entry')
  189. add_namespaces(entry)
  190. description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}"
  191. append_element(entry, 'id', TagManager.instance.unique_tag(favourite.created_at, favourite.id, 'Favourite'))
  192. append_element(entry, 'title', description)
  193. append_element(entry, 'content', description, type: :html)
  194. entry << author(favourite.account)
  195. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  196. append_element(entry, 'activity:verb', TagManager::VERBS[:favorite])
  197. entry << object(favourite.status)
  198. append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status))
  199. entry
  200. end
  201. def unfavourite_salmon(favourite)
  202. entry = Ox::Element.new('entry')
  203. add_namespaces(entry)
  204. description = "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}"
  205. append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, favourite.id, 'Favourite'))
  206. append_element(entry, 'title', description)
  207. append_element(entry, 'content', description, type: :html)
  208. entry << author(favourite.account)
  209. append_element(entry, 'activity:object-type', TagManager::TYPES[:activity])
  210. append_element(entry, 'activity:verb', TagManager::VERBS[:unfavorite])
  211. entry << object(favourite.status)
  212. append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status))
  213. entry
  214. end
  215. private
  216. def append_element(parent, name, content = nil, attributes = {})
  217. element = Ox::Element.new(name)
  218. attributes.each { |k, v| element[k] = sanitize_str(v) }
  219. element << sanitize_str(content) unless content.nil?
  220. parent << element
  221. end
  222. def sanitize_str(raw_str)
  223. raw_str.to_s
  224. end
  225. def add_namespaces(parent)
  226. parent['xmlns'] = TagManager::XMLNS
  227. parent['xmlns:thr'] = TagManager::THR_XMLNS
  228. parent['xmlns:activity'] = TagManager::AS_XMLNS
  229. parent['xmlns:poco'] = TagManager::POCO_XMLNS
  230. parent['xmlns:media'] = TagManager::MEDIA_XMLNS
  231. parent['xmlns:ostatus'] = TagManager::OS_XMLNS
  232. parent['xmlns:mastodon'] = TagManager::MTDN_XMLNS
  233. end
  234. def serialize_status_attributes(entry, status)
  235. append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text?
  236. append_element(entry, 'content', Formatter.instance.format(status.proper).to_str, type: 'html', 'xml:lang': status.language)
  237. status.mentions.each do |mentioned|
  238. append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:person], href: TagManager.instance.uri_for(mentioned.account))
  239. end
  240. append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:collection], href: TagManager::COLLECTIONS[:public]) if status.public_visibility?
  241. status.tags.each do |tag|
  242. append_element(entry, 'category', nil, term: tag.name)
  243. end
  244. append_element(entry, 'category', nil, term: 'nsfw') if status.sensitive?
  245. status.media_attachments.each do |media|
  246. 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)))
  247. end
  248. append_element(entry, 'mastodon:scope', status.visibility)
  249. end
  250. end