Skip to content

Fix handling of logout event #15323

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 2 commits into from
Apr 7, 2021

Conversation

zeripath
Copy link
Contributor

@zeripath zeripath commented Apr 7, 2021

It appears that there is a slight bug in the handling of the data of logout event -
the javascript should be testing the data field of the data field for the logout
instruction.

Signed-off-by: Andrew Thornton [email protected]

It appears that there is a slight bug in the handling of the data of logout event -
the javascript should be testing the data field of the data field for the logout
instruction.

Signed-off-by: Andrew Thornton <[email protected]>
@silverwind
Copy link
Member

silverwind commented Apr 7, 2021

Don't you need to JSON.parse(event.data) like it's done in receiveUpdateCount? Also that method uses uppercase property access like .Count so maybe it's .Data actually?

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 7, 2021
@zeripath
Copy link
Contributor Author

zeripath commented Apr 7, 2021

Don't you need to JSON.parse(event.data) like it's done in receiveUpdateCount?

Don't appear to need it. Have you found that you do need it?


Looking again at this - the data is not JSON data so no it does not need to be parsed no.


Also that method uses uppercase property access like .Count so maybe it's .Data actually?

Which method?

@silverwind
Copy link
Member

silverwind commented Apr 7, 2021

Just a few lines above event.data is passed into receiveUpdateCount:

receiveUpdateCount(event.data);

which then does JSON.parse on the original event.data.data (argument to the function is deceivingly mislabeled as event, it should be data):

const data = JSON.parse(event.data);

And finally it accesses title-cased .Count on the parsed object:

notificationCount.textContent = `${data.Count}`;

I think this requires more refactoring to be clear. I would JSON.parse immediately in the event handler and then pass around the parsed object only.

@zeripath
Copy link
Contributor Author

zeripath commented Apr 7, 2021

the data from a logout event is simply text. So this is correct as it stands.

@silverwind
Copy link
Member

silverwind commented Apr 7, 2021

The only way this can work is that in the event notification-count, data is a JSON-encoded string while for logout it is just the here string (and invalid JSON). I think for clarity we should just always send JSON-parseable data.

@zeripath
Copy link
Contributor Author

zeripath commented Apr 7, 2021

No you've got it wrong. Why don't you test it.

@silverwind
Copy link
Member

That's something I've meaning to ask. How does one generate fake eventsource events on the server? Is there some convenient way to do so?

@zeripath
Copy link
Contributor Author

zeripath commented Apr 7, 2021

there's no built in way of doing this. All of the events are created in routers/events.go:

event := (&eventsource.Event{
Name: "close",
Data: "unauthorized",
})
_, _ = event.WriteTo(ctx)
ctx.Resp.Flush()

is the simplest example.

In this case the important event is:

_, _ = (&eventsource.Event{
Name: "logout",
Data: "here",
}).WriteTo(ctx.Resp)
ctx.Resp.Flush()

Which will simply be emitted as:

event: logout
data: here

The eventsource will then send a js object event:

{
  ...
  "data": {
    "type": "logout",
    "data": "here",
  }
}

which gets pushed to the workers as an "event":

{
   "type": "logout",
   "data": {
    "type": "logout",
    "data": "here",
   },
}

The double wrapping of the data is an unfortunate consequence of the way events are passed around js - this could be stopped by changing:

this.eventSource.addEventListener(eventType, (event) => {
this.notifyClients({
type: eventType,
data: event.data
});

L53 to event.data.data instead of event.data but then you lose the ID and Retry time should we wish to use those.


In the case of notification events the data is []byte of json data in the Event struct.

@zeripath
Copy link
Contributor Author

zeripath commented Apr 7, 2021

I need to move the stopwatches query out of the event stream and instead consider a similar db query to how we do the notification stream but I've not had the inclination do that yet.

@silverwind
Copy link
Member

silverwind commented Apr 7, 2021

I think I understand now. My assumption was that the inner data would always be a JSON string but that's just not the case (would still prefer if we would not mix types like that for different events).

Event{
  Name: "notification-count",
  Data: uidCount,
}

Event{ 
  Name: "logout", 
  Data: "here", 
}

# uidCount 
type UserIDCount struct {
	UserID int64
	Count  int64
}

I did not find where the JSON serialization happens but it must be somewhere.

Refactoring this data.data stuff would certainly be welcome for another PR.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 7, 2021
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Apr 7, 2021
@lafriks lafriks merged commit 05b7e32 into go-gitea:master Apr 7, 2021
@zeripath zeripath deleted the eventsource-logout-fixes branch April 8, 2021 03:44
zeripath added a commit to zeripath/gitea that referenced this pull request Apr 8, 2021
Backport go-gitea#15323

It appears that there is a slight bug in the handling of the data of logout event -
the javascript should be testing the data field of the data field for the logout
instruction.

Signed-off-by: Andrew Thornton <[email protected]>
@zeripath zeripath added the backport/done All backports for this PR have been created label Apr 8, 2021
6543 pushed a commit that referenced this pull request Apr 8, 2021
Backport #15323

It appears that there is a slight bug in the handling of the data of logout event -
the javascript should be testing the data field of the data field for the logout
instruction.

Signed-off-by: Andrew Thornton <[email protected]>
@go-gitea go-gitea locked and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants