ActivityPub:去中心化社交网络协议

ActivityPub 是一个去中心化的社交网络协议,基于 ActivityStreams 2.0 数据格式。它提供了从客户端到服务器的 API,用于创建、更新和删除内容,以及一个从服务器到服务器的 API,用于传递通知和内容。本文将深入探讨 ActivityPub 的核心概念和实现方式。

什么是 ActivityPub?

ActivityPub 是一种标准化的协议,旨在实现去中心化的社交网络。它包括两个主要部分:

  1. 客户端到服务器的协议:允许用户(包括真实用户、机器人和其他自动化进程)通过他们在服务器上的账户与 ActivityPub 通信。这可以通过手机、桌面应用或网页应用实现。
  2. 服务器到服务器的协议:使去中心化网站能够共享信息和内容。

基本概念

在 ActivityPub 中,用户通过其在服务器上的账户表示为“actors”。每个 actor 都有一个收件箱(inbox)和发件箱(outbox),用于接收和发送消息。

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "type": "Person",
  "id": "https://social.example/alyssa/",
  "name": "Alyssa P. Hacker",
  "preferredUsername": "alyssa",
  "summary": "Lisp enthusiast hailing from MIT",
  "inbox": "https://social.example/alyssa/inbox/",
  "outbox": "https://social.example/alyssa/outbox/",
  "followers": "https://social.example/alyssa/followers/",
  "following": "https://social.example/alyssa/following/",
  "liked": "https://social.example/alyssa/liked/"
}

客户端到服务器的交互

客户端通过向 actor 的发件箱(outbox)发送 POST 请求来发布活动。请求必须包含一个 Activity 对象,服务器随后会将其处理并传递到目标收件箱。

发布活动示例

假设 Alyssa 想给她的朋友 Ben 发送一条消息,询问他是否还书。她的消息可以表示为一个 ActivityStreams 对象,并通过 POST 请求发送到她的 outbox。

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "type": "Note",
  "to": ["https://chatty.example/ben/"],
  "attributedTo": "https://social.example/alyssa/",
  "content": "Say, did you finish reading that book I lent you?"
}

服务器会将此消息包装在一个 Create 活动中,并将其 POST 到 Ben 的收件箱。

接收消息

Alyssa 的手机会通过 GET 请求轮询她的收件箱,以获取新消息。当 Ben 回复了她的消息,她会看到如下内容:

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "type": "Create",
  "id": "https://chatty.example/ben/p/51086",
  "to": ["https://social.example/alyssa/"],
  "actor": "https://chatty.example/ben/",
  "object": {
    "type": "Note",
    "id": "https://chatty.example/ben/p/51085",
    "attributedTo": "https://chatty.example/ben/",
    "to": ["https://social.example/alyssa/"],
    "inReplyTo": "https://social.example/alyssa/posts/49e2d03d-b53a-4c4c-a95c-94a6abf45a19",
    "content": "<p>Argh, yeah, sorry, I'll get it back to you tomorrow.</p><p>I was reviewing the section on register machines, since it's been a while since I wrote one.</p>"
  }
}

服务器到服务器的交互

服务器之间通过向 actor 的收件箱发送 POST 请求来传递消息。这种交互方式允许去中心化的社交网络跨不同服务器传递信息。

示例:服务器间的消息传递

当 Alyssa 发布一条消息到她的 outbox,服务器会查找消息的目标收件箱,并将消息 POST 到这些收件箱。这样,Ben 的服务器就能接收到 Alyssa 的消息,并将其存储在 Ben 的收件箱中。

安全性与认证

ActivityPub 使用认证机制来验证客户端与服务器之间、以及服务器与服务器之间的互动。为了确保内容的真实性,服务器应验证接收到的内容,确保其来自声称的 actor,并且该 actor 有权限更新资源。

验证示例

服务器在接收到一个活动后,应验证该活动的 idactor 属性,确保其真实性。例如:

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "type": "Like",
  "actor": "https://example.net/~mallory",
  "to": ["https://hatchat.example/sarah/", "https://example.com/peeps/john/"],
  "object": {
    "@context": {"@language": "en"},
    "id": "https://example.org/~alice/note/23",
    "type": "Note",
    "attributedTo": "https://example.org/~alice",
    "content": "I'm a goat"
  }
}

服务器应确保 idactor 属性合法,并查验对象是否由 actor 创建。

结论

ActivityPub 通过提供去中心化的社交网络协议,实现了更高的自由度和隐私保护。无论是客户端到服务器的交互,还是服务器到服务器的消息传递,ActivityPub 都为构建去中心化的社交网络提供了强大的工具。

参考文献

通过了解和实现 ActivityPub,我们可以构建一个更加自由和开放的互联网社交网络环境。

image.png

0 0 投票数
Article Rating
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x