🍓

[Mastodon, Misskey]10種類のWebフレームワークでActivityPub実装(StrawberryFields)

2020/12/23に公開

メリークリスマス

この記事はFediverse (3) Advent Calendar 2020の23日目です。明日はID:weepさんの「#InstanceTicker のあれやこれ!」です。

今回16種類のWebフレームワークでActivityPub実装を行うプロジェクト「StrawberryFields」を立ち上げました。

https://acefed.gitlab.io/strawberryfields

TL;DR

  • Mastodon上でActivityPub実装をフォロー、アンフォローできるようにする。
  • ActivityPubとは無関係のエンドポイントへHTMLを使わずcurlで直接クエリをPOSTし、Mastodonアカウントをフォロー、アンフォローできるようにする。

今回は上記2点を目標とし、実装していきたいと思います。そのために必要な技術は以下のとおりです。

  • Webアプリケーションフレームワークを使用してJSONのレスポンスを返す。
  • HTTPリクエストクライアントを使用してJSONのリクエストを送る。
  • 返す必要のあるレスポンスヘッダ、レスポンスボディを理解する。
  • 送る必要のあるリクエストヘッダ、リクエストボディを理解する。
  • ActivityPubを実装する。
  • Actorのエンドポイントを発見できるようにする必要があるため、WebFingerを実装する。
  • Activityに必要なActivity Streamsを理解する。
  • 必要なJSON-LDとしてActivity VocabularyとSecurity Vocabularyを理解する。
  • 署名をする必要があるので、HTTP Message Signatures(旧HTTP Signatures)を実装する。
  • なるほどRSA-SHA256の署名と検証を学ぶ必要があったんですね。
  • SHA-256?
  • RSA難しい。
  • ActivityPubって何だ?
  • ActivityPub何もわからん。
  • ?????????
  • ActivityPub完全に理解した。

最初からMVCに基づいたSNSを作っても破綻するし、ActivityPubチョットデキルことを目標としたほうが良さそうなので、まずは10種類のWebフレームワークでActivityPub実装することにしました。
当初JavaのSparkとSpringでも実装を行う予定で12種類だったのですが、コンテナの都合上未定となりました。

実装

2022/07/20更新:やる気のない文章を残しておいても陰口を叩かれるだけ誰も得しないのでソースコード全文貼り付けるだけにしました。よかったですね。
2024/01/05更新:昨年JavaをはじめとしたJVM言語の実装に成功したので4種類のソースコードと続編記事を追加しました。
2024/03/10更新:C#とRustの実装に成功したので2種類のソースコードを追加しました。

https://zenn.dev/tkithrta/articles/fb96667e0b19f6

MIT License
Copyright (c) 2020, 2021, 2022, 2023, 2024 Acefed

Express

https://gitlab.com/acefed/strawberryfields-express/-/blob/master/index.js

Fastify

https://gitlab.com/acefed/strawberryfields-fastify/-/blob/master/index.js

Flask

https://gitlab.com/acefed/strawberryfields-flask/-/blob/master/app.py

FastAPI

https://gitlab.com/acefed/strawberryfields-fastapi/-/blob/master/app.py

Lumen

https://gitlab.com/acefed/strawberryfields-lumen/-/tree/master/app/Http/Controllers

Laravel

https://gitlab.com/acefed/strawberryfields-laravel/-/tree/master/app/Http/Controllers

Sinatra

https://gitlab.com/acefed/strawberryfields-sinatra/-/blob/master/app.rb

Rails

https://gitlab.com/acefed/strawberryfields-rails/-/tree/master/app/controllers

Gin

https://gitlab.com/acefed/strawberryfields-gin/-/blob/master/main.go

Django

https://gitlab.com/acefed/strawberryfields-django/-/blob/master/app/views.py

Spark

https://gitlab.com/acefed/strawberryfields-spark/-/blob/main/src/main/java/io/gitlab/acefed/strawberryfieldsspark/StrawberryfieldsSparkApplication.java

Spring

https://gitlab.com/acefed/strawberryfields-spring/-/blob/main/src/main/java/io/gitlab/acefed/strawberryfieldsspring/StrawberryfieldsSpringApplication.java

Ktor

https://gitlab.com/acefed/strawberryfields-ktor/-/blob/main/src/main/kotlin/io/gitlab/acefed/strawberryfieldsktor/StrawberryfieldsKtorApplication.kt

Play

https://gitlab.com/acefed/strawberryfields-play/-/blob/main/app/controllers/HomeController.scala

.Net

https://gitlab.com/acefed/strawberryfields-dotnet/-/blob/main/Program.cs

Actix

https://gitlab.com/acefed/strawberryfields-actix/-/blob/main/src/main.rs

参考文献

以下2024/01/05更新

Discussion