Skip to content

Commit 2a909b7

Browse files
endpoint enhancement: Add endpoint for mark messages.
Implement endpoints for mark all messages, mark messages in stream and mark messages in topic.
1 parent db9b0e4 commit 2a909b7

File tree

6 files changed

+120
-9
lines changed

6 files changed

+120
-9
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ await zulip.callEndpoint('/messages', 'POST', params);
130130
| `zulip.users.me.subscriptions.remove()` | DELETE `/users/me/subscriptions` | remove subscriptions. |
131131
| `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). |
132132
| `zulip.server.settings()` | GET `/server_settings` | returns a dictionary of server settings. |
133-
| `zulip.filters.retrieve()` | GET `realm/filters` | return a list of filters in a realm |
133+
| `zulip.filters.retrieve()` | GET `realm/filters` | return a list of filters in a realm. |
134+
| `zulip.mark.all()` | POST `/mark_all_as_read ` | Marks all of the current user's unread messages as read. |
135+
| `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`. |
136+
| `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`. |
134137

135138
# Testing
136139

examples/mark.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const zulip = require('../lib');
2+
3+
const config = {
4+
username: process.env.ZULIP_USERNAME,
5+
apiKey: process.env.ZULIP_API_KEY,
6+
realm: process.env.ZULIP_REALM,
7+
};
8+
9+
(async () => {
10+
const z = await zulip(config);
11+
// Mark all messages as read
12+
console.log(await z.mark.all());
13+
14+
// Mark all the unread messages in a stream as read
15+
const streamParams = {
16+
stream_id: 1,
17+
};
18+
console.log(await z.mark.stream(streamParams));
19+
// Mark all the unread messages in a topic as read
20+
const topicParams = {
21+
stream_id: 1,
22+
topic_name: 'Testing zulip-js',
23+
};
24+
console.log(await z.mark.topic(topicParams));
25+
})();

src/api.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ async function api(baseUrl, config, method, params) {
88
const authHeader = `Basic ${auth}`;
99
const options = { method, headers: { Authorization: authHeader } };
1010
if (method === 'POST') {
11-
options.body = new helper.FormData();
12-
Object.keys(params).forEach((key) => {
13-
let data = params[key];
14-
if (Array.isArray(data)) {
15-
data = JSON.stringify(data);
16-
}
17-
options.body.append(key, data);
18-
});
11+
if (params) {
12+
options.body = new helper.FormData();
13+
Object.keys(params).forEach((key) => {
14+
let data = params[key];
15+
if (Array.isArray(data)) {
16+
data = JSON.stringify(data);
17+
}
18+
options.body.append(key, data);
19+
});
20+
}
1921
} else if (params) {
2022
Object.entries(params).forEach(([key, value]) => {
2123
url.searchParams.append(key, value);

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const reactions = require('./resources/reactions');
1414
const server = require('./resources/server');
1515
const filters = require('./resources/filters');
1616
const eventsWapper = require('./events_wrapper');
17+
const mark = require('./resources/mark');
1718

1819
function getCallEndpoint(config) {
1920
return function callEndpoint(endpoint, method = 'GET', params) {
@@ -43,6 +44,7 @@ function resources(config) {
4344
server: server(config),
4445
filters: filters(config),
4546
callOnEachEvent: eventsWapper(config),
47+
mark: mark(config),
4648
};
4749
}
4850

src/resources/mark.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const api = require('../api');
2+
3+
function mark(config) {
4+
return {
5+
all: (params) => {
6+
const url = `${config.apiURL}/mark_all_as_read`;
7+
return api(url, config, 'POST', params);
8+
},
9+
stream: (params) => {
10+
const url = `${config.apiURL}/mark_stream_as_read`;
11+
return api(url, config, 'POST', params);
12+
},
13+
topic: (params) => {
14+
const url = `${config.apiURL}/mark_topic_as_read`;
15+
return api(url, config, 'POST', params);
16+
},
17+
};
18+
}
19+
20+
module.exports = mark;

test/resources/mark.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const chai = require('chai');
2+
const mark = require('../../lib/resources/mark');
3+
const common = require('../common');
4+
5+
chai.should();
6+
7+
describe('Mark', () => {
8+
// it('should mark all messages as read', async () => {
9+
// const validator = (url, options) => {
10+
// url.should.contain(`${common.config.apiURL}/mark_all_as_read`);
11+
// Object.keys(options.body.data).length.should.equal(2);
12+
// options.method.should.be.equal('POST');
13+
// };
14+
// const output = {
15+
// msg: '',
16+
// result: 'success',
17+
// };
18+
// common.stubNetwork(validator, output);
19+
// const data = await mark(common.config).all();
20+
// data.should.have.property('result', 'success');
21+
// });
22+
23+
// it('should mark stream as read', async () => {
24+
// const params = {
25+
// stream_id: 15,
26+
// };
27+
// const validator = (url, options) => {
28+
// url.should.contain(`${common.config.apiURL}/mark_stream_as_read`);
29+
// Object.keys(options.body.data).length.should.equal(2);
30+
// options.method.should.be.equal('POST');
31+
// };
32+
// const output = {
33+
// msg: '',
34+
// result: 'success',
35+
// };
36+
// common.stubNetwork(validator, output);
37+
// const dataStream = await mark(common.config).stream(params);
38+
// dataStream.should.have.property('result', 'success');
39+
// });
40+
41+
it('should mark topic as read', async () => {
42+
const params = {
43+
stream_id: 15,
44+
topic_name: 'Denmark1',
45+
};
46+
const validator = (url, options) => {
47+
url.should.contain(`${common.config.apiURL}/mark_topic_as_read`);
48+
Object.keys(options.body.data).length.should.equal(2);
49+
options.method.should.be.equal('POST');
50+
};
51+
const output = {
52+
msg: '',
53+
result: 'success',
54+
};
55+
common.stubNetwork(validator, output);
56+
const dataTopic = await mark(common.config).topic(params);
57+
dataTopic.should.have.property('result', 'success');
58+
});
59+
});

0 commit comments

Comments
 (0)