qiscus-sdk-core 2.16.0 → 2.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,160 +1,134 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports["default"] = void 0;
8
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
- var RoomAdapter = exports["default"] = /*#__PURE__*/function () {
2
+ class RoomAdapter {
11
3
  /**
12
4
  * Params used in this class
13
5
  * @method constructor
14
6
  * @param {Object} HTTPAdapter [Qiscus HTTP adapter]
15
7
  * @return {void} Returns nothing
16
8
  */
17
- function RoomAdapter(HTTPAdapter) {
18
- (0, _classCallCheck2["default"])(this, RoomAdapter);
9
+ constructor(HTTPAdapter) {
19
10
  this.HTTPAdapter = HTTPAdapter;
20
11
  this.token = HTTPAdapter.token;
21
12
  }
22
- return (0, _createClass2["default"])(RoomAdapter, [{
23
- key: "getOrCreateRoom",
24
- value: function getOrCreateRoom(email, options, distinctId) {
25
- var params = {
26
- emails: email
27
- };
28
- if (distinctId) params[distinctId] = distinctId;
29
- if (options) params['options'] = JSON.stringify(options);
30
- return this.HTTPAdapter.post("api/v2/sdk/get_or_create_room_with_target", params).then(function (res) {
31
- if (res.body.status !== 200) return Promise.reject(res);
32
- var room = res.body.results.room;
33
- room.avatar = room.avatar_url;
34
- room.comments = res.body.results.comments.reverse();
35
- var rivalUser = room.participants.find(function (p) {
36
- return p.email === email;
37
- });
38
- room.name = rivalUser ? rivalUser.username : 'Room name';
39
- return Promise.resolve(room);
40
- });
41
- }
42
- }, {
43
- key: "getRoomById",
44
- value: function getRoomById(id) {
45
- return this.HTTPAdapter.get("api/v2/sdk/get_room_by_id?id=".concat(id)).then(function (res) {
46
- return res.body;
47
- });
48
- }
49
- }, {
50
- key: "getOrCreateRoomByUniqueId",
51
- value: function getOrCreateRoomByUniqueId(id, name, avatarURL) {
52
- var params = {
53
- unique_id: id,
54
- name: name,
55
- avatar_url: avatarURL
56
- };
57
- return this.HTTPAdapter.post("api/v2/sdk/get_or_create_room_with_unique_id", params).then(function (res) {
58
- if (res.body.status !== 200) return Promise.reject(res);
59
- var room = res.body.results.room;
60
- room.avatar = room.avatar_url;
61
- room.comments = res.body.results.comments.reverse();
62
- room.name = room.room_name;
63
- return Promise.resolve(room);
64
- });
65
- }
66
- }, {
67
- key: "createRoom",
68
- value: function createRoom(name, emails) {
69
- var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
70
- var optionalData = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
71
- var optsData = Object.keys(optionalData).length <= 0 ? null : JSON.stringify(optionalData);
72
- var body = {
73
- name: name,
74
- 'participants[]': emails,
75
- avatar_url: opts.avatarURL,
76
- options: optsData
77
- };
78
- return this.HTTPAdapter.post("api/v2/sdk/create_room", body).then(function (res) {
79
- if (res.body.status !== 200) return Promise.reject(res);
80
- var room = res.body.results.room;
81
- room.comments = res.body.results.comments;
82
- return Promise.resolve({
83
- id: room.id,
84
- name: room.room_name,
85
- lastCommentId: room.last_comment_id,
86
- lastCommentMessage: room.last_comment_message,
87
- lastTopicId: room.last_topic_id,
88
- avatarURL: room.avatar_url,
89
- options: room.options,
90
- participants: room.participants.map(function (participant) {
91
- return {
92
- id: participant.id,
93
- email: participant.email,
94
- username: participant.username,
95
- avatarURL: participant.avatar_url
96
- };
97
- })
98
- });
13
+ getOrCreateRoom(email, options, distinctId) {
14
+ let params = { emails: email };
15
+ if (distinctId) params[distinctId] = distinctId;
16
+ if (options) params["options"] = JSON.stringify(options);
17
+ return this.HTTPAdapter.post(
18
+ `api/v2/sdk/get_or_create_room_with_target`,
19
+ params
20
+ ).then((res) => {
21
+ if (res.body.status !== 200) return Promise.reject(res);
22
+ const room = res.body.results.room;
23
+ room.avatar = room.avatar_url;
24
+ room.comments = res.body.results.comments.reverse();
25
+ const rivalUser = room.participants.find((p) => p.email === email);
26
+ room.name = rivalUser ? rivalUser.username : "Room name";
27
+ return Promise.resolve(room);
28
+ });
29
+ }
30
+ getRoomById(id) {
31
+ return this.HTTPAdapter.get(`api/v2/sdk/get_room_by_id?id=${id}`).then(
32
+ (res) => res.body
33
+ );
34
+ }
35
+ getOrCreateRoomByUniqueId(id, name, avatarURL) {
36
+ let params = {
37
+ unique_id: id,
38
+ name,
39
+ avatar_url: avatarURL
40
+ };
41
+ return this.HTTPAdapter.post(
42
+ `api/v2/sdk/get_or_create_room_with_unique_id`,
43
+ params
44
+ ).then((res) => {
45
+ if (res.body.status !== 200) return Promise.reject(res);
46
+ const room = res.body.results.room;
47
+ room.avatar = room.avatar_url;
48
+ room.comments = res.body.results.comments.reverse();
49
+ room.name = room.room_name;
50
+ return Promise.resolve(room);
51
+ });
52
+ }
53
+ createRoom(name, emails, opts = {}, optionalData = {}) {
54
+ const optsData = Object.keys(optionalData).length <= 0 ? null : JSON.stringify(optionalData);
55
+ const body = {
56
+ name,
57
+ "participants[]": emails,
58
+ avatar_url: opts.avatarURL,
59
+ options: optsData
60
+ };
61
+ return this.HTTPAdapter.post(`api/v2/sdk/create_room`, body).then((res) => {
62
+ if (res.body.status !== 200) return Promise.reject(res);
63
+ const room = res.body.results.room;
64
+ room.comments = res.body.results.comments;
65
+ return Promise.resolve({
66
+ id: room.id,
67
+ name: room.room_name,
68
+ lastCommentId: room.last_comment_id,
69
+ lastCommentMessage: room.last_comment_message,
70
+ lastTopicId: room.last_topic_id,
71
+ avatarURL: room.avatar_url,
72
+ options: room.options,
73
+ participants: room.participants.map((participant) => ({
74
+ id: participant.id,
75
+ email: participant.email,
76
+ username: participant.username,
77
+ avatarURL: participant.avatar_url
78
+ }))
99
79
  });
100
- }
101
- }, {
102
- key: "updateRoom",
103
- value: function updateRoom(args) {
104
- if (!args.id) throw new Error('id is required');
105
- var params = {
106
- id: args.id
107
- };
108
- if (args.room_name) params['room_name'] = args.room_name;
109
- if (args.avatar_url) params['avatar_url'] = args.avatar_url;
110
- if (args.options) params['options'] = JSON.stringify(args.options);
111
- return this.HTTPAdapter.post("api/v2/sdk/update_room", params).then(function (res) {
80
+ });
81
+ }
82
+ updateRoom(args) {
83
+ if (!args.id) throw new Error("id is required");
84
+ let params = { id: args.id };
85
+ if (args.room_name) params["room_name"] = args.room_name;
86
+ if (args.avatar_url) params["avatar_url"] = args.avatar_url;
87
+ if (args.options) params["options"] = JSON.stringify(args.options);
88
+ return this.HTTPAdapter.post(`api/v2/sdk/update_room`, params).then(
89
+ (res) => {
112
90
  if (res.body.status !== 200) return Promise.reject(res);
113
91
  return Promise.resolve(res.body.results.room);
114
- });
115
- }
116
- }, {
117
- key: "getTotalUnreadCount",
118
- value: function getTotalUnreadCount() {
119
- return this.HTTPAdapter.get("api/v2/sdk/total_unread_count").then(function (resp) {
120
- return resp.body.results.total_unread_count;
121
- });
122
- }
123
- }, {
124
- key: "getRoomUnreadCount",
125
- value: function getRoomUnreadCount() {
126
- return this.HTTPAdapter.get("api/v2/sdk/get_room_unread_count").then(function (r) {
127
- return r.body.results.total_unread_count;
128
- });
129
- }
130
- }, {
131
- key: "addParticipantsToGroup",
132
- value: function addParticipantsToGroup(roomId) {
133
- var emails = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
134
- if (!roomId || !emails) throw new Error('room_id and emails is required');
135
- var params = {
136
- room_id: roomId,
137
- 'emails[]': emails
138
- };
139
- return this.HTTPAdapter.post("api/v2/sdk/add_room_participants", params).then(function (res) {
140
- if (res.body.status !== 200) return Promise.reject(res);
141
- return Promise.resolve(res.body.results.participants_added);
142
- });
143
- }
144
- }, {
145
- key: "removeParticipantsFromGroup",
146
- value: function removeParticipantsFromGroup(roomId) {
147
- var emails = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
148
- if (!roomId || !emails) throw new Error('room_id and emails is required');
149
- var params = {
150
- room_id: roomId,
151
- 'emails[]': emails
152
- };
153
- return this.HTTPAdapter.post("api/v2/sdk/remove_room_participants", params).then(function (res) {
154
- if (res.body.status !== 200) return Promise.reject(res);
155
- return Promise.resolve(res.body.results.participants_removed);
156
- });
157
- }
158
- }]);
159
- }();
160
- module.exports = exports.default;
92
+ }
93
+ );
94
+ }
95
+ getTotalUnreadCount() {
96
+ return this.HTTPAdapter.get(`api/v2/sdk/total_unread_count`).then(
97
+ (resp) => resp.body.results.total_unread_count
98
+ );
99
+ }
100
+ getRoomUnreadCount() {
101
+ return this.HTTPAdapter.get(`api/v2/sdk/get_room_unread_count`).then(
102
+ (r) => r.body.results.total_unread_count
103
+ );
104
+ }
105
+ addParticipantsToGroup(roomId, emails = []) {
106
+ if (!roomId || !emails) throw new Error("room_id and emails is required");
107
+ let params = {
108
+ room_id: roomId,
109
+ "emails[]": emails
110
+ };
111
+ return this.HTTPAdapter.post(
112
+ `api/v2/sdk/add_room_participants`,
113
+ params
114
+ ).then((res) => {
115
+ if (res.body.status !== 200) return Promise.reject(res);
116
+ return Promise.resolve(res.body.results.participants_added);
117
+ });
118
+ }
119
+ removeParticipantsFromGroup(roomId, emails = []) {
120
+ if (!roomId || !emails) throw new Error("room_id and emails is required");
121
+ let params = {
122
+ room_id: roomId,
123
+ "emails[]": emails
124
+ };
125
+ return this.HTTPAdapter.post(
126
+ `api/v2/sdk/remove_room_participants`,
127
+ params
128
+ ).then((res) => {
129
+ if (res.body.status !== 200) return Promise.reject(res);
130
+ return Promise.resolve(res.body.results.participants_removed);
131
+ });
132
+ }
133
+ }
134
+ module.exports = RoomAdapter;