Stream Feeds - Legacy

Warning:

These docs are for Feeds v2, which is deprecated and considered a legacy product. You can find documentation for Feeds v3 here.

Introduction

Moderation in Feeds is made possible through the work on the new moderation API. Moderation capabilities have been added to the Feeds API with the following features:

  • Automatic moderation of new activities
  • Automatic moderation of activities on update
  • Automatic moderation of new reactions
  • Automatic moderation of reactions on update
  • Flagging activities
  • Flagging reactions
  • Flagging users
  • Prevent banned users from reading a feed
  • Prevent banned users from creating activities/reactions

Prerequisites

  • Since moderation in Feeds is behind a feature flag, make sure this is enabled for your app. Get in touch with Stream Support Team for this purpose.

Create Moderation Policy

A moderation policy is a set of rules that define how content should be moderated in your application. See Configuration for details on setting up moderation policies.

Setup Moderation Templates

Moderation templates are Feeds-specific and are needed because activities (or reactions) are generic in structure. The template is used to tell the API which part(s) of the activity should be subject to moderation and which type they are (text or image). As an example, your activities might look like this:

{
  "actor": "jimmy",
  "verb": "post",
  "object": {
    "title": "this is the title",
    "body": "this is the body..."
  },
  "image": "https://example.com/post_image.png"
}

and you want to moderate the fields object.title , object.body and image you would create a moderation template like this, lets name it mod_tmpl_1

{
  "data_types": {
    "object.title": "text",
    "object.body": "text",
    "image": "image"
  }
}

If you want to moderate reactions, you need to create a separate template for it. As an example, your reaction might look like this:

{
  "text": "This is the reaction text",
  "attachments": ["https://example.com/post_image.png"]
}

and you want to moderate the fields text and attachments, you would create a moderation template like this, lets name it moderation_template_reaction

{
  "data_types": {
    "text": "text",
    "attachments": "image"
  }
}

If you have activities that varies in structure (or reactions) you can create multiple templates to use for them as well.

It's important to understand that when it comes to moderation in feeds, the user will be determined by the actor on the activities and the user_id on reactions. The feeds moderation integration will automatically create users on the moderation side to be able to track bans/blocks etc there. This is not to be confused with the Feeds user which is a separate entity.

Create a Template via API

await client.moderation.v2UpsertTemplate({
  name: "mod_tmpl_1",
  config: {
    data_types: { text: "text", attachments: "image" },
    config_key: "feeds:default",
  },
});

Query Templates

await client.moderation.v2QueryTemplates();

Delete Template

await client.moderation.v2DeleteTemplate({ name: "mod_tmpl_1" });

Client Side Feeds Integration

Auto moderation of activities

To enable moderation of activities for client-side integration, you need to include the required_moderation_template (Moderation Template which you created for activities) in your JWT token payload. This ensures that any activities created from the client are automatically moderated according to your policy. You must include this specific JWT token only for activities and not for reactions.

Generate JWT Token

Generate the token on your server with the Feeds API secret. Do not put the secret in a browser, a mobile app, or a public token debugger.

Use this payload:

{
  "resource": "feed",
  "action": "write",
  "feed_id": "feed123",
  "user_id": "user123",
  "required_moderation_template": "mod_tmpl_1"
}

Important: Replace "mod_tmpl_1" with the name of your specific moderation template.

Sign that payload with the server secret and pass the resulting token to the client SDK.

Auto moderation of reactions

Important: You need a different JWT Token for reactions for moderation to work with it. Don't confuse this with the token you generated above for the auto moderation of activities.

To enable moderation of reactions for client-side integration, you need to include the required_moderation_template (Moderation Template which you created for reactions) in your JWT token payload. This ensures that any activities created from the client are automatically moderated according to your policy. You must include this specific JWT token only for reactions and not for activities.

Generate JWT Token

Generate this token on your server as well. Use this payload:

{
  "resource": "reactions",
  "action": "write",
  "feed_id": "feed123",
  "user_id": "user123",
  "required_moderation_template": "moderation_template_reaction"
}

Important: Replace "moderation_template_reaction" with the name of your specific moderation template.

Sign that payload on the server and pass the token to the client SDK.

Server Side Feeds Integration

Auto Moderation of Activities

Info:

Moderation is not supported on batch activity inserts (the Feeds API doesn't support partial responses, and the moderation API doesn't have a batch endpoint for this yet)

Now that you have a moderation config and at least one moderation template, you can start automatically moderating content in your feeds. Auto-moderation allows you to proactively screen activities before they are added to feeds, helping maintain content quality and safety.

When an activity with a moderation template is added, Stream will:

  1. Extract the content specified in the template (text fields, images, etc.)
  2. Run it through the configured moderation policy
  3. Take the appropriate action based on policy rules (block, flag, etc.)

To enable auto-moderation for activities, simply supply the moderation_template field in your activity object when making requests to the feeds API, like so:

Warning:

You need to supply the moderation_template field while adding the activity. Learn how to create moderation templates in the Setup Moderation Templates section.

{
  "actor": "jimmy",
  "verb": "post",
  "object": {
    "title": "this is the title",
    "body": "this is the body..."
  },
  "image": "https://example.com/post_image.png",
  "moderation_template": "mod_tmpl_1" // this points to the moderation template
}

Here are code examples showing how to add an activity with the moderation_template field:

const response = await client.feed("user", "user-1").addActivity({
  verb: "eat",
  object: "object",
  moderation_template: "moderation_template_activity",
  a: "buy followers now",
  text: "buy followers now",
  attachment: {
    images: ["image1", "image2"],
  },
  foreign_id: "random_foreign_id",
  time: new Date(),
});
console.log(response.moderation.recommended_action); // "remove"
console.log(response.moderation.status); // "complete"

Auto moderation of reactions

To add a reaction with moderation, you need to supply the moderation_template field in the request.

const responseReaction = await client.reactions.add(
  "comment",
  responseActivity.id,
  {
    text: "buy followers now",
    moderation_template: "moderation_template_reaction",
  },
);
console.log(responseReaction.moderation.response.recommended_action); // "remove"
console.log(responseReaction.moderation.response.status); // "complete"

User-Driven Actions

Flag a Feeds user, activity, or reaction with the moderation flag method. The old Feeds client helpers (flagUser, flagActivity, flagReaction) are not the moderation API.

await client.moderation.flag({
  entity_type: "stream:feeds:v2:activity",
  entity_id: activityId,
  entity_creator_id: actorId,
  reason: "inappropriate_content",
  user_id: reporterId,
});

Use stream:user for a user and stream:feeds:v2:reaction for a reaction. The full samples are on Flag, Mute, and Ban.