Skip to content
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

When subscribed to a new_topic, send endpoints to all apps #70

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

karmanyaahm
Copy link

@karmanyaahm karmanyaahm commented Dec 16, 2023

Also needs cleaning and organization, but I'm opening this because I'm very excited to finally have something that works. So we previously discussed that I would do it using just timing data on the app because that would be simpler, but it really was not. Keeping track of the connection state was a whole thing and the worst thing is that it didn't solve the problem of losing the connection on server restart. This implementation only sends out new_endpoint after subscribing to the topic on the ntfy server.
Goes along with binwiederhier/ntfy#979

@karmanyaahm karmanyaahm changed the title When the server says we subscribed to a new_topic, send out new_endpoints to all apps When subscribed to a new_topic, send endpoints to all apps Dec 16, 2023
@karmanyaahm karmanyaahm marked this pull request as ready for review December 17, 2023 22:00
Comment on lines +261 to +262
io.heckel.ntfy.up.BroadcastReceiver.sendRegistration(baseContext, connectionId.baseUrl, topic)
// TODO is that the right context - looks like it works???
Copy link
Author

Choose a reason for hiding this comment

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

Should that work? Or do I need to make a whole chain of contexts being passed down and stored? Not really sure what context is used for when creating the repository downstream. It seems to work as is.

Comment on lines 96 to 101
/* We don't send the endpoint here anymore, the foreground service will do that after
registering with the push server. This avoids a race condition where the application server
is rejected before ntfy even establishes that this topic exists.
This is fine from an application perspective, because other distributors can't even register
without a connection to the push server.
Unless the app sends registration twice. Then it'll get the endpoint.*/
Copy link
Author

Choose a reason for hiding this comment

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

^ Pointing this out, hopefully is fine

Copy link
Author

Choose a reason for hiding this comment

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

Actually, assuming the Android app is pushed out before the server changes, I'll have to leave the endpoint sending in for now. Otherwise, the new Android app will break for old servers.

Comment on lines +151 to +168

// TODO Where's the best place to put this function? This seems to be the only place
// with the access to the locks, but also globally accessible
// but also, broadcast receiver is for *receiving Android broadcasts*
public fun sendRegistration(context: Context, baseUrl : String, topic : String) {
val app = context.applicationContext as Application
val repository = app.repository
val distributor = Distributor(app)
GlobalScope.launch(Dispatchers.IO) {
// We're doing all of this inside a critical section, because of possible races.
// See https://github.com/binwiederhier/ntfy/issues/230 for details.

mutex.withLock {
val existingSubscription = repository.getSubscription(baseUrl, topic) ?: return@launch
val appId = existingSubscription.upAppId ?: return@launch
val connectorToken = existingSubscription.upConnectorToken ?: return@launch
val endpoint = topicUrlUp(existingSubscription.baseUrl, existingSubscription.topic)
distributor.sendEndpoint(appId, connectorToken, endpoint)
Copy link
Author

Choose a reason for hiding this comment

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

Where should I put this function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant