### Instantiate and Get Activities Source: https://github.com/getstream/stream-js/blob/main/README.md Instantiate a user feed and retrieve activities with pagination and filtering. API calls are asynchronous and return Promises. ```javascript // Instantiate a feed object server side user1 = client.feed('user', '1'); // Get activities from 5 to 10 (slow pagination) user1.get({ limit: 5, offset: 5 }); // Filter on an id less than a given UUID user1.get({ limit: 5, id_lt: 'e561de8f-00f1-11e4-b400-0cc47a024be0' }); // All API calls are performed asynchronous and return a Promise object user1 .get({ limit: 5, id_lt: 'e561de8f-00f1-11e4-b400-0cc47a024be0' }) .then(function (body) { /* on success */ }) .catch(function (reason) { /* on failure, reason.error contains an explanation */ }); ``` -------------------------------- ### Node.js API Client Setup Source: https://github.com/getstream/stream-js/blob/main/README.md Instantiate a new Stream API client for server-side applications using your API key and secret. Optionally configure location and timeout. ```javascript import { connect } from 'getstream'; // or if you are on commonjs const { connect } = require('getstream'); // Instantiate a new client (server side) const client = connect('YOUR_API_KEY', 'API_KEY_SECRET'); // Optionally supply the app identifier and an options object specifying the data center to use and timeout for requests (15s) const client = connect('YOUR_API_KEY', 'API_KEY_SECRET', 'APP_ID', { location: 'us-east', timeout: 15000 }); ``` -------------------------------- ### Install Stream JS SDK via NPM or YARN Source: https://github.com/getstream/stream-js/blob/main/README.md Use this command to add the Stream JavaScript SDK to your project dependencies using either npm or yarn. ```bash npm install getstream ``` ```bash yarn add getstream ``` -------------------------------- ### Mark Notification Feed Read/Seen Source: https://github.com/getstream/stream-js/blob/main/README.md Mark a notification feed as read or seen using parameters in the get request. ```javascript // mark a notification feed as read notification1 = client.feed('notification', '1'); params = { mark_read: true }; notification1.get(params); // mark a notification feed as seen params = { mark_seen: true }; notification1.get(params); ``` -------------------------------- ### TypeScript: Get User Data with Type Guard Source: https://github.com/getstream/stream-js/blob/main/README.md Retrieves a user's data and uses a type guard to return either the username (if User1Type) or the user ID. ```typescript client .user('user_id') .get() .then((user) => { const { data, id } = user; if (isUser1Type(data)) return data.username; return id; }); ``` -------------------------------- ### TypeScript: Get Collection Item Source: https://github.com/getstream/stream-js/blob/main/README.md Retrieves a specific item from a collection and returns its rating if available, otherwise returns the item's ID. ```typescript client.collections.get('collection_1', 'taco').then((item: CollectionEntry) => { if (item.data.rating) return { [item.data.cid]: item.data.rating }; return item.id; }); ``` -------------------------------- ### Test Directory Structure Source: https://github.com/getstream/stream-js/blob/main/test/README.md This bash snippet illustrates the organization of the test directories within the project. It separates unit and integration tests, further categorizing them by environment (browser, node, common). ```bash test ├── integration # Integration tests (only ran on release) │ ├── browser # tests ran in browser environment │ ├── common # tests ran in both node and browser environment │ ├── node # tests only ran in the node environment │ └── utils # files containing configuration and mocks └── unit # Unit tests (run on Travis) ├── browser # files needed by tests ├── common # files needed by tests ├── node # files needed by tests └── utils # files needed by tests ``` -------------------------------- ### Running Individual Mocha Tests Source: https://github.com/getstream/stream-js/blob/main/test/README.md These bash commands demonstrate how to execute specific tests using Mocha. You can run an entire file, all tests within a directory matching a pattern, or all unit tests. ```bash # Whole file mocha test/unit/common/client_test.js # Whole dir mocha test/unit/common/*_test.js # All unit tests yarn test ``` -------------------------------- ### Initialize Stream JS Client in Browser Source: https://github.com/getstream/stream-js/blob/main/test/full-browser-build.html Connect to Stream services using your API key, user token, and app ID. Ensure you have these credentials before initializing the client. The `setUser` method is called to authenticate the current user. ```javascript const apiKey = 'sesb46h7zb6p'; const appId = '66001'; const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYmF0bWFuIn0.8aYd7O_fx-1YMx28DXG1n274o4pa3SjHnRM8AIHLqkE'; const client = stream.connect(apiKey, token, appId); client.setUser({ name: 'Amin' }).then(console.log); ``` -------------------------------- ### Batch Create Follow Relations (Server-Side) Source: https://github.com/getstream/stream-js/blob/main/README.md Create multiple follow relations in a single request. This operation is server-side only. ```javascript // Batch create follow relations (let flat:1 follow user:1, user:2 and user:3 feeds in one single request) follows = [ { source: 'flat:1', target: 'user:1' }, { source: 'flat:1', target: 'user:2' }, { source: 'flat:1', target: 'user:3' }, ]; // ⚠️ server-side only! client.followMany(follows); ``` -------------------------------- ### Client API Initialization with User Token Source: https://github.com/getstream/stream-js/blob/main/README.md Instantiate the Stream API client in a browser or mobile application using a pre-generated user token. This ensures secure API access without exposing server secrets. ```javascript import { connect } from 'getstream'; // or if you are on commonjs const { connect } = require('getstream'); // Instantiate new client with a user token const client = connect('apikey', userToken, 'appid'); ``` -------------------------------- ### Activity Management Source: https://github.com/getstream/stream-js/blob/main/README.md Add, add multiple, remove, and partially update activities. ```APIDOC ## Activity Management ### Description Methods for adding, removing, and updating activities in a feed. ### Methods #### Add Activity - `feed.addActivity(activity)`: Adds a single activity to the feed. - `feed.addActivities(activities)`: Adds multiple activities to the feed. - `client.addToMany(activity, feeds)`: (Server-side only) Adds a single activity to multiple feeds. #### Remove Activity - `feed.removeActivity(id)`: Removes an activity by its ID. - `feed.removeActivity(foreign_id_object)`: Removes an activity by its foreign ID. #### Partial Update Activity - `client.activityPartialUpdate(params)`: Updates parts of an activity. ### Parameters #### `addActivity` and `addActivities` - **activity** (object) - The activity object to add. - **activities** (array) - An array of activity objects to add. - **actor** (string | number) - The actor of the activity. - **verb** (string) - The verb of the activity. - **object** (string | number) - The object of the activity. - **foreign_id** (string) - Optional - A unique identifier for the activity. - **to** (array) - Optional - An array of feed IDs to push the activity to. #### `removeActivity` - **id** (string) - The ID of the activity to remove. - **foreign_id_object** (object) - An object containing the `foreign_id` of the activity to remove. - **foreign_id** (string) - The foreign ID of the activity. #### `addToMany` (Server-side only) - **activity** (object) - The activity object to add. - **feeds** (array) - An array of feed IDs to add the activity to. #### `activityPartialUpdate` - **id** (string) - Optional - The ID of the activity to update. - **foreign_id** (string) - Optional - The foreign ID of the activity to update. - **time** (string) - Optional - The timestamp of the activity to update (used with `foreign_id`). - **set** (object) - An object containing fields to set or update. - **unset** (array) - An array of field names to unset. ### Request Example ```js // Create a new activity activity = { actor: 1, verb: 'tweet', object: 1, foreign_id: 'tweet:1' }; user1.addActivity(activity); // Create a bit more complex activity activity = { actor: 1, verb: 'run', object: 1, foreign_id: 'run:1', course: { name: 'Golden Gate park', distance: 10 }, participants: ['Thierry', 'Tommaso'], started_at: new Date(), }; user1.addActivity(activity); // Add multiple activities activities = [ { actor: 1, verb: 'tweet', object: 1 }, { actor: 2, verb: 'tweet', object: 3 }, ]; user1.addActivities(activities); // Add activity to multiple feeds (server-side only) to = ['user:2', 'user:3']; activity = { to: to, actor: 1, verb: 'tweet', object: 1, foreign_id: 'tweet:1', }; user1.addActivity(activity); // Note: The example shows user1.addActivity, but client.addToMany is the correct method for multiple feeds. // Remove an activity by its id user1.removeActivity('e561de8f-00f1-11e4-b400-0cc47a024be0'); // Remove by the foreign id user1.removeActivity({ foreign_id: 'tweet:1' }); // Updating parts of an activity set = { 'product.price': 19.99, shares: { facebook: '...', twitter: '...', }, }; unset = ['daily_likes', 'popularity']; // ...by ID client.activityPartialUpdate({ id: '54a60c1e-4ee3-494b-a1e3-50c06acb5ed4', set: set, unset: unset, }); // ...or by combination of foreign ID and time client.activityPartialUpdate({ foreign_id: 'product:123', time: '2016-11-10T13:20:00.000000', set: set, unset: unset, }); ``` ``` -------------------------------- ### Feed Operations Source: https://github.com/getstream/stream-js/blob/main/README.md Instantiate a feed object and retrieve activities with pagination and filtering. ```APIDOC ## Feed Instantiation and Retrieval ### Description Instantiate a feed object and retrieve activities with pagination and filtering. ### Method `feed.get(params)` ### Parameters #### Query Parameters - **limit** (number) - Optional - The maximum number of activities to retrieve. - **offset** (number) - Optional - The offset for pagination. - **id_lt** (string) - Optional - Filter activities with an ID less than the specified value. - **mark_read** (boolean) - Optional - Mark all retrieved notifications as read. - **mark_seen** (boolean) - Optional - Mark all retrieved notifications as seen. ### Request Example ```js // Instantiate a feed object server side user1 = client.feed('user', '1'); // Get activities from 5 to 10 (slow pagination) user1.get({ limit: 5, offset: 5 }); // Filter on an id less than a given UUID user1.get({ limit: 5, id_lt: 'e561de8f-00f1-11e4-b400-0cc47a024be0' }); // Mark notifications as read notification1 = client.feed('notification', '1'); params = { mark_read: true }; notification1.get(params); // Mark notifications as seen params = { mark_seen: true }; notification1.get(params); ``` ### Response #### Success Response (200) - **body** (object) - Contains the retrieved activities and pagination information. ``` -------------------------------- ### List Followers and Following Source: https://github.com/getstream/stream-js/blob/main/README.md Retrieve lists of followers and feeds being followed. ```APIDOC ## List Followers and Following ### Description Retrieve lists of followers for a feed and feeds that a feed is following. ### Methods - `feed.followers(params)`: Lists the followers of a feed. - `feed.following(params)`: Lists the feeds that a feed is following. ### Parameters #### `followers` and `following` - **params** (object) - Optional - Parameters for pagination. - **limit** (number) - Optional - The maximum number of results to return. - **offset** (number) - Optional - The offset for pagination. ### Request Example ```js user1.followers({ limit: '10', offset: '10' }); user1.following({ limit: '10', offset: '0' }); ``` ``` -------------------------------- ### List Followers and Following Source: https://github.com/getstream/stream-js/blob/main/README.md List the followers of a feed and the feeds that a feed is following, with pagination options. ```javascript // List followers, following user1.followers({ limit: '10', offset: '10' }); user1.following({ limit: '10', offset: '0' }); ``` -------------------------------- ### Realtime (Faye): Cancel Subscription Source: https://github.com/getstream/stream-js/blob/main/README.md Demonstrates how to cancel a Faye subscription using the `cancel()` method on the subscription object, effectively removing the listener for feed changes. ```javascript // To cancel a subscription you can call cancel on the // object returned from a subscribe call. // This will remove the listener from this channel. subscription.cancel(); ``` -------------------------------- ### Create Redirect URLs (Server-Side) Source: https://github.com/getstream/stream-js/blob/main/README.md Create redirect URLs with associated events (impressions, engagements). This operation is server-side only. ```javascript // ⚠️ server-side only! // Create redirect urls impression = { content_list: ['tweet:1', 'tweet:2', 'tweet:3'], user_data: 'tommaso', location: 'email', feed_id: 'user:global', }; engagement = { content: 'tweet:2', label: 'click', position: 1, user_data: 'tommaso', location: 'email', feed_id: 'user:global', }; events = [impression, engagement]; redirectUrl = client.createRedirectUrl('http://google.com', 'user_id', events); ``` -------------------------------- ### Create Redirect URL Source: https://github.com/getstream/stream-js/blob/main/README.md Generate a redirect URL with associated events. ```APIDOC ## Create Redirect URL ### Description Creates a redirect URL that can track user engagement events. ### Method `client.createRedirectUrl(url, userId, events)` ### Parameters - **url** (string) - The target URL to redirect to. - **userId** (string) - The ID of the user associated with the redirect. - **events** (array) - An array of event objects to associate with the redirect. - **impression** (object) - An impression event. - **content_list** (array) - List of content IDs. - **user_data** (string) - User-specific data. - **location** (string) - The location where the impression occurred. - **feed_id** (string) - The ID of the feed associated with the impression. - **engagement** (object) - An engagement event. - **content** (string) - The content ID engaged with. - **label** (string) - The type of engagement (e.g., 'click'). - **position** (number) - The position of the content. - **user_data** (string) - User-specific data. - **location** (string) - The location where the engagement occurred. - **feed_id** (string) - The ID of the feed associated with the engagement. ### Request Example ```js // Create redirect urls impression = { content_list: ['tweet:1', 'tweet:2', 'tweet:3'], user_data: 'tommaso', location: 'email', feed_id: 'user:global', }; engagement = { content: 'tweet:2', label: 'click', position: 1, user_data: 'tommaso', location: 'email', feed_id: 'user:global', }; events = [impression, engagement]; redirectUrl = client.createRedirectUrl('http://google.com', 'user_id', events); ``` ``` -------------------------------- ### Realtime (Faye): Subscribe to Feed Changes Source: https://github.com/getstream/stream-js/blob/main/README.md Connects to Stream using an API key and user token, then subscribes to changes on a specific user feed. The callback function is executed whenever the feed is updated. ```javascript const { connect } = require('getstream'); // ⚠️ userToken is generated server-side (see previous section) const client = connect('YOUR_API_KEY', userToken, 'APP_ID'); const user1 = client.feed('user', '1'); // subscribe to the changes const subscription = user1.subscribe(function (data) { console.log(data); }); // now whenever something changes to the feed user 1 // the callback will be called ``` -------------------------------- ### TypeScript: Fetch Timeline Feed with Options Source: https://github.com/getstream/stream-js/blob/main/README.md Fetches activities from the 'timeline' feed, including options to retrieve own children and reactions. Maps results to extract specific activity data. ```typescript // notification: StreamFeed const timeline = client.feed('timeline', 'feed_id'); timeline.get({ withOwnChildren: true, withOwnReactions: true }).then((response) => { // response: FeedAPIResponse if (response.next !== '') return response.next; return (response.results as EnrichedActivity[]).map((activity) => { return activity.id + activity.text + (activity.actor as User2Type).name; }); }); ``` -------------------------------- ### Add Multiple Activities Source: https://github.com/getstream/stream-js/blob/main/README.md Add multiple activities to a feed in a single request. ```javascript // adding multiple activities activities = [ { actor: 1, verb: 'tweet', object: 1 }, { actor: 2, verb: 'tweet', object: 3 }, ]; user1.addActivities(activities); ``` -------------------------------- ### Follow Operations Source: https://github.com/getstream/stream-js/blob/main/README.md Manage follow relationships between feeds. ```APIDOC ## Follow Operations ### Description Methods for managing follow relationships between feeds. ### Methods #### Follow - `feed.follow(targetFeedType, targetFeedId, [params])`: Makes the current feed follow another feed. - `client.followMany(follows)`: (Server-side only) Creates multiple follow relationships in a single request. #### Unfollow - `feed.unfollow(targetFeedType, targetFeedId, [params])`: Stops the current feed from following another feed. ### Parameters #### `follow` and `unfollow` - **targetFeedType** (string) - The type of the target feed (e.g., 'flat', 'user'). - **targetFeedId** (string) - The ID of the target feed. - **params** (object) - Optional - Additional parameters. - **keepHistory** (boolean) - Optional - If true, previously published activities from the target feed are kept. - **limit** (number) - Optional - Used when following to specify the number of past activities to copy (0 means no history). #### `followMany` (Server-side only) - **follows** (array) - An array of follow relationship objects. - **source** (string) - The source feed ID. - **target** (string) - The target feed ID. ### Request Example ```js // Follow another feed user1.follow('flat', '42'); // Stop following another feed user1.unfollow('flat', '42'); // Stop following another feed while keeping previously published activities user1.unfollow('flat', '42', { keepHistory: true }); // Follow another feed without copying the history user1.follow('flat', '42', { limit: 0 }); // Batch create follow relations (server-side only) follows = [ { source: 'flat:1', target: 'user:1' }, { source: 'flat:1', target: 'user:2' }, { source: 'flat:1', target: 'user:3' }, ]; client.followMany(follows); ``` ``` -------------------------------- ### TypeScript: Define Custom Stream Types and Connect Source: https://github.com/getstream/stream-js/blob/main/README.md Defines custom types for users, activities, collections, and reactions to be used with the Stream client. Connects to the Stream API using provided credentials. ```typescript import { connect, UR, EnrichedActivity, NotificationActivity } from 'getstream'; type User1Type = { name: string; username: string; image?: string }; type User2Type = { name: string; avatar?: string }; type ActivityType = { attachments: string[]; text: string }; type Collection1Type = { cid: string; rating?: number }; type Collection2Type = { branch: number; location: string }; type ReactionType = { text: string }; type ChildReactionType = { text?: string }; type StreamType = { userType: User1Type | User2Type, activityType: ActivityType, collectionType: Collection1Type | Collection2Type, reactionType: ReactionType, childReactionType: ChildReactionType, personalizationType: UR, } const client = connect('api_key', 'secret!', 'app_id'); ``` -------------------------------- ### Include Stream JS SDK via JS Deliver CDN Source: https://github.com/getstream/stream-js/blob/main/README.md Include the Stream JavaScript SDK directly in your HTML using a CDN link. It is recommended to pin a specific version to avoid breaking changes. ```html ``` ```html ``` -------------------------------- ### Update Activity Targets Source: https://github.com/getstream/stream-js/blob/main/README.md Update the 'to' fields on an existing activity. ```APIDOC ## Update Activity Targets ### Description Updates the 'to' fields (targets) on an existing activity. This method is not intended for browser environments. ### Method `client.feed(feedGroup, userId).updateActivityToTargets(foreignId, timestamp, newTargets, addedTargets, removedTargets)` ### Parameters - **foreignId** (string) - The foreign ID of the activity. - **timestamp** (string) - The timestamp of the activity. - **newTargets** (array) - Optional - An array of feed IDs that will replace all existing targets. - **addedTargets** (array) - Optional - An array of feed IDs to add to the existing targets. - **removedTargets** (array) - Optional - An array of feed IDs to remove from the existing targets. *Note: Provide either `newTargets` OR `addedTargets` and `removedTargets`.* ### Request Example ```js // Replace all targets client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, ['feed:1234']); // Add targets client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, ['feed:1234']); // Remove targets client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, null, ['feed:1234']); ``` ``` -------------------------------- ### Partial Activity Update (Server-Side) Source: https://github.com/getstream/stream-js/blob/main/README.md Update parts of an existing activity by ID or by a combination of foreign ID and time. This operation is server-side only. ```javascript // Updating parts of an activity set = { 'product.price': 19.99, shares: { facebook: '...', twitter: '...', }, }; unset = ['daily_likes', 'popularity']; // ...by ID client.activityPartialUpdate({ id: '54a60c1e-4ee3-494b-a1e3-50c06acb5ed4', set: set, unset: unset, }); // ...or by combination of foreign ID and time client.activityPartialUpdate({ foreign_id: 'product:123', time: '2016-11-10T13:20:00.000000', set: set, unset: unset, }); ``` -------------------------------- ### Add Activity to Many Feeds (Server-Side) Source: https://github.com/getstream/stream-js/blob/main/README.md Add a single activity to multiple feeds simultaneously. This operation is server-side only. ```javascript // adding one activity to multiple feeds feeds = ['flat:1', 'flat:2', 'flat:3', 'flat:4']; activity = { actor: 'User:2', verb: 'pin', object: 'Place:42', target: 'Board:1', }; // ⚠️ server-side only! client.addToMany(activity, feeds); ``` -------------------------------- ### Update Activity Targets (Server-Side) Source: https://github.com/getstream/stream-js/blob/main/README.md Update the 'to' fields on an existing activity. This method is not intended for browser environments. ```javascript // update the 'to' fields on an existing activity // client.feed("user", "ken").function (foreign_id, timestamp, new_targets, added_targets, removed_targets) // new_targets, added_targets, and removed_targets are all arrays of feed IDs // either provide only the `new_targets` parameter (will replace all targets on the activity), // OR provide the added_targets and removed_targets parameters // NOTE - the updateActivityToTargets method is not intended to be used in a browser environment. client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, ['feed:1234']); client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, ['feed:1234']); client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, null, ['feed:1234']); ``` -------------------------------- ### TypeScript: Fetch Notification Feed with Mark Options Source: https://github.com/getstream/stream-js/blob/main/README.md Fetches notifications from the 'notification' feed, marking them as read and seen. Maps results to extract specific notification group data. ```typescript // notification: StreamFeed const notification = client.feed('notification', 'feed_id'); notification.get({ mark_read: true, mark_seen: true }).then((response) => { // response: FeedAPIResponse if (response.unread || response.unseen) return response.next; return (response.results as NotificationActivity[]).map((activityGroup) => { const { activities, id, verb, activity_count, actor_count } = activityGroup; return activities[0].text + id + actor_count + activity_count + verb; }); }); ``` -------------------------------- ### Server-side Token Generation for User Authentication Source: https://github.com/getstream/stream-js/blob/main/README.md Generate a user token on the server-side for authenticating users in client-side applications. This is crucial for security as it prevents exposing your API secret. ```javascript import { connect } from 'getstream'; // or if you are on commonjs const { connect } = require('getstream'); // Instantiate a new client (server side) const client = connect('YOUR_API_KEY', 'API_KEY_SECRET'); // Optionally supply the app identifier and an options object specifying the data center to use and timeout for requests (15s) const client = connect('YOUR_API_KEY', 'API_KEY_SECRET', 'APP_ID', { location: 'us-east', timeout: 15000 }); // Create a token for user with id "the-user-id" const userToken = client.createUserToken('the-user-id'); ``` -------------------------------- ### Flag User Source: https://github.com/getstream/stream-js/blob/main/README.md Flag a user for moderation. ```APIDOC ## Flag User ### Description Flags a user for moderation with a specified reason. ### Methods - `client.flagUser(userId, params)`: Flags a user globally. - `user.flag(params)`: Flags a user via the user object. ### Parameters - **userId** (string) - The ID of the user to flag. - **params** (object) - Optional - Parameters for flagging. - **reason** (string) - The reason for flagging the user. ### Request Example ```js // Flag a user for moderation client.flagUser('suspicious-user-123', { reason: 'spam' }); client.flagUser('bad-actor-456', { reason: 'inappropriate_content' }); // Or using the user object method const user = client.user('suspicious-user-123'); user.flag({ reason: 'spam' }); user.flag({ reason: 'inappropriate_content' }); ``` ``` -------------------------------- ### Add Activity to Specific Feeds Source: https://github.com/getstream/stream-js/blob/main/README.md Add an activity and specify additional feeds to push it to using the 'to' parameter, useful for notification feeds. ```javascript // specifying additional feeds to push the activity to using the to param // especially useful for notification style feeds to = ['user:2', 'user:3']; activity = { to: to, actor: 1, verb: 'tweet', object: 1, foreign_id: 'tweet:1', }; user1.addActivity(activity); ``` -------------------------------- ### Bypass Browser Security Check for API Client Source: https://github.com/getstream/stream-js/blob/main/README.md If running backend code in trusted environments like Google Cloud, you can bypass the browser security check by setting `browser: false` in the options when connecting the client. ```javascript const client = connect('YOUR_API_KEY', 'API_KEY_SECRET', 'APP_ID', { browser: false }); ``` -------------------------------- ### Follow and Unfollow Feeds Source: https://github.com/getstream/stream-js/blob/main/README.md Follow and unfollow other feeds. Options include keeping history or limiting copied activities. ```javascript // Follow another feed user1.follow('flat', '42'); // Stop following another feed user1.unfollow('flat', '42'); // Stop following another feed while keeping previously published activities // from that feed user1.unfollow('flat', '42', { keepHistory: true }); // Follow another feed without copying the history user1.follow('flat', '42', { limit: 0 }); ``` -------------------------------- ### Flag User for Moderation Source: https://github.com/getstream/stream-js/blob/main/README.md Flag a user for moderation with a specified reason, either directly or via the user object method. ```javascript // Flag a user for moderation client.flagUser('suspicious-user-123', { reason: 'spam' }); client.flagUser('bad-actor-456', { reason: 'inappropriate_content' }); // Or using the user object method const user = client.user('suspicious-user-123'); user.flag({ reason: 'spam' }); user.flag({ reason: 'inappropriate_content' }); ``` -------------------------------- ### Add Single Activity Source: https://github.com/getstream/stream-js/blob/main/README.md Add a single activity to a feed. Activities can be simple or complex, including nested objects and timestamps. ```javascript // Create a new activity activity = { actor: 1, verb: 'tweet', object: 1, foreign_id: 'tweet:1' }; user1.addActivity(activity); // Create a bit more complex activity activity = { actor: 1, verb: 'run', object: 1, foreign_id: 'run:1', course: { name: 'Golden Gate park', distance: 10 }, participants: ['Thierry', 'Tommaso'], started_at: new Date(), }; user1.addActivity(activity); ``` -------------------------------- ### TypeScript: Type Guard for Union Types Source: https://github.com/getstream/stream-js/blob/main/README.md Implements a type guard function to differentiate between union types, specifically for user types, enabling type-safe access to properties. ```typescript // if you have different union types like "User1Type | User2Type" you can use type guards as follow: // https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types function isUser1Type(user: User1Type | User2Type): user is User1Type { return (user as User1Type).username !== undefined; } ``` -------------------------------- ### Remove Activity Source: https://github.com/getstream/stream-js/blob/main/README.md Remove an activity from a feed either by its ID or its foreign ID. ```javascript // Remove an activity by its id user1.removeActivity('e561de8f-00f1-11e4-b400-0cc47a024be0'); // or remove by the foreign id user1.removeActivity({ foreign_id: 'tweet:1' }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.