-
-
Notifications
You must be signed in to change notification settings - Fork 473
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
Conversation
There was a problem hiding this 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}); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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;
@cPhost The problem with loading the bots already outside the |
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. |
Oh, in that case it won't be a issue. I'll make the changes and push it. |
There was a problem hiding this 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 => { |
There was a problem hiding this 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 call loadBots()
here instead here.
@@ -92,6 +134,10 @@ function setupReply(id) { | |||
narrow.by_subject(id, { trigger: 'notification' }); | |||
} | |||
|
|||
window.addEventListener('load', () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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]) => { |
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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
.
@akashnimare Amended commit with comments at specified places and moved |
Before merging, I would want you to handle following cases-
jquery.js?61dd:9566 GET https://chat.zulip.org/json/users 401 ()
jquery.js?61dd:9566 GET https://playground.zulipdev.org/json/users 401 (Unauthorized)
|
@akashnimare The 401() error would be thrown on the console by jQuery. How do you wish to handle these cases? |
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) |
@abhigyank merged, thanks 🚀 |
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 usingis_bots
property.