Skip to content

Commit

Permalink
enhancement(sciencemesh): emit event whenever a new invite request is…
Browse files Browse the repository at this point in the history
… received
  • Loading branch information
fschade committed Sep 3, 2024
1 parent 8c1d8be commit e67e3be
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
78 changes: 54 additions & 24 deletions internal/http/services/sciencemesh/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package sciencemesh
import (
"encoding/json"
"errors"
"html/template"
"mime"
"net/http"

Expand All @@ -33,22 +32,17 @@ import (

"github.com/cs3org/reva/v2/internal/http/services/reqres"
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/stream"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/smtpclient"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/cs3org/reva/v2/pkg/utils/list"
)

type tokenHandler struct {
gatewayClient gateway.GatewayAPIClient
smtpCredentials *smtpclient.SMTPCredentials
meshDirectoryURL string
providerDomain string
tplSubj *template.Template
tplBody *template.Template
eventStream events.Stream
}

Expand Down Expand Up @@ -84,41 +78,77 @@ type token struct {
// will send an email containing the link the user will use to accept the
// invitation.
func (h *tokenHandler) Generate(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := getGenerateRequest(r)
if err != nil {
reqres.WriteError(w, r, reqres.APIErrorInvalidParameter, "missing parameters in request", err)
return
}

query := r.URL.Query()
token, err := h.gatewayClient.GenerateInviteToken(ctx, &invitepb.GenerateInviteTokenRequest{
Description: query.Get("description"),
ctx := r.Context()
genTokenRes, err := h.gatewayClient.GenerateInviteToken(ctx, &invitepb.GenerateInviteTokenRequest{
Description: req.Description,
})
if err != nil {
switch {
case err != nil:
reqres.WriteError(w, r, reqres.APIErrorServerError, "error generating token", err)
return
case genTokenRes.GetStatus().GetCode() == rpc.Code_CODE_NOT_FOUND:
reqres.WriteError(w, r, reqres.APIErrorNotFound, genTokenRes.GetStatus().GetMessage(), nil)
return
case genTokenRes.GetStatus().GetCode() != rpc.Code_CODE_OK:
reqres.WriteError(w, r, reqres.APIErrorServerError, genTokenRes.GetStatus().GetMessage(), errors.New(genTokenRes.GetStatus().GetMessage()))
return
}

tknRes := h.prepareGenerateTokenResponse(genTokenRes.GetInviteToken())
if err := json.NewEncoder(w).Encode(tknRes); err != nil {
reqres.WriteError(w, r, reqres.APIErrorServerError, "error marshalling token data", err)
return
}

user := ctxpkg.ContextMustGetUser(ctx)
recipient := query.Get("recipient")
if err := events.Publish(ctx, h.eventStream, events.ScienceMeshInviteTokenGenerated{
Sharer: user.Id,
Token: token.InviteToken.Token,
MeshDirectoryURL: h.meshDirectoryURL,
Recipient: recipient,
Timestamp: utils.TSNow(),
Sharer: genTokenRes.GetInviteToken().GetUserId(),
RecipientMail: req.Recipient,
Token: tknRes.Token,
Description: tknRes.Description,
Expiration: tknRes.Expiration,
InviteLink: tknRes.InviteLink,
Timestamp: utils.TSNow(),
}); err != nil {
log := appctx.GetLogger(ctx)
log.Error().Err(err).
Msg("failed to publish the science-mesh invite token generated event")
}

tknRes := h.prepareGenerateTokenResponse(token.InviteToken)
if err := json.NewEncoder(w).Encode(tknRes); err != nil {
reqres.WriteError(w, r, reqres.APIErrorServerError, "error marshalling token data", err)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}

// generateRequest is the request body for the Generate endpoint.
type generateRequest struct {
Description string `json:"description"`
Recipient string `json:"recipient" validate:"omitempty,email"`
}

func getGenerateRequest(r *http.Request) (*generateRequest, error) {
var req generateRequest
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err == nil && contentType == "application/json" {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, err
}
} else {
return nil, errors.New("body request not recognised")
}

// validate the request
if err := validate.Struct(req); err != nil {
return nil, err
}

return &req, nil
}

func (h *tokenHandler) prepareGenerateTokenResponse(tkn *invitepb.InviteToken) *token {
res := &token{
Token: tkn.Token,
Expand Down
12 changes: 7 additions & 5 deletions pkg/events/sciencemesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

// ScienceMeshInviteTokenGenerated is emitted when a sciencemesh token is generated
type ScienceMeshInviteTokenGenerated struct {
Sharer *user.UserId
Token string
MeshDirectoryURL string // fixMe: we can get the value from the config too, lets keep it for now
Recipient string
Timestamp *types.Timestamp
Sharer *user.UserId
RecipientMail string
Token string
Description string
Expiration uint64
InviteLink string
Timestamp *types.Timestamp
}

// Unmarshal to fulfill unmarshaller interface
Expand Down

0 comments on commit e67e3be

Please sign in to comment.