Skip to content

Commit

Permalink
endpoint enhancement: Add endpoint for mark messages.
Browse files Browse the repository at this point in the history
Implement endpoints for mark all messages,
mark messages in stream and
mark messages in topic.
  • Loading branch information
yasiruRathnayaka97 committed Apr 12, 2021
1 parent db9b0e4 commit 34cdc46
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
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');
});
});

0 comments on commit 34cdc46

Please sign in to comment.