Difference between revisions of "ActivityPub Code"

From Organic Design wiki
m (User Discovery)
(User Discovery: Added JS code.)
 
Line 20: Line 20:
  
 
That command should return an object describing the relevant endpoints for that user.
 
That command should return an object describing the relevant endpoints for that user.
 +
 +
==== JS Code ====
 +
<source lang="javascript">
 +
const fetch = require("node-fetch");
 +
 +
const url = "mastodon.online";
 +
const user = "john";
 +
 +
const fetcher = async (url, type = "application/json") => {
 +
const data = await fetch(url, {
 +
headers: {
 +
Accept: type
 +
}
 +
});
 +
 +
return await data.json();
 +
}
 +
 +
const getLatestActivities = async () => {
 +
let data = await fetcher(`https://${url}/.well-known/webfinger?resource=${user}@${url}`);
 +
 +
let userUrl;
 +
 +
for (const link of data.links) {
 +
if (link.type === "application/activity+json") {
 +
userUrl = link.href;
 +
break;
 +
}
 +
}
 +
 +
if (!userUrl)
 +
throw "Could not find user activity url.";
 +
 +
data = await fetcher(userUrl, "application/activity+json");
 +
 +
if (!data.outbox)
 +
throw "No outbox specifed under the user.";
 +
 +
data = await fetcher(data.outbox);
 +
 +
if (!data.last)
 +
throw "Could not find last items in outbox.";
 +
 +
data = await fetcher(data.last);
 +
 +
return data;
 +
};
 +
</source>

Latest revision as of 23:00, 7 October 2021

https://www.w3.org/TR/activitypub

Client/Server

The specification doesn't seem to provide endpoints for interacting with an AP server, that is why I believe that most servers use WebFinger. The only issue is that I can't find that in the specification either.

User Discovery

To request the user's endpoint from the server run the following command.

curl <URL>/.well-known/webfinger?resource=<ACCOUNT NAME>@<URL> \
	-X GET \
	-H "Accept: application/json"

This should return an object describing the user on the server, you should look for inside the links property for an object that has the property "type": "application/activity+json", the next command should be made to the href property of that object.

curl <HREF PROPERTY> \
	-X GET \
	-H "Accept: application/activity+json"

That command should return an object describing the relevant endpoints for that user.

JS Code

const fetch = require("node-fetch");

const url = "mastodon.online";
const user = "john";

const fetcher = async (url, type = "application/json") => {
	const data = await fetch(url, {
		headers: {
			Accept: type
		}
	});

	return await data.json();
}

const getLatestActivities = async () => {
	let data = await fetcher(`https://${url}/.well-known/webfinger?resource=${user}@${url}`);

	let userUrl;

	for (const link of data.links) {
		if (link.type === "application/activity+json") {
			userUrl = link.href;
			break;
		}
	}

	if (!userUrl)
		throw "Could not find user activity url.";

	data = await fetcher(userUrl, "application/activity+json");

	if (!data.outbox)
		throw "No outbox specifed under the user.";

	data = await fetcher(data.outbox);

	if (!data.last)
		throw "Could not find last items in outbox.";

	data = await fetcher(data.last);

	return data;
};