From 34cdc462d00410b8652032dd00bceb3c3dbf657a Mon Sep 17 00:00:00 2001 From: yasiruRathnayaka97 Date: Mon, 12 Apr 2021 23:55:57 +0530 Subject: [PATCH] endpoint enhancement: Add endpoint for mark messages. Implement endpoints for mark all messages, mark messages in stream and mark messages in topic. --- README.md | 5 +++- examples/mark.js | 25 ++++++++++++++++++ src/api.js | 16 +++++++----- src/index.js | 2 ++ src/resources/mark.js | 20 ++++++++++++++ test/resources/mark.js | 59 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 examples/mark.js create mode 100644 src/resources/mark.js create mode 100644 test/resources/mark.js diff --git a/README.md b/README.md index 8d779c4..a11fcdf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/mark.js b/examples/mark.js new file mode 100644 index 0000000..9f38f4b --- /dev/null +++ b/examples/mark.js @@ -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)); +})(); diff --git a/src/api.js b/src/api.js index dbd2cbf..6f221b0 100644 --- a/src/api.js +++ b/src/api.js @@ -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); diff --git a/src/index.js b/src/index.js index 82cea08..5312413 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -43,6 +44,7 @@ function resources(config) { server: server(config), filters: filters(config), callOnEachEvent: eventsWapper(config), + mark: mark(config), }; } diff --git a/src/resources/mark.js b/src/resources/mark.js new file mode 100644 index 0000000..fe41ef7 --- /dev/null +++ b/src/resources/mark.js @@ -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; diff --git a/test/resources/mark.js b/test/resources/mark.js new file mode 100644 index 0000000..16e8c8e --- /dev/null +++ b/test/resources/mark.js @@ -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'); + }); +});