Skip to content

Commit

Permalink
Merge pull request #20 from go-park-mail-ru/dev4
Browse files Browse the repository at this point in the history
Dev4
  • Loading branch information
Gvidow committed Dec 15, 2023
2 parents 4143183 + 430f974 commit b7224ed
Show file tree
Hide file tree
Showing 47 changed files with 2,895 additions and 309 deletions.
13 changes: 11 additions & 2 deletions api/proto/realtime.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ package realtime;

service RealTime {
rpc Publish(PublishMessage) returns (google.protobuf.Empty) {}
rpc Subscribe(Channel) returns (stream Message) {}
rpc Subscribe(Channels) returns (stream Message) {}
}

message Channels {
repeated Channel chans = 1;
}

message Channel {
Expand All @@ -27,10 +31,15 @@ message EventObject {
EventType type = 2;
}

message EventMap {
int64 type = 1;
map<string, string> m = 2;
}

message Message {
oneof body {
EventObject object = 1;
string content = 2;
EventMap content = 2;
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/google/uuid v1.3.1
github.com/jackc/pgx/v5 v5.4.3
github.com/joho/godotenv v1.5.1
github.com/mailru/easyjson v0.7.7
github.com/microcosm-cc/bluemonday v1.0.26
github.com/pashagolub/pgxmock/v2 v2.12.0
github.com/prometheus/client_golang v1.17.0
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/klauspost/compress v1.16.7 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
297 changes: 221 additions & 76 deletions internal/api/realtime/realtime.pb.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions internal/api/realtime/realtime_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion internal/api/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ func (r Router) RegisterRoute(handler *deliveryHTTP.HandlerHTTP, wsHandler *deli
r.Delete("/like/{pinID:\\d+}", handler.DeleteLikePin)
r.Delete("/delete/{pinID:\\d+}", handler.DeletePin)
})

r.Route("/comment", func(r chi.Router) {
r.Get("/feed/{pinID:\\d+}", handler.ViewFeedComment)

r.With(auth.RequireAuth).Group(func(r chi.Router) {
r.Post("/{pinID:\\d+}", handler.WriteComment)
r.Delete("/{commentID:\\d+}", handler.DeleteComment)
})
})
})

