3 var _ = require('lodash');
4 var context = require('gitter-web-client-context');
5 var appEvents = require('../../utils/appevents');
6 var apiClient = require('../../components/api-client');
7 var unreadItemsClient = require('../../components/unread-items-client');
9 function updateNotifications(mode) {
10 apiClient.userRoom.put('/settings/notifications', { mode: mode }).then(function() {
11 appEvents.triggerParent('user_notification', {
12 title: 'Notifications',
13 text: 'Notification settings have been updated for this room'
18 const mentionRegexp = `@([\\w\\-.:\\[\\]]+)`;
22 command: 'ban @username',
23 description: 'Ban somebody from the room.',
24 criteria: function() {
25 var isOrgRoom = false;
26 if (context.troupe().get('githubType') == 'ORG') isOrgRoom = true;
27 return !context.inOneToOneTroupeContext() && context.isTroupeAdmin() && !isOrgRoom;
31 action: function(text) {
32 const userMatch = text.match(new RegExp(`\\/ban ${mentionRegexp}(\\s+(removemsgs))?\\s*$`));
33 if (!userMatch) return;
34 var user = userMatch[1];
35 var removeMessages = !!userMatch[3];
38 .post('/bans', { username: user, removeMessages: removeMessages })
43 errorMessage = 'You do not have permission to ban people.';
47 errorMessage = e.friendlyMessage || 'Ban failed';
51 errorMessage = 'That person does not exist (on Gitter that is)';
54 errorMessage = 'Ban failed';
57 appEvents.triggerParent('user_notification', {
58 title: 'Could not ban user',
60 className: 'notification-error'
67 description: 'Create a channel',
68 completion: 'channel ',
70 action: function(text) {
71 var channelMatch = text.match(/^\s*\/channel(?:\s+([\w-]+))?/);
72 var channel = channelMatch[1];
75 appEvents.trigger('route', 'createroom/' + channel);
77 appEvents.trigger('route', 'createroom');
83 description: 'Toggle the room as a favourite',
87 var isFavourite = !context.troupe().get('favourite');
89 apiClient.userRoom.put('', { favourite: isFavourite });
94 description: 'Goto the room (foo/bar)',
97 action: function(text) {
98 const gotoMatch = text.match(/^\s*\/goto(?:\s+((?:[^/]+\/?){2,3}))?/);
99 if (!gotoMatch) return;
100 const roomUri = gotoMatch[1];
102 appEvents.trigger('navigation', `/${roomUri}`, 'chat', roomUri);
107 description: 'Goto the room (foo/bar)',
110 action: function(text) {
111 const joinMatch = text.match(/^\s*\/join(?:\s+((?:[^/]+\/?){2,3}))?/);
112 if (!joinMatch) return;
113 const roomUri = joinMatch[1];
115 appEvents.trigger('navigation', `/${roomUri}`, 'chat', roomUri);
120 description: 'Leave the room',
121 completion: 'leave ',
122 regexp: /^\/(leave|part)\s*$/,
123 criteria: function() {
124 return !context.inOneToOneTroupeContext();
127 context.troupe().set('aboutToLeave', true);
128 apiClient.room.delete('/users/' + context.getUserId(), {}).then(function() {
129 appEvents.trigger('navigation', '/home', 'home', '');
130 context.troupe().set('roomMember', false);
136 description: "Let people know what's happening",
141 command: 'query @username',
142 description: 'Have a private conversation with @username',
143 completion: 'query @',
145 action: function(text) {
146 const userMatch = text.match(new RegExp(`\\/query ${mentionRegexp}\\s*$`));
147 if (!userMatch) return;
148 var user = userMatch[1];
150 var url = '/' + user;
151 var type = user === context.user().get('username') ? 'home' : 'chat';
154 appEvents.trigger('navigation', url, type, title);
158 command: 'remove @username',
159 description: 'Remove somebody from the room',
160 criteria: function() {
161 return !context.inOneToOneTroupeContext() && context.isTroupeAdmin();
163 completion: 'remove @',
165 action: function(text) {
166 const userMatch = text.match(new RegExp(`\\/remove ${mentionRegexp}\\s*$`));
167 if (!userMatch) return;
168 var user = userMatch[1];
169 appEvents.trigger('command.room.remove', user);
174 regexp: /^s\/([^\/]+)\/([^\/]*)\/i?(g?)\s*$/,
175 action: function(text) {
176 var re = this.regexp.exec(text);
179 var global = !!re[3];
181 'chatCollectionView:substLastChat',
190 command: 'topic foo',
191 description: 'Set room topic to foo',
192 criteria: function() {
193 return !context.inOneToOneTroupeContext() && context.isTroupeAdmin();
195 completion: 'topic ',
197 action: function(text) {
198 var topicMatch = text.match(/^\/topic (.+)/);
200 var topic = topicMatch[1];
202 context.troupe().set('topic', topic);
204 apiClient.room.put('', { topic: topic });
209 command: 'unban @username',
210 description: 'Unban somebody from the room',
211 criteria: function() {
212 var isOrgRoom = false;
213 if (context.troupe().get('githubType') == 'ORG') isOrgRoom = true;
214 return !context.inOneToOneTroupeContext() && context.isTroupeAdmin() && !isOrgRoom;
216 completion: 'unban @',
218 action: function(text) {
219 const userMatch = text.match(new RegExp(`\\/unban ${mentionRegexp}\\s*$`));
220 if (!userMatch) return;
221 var user = userMatch[1];
223 apiClient.room.delete('/bans/' + user, {}).catch(function(e) {
227 errorMessage = 'You do not have permission to unban people.';
231 errorMessage = e.friendlyMessage || 'Unban failed';
235 errorMessage = 'That person is not on the banned list.';
238 errorMessage = 'Unban failed';
241 appEvents.triggerParent('user_notification', {
242 title: 'Could not unban user',
244 className: 'notification-error'
250 command: 'notify-all',
251 description: 'Get notified on all messages',
252 completion: 'notify-all',
253 regexp: /^\/notify-all\s*$/,
255 updateNotifications('all');
259 command: 'notify-announcements',
260 description: 'Get notified on mentions and group messages',
261 completion: 'notify-announcements',
262 regexp: /^\/notify-announcements\s*$/,
264 updateNotifications('announcement');
268 command: 'notify-mute',
269 description: 'Mute all notifications, except direct mentions',
270 completion: 'notify-mute',
271 regexp: /^\/notify-mute\s*$/,
273 updateNotifications('mute');
278 command: 'mark-all-read',
279 description: 'Mark all chat items as read',
280 completion: 'mark-all-read',
281 regexp: /^\/mark-all-read\s*$/,
283 unreadItemsClient.markAllRead();
289 command: 'update-default-mode',
290 description: 'Update default mode for rooms',
291 completion: 'update-default-mode',
292 regexp: /^\/update-default-mode\b/,
294 // regexp: /^\/mark-all-read\s*$/,
295 action: function(text) {
296 var topicMatch = text.match(/^\/update-default-mode\s+(all|annoucement|mute)(\s+override)?/);
297 if (!topicMatch) return;
299 var override = !!topicMatch[2];
301 apiClient.user.put('/settings/defaultRoomMode', { mode: topicMatch[1], override: override });
307 size: commandsList.length,
309 getSuggestions: function(term) {
310 return commandsList.filter(function(cmd) {
311 var elligible = !cmd.hidden && (!cmd.criteria || cmd.criteria()) && cmd.completion;
312 return elligible && cmd.command.indexOf(term) === 0;
316 findMatch: function(text) {
317 return _.find(commandsList, function(cmd) {
318 return text.match(cmd.regexp);