Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

endpoint enhancement: Add endpoint for mark messages. #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ await zulip.callEndpoint('/messages', 'POST', params);
| `zulip.users.me.subscriptions.remove()` | DELETE `/users/me/subscriptions` | remove subscriptions. |
| `zulip.users.me.pointer.update()` | POST `users/me/pointer` | updates the pointer for the user, for moving the home view. Accepts a message id. This has the side effect of marking some messages as read. Will not return success if the message id is invalid. Will always succeed if the id is less than the current value of the pointer (the id of the last message read). |
| `zulip.server.settings()` | GET `/server_settings` | returns a dictionary of server settings. |
| `zulip.filters.retrieve()` | GET `realm/filters` | return a list of filters in a realm |
| `zulip.filters.retrieve()` | GET `realm/filters` | return a list of filters in a realm. |
| `zulip.mark.all()` | POST `/mark_all_as_read ` | Marks all of the current user's unread messages as read. |
| `zulip.mark.stream()` | POST `/mark_stream_as_read ` | Mark all the unread messages in a stream as read. Accepts a params object with `stream_id`. |
| `zulip.mark.topic()` | POST `/mark_topic_as_read ` | Mark all the unread messages in a topic as read. Accepts a params object with `stream_id` and `topic_name`. |

# Testing

Expand Down
25 changes: 25 additions & 0 deletions examples/mark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const zulip = require('../lib');

const config = {
username: process.env.ZULIP_USERNAME,
apiKey: process.env.ZULIP_API_KEY,
realm: process.env.ZULIP_REALM,
};

(async () => {
const z = await zulip(config);
// Mark all messages as read
console.log(await z.mark.all());

// Mark all the unread messages in a stream as read
const streamParams = {
stream_id: 1,
};
console.log(await z.mark.stream(streamParams));
// Mark all the unread messages in a topic as read
const topicParams = {
stream_id: 1,
topic_name: 'Testing zulip-js',
};
console.log(await z.mark.topic(topicParams));
})();
16 changes: 9 additions & 7 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ async function api(baseUrl, config, method, params) {
const options = { method, headers: { Authorization: authHeader } };
if (method === 'POST') {
options.body = new helper.FormData();
Object.keys(params).forEach((key) => {
let data = params[key];
if (Array.isArray(data)) {
data = JSON.stringify(data);
}
options.body.append(key, data);
});
if (params) {
Object.keys(params).forEach((key) => {
let data = params[key];
if (Array.isArray(data)) {
data = JSON.stringify(data);
}
options.body.append(key, data);
});
}
} else if (params) {
Object.entries(params).forEach(([key, value]) => {
url.searchParams.append(key, value);
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const reactions = require('./resources/reactions');
const server = require('./resources/server');
const filters = require('./resources/filters');
const eventsWapper = require('./events_wrapper');
const mark = require('./resources/mark');

function getCallEndpoint(config) {
return function callEndpoint(endpoint, method = 'GET', params) {
Expand Down Expand Up @@ -43,6 +44,7 @@ function resources(config) {
server: server(config),
filters: filters(config),
callOnEachEvent: eventsWapper(config),
mark: mark(config),
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/resources/mark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const api = require('../api');

function mark(config) {
return {
all: (params) => {
const url = `${config.apiURL}/mark_all_as_read`;
return api(url, config, 'POST', params);
},
stream: (params) => {
const url = `${config.apiURL}/mark_stream_as_read`;
return api(url, config, 'POST', params);
},
topic: (params) => {
const url = `${config.apiURL}/mark_topic_as_read`;
return api(url, config, 'POST', params);
},
};
}

module.exports = mark;
59 changes: 59 additions & 0 deletions test/resources/mark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const chai = require('chai');
const mark = require('../../lib/resources/mark');
const common = require('../common');

chai.should();

describe('Mark', () => {
it('should mark all messages as read', async () => {
const validator = (url, options) => {
url.should.contain(`${common.config.apiURL}/mark_all_as_read`);
Object.keys(options.body.data).length.should.equal(0);
options.method.should.be.equal('POST');
};
const output = {
msg: '',
result: 'success',
};
common.stubNetwork(validator, output);
const data = await mark(common.config).all();
data.should.have.property('result', 'success');
});

it('should mark all messages in a stream as read', async () => {
const paramsStream = {
stream_id: 15,
};
const validator = (url, options) => {
url.should.contain(`${common.config.apiURL}/mark_stream_as_read`);
Object.keys(options.body.data).length.should.equal(1);
options.method.should.be.equal('POST');
};
const outputStream = {
msg: '',
result: 'success',
};
common.stubNetwork(validator, outputStream);
const dataStream = await mark(common.config).stream(paramsStream);
dataStream.should.have.property('result', 'success');
});

it('should mark all messages in a topic as read', async () => {
const paramsTopic = {
stream_id: 15,
topic_name: 'Denmark1',
};
const validator = (url, options) => {
url.should.contain(`${common.config.apiURL}/mark_topic_as_read`);
Object.keys(options.body.data).length.should.equal(2);
options.method.should.be.equal('POST');
};
const outputTopic = {
msg: '',
result: 'success',
};
common.stubNetwork(validator, outputTopic);
const dataTopic = await mark(common.config).topic(paramsTopic);
dataTopic.should.have.property('result', 'success');
});
});