r.Route("/board", func(r chi.Router) {
Expand Down Expand Up @@ -132,6 +141,7 @@ func (r Router) RegisterRoute(handler *deliveryHTTP.HandlerHTTP, wsHandler *deli
})

r.Mux.With(auth.RequireAuth).Route("/websocket/connect", func(r chi.Router) {
r.Get("/chat", wsHandler.WebSocketConnect)
r.Get("/chat", wsHandler.Chat)
r.Get("/notification", wsHandler.Notification)
})
}
36 changes: 33 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@ import (

authProto "github.com/go-park-mail-ru/2023_2_OND_team/internal/api/auth"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/api/messenger"
rt "github.com/go-park-mail-ru/2023_2_OND_team/internal/api/realtime"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/api/server"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/api/server/router"
deliveryHTTP "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/delivery/http/v1"
deliveryWS "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/delivery/websocket"
notify "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/notification"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/metrics"
commentNotify "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/notification/comment"
boardRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/board/postgres"
commentRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/comment"
imgRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/image"
pinRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/pin"
searchRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/search/postgres"
subRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/subscription/postgres"
userRepo "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/repository/user"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/auth"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/board"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/comment"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/image"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/message"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/pin"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/realtime"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/realtime/chat"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/realtime/notification"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/search"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/subscription"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/user"
Expand Down Expand Up @@ -64,8 +72,29 @@ func Run(ctx context.Context, log *log.Logger, cfg ConfigFiles) {
}
defer connMessMS.Close()

connRealtime, err := grpc.Dial("localhost:8090", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Error(err.Error())
return
}
defer connRealtime.Close()

rtClient := rt.NewRealTimeClient(connRealtime)

commentRepository := commentRepo.NewCommentRepoPG(pool)

imgCase := image.New(log, imgRepo.NewImageRepoFS(uploadFiles))
messageCase := message.New(messenger.NewMessengerClient(connMessMS))
messageCase := message.New(log, messenger.NewMessengerClient(connMessMS), chat.New(realtime.NewRealTimeChatClient(rtClient), log))
pinCase := pin.New(log, imgCase, pinRepo.NewPinRepoPG(pool))

notifyBuilder, err := notify.NewWithType(notify.NotifyComment)
if err != nil {
log.Error(err.Error())
return
}

notifyCase := notification.New(realtime.NewRealTimeNotificationClient(rtClient), log,
notification.Register(commentNotify.NewCommentNotify(notifyBuilder, comment.New(commentRepository, pinCase, nil), pinCase)))

conn, err := grpc.Dial(cfg.AddrAuthServer, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand All @@ -78,14 +107,15 @@ func Run(ctx context.Context, log *log.Logger, cfg ConfigFiles) {
handler := deliveryHTTP.New(log, deliveryHTTP.UsecaseHub{
AuhtCase: ac,
UserCase: user.New(log, imgCase, userRepo.NewUserRepoPG(pool)),
PinCase: pin.New(log, imgCase, pinRepo.NewPinRepoPG(pool)),
PinCase: pinCase,
BoardCase: board.New(log, boardRepo.NewBoardRepoPG(pool), userRepo.NewUserRepoPG(pool), bluemonday.UGCPolicy()),
SubscriptionCase: subscription.New(log, subRepo.NewSubscriptionRepoPG(pool), userRepo.NewUserRepoPG(pool), bluemonday.UGCPolicy()),
SearchCase: search.New(log, searchRepo.NewSearchRepoPG(pool), bluemonday.UGCPolicy()),
MessageCase: messageCase,
CommentCase: comment.New(commentRepo.NewCommentRepoPG(pool), pinCase, notifyCase),
})

wsHandler := deliveryWS.New(log, messageCase,
wsHandler := deliveryWS.New(log, messageCase, notifyCase,
deliveryWS.SetOriginPatterns([]string{"pinspire.online", "pinspire.online:*"}))

cfgServ, err := server.NewConfig(cfg.ServerConfigFile)
Expand Down
6 changes: 4 additions & 2 deletions internal/microservices/realtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s Server) Publish(ctx context.Context, pm *rt.PublishMessage) (*empty.Empt
return &empty.Empty{}, nil
}

func (s Server) Subscribe(c *rt.Channel, ss rt.RealTime_SubscribeServer) error {
func (s Server) Subscribe(chans *rt.Channels, ss rt.RealTime_SubscribeServer) error {
id, err := uuid.NewRandom()
if err != nil {
return status.Error(codes.Internal, "generate uuid v4")
Expand All @@ -52,7 +52,9 @@ func (s Server) Subscribe(c *rt.Channel, ss rt.RealTime_SubscribeServer) error {
transport: ss,
}

s.node.AddSubscriber(c, client)
for _, ch := range chans.GetChans() {
s.node.AddSubscriber(ch, client)
}

<-ss.Context().Done()
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/delivery/http/v1/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (h *HandlerHTTP) DeleteMessage(w http.ResponseWriter, r *http.Request) {
return
}

err = h.messageCase.DeleteMessage(r.Context(), userID, messageID)
err = h.messageCase.DeleteMessage(r.Context(), userID, &message.Message{ID: messageID})
if err != nil {
logger.Warn(err.Error())
err = responseError(w, "delete_message", "fail deleting a message")
Expand Down
119 changes: 119 additions & 0 deletions internal/pkg/delivery/http/v1/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package v1

import (
"net/http"

"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/comment"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/user"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/middleware/auth"
"github.com/mailru/easyjson"
)

func (h *HandlerHTTP) WriteComment(w http.ResponseWriter, r *http.Request) {
logger := h.getRequestLogger(r)
userID := r.Context().Value(auth.KeyCurrentUserID).(int)

pinID, err := fetchURLParamInt(r, "pinID")
if err != nil {
err = responseError(w, "parse_url", "the request url could not be get pin id")
if err != nil {
logger.Error(err.Error())
return
}
}

comment := &comment.Comment{}

err = easyjson.UnmarshalFromReader(r.Body, comment)
defer r.Body.Close()
if err != nil {
logger.Warn(err.Error())

err = responseError(w, "parse_body", "the request body could not be parsed to send a comment")
if err != nil {
logger.Error(err.Error())
return
}
}

comment.PinID = pinID
_, err = h.commentCase.PutCommentOnPin(r.Context(), userID, comment)
if err != nil {
logger.Error(err.Error())
err = responseError(w, "create_comment", "couldn't leave a comment under the selected pin")
} else {
err = responseOk(http.StatusCreated, w, "the comment has been added successfully", nil)
}
if err != nil {
logger.Error(err.Error())
}
}

func (h *HandlerHTTP) DeleteComment(w http.ResponseWriter, r *http.Request) {
logger := h.getRequestLogger(r)
userID := r.Context().Value(auth.KeyCurrentUserID).(int)

commentID, err := fetchURLParamInt(r, "commentID")
if err != nil {
err = responseError(w, "parse_url", "the request url could not be get pin id")
if err != nil {
logger.Error(err.Error())
return
}
}

err = h.commentCase.DeleteComment(r.Context(), userID, commentID)
if err != nil {
logger.Error(err.Error())
err = responseError(w, "delete_comment", "couldn't delete pin comment")
} else {
err = responseOk(http.StatusOK, w, "the comment was successfully deleted", nil)
}
if err != nil {
logger.Error(err.Error())
}
}

func (h *HandlerHTTP) ViewFeedComment(w http.ResponseWriter, r *http.Request) {
logger := h.getRequestLogger(r)
userID, ok := r.Context().Value(auth.KeyCurrentUserID).(int)
if !ok {
userID = user.UserUnknown
}

pinID, err := fetchURLParamInt(r, "pinID")
if err != nil {
err = responseError(w, "parse_url", "the request url could not be get pin id")
if err != nil {
logger.Error(err.Error())
return
}
}

count, lastID, err := FetchValidParamForLoadFeed(r.URL)
if err != nil {
err = responseError(w, "query_param", "the parameters for displaying the pin feed could not be extracted from the request")
if err != nil {
logger.Error(err.Error())
return
}
}

feed, newLastID, err := h.commentCase.GetFeedCommentOnPin(r.Context(), userID, pinID, count, lastID)
if err != nil && len(feed) == 0 {
err = responseError(w, "feed_view", "error displaying pin comments")
if err != nil {
logger.Error(err.Error())
}
return
}

if err != nil {
logger.Error(err.Error())
}

err = responseOk(http.StatusOK, w, "feed comment to pin", map[string]any{"comments": feed, "lastID": newLastID})
if err != nil {
logger.Error(err.Error())
}
}
4 changes: 2 additions & 2 deletions internal/pkg/delivery/http/v1/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"strconv"

"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/pin"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/user"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/middleware/auth"
usecase "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/pin"
log "github.com/go-park-mail-ru/2023_2_OND_team/pkg/logger"
)

func (h *HandlerHTTP) FeedPins(w http.ResponseWriter, r *http.Request) {
logger := h.getRequestLogger(r)
userID, isAuth := r.Context().Value(auth.KeyCurrentUserID).(int)
if !isAuth {
userID = usecase.UserUnknown
userID = user.UserUnknown
}

logger.Info("request on getting feed of pins", log.F{"rawQuery", r.URL.RawQuery})
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/delivery/http/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/auth"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/board"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/comment"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/message"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/pin"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/usecase/search"
Expand All @@ -20,6 +21,7 @@ type HandlerHTTP struct {
subCase subscription.Usecase
searchCase search.Usecase
messageCase message.Usecase
commentCase comment.Usecase
}

func New(log *logger.Logger, hub UsecaseHub) *HandlerHTTP {
Expand All @@ -32,6 +34,7 @@ func New(log *logger.Logger, hub UsecaseHub) *HandlerHTTP {
subCase: hub.SubscriptionCase,
searchCase: hub.SearchCase,
messageCase: hub.MessageCase,
commentCase: hub.CommentCase,
}
}

Expand All @@ -43,4 +46,5 @@ type UsecaseHub struct {
SubscriptionCase subscription.Usecase
SearchCase search.Usecase
MessageCase message.Usecase
CommentCase comment.Usecase
}
Loading

0 comments on commit b7224ed

Please sign in to comment.