2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-02 02:22:10 +02:00
|
|
|
/* eslint-disable arrow-body-style */
|
|
|
|
/*
|
|
|
|
This file makes use of functional comments in a way that makes the
|
|
|
|
code confusing with the arrow-body-style lint rule applied.
|
|
|
|
*/
|
|
|
|
|
2020-05-17 11:37:18 +02:00
|
|
|
const ExamplesHandler = function () {
|
|
|
|
const config = {
|
|
|
|
username: process.env.ZULIP_USERNAME,
|
|
|
|
apiKey: process.env.ZULIP_API_KEY,
|
|
|
|
realm: process.env.ZULIP_REALM,
|
|
|
|
};
|
2020-05-19 18:39:17 +02:00
|
|
|
const examples = {};
|
|
|
|
const response_data = [];
|
2020-05-17 11:37:18 +02:00
|
|
|
|
|
|
|
const make_result_object = (example, result, count = false) => {
|
|
|
|
const name = count !== false ? `${example.name}_${count}` : example.name;
|
|
|
|
return {
|
|
|
|
name,
|
2020-07-15 01:29:15 +02:00
|
|
|
endpoint: example.endpoint.split(":")[0],
|
|
|
|
method: example.endpoint.split(":")[1],
|
2020-05-17 11:37:18 +02:00
|
|
|
status_code: example.status_code.toString(),
|
|
|
|
result,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-19 18:39:17 +02:00
|
|
|
const generate_validation_data = async (client, example) => {
|
|
|
|
const result = await example.func(client);
|
|
|
|
if (Array.isArray(result)) {
|
|
|
|
// Handle special cases where some examples make
|
|
|
|
// more than 1 API requests.
|
|
|
|
result.forEach((r, index) => {
|
|
|
|
response_data.push(make_result_object(example, r, index));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
response_data.push(make_result_object(example, result));
|
2020-05-17 11:37:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const main = async () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const Zulip = require("zulip-js");
|
2020-05-19 18:39:17 +02:00
|
|
|
const client = await Zulip(config);
|
|
|
|
|
|
|
|
await generate_validation_data(client, examples.send_message);
|
|
|
|
await generate_validation_data(client, examples.create_user);
|
2020-06-21 20:43:09 +02:00
|
|
|
await generate_validation_data(client, examples.get_custom_emoji);
|
2020-06-21 20:51:21 +02:00
|
|
|
await generate_validation_data(client, examples.delete_queue);
|
2020-06-21 21:46:53 +02:00
|
|
|
await generate_validation_data(client, examples.get_messages);
|
2020-06-21 21:58:07 +02:00
|
|
|
await generate_validation_data(client, examples.get_own_user);
|
2020-06-21 22:25:48 +02:00
|
|
|
await generate_validation_data(client, examples.get_stream_id);
|
2020-06-21 22:32:42 +02:00
|
|
|
await generate_validation_data(client, examples.get_stream_topics);
|
2020-06-21 22:41:13 +02:00
|
|
|
await generate_validation_data(client, examples.get_subscriptions);
|
2020-06-21 22:51:47 +02:00
|
|
|
await generate_validation_data(client, examples.get_users);
|
2020-06-21 22:59:39 +02:00
|
|
|
await generate_validation_data(client, examples.register_queue);
|
2020-06-21 23:18:59 +02:00
|
|
|
await generate_validation_data(client, examples.render_message);
|
2020-06-21 23:25:38 +02:00
|
|
|
await generate_validation_data(client, examples.set_typing_status);
|
2020-06-21 23:37:53 +02:00
|
|
|
await generate_validation_data(client, examples.add_subscriptions);
|
2020-06-21 23:46:13 +02:00
|
|
|
await generate_validation_data(client, examples.remove_subscriptions);
|
2020-06-22 00:13:00 +02:00
|
|
|
await generate_validation_data(client, examples.update_message_flags);
|
2020-06-22 00:31:56 +02:00
|
|
|
await generate_validation_data(client, examples.update_message);
|
2020-06-29 18:14:47 +02:00
|
|
|
await generate_validation_data(client, examples.get_events);
|
2020-06-22 00:45:16 +02:00
|
|
|
await generate_validation_data(client, examples.get_streams);
|
2020-05-19 18:39:17 +02:00
|
|
|
|
|
|
|
console.log(JSON.stringify(response_data));
|
|
|
|
return;
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const add_example = (name, endpoint, status_code, func) => {
|
2020-05-19 18:39:17 +02:00
|
|
|
const example = {
|
2020-05-17 11:37:18 +02:00
|
|
|
name,
|
|
|
|
endpoint,
|
|
|
|
status_code,
|
|
|
|
func,
|
2020-05-19 18:39:17 +02:00
|
|
|
};
|
|
|
|
examples[name] = example;
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
main,
|
|
|
|
add_example,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-17 12:26:12 +02:00
|
|
|
const {main, add_example} = ExamplesHandler();
|
2020-05-17 11:37:18 +02:00
|
|
|
|
2020-07-10 00:17:33 +02:00
|
|
|
const send_test_message = async (client) => {
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
to: "Verona",
|
|
|
|
type: "stream",
|
|
|
|
topic: "Castle",
|
2020-07-10 00:17:33 +02:00
|
|
|
// Use some random text for easier debugging if needed. We don't
|
|
|
|
// depend on the content of these messages for the tests.
|
|
|
|
content: `Random test message ${Math.random()}`,
|
|
|
|
};
|
|
|
|
const result = await client.messages.send(params);
|
|
|
|
// Only return the message id.
|
|
|
|
return result.id;
|
|
|
|
};
|
|
|
|
|
2020-05-17 11:37:18 +02:00
|
|
|
// Declare all the examples below.
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("send_message", "/messages:post", 200, async (client) => {
|
2020-05-17 12:29:49 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Send a stream message
|
|
|
|
let params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
to: "social",
|
|
|
|
type: "stream",
|
|
|
|
topic: "Castle",
|
|
|
|
content: "I come not, friends, to steal away your hearts.",
|
2020-05-17 12:29:49 +02:00
|
|
|
};
|
|
|
|
const result_1 = await client.messages.send(params);
|
|
|
|
// {code_example|end}
|
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// Send a private message
|
|
|
|
const user_id = 9;
|
|
|
|
params = {
|
|
|
|
to: [user_id],
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
|
|
|
content: "With mirth and laughter let old wrinkles come.",
|
2020-05-17 12:29:49 +02:00
|
|
|
};
|
|
|
|
const result_2 = await client.messages.send(params);
|
|
|
|
// {code_example|end}
|
|
|
|
return [result_1, result_2];
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("create_user", "/users:post", 200, async (client) => {
|
2020-05-17 12:26:12 +02:00
|
|
|
// {code_example|start}
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "notnewbie@zulip.com",
|
|
|
|
password: "temp",
|
|
|
|
full_name: "New User",
|
2020-05-17 12:26:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return await client.users.create(params);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_custom_emoji", "/realm/emoji:get", 200, async (client) => {
|
2020-06-21 20:43:09 +02:00
|
|
|
// {code_example|start}
|
|
|
|
return await client.emojis.retrieve();
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("delete_queue", "/events:delete", 200, async (client) => {
|
2020-06-21 20:51:21 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Register a queue
|
|
|
|
const queueParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
event_types: ["message"],
|
2020-06-21 20:51:21 +02:00
|
|
|
};
|
|
|
|
const res = await client.queues.register(queueParams);
|
|
|
|
|
|
|
|
// Delete a queue
|
|
|
|
const deregisterParams = {
|
|
|
|
queue_id: res.queue_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
return await client.queues.deregister(deregisterParams);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_messages", "/messages:get", 200, async (client) => {
|
2020-06-21 21:46:53 +02:00
|
|
|
// {code_example|start}
|
|
|
|
const readParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
anchor: "newest",
|
2020-06-21 21:46:53 +02:00
|
|
|
num_before: 100,
|
|
|
|
num_after: 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
narrow: [
|
|
|
|
{operator: "sender", operand: "iago@zulip.com"},
|
|
|
|
{operator: "stream", operand: "Verona"},
|
|
|
|
],
|
2020-06-21 21:46:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get the 100 last messages sent by "iago@zulip.com" to the stream "Verona"
|
|
|
|
return await client.messages.retrieve(readParams);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_own_user", "/users/me:get", 200, async (client) => {
|
2020-06-21 21:58:07 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get the profile of the user/bot that requests this endpoint,
|
|
|
|
// which is `client` in this case:
|
|
|
|
return await client.users.me.getProfile();
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_stream_id", "/get_stream_id:get", 200, async (client) => {
|
2020-06-21 22:25:48 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get the ID of a given stream
|
2020-07-15 01:29:15 +02:00
|
|
|
return await client.streams.getStreamId("Denmark");
|
2020-06-21 22:25:48 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_stream_topics", "/users/me/{stream_id}/topics:get", 200, async (client) => {
|
2020-06-21 22:32:42 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all the topics in stream with ID 1
|
2020-07-16 22:40:18 +02:00
|
|
|
return client.streams.topics.retrieve({stream_id: 1});
|
2020-06-21 22:32:42 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_subscriptions", "/users/me/subscriptions:get", 200, async (client) => {
|
2020-06-21 22:41:13 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all streams that the user is subscribed to
|
|
|
|
return await client.streams.subscriptions.retrieve();
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_users", "/users:get", 200, async (client) => {
|
2020-06-21 22:51:47 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all users in the realm
|
|
|
|
const result_1 = await client.users.retrieve();
|
|
|
|
// {code_example|end}
|
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// You may pass the `client_gravatar` query parameter as follows:
|
|
|
|
const result_2 = await client.users.retrieve({client_gravatar: true});
|
|
|
|
// {code_example|end}
|
|
|
|
return [result_1, result_2];
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("register_queue", "/register:post", 200, async (client) => {
|
2020-06-21 22:59:39 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Register a queue
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
event_types: ["message"],
|
2020-06-21 22:59:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return await client.queues.register(params);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("render_message", "/messages/render:post", 200, async (client) => {
|
2020-06-21 23:18:59 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Render a message
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "**foo**",
|
2020-06-21 23:18:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return await client.messages.render(params);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("set_typing_status", "/typing:post", 200, async (client) => {
|
2020-06-21 23:25:38 +02:00
|
|
|
// {code_example|start}
|
|
|
|
const user_id1 = 9;
|
|
|
|
const user_id2 = 10;
|
|
|
|
|
|
|
|
const typingParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
op: "start",
|
2020-06-21 23:25:38 +02:00
|
|
|
to: [user_id1, user_id2],
|
|
|
|
};
|
|
|
|
|
|
|
|
// The user has started to type in the group PM with Iago and Polonius
|
|
|
|
return await client.typing.send(typingParams);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("add_subscriptions", "/users/me/subscriptions:post", 200, async (client) => {
|
2020-06-21 23:37:53 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Subscribe to the streams "Verona" and "Denmark"
|
|
|
|
const meParams = {
|
2020-07-15 00:34:28 +02:00
|
|
|
subscriptions: JSON.stringify([{name: "Verona"}, {name: "Denmark"}]),
|
2020-06-21 23:37:53 +02:00
|
|
|
};
|
|
|
|
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 = {
|
2020-07-15 00:34:28 +02:00
|
|
|
subscriptions: JSON.stringify([{name: "Verona"}, {name: "Denmark"}]),
|
2020-06-21 23:37:53 +02:00
|
|
|
principals: JSON.stringify([user_id]),
|
|
|
|
};
|
|
|
|
const result_2 = await client.users.me.subscriptions.add(anotherUserParams);
|
|
|
|
// {code_example|end}
|
|
|
|
return [result_1, result_2];
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("remove_subscriptions", "/users/me/subscriptions:delete", 200, async (client) => {
|
2020-06-21 23:46:13 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Unsubscribe from the stream "Denmark"
|
|
|
|
const meParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
subscriptions: JSON.stringify(["Denmark"]),
|
2020-06-21 23:46:13 +02:00
|
|
|
};
|
|
|
|
const result_1 = await client.users.me.subscriptions.remove(meParams);
|
|
|
|
// {code_example|end}
|
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
const user_id = 7;
|
|
|
|
// Unsubscribe Zoe from the stream "Denmark"
|
|
|
|
const zoeParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
subscriptions: JSON.stringify(["Denmark"]),
|
2020-06-21 23:46:13 +02:00
|
|
|
principals: JSON.stringify([user_id]),
|
|
|
|
};
|
|
|
|
const result_2 = await client.users.me.subscriptions.remove(zoeParams);
|
|
|
|
// {code_example|end}
|
|
|
|
return [result_1, result_2];
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("update_message_flags", "/messages/flags:post", 200, async (client) => {
|
2020-07-10 00:17:33 +02:00
|
|
|
// Send 3 messages to run this example on
|
|
|
|
const message_ids = [...Array(3)];
|
|
|
|
for (let i = 0; i < message_ids.length; i = i + 1) {
|
|
|
|
message_ids[i] = await send_test_message(client);
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:13:00 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Add the "read" flag to the messages with IDs in "message_ids"
|
|
|
|
const addflag = {
|
|
|
|
messages: message_ids,
|
2020-07-15 01:29:15 +02:00
|
|
|
flag: "read",
|
2020-06-22 00:13:00 +02:00
|
|
|
};
|
|
|
|
const result_1 = await client.messages.flags.add(addflag);
|
|
|
|
// {code_example|end}
|
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// Remove the "starred" flag from the messages with IDs in "message_ids"
|
|
|
|
const removeflag = {
|
|
|
|
messages: message_ids,
|
2020-07-15 01:29:15 +02:00
|
|
|
flag: "starred",
|
2020-06-22 00:13:00 +02:00
|
|
|
};
|
|
|
|
const result_2 = await client.messages.flags.remove(removeflag);
|
|
|
|
// {code_example|end}
|
|
|
|
return [result_1, result_2];
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("update_message", "/messages/{message_id}:patch", 200, async (client) => {
|
2020-06-22 00:31:56 +02:00
|
|
|
const request = {
|
2020-07-15 01:29:15 +02:00
|
|
|
to: "Denmark",
|
|
|
|
type: "stream",
|
|
|
|
topic: "Castle",
|
|
|
|
content: "I come not, friends, to steal away your hearts.",
|
2020-06-22 00:31:56 +02:00
|
|
|
};
|
|
|
|
const result = await client.messages.send(request);
|
|
|
|
const message_id = result.id;
|
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// Update a message with the given "message_id"
|
|
|
|
const params = {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "New Content",
|
2020-06-22 00:31:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return await client.messages.update(params);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_events", "/events:get", 200, async (client) => {
|
2020-06-29 18:14:47 +02:00
|
|
|
// Register queue to receive messages for user.
|
|
|
|
const queueParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
event_types: ["message"],
|
2020-06-29 18:14:47 +02:00
|
|
|
};
|
|
|
|
const res = await client.queues.register(queueParams);
|
|
|
|
const queue_id = res.queue_id;
|
|
|
|
// For setup, we send a message to ensure there are events in the
|
|
|
|
// queue; this lets the automated tests complete quickly.
|
2020-07-10 00:17:33 +02:00
|
|
|
await send_test_message(client);
|
2020-06-29 18:14:47 +02:00
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// Retrieve events from a queue with given "queue_id"
|
|
|
|
const eventParams = {
|
2020-07-20 22:18:43 +02:00
|
|
|
queue_id,
|
2020-06-29 18:14:47 +02:00
|
|
|
last_event_id: -1,
|
|
|
|
};
|
|
|
|
|
|
|
|
return await client.events.retrieve(eventParams);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
add_example("get_streams", "/streams:get", 200, async (client) => {
|
2020-06-22 00:45:16 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all streams that the user has access to
|
|
|
|
return await client.streams.retrieve();
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-05-17 11:37:18 +02:00
|
|
|
main();
|