Skip to content

notifications: Adds bots mention support to reply option. #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2018
Merged

notifications: Adds bots mention support to reply option. #395

merged 1 commit into from
Jan 30, 2018

Conversation

abhigyank
Copy link
Collaborator

@abhigyank abhigyank commented Jan 25, 2018

Fixes: #391


What's this PR do?
Adds bots mention support to reply option in notifications.
Any background context you want to provide?
Make a getJSON request to /json/users/ to get list of members and filter out bots using is_bots property.

Copy link
Member

@priyank-p priyank-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature work correctly here but this needs a bit of code refactoring and the approach or requesting data on each notification is not correct here instead what we want to do is:

Load the bots already outside the parseReply function and asyncronusly.

@@ -83,6 +83,36 @@ function parseReply(reply) {
reply = reply.replace(regex, streamMention);
});

window.$.ajaxSetup({async: false});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right here blocking the thread using synchronous request is not the right approach here

const bots = [];
members.forEach(membersRow => {
if (membersRow.is_bot) {
botsList.push(membersRow);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right here instead of pushing membersRow here you should push memberRow.full_name and the mention syntax so you do not need to loop through it again.

@@ -83,6 +83,36 @@ function parseReply(reply) {
reply = reply.replace(regex, streamMention);
});

window.$.ajaxSetup({async: false});

window.$.getJSON('/json/users', data => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more minor comment you should deconstruct the $ variable up top.
const { $ } = window;

@abhigyank
Copy link
Collaborator Author

abhigyank commented Jan 26, 2018

@cPhost The problem with loading the bots already outside the parseReply function would be that the list would be static. After the list is generated, if someone creates a new bot, then that bot can't be mentioned in the reply since that list wouldn't have that bot. Hence, despite knowing that blocking the thread using synchronous request is not the a good way, I have done that. I feel we need to get the list from the server every time parseReply is called to get new bots if there are any.

@priyank-p
Copy link
Member

Do get your point but we don't care if new bot was added or not this is like a extra convince feature we provide people are not likely to be mentioning bots specifically new bots + in a mention so we shouldn't worry about it too much.

@abhigyank
Copy link
Collaborator Author

Oh, in that case it won't be a issue. I'll make the changes and push it.

Copy link
Member

@priyank-p priyank-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to call loadsBots instead of repeating the code and in loadBots reintialze the botList array like botsList = [] in callback so no duplicates are being added.

And remove the function out of load event.

} else {
$.ajaxSetup({async: false});

$.getJSON('/json/users', data => {
Copy link
Member

@priyank-p priyank-p Jan 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better call loadBots() here instead here.

@@ -92,6 +134,10 @@ function setupReply(id) {
narrow.by_subject(id, { trigger: 'notification' });
}

window.addEventListener('load', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should't put this under load because the while purpose of the function is to load bots before hand not after load event where notification could be created.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that, before load, $ is not defined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah i did not think about it you can ignore this then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhigyank, @cPhost works great. I think the load listener should be added in notification/index.js. Can you do that?

Copy link
Member

@priyank-p priyank-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhigyank this LGTM. requesting final review @akashnimare

const botsList = [];
let botsListLoaded = false;

function loadBots(sync = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhigyank would be great if you can add few comments here about the function? In general, it's better to document your code so that other devs can see what's going on :)

loadBots(true);
}

botsList.forEach(([bot, mention]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here as well.

@@ -92,6 +125,10 @@ function setupReply(id) {
narrow.by_subject(id, { trigger: 'notification' });
}

window.addEventListener('load', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this in /notification/index.js.

@zulipbot zulipbot added size: L and removed size: M labels Jan 29, 2018
@abhigyank
Copy link
Collaborator Author

abhigyank commented Jan 29, 2018

@akashnimare Amended commit with comments at specified places and moved load to index.js . Request review.

@akashnimare
Copy link
Member

Before merging, I would want you to handle following cases-

  • if the user is not logged in -
jquery.js?61dd:9566 GET https://chat.zulip.org/json/users 401 ()
  • In case of muliple servers -
jquery.js?61dd:9566 GET https://playground.zulipdev.org/json/users 401 (Unauthorized)


@abhigyank
Copy link
Collaborator Author

@akashnimare The 401() error would be thrown on the console by jQuery. How do you wish to handle these cases?

@akashnimare
Copy link
Member

For now, we can just log them. Maybe use something like -

function loadBots(sync = false) {
	const { $ } = window;
	botsList.length = 0;
	if (sync) {
		$.ajaxSetup({ async: false });
	}
	$.getJSON('/json/users')
		.done((data) => {
			const members = data.members;
			members.forEach(membersRow => {
				if (membersRow.is_bot) {
					const bot = `@${membersRow.full_name}`;
					const mention = `@**${bot.replace(/^@/, '')}**`;
					botsList.push([bot, mention]);
				}
			});
			botsListLoaded = true;
			if (sync) {
				$.ajaxSetup({ async: true });
			}
		})
		.fail((error) => {
			console.log("Request Failed: ", error.responseText);
			console.log(`Request status: ${error.status + error.statusText}`);
		});
}

(dummy code feel free to refactor)

@akashnimare akashnimare merged commit 4a40c75 into zulip:master Jan 30, 2018
@akashnimare
Copy link
Member

@abhigyank merged, thanks 🚀

@abhigyank abhigyank deleted the bots_reply branch June 26, 2018 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants