The code powering m.abunchtell.com https://m.abunchtell.com
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

373 wiersze
15 KiB

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