-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
398 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/ThreeDotsLabs/watermill" | ||
"github.com/ThreeDotsLabs/watermill-googlecloud/pkg/googlecloud" | ||
"github.com/pkg/errors" | ||
"github.com/planetary-social/go-notification-service/service/config" | ||
"github.com/planetary-social/go-notification-service/service/domain" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
const googlePubSubFollowChangeTopic = "follow-changes" | ||
|
||
func NewWatermillSubscriber(config config.Config, logger watermill.LoggerAdapter) (*googlecloud.Subscriber, error) { | ||
var options []option.ClientOption | ||
|
||
if j := config.GooglePubSubCredentialsJSON(); len(j) > 0 { | ||
options = append(options, option.WithCredentialsJSON(config.GooglePubSubCredentialsJSON())) | ||
} | ||
|
||
publisherConfig := googlecloud.SubscriberConfig{ | ||
ProjectID: config.GooglePubSubProjectID(), | ||
DoNotCreateTopicIfMissing: true, | ||
ClientOptions: options, | ||
} | ||
|
||
return googlecloud.NewSubscriber(publisherConfig, logger) | ||
} | ||
|
||
type GCPFollowChangeSubscriber struct { | ||
subscriber *googlecloud.Subscriber | ||
logger watermill.LoggerAdapter | ||
} | ||
|
||
func NewFollowChangeSubscriber(subscriber *googlecloud.Subscriber, logger watermill.LoggerAdapter) *GCPFollowChangeSubscriber { | ||
return &GCPFollowChangeSubscriber{subscriber: subscriber, logger: logger} | ||
} | ||
|
||
func (p *GCPFollowChangeSubscriber) Subscribe(ctx context.Context) (<-chan *domain.FollowChange, error) { | ||
subChan, err := p.subscriber.Subscribe(ctx, googlePubSubFollowChangeTopic) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error subscribing") | ||
} | ||
|
||
ch := make(chan *domain.FollowChange) | ||
|
||
defer func() { | ||
close(ch) | ||
p.subscriber.Close() | ||
}() | ||
|
||
go func() { | ||
var payload domain.FollowChange | ||
for message := range subChan { | ||
if err := json.Unmarshal(message.Payload, &payload); err != nil { | ||
p.logger.Error("error unmarshaling follow change payload", err, watermill.LogFields{"payload": string(message.Payload)}) | ||
return | ||
} | ||
|
||
message.Ack() | ||
|
||
select { | ||
case ch <- &payload: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
|
||
return ch, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/planetary-social/go-notification-service/service/domain" | ||
) | ||
|
||
type NoopSubscriber struct { | ||
} | ||
|
||
func NewNoopSubscriber() *NoopSubscriber { | ||
return &NoopSubscriber{} | ||
} | ||
func (p *NoopSubscriber) Subscribe(ctx context.Context) (<-chan *domain.FollowChange, error) { | ||
ch := make(chan *domain.FollowChange) | ||
return ch, nil | ||
} |
19 changes: 19 additions & 0 deletions
19
service/adapters/mocks/external_follow_change_subscriber.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/planetary-social/go-notification-service/service/domain" | ||
) | ||
|
||
type MockExternalFollowChangeSubscriber struct { | ||
} | ||
|
||
func NewMockExternalFollowChangeSubscriber() *MockExternalFollowChangeSubscriber { | ||
return &MockExternalFollowChangeSubscriber{} | ||
} | ||
|
||
func (m MockExternalFollowChangeSubscriber) Subscribe(ctx context.Context) (<-chan *domain.FollowChange, error) { | ||
ch := make(chan *domain.FollowChange) | ||
return ch, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pubsub | ||
|
||
import ( | ||
"github.com/planetary-social/go-notification-service/service/domain" | ||
) | ||
|
||
type FollowChangePublisher struct { | ||
pubsub *GoChannelPubSub[domain.FollowChange] | ||
} | ||
|
||
func NewFollowChangePublisher() *FollowChangePublisher { | ||
return &FollowChangePublisher{ | ||
pubsub: NewGoChannelPubSub[domain.FollowChange](), | ||
} | ||
} | ||
|
||
func (m *FollowChangePublisher) Publish(followChange domain.FollowChange) { | ||
m.pubsub.Publish(followChange) | ||
} |
Oops, something went wrong.