jasnell / activitystrea.ms

Activity Streams 2.0 for Node.js (this package is not actively maintained, if you'd like to help, let me know)
Apache License 2.0
148 stars 32 forks source link

Cannot expand @context #39

Open Jimmy89 opened 5 months ago

Jimmy89 commented 5 months ago

Hello,

I'm trying out this plugin for activity streams and I noticed that there is no way to expand the @context of a record.

For example, I am trying to rebuild the example within the spec

{
  "@context": [
    "https://www.w3.org/ns/activitystreams",
    {"vcard": "http://www.w3.org/2006/vcard/ns#"}
  ],
  "summary": "Sally created a note",
  "type": "Create",
  "actor": {
    "type": ["Person", "vcard:Individual"],
    "id": "http://sally.example.org",
    "name": "Sally Smith",
    "vcard:given-name": "Sally",
    "vcard:family-name": "Smith"
  },
  "object": {
    "type": "Note",
    "content": "This is a simple note"
  }
}

Where the @context is an array with a second item for vcard. Within the test suite of this library I found a test:

  it("should allow using .set() when the @context is an array", async () => {
    const string = await as.activity()
      .context([
          'https://www.w3.org/ns/activitystreams',
          { vcard: 'http://www.w3.org/2006/vcard/ns#' }
      ])
      .actor(
          as.person()
              .name('Denis Prša')
              .id('https://github.com/denisprsa')
      )
      .object(
          as.offer()
              .name('Thank you')
              .id('https://github.com/jasnell/activitystrea.ms/issues/13#issuecomment-376722358')
              .set('vcard:given-name', 'Thank')
      )
      .name('The offer')
      .content('Evan offered his thanks to Denis for his bug report')
      .set({h: 'd'})
      .published(new Date())
      .prettyWrite();

    assert(string); // <-- seems to me missing a test case.
  })

However, this test is invalid. Because the .context adjusts the context field, which has a different meaning than the @context. The input of the test is something that must be added to @context instead.

In other words: there seems to be no way of setting @context currently and its test is invalid.

Side note: I also think the vcard:given-name should also be set on the person and not on the offer, in which case the as.person() should be rewritten as as.person("vcard:Individual") Also not so sure what.set({h: 'd'}) should do, as that item is not set at all. Maybe the test should check that the property was not created? I guess that the test could also check whether the keyword namespace is included within the @context.

Jimmy89 commented 4 months ago

I found out that @context could be expanded through the environment option when creating an activity. For that you need to implement the Environment class, which is not exported.

As workaround you can import import Environment from "activitystrea.ms/src/environment.js";

Then at initialization you must specify the @context through:

as.create(undefined,
    new Environment({ "@context": [
       'https://www.w3.org/ns/activitystreams',
       { vcard: 'http://www.w3.org/2006/vcard/ns#' },
    ] }))

Secondly, when using the write or similar command, you must provide the option useOriginalContext: true.

However, there is a bug within the function, it looks for the getter options.origContext, which is the wrong getter name, it should be originalContext.

So, two bugs: 1) provide a direct export for the environment class; 2) search for originalContext instead of origContext.

The current workaround is:


import Environment from "activitystrea.ms/src/environment.js";

class EnvironmentFixed extends Environment {
  constructor(...args) {
      super(...args);
  }
  get origContext() {
    return super.originalContext;
  }
}

const result = await as.create(undefined,
    new EnvironmentFixed({ "@context": [
      'https://www.w3.org/ns/activitystreams',
      {
        "vcard": "http://www.w3.org/2006/vcard/ns",
      },
    ] })).write({ useOriginalContext: true });
.....