mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	js_examples: Migrate and test add_subscriptions example.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							cf6e1de0de
						
					
				
				
					commit
					db303ccaef
				
			@@ -13,36 +13,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
More examples and documentation can be found [here](https://github.com/zulip/zulip-js).
 | 
					More examples and documentation can be found [here](https://github.com/zulip/zulip-js).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```js
 | 
					{generate_code_example(javascript)|/users/me/subscriptions:post|example}
 | 
				
			||||||
const zulip = require('zulip-js');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Pass the path to your zuliprc file here.
 | 
					 | 
				
			||||||
const config = {
 | 
					 | 
				
			||||||
    zuliprc: 'zuliprc',
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
zulip(config).then((client) => {
 | 
					 | 
				
			||||||
    // Subscribe to the streams "Verona" and "Denmark"
 | 
					 | 
				
			||||||
    const meParams = {
 | 
					 | 
				
			||||||
        subscriptions: JSON.stringify([
 | 
					 | 
				
			||||||
            {'name': 'Verona'},
 | 
					 | 
				
			||||||
            {'name': 'Denmark'}
 | 
					 | 
				
			||||||
        ]),
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    client.users.me.subscriptions.add(meParams).then(console.log);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // To subscribe another user to a stream, you may pass in
 | 
					 | 
				
			||||||
    // the `principals` parameter, like so:
 | 
					 | 
				
			||||||
    const anotherUserParams = {
 | 
					 | 
				
			||||||
        subscriptions: JSON.stringify([
 | 
					 | 
				
			||||||
            {'name': 'Verona'},
 | 
					 | 
				
			||||||
            {'name': 'Denmark'}
 | 
					 | 
				
			||||||
        ]),
 | 
					 | 
				
			||||||
        principals: JSON.stringify(['ZOE@zulip.org']),
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    client.users.me.subscriptions.add(anotherUserParams).then(console.log);
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
{tab|curl}
 | 
					{tab|curl}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,6 +48,7 @@ const ExamplesHandler = function () {
 | 
				
			|||||||
        await generate_validation_data(client, examples.register_queue);
 | 
					        await generate_validation_data(client, examples.register_queue);
 | 
				
			||||||
        await generate_validation_data(client, examples.render_message);
 | 
					        await generate_validation_data(client, examples.render_message);
 | 
				
			||||||
        await generate_validation_data(client, examples.set_typing_status);
 | 
					        await generate_validation_data(client, examples.set_typing_status);
 | 
				
			||||||
 | 
					        await generate_validation_data(client, examples.add_subscriptions);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        console.log(JSON.stringify(response_data));
 | 
					        console.log(JSON.stringify(response_data));
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
@@ -228,4 +229,32 @@ add_example('set_typing_status', '/typing:post', 200, async (client) => {
 | 
				
			|||||||
    // {code_example|end}
 | 
					    // {code_example|end}
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_example('add_subscriptions', '/users/me/subscriptions:post', 200, async (client) => {
 | 
				
			||||||
 | 
					    // {code_example|start}
 | 
				
			||||||
 | 
					    // Subscribe to the streams "Verona" and "Denmark"
 | 
				
			||||||
 | 
					    const meParams = {
 | 
				
			||||||
 | 
					        subscriptions: JSON.stringify([
 | 
				
			||||||
 | 
					            {name: 'Verona'},
 | 
				
			||||||
 | 
					            {name: 'Denmark'},
 | 
				
			||||||
 | 
					        ]),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    const result_1 = await client.users.me.subscriptions.add(meParams);
 | 
				
			||||||
 | 
					    // {code_example|end}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // {code_example|start}
 | 
				
			||||||
 | 
					    // To subscribe another user to a stream, you may pass in
 | 
				
			||||||
 | 
					    // the `principals` parameter, like so:
 | 
				
			||||||
 | 
					    const user_id = 7;
 | 
				
			||||||
 | 
					    const anotherUserParams = {
 | 
				
			||||||
 | 
					        subscriptions: JSON.stringify([
 | 
				
			||||||
 | 
					            {name: 'Verona'},
 | 
				
			||||||
 | 
					            {name: 'Denmark'},
 | 
				
			||||||
 | 
					        ]),
 | 
				
			||||||
 | 
					        principals: JSON.stringify([user_id]),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    const result_2 = await client.users.me.subscriptions.add(anotherUserParams);
 | 
				
			||||||
 | 
					    // {code_example|end}
 | 
				
			||||||
 | 
					    return [result_1, result_2];
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
main();
 | 
					main();
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user