-
Notifications
You must be signed in to change notification settings - Fork 231
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
feat(service): Add Ntfy #469
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.
Hello @pushkar803 !
First of all, thank you very much for your time and contribution! We really appreciate it!
Your PR needs some improvements to comply with the standards that we're trying to set for the project. We would be more than happy if you'd resolve those issues at your convenience!
Some basic guidelines for a new service implementation that we're trying to adhere to are:
- The actual service implementation
- A usage guide for the new service (you provided both of them! 🙏 )
- Mocks generated by the use of mockery in order to unit-test your implementation
You can find hints on how to do so in our CONTRIBUTING.md
- Tests at least for the exported functions of the new service (we're striving to increase our test coverage)
- A
doc.go
file including instructions on how to use the service programmatically, please find a reference here
Also, I left some comments regarding your implementation. If you have any questions or might need any guidance, etc. please always feel free to shoot it and I and/or @nikoksr would be happy to provide help.
} | ||
|
||
// DefaultServerURL is the default server to use for the Ntfy service. | ||
const DefaultServerURL = "https://ntfy.sh" |
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.
I don't see a need for this to be exported. We can keep the usage experience a little bit cleaner by making it non-exported.
const DefaultServerURL = "https://ntfy.sh" | |
const defaultServerURL = "https://ntfy.sh" |
// NewWithServers returns a new instance of Ntfy service. You can use this service to send messages to Ntfy. You can | ||
// specify the servers to send the messages to. By default, the service will use the default server | ||
// (https://api.day.app/) if you don't specify any servers. | ||
func NewWithServers(serverURLs ...string) *Service { |
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.
@svaloumas @pushkar803 I'm torn about this one. On one hand I like the convenience this offers (we're doing the same in the notify package), but on the other hand I see two things I dislike.
- No other service (to my knowledge) implements this type of function so far, which makes the library less predictable and intuitiv. So either, we adapt this pattern and extend all or most other services with the same function or we remove it from here.
- The inconsistent naming. We have a
AddReceivers
andNewWithServers
, the latter callsAddReceivers
internally. I think, in case we stick with it, this should be renamed toNewWithReceivers
to stay consistent and intuitiv. - A call to
NewWithServers
without is functionally identical to just callingNew
with an empty parameter list. So maybe, we can resolve my issue above (no. 2) by droppingNewWithServers
entirely and extendNew
with a variadic receivers list. However, this again makes the package not follow our current behavioral standards, which makes it less predictable. Maybe it's better if we stay explicit with two seperate constructor functions as it currently is the case. Feedback on this please!
if len(serverURLs) == 0 { | ||
serverURLs = append(serverURLs, DefaultServerURL) | ||
} |
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.
Length check is obsolete. An empty list will not alter the existsing serverURLs anyway.
if len(serverURLs) == 0 { | |
serverURLs = append(serverURLs, DefaultServerURL) | |
} | |
serverURLs = append(serverURLs, DefaultServerURL) |
isJson := json.Valid(bodyByte) | ||
if !isJson { | ||
err := errors.New("Invalid JSON Body") | ||
return err | ||
} |
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.
Potential simplification.
isJson := json.Valid(bodyByte) | |
if !isJson { | |
err := errors.New("Invalid JSON Body") | |
return err | |
} | |
if !json.Valid(bodyByte) { | |
return errors.New("Invalid JSON Body") | |
} |
err := errors.Wrap(err, "Invalid PostData structure") | ||
return err |
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.
err := errors.Wrap(err, "Invalid PostData structure") | |
return err | |
return errors.Wrap(err, "Invalid PostData structure") |
func TestNtfy_send(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service := New() | ||
assert.NotNil(service) | ||
|
||
service.client = defaultHTTPClient() | ||
|
||
ctx := context.Background() | ||
serverURL := DefaultServerURL | ||
topic := "pushkar" | ||
content := `{ | ||
"message": "Disk space is low at 5.1 GB", | ||
"title": "Low disk space alert", | ||
"tags": ["warning","cd"], | ||
"priority": 4, | ||
"attach": "https://filesrv.lan/space.jpg", | ||
"filename": "diskspace.jpg", | ||
"click": "https://homecamera.lan/xasds1h2xsSsa/", | ||
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }] | ||
}` | ||
|
||
err := service.send(ctx, serverURL, topic, content) | ||
assert.Nil(err) | ||
|
||
err = service.send(ctx, "", topic, content) | ||
assert.NotNil(err) | ||
|
||
err = service.send(ctx, "xyz", topic, content) | ||
assert.NotNil(err) | ||
|
||
err = service.send(ctx, serverURL, topic, "sample_body") | ||
assert.NotNil(err) | ||
|
||
content = `{ | ||
"message": "Disk space is low at 5.1 GB", | ||
"title": "Low disk space alert", | ||
"priority": 4222, | ||
}` | ||
|
||
err = service.send(ctx, serverURL, topic, content) | ||
assert.NotNil(err) | ||
|
||
} | ||
|
||
func TestNtfy_Send(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service := New() | ||
assert.NotNil(service) | ||
|
||
ctx := context.Background() | ||
topic := "pushkar" | ||
content := `{ | ||
"message": "Disk space is low at 5.1 GB", | ||
"title": "Low disk space alert", | ||
"tags": ["warning","cd"], | ||
"priority": 4, | ||
"attach": "https://filesrv.lan/space.jpg", | ||
"filename": "diskspace.jpg", | ||
"click": "https://homecamera.lan/xasds1h2xsSsa/", | ||
"actions": [{ "action": "view", "label": "Admin panel", "url": "https://filesrv.lan/admin" }] | ||
}` | ||
|
||
err := service.Send(ctx, topic, content) | ||
assert.Nil(err) | ||
|
||
content = `{ | ||
"message": "Disk space is low at 5.1 GB", | ||
"title": "Low disk space alert", | ||
"priority": 4222, | ||
}` | ||
|
||
err = service.Send(ctx, topic, content) | ||
assert.NotNil(err) | ||
} |
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.
If I'm not completely misunderstanding this, we're pinging the actual endpoint here. In that case, we can't let this go through. We're solely relying on mocked tests for all other services and should do the same here.
@pushkar803 we greatly appreciate your contribution! So excited about this. Please take a look at the feedback. The crucial points to me are the tests for the send methods and the missing |
@pushkar803 anything I can help you with to push this forward? Please let me know if anything is unclear or you need help with something. |
A few days ago Ntfy ( https://github.com/binwiederhier/ntfy ) with PWA web push is now in main head. You can try it here: https://ntfy.sh/app On Mobile or Desktop you can do Add to Home Screen and notifications do work. Its very easily release with a few small bugs still but will get better. Dev is ticking along well so def worth trying to get this PR in i think. |
@gedw99 I'll get working on this soon. Closing since it's dead. |
Description
Motivation and Context
There was feature request created in issue to add Ntfy service #461
How Has This Been Tested?
Test program included in readme.md file
Screenshots / Output (if appropriate):
Types of changes
Checklist: