Skip to content

Fix subscription module accessing an invalid URL for REST-API #85

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion code_comments/htdocs/code-comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
var underscore = _.noConflict();

function addSubscriptionButton() {
if (jQuery('h1').length == 0) {
return;
}

var rev = new URLSearchParams(window.location.search).get('rev') || '';

var button = jQuery(
'<button>',
{
id: 'subscribe',
disabled: true,
role: 'button',
title: 'Code comment subscriptions require JavaScript to be enabled',
class: 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary',
'data-base-url': window.location.origin,
'data-path': window.location.pathname,
'data-rev': rev
}
);

button.append(
jQuery(
'<span>',
{
class: 'ui-button-icon-primary ui-icon ui-icon-closethick'
}
)
);

button.append(
jQuery(
'<span>',
{
text: 'Subscribe',
class: 'ui-button-text'
}
)
);

jQuery('h1').before(button);
}

(function($) { $(function() {
var _ = window.underscore,
jQuery = $; // just in case something uses jQuery() instead of $()
Expand Down Expand Up @@ -328,7 +371,7 @@ var underscore = _.noConflict();
});

window.SubscriptionView = Backbone.View.extend({
el: $('button#subscribe'),
el: 'button#subscribe',

initialize: function(){
_.bindAll(this, "render");
Expand Down Expand Up @@ -372,6 +415,7 @@ var underscore = _.noConflict();
});

window.subscription = new Subscription();
addSubscriptionButton()
window.subscriptionView = new SubscriptionView({model: subscription});
if (subscriptionView.el) {
subscription.fetch();
Expand Down
30 changes: 2 additions & 28 deletions code_comments/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
from trac.admin import IAdminCommandProvider
from trac.attachment import Attachment, IAttachmentChangeListener
from trac.core import Component, implements
from trac.util.html import html as tag
from trac.versioncontrol import (
RepositoryManager, NoSuchChangeset, IRepositoryChangeListener)
from trac.web.api import HTTPNotFound, IRequestHandler, ITemplateStreamFilter

from genshi.filters import Transformer
from trac.web.api import HTTPNotFound, IRequestHandler

from code_comments.api import ICodeCommentChangeListener
from code_comments.comments import Comments
Expand Down Expand Up @@ -439,7 +436,7 @@ def comment_created(self, comment):


class SubscriptionModule(Component):
implements(IRequestHandler, ITemplateStreamFilter)
implements(IRequestHandler)

# IRequestHandler methods

Expand All @@ -459,17 +456,6 @@ def process_request(self, req):
return self._do_PUT(req)
return self._do_GET(req)

# ITemplateStreamFilter methods

def filter_stream(self, req, method, filename, stream, data):
if re.match(r'^/(changeset|browser|attachment/ticket/\d+/.?).*',
req.path_info):
filter = Transformer('//h1')
button = self._subscription_button(req.path_info,
req.args.get('rev'))
stream |= filter.before(button)
return stream

# Internal methods

def _do_GET(self, req):
Expand All @@ -496,15 +482,3 @@ def _do_PUT(self, req):
subscription.update()
req.send(json.dumps(subscription, cls=SubscriptionJSONEncoder),
'application/json')

def _subscription_button(self, path, rev):
"""
Generates a (disabled) button to connect JavaScript to.
"""
return tag.button(
'Subscribe', id_='subscribe', disabled=True,
title=('Code comment subscriptions require JavaScript '
'to be enabled'),
data_base_url=self.env.project_url or self.env.abs_href(),
data_path=path,
data_rev=rev)