The code powering m.abunchtell.com https://m.abunchtell.com
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

352 linhas
14 KiB

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