-
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.
Add an endpoint returning the number of followers and followees
The endpoint is: /public-keys/{hex-public-key} The response looks like this: { "followees": 0, "followers": 0 }
- Loading branch information
Showing
7 changed files
with
272 additions
and
4 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/boreq/errors" | ||
"github.com/planetary-social/nos-event-service/internal/logging" | ||
"github.com/planetary-social/nos-event-service/service/domain" | ||
) | ||
|
||
type GetPublicKeyInfo struct { | ||
publicKey domain.PublicKey | ||
} | ||
|
||
func NewGetPublicKeyInfo(publicKey domain.PublicKey) GetPublicKeyInfo { | ||
return GetPublicKeyInfo{publicKey: publicKey} | ||
} | ||
|
||
type PublicKeyInfo struct { | ||
numberOfFollowees int | ||
numberOfFollowers int | ||
} | ||
|
||
func NewPublicKeyInfo(numberOfFollowees int, numberOfFollowers int) PublicKeyInfo { | ||
return PublicKeyInfo{numberOfFollowees: numberOfFollowees, numberOfFollowers: numberOfFollowers} | ||
} | ||
|
||
func (p PublicKeyInfo) NumberOfFollowees() int { | ||
return p.numberOfFollowees | ||
} | ||
|
||
func (p PublicKeyInfo) NumberOfFollowers() int { | ||
return p.numberOfFollowers | ||
} | ||
|
||
type GetPublicKeyInfoHandler struct { | ||
transactionProvider TransactionProvider | ||
logger logging.Logger | ||
metrics Metrics | ||
} | ||
|
||
func NewGetPublicKeyInfoHandler( | ||
transactionProvider TransactionProvider, | ||
logger logging.Logger, | ||
metrics Metrics, | ||
) *GetPublicKeyInfoHandler { | ||
return &GetPublicKeyInfoHandler{ | ||
transactionProvider: transactionProvider, | ||
logger: logger.New("getPublicKeyInfoHandler"), | ||
metrics: metrics, | ||
} | ||
} | ||
|
||
func (h *GetPublicKeyInfoHandler) Handle(ctx context.Context, cmd GetPublicKeyInfo) (publicKeyInfo PublicKeyInfo, err error) { | ||
defer h.metrics.StartApplicationCall("getPublicKeyInfo").End(&err) | ||
|
||
var followeesCount, followersCount int | ||
|
||
if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error { | ||
tmp, err := adapters.Contacts.CountFollowees(ctx, cmd.publicKey) | ||
if err != nil { | ||
return errors.Wrap(err, "error counting followees") | ||
} | ||
followeesCount = tmp | ||
|
||
tmp, err = adapters.Contacts.CountFollowers(ctx, cmd.publicKey) | ||
if err != nil { | ||
return errors.Wrap(err, "error counting followers") | ||
} | ||
followersCount = tmp | ||
|
||
return nil | ||
}); err != nil { | ||
return PublicKeyInfo{}, errors.Wrap(err, "transaction error") | ||
} | ||
|
||
return NewPublicKeyInfo(followeesCount, followersCount), 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