```javascript
import { Web5 } from '@tbd54566975/web5';
const { web5, did } = await Web5.connect();
let protocolDefinition = {
"protocol": "http://social-media.xyz",
"types": {
"message": {
"schema": "messageSchema",
"dataFormats": ["text/plain"]
},
"reply": {
"schema": "replySchema",
"dataFormats": ["text/plain"]
},
"image": {
"schema": "imageSchema",
"dataFormats": ["image/jpeg"]
},
"caption": {
"schema": "captionSchema",
"dataFormats": ["text/plain"]
}
},
"structure": {
/**
* message - anyone write
* reply - write by recipient
*
* image - anyone write, anyone read?
* caption - write by author
* reply - read by author, write by recipient
* */
"message": {
"$actions": [
{
"who": "anyone",
"can": "write"
}
],
"reply": {
"$actions": [
{
"who": "recipient",
"of": "message",
"can": "write"
}
]
}
},
"image": {
"$actions": [
{
"who": "anyone",
"can": "read"
},
{
"who": "anyone",
"can": "write"
}
],
"caption": {
"$actions": [
{
"who": "anyone",
"can": "read"
},
{
"who": "author",
"of": "image",
"can": "write"
}
]
},
"reply": {
"$actions": [
{
"who": "author",
"of": "image",
"can": "read"
},
{
"who": "recipient",
"of": "image",
"can": "write"
}
]
}
}
}
}
let aliceDid = "did:ion:EiAdvLj9iK2qbhY_ZNr671YtCWgKD8roaQefAMgBgz-aMA:eyJkZWx0YSI6eyJwYXRjaGVzIjpbeyJhY3Rpb24iOiJyZXBsYWNlIiwiZG9jdW1lbnQiOnsicHVibGljS2V5cyI6W3siaWQiOiJhdXRoeiIsInB1YmxpY0tleUp3ayI6eyJjcnYiOiJzZWNwMjU2azEiLCJrdHkiOiJFQyIsIngiOiJGQXdBSndvdFhjNmJqcm5NZDFHMTY4UXFXUlFoRzZncklaTExHZmNlYTZJIiwieSI6IlRoSEtRaTJoaER6d3h1Mll0R0ZFMWJkQXcwbjRKcl9nb01iZkFyZlJtMncifSwicHVycG9zZXMiOlsiYXV0aGVudGljYXRpb24iXSwidHlwZSI6Ikpzb25XZWJLZXkyMDIwIn0seyJpZCI6ImVuYyIsInB1YmxpY0tleUp3ayI6eyJjcnYiOiJzZWNwMjU2azEiLCJrdHkiOiJFQyIsIngiOiJvVEt6eWhsY2lmUHdSU1RGSEhTNGI5SllHSUt2d2hJUS1iTTFzRUk3YUhZIiwieSI6InFJVkNobjQxRVNPT2ZHSnZsaW9qbEFmYTY5M01nZXVJSEg1czFCTEVQS1UifSwicHVycG9zZXMiOlsia2V5QWdyZWVtZW50Il0sInR5cGUiOiJKc29uV2ViS2V5MjAyMCJ9XSwic2VydmljZXMiOlt7ImlkIjoiZHduIiwic2VydmljZUVuZHBvaW50Ijp7Im1lc3NhZ2VBdXRob3JpemF0aW9uS2V5cyI6WyIjYXV0aHoiXSwibm9kZXMiOlsiaHR0cHM6Ly9kd24udGJkZGV2Lm9yZy9kd24xIiwiaHR0cHM6Ly9kd24udGJkZGV2Lm9yZy9kd24yIl0sInJlY29yZEVuY3J5cHRpb25LZXlzIjpbIiNlbmMiXX0sInR5cGUiOiJEZWNlbnRyYWxpemVkV2ViTm9kZSJ9XX19XSwidXBkYXRlQ29tbWl0bWVudCI6IkVpRENfQ3lwaG1BY2Y0cUhja0o5bkREVm9tNkxKT3Z1ZTJJVlNYbGY1bUZzZGcifSwic3VmZml4RGF0YSI6eyJkZWx0YUhhc2giOiJFaUFBbER3Q3B6Qy05YkwtTDNjVzkxSzhoTXFkLS1pN19PcHdRR1dUQjVOcnBRIiwicmVjb3ZlcnlDb21taXRtZW50IjoiRWlBYU5aTk9VNG1yWlBfQkFHTXo1Z0xhSmpDbUhFbnBKa2hMMUFsZG9pUThIdyJ9fQ"
await configureMyProtocol(protocolDefinition);
await checkIfAliceHasProtocol(protocolDefinition);
await sendMessage();
// this is what's in the doc
// const { record, status } = await web5.dwn.records.create({
// data: 'Hello, world!',
// message: {
// recipient: 'did:example:alice',
// schema: 'message',
// dataFormat: 'text/plain',
// protocol: 'social-media',
// },
// });
// await record.send('did:example:alice')
async function sendMessage() {
const { record, status: createStatus } = await web5.dwn.records.create({
data: 'Hello, world!',
message: {
recipient: aliceDid,
schema: 'http://messageschema',
dataFormat: 'text/plain',
protocol: protocolDefinition.protocol, // "http://social-media.xyz",
protocolPath: 'message'
},
});
if (createStatus.code !== 202) {
console.log('create failed ' + createStatus.code, createStatus.detail);
return;
}
console.log('create success! now sending to alice...')
const { status: sendStatus } = await record.send(aliceDid)
// 401 ’unable to find protocol definition for http://social-media.xyz'
if (sendStatus.code !== 202) {
console.log('send to alice failed ' + sendStatus.code, sendStatus.detail);
return;
}
console.log('send success!!!')
}
async function configureMyProtocol(protocolDefinition) {
/*
this is what's in the doc
const response = await web5.dwn.protocols.configure(myDid.id, protocolObject);
*/
const { protocols: myprotocols, status: mystatus } = await web5.dwn.protocols.query({
message: {
filter: {
protocol: protocolDefinition.protocol
}
}
});
if (mystatus.code !== 200) {
console.error('Failed to query my protocols', mystatus);
return;
}
// protocol already exists
if (myprotocols.length > 0) {
console.log('protocol already exists for me');
return;
}
// create protocol
const { status: configureStatus } = await web5.dwn.protocols.configure({
message: {
definition: protocolDefinition
}
});
console.log('configure protocol status', configureStatus);
}
async function checkIfAliceHasProtocol(protocolDefinition) {
// this is what's in the doc
// const { record } = await web5.dwn.protocols.query({
// from: 'did:example:alice',
// message: {
// filter: {
// protocol: 'social-media',
// },
// },
// });
// check if alice has protocol
const { protocols, status } = await web5.dwn.protocols.query({
from: aliceDid,
message: {
filter: {
protocol: protocolDefinition.protocol
}
}
});
if (status.code !== 200) {
console.error('Failed to query alice protocols', status);
return;
}
// protocol already exists
if (protocols.length > 0) {
console.log('protocol already exists for alice', JSON.stringify(protocols[0], null, 4));
console.log('alice protocol name', protocols[0].definition.protocol)
return;
}
}
```