From 9e095282c5e786cf444d29bd6681e1d282026689 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 22 Oct 2018 11:39:55 +0200 Subject: [PATCH] Implement 'attach to thread' --- sam/docs/README.md | 1 + sam/docs/src/spec.json | 1 + sam/docs/src/spec/channel.json | 6 ++++++ sam/rest/channel.go | 6 ++++-- sam/rest/request/channel.go | 15 ++++++++++++--- sam/service/attachment.go | 22 ++++++++++++---------- 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/sam/docs/README.md b/sam/docs/README.md index 905c836db..41f693b79 100644 --- a/sam/docs/README.md +++ b/sam/docs/README.md @@ -368,6 +368,7 @@ A channel is a representation of a sequence of messages. It has meta data like c | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | | channelID | uint64 | PATH | Channel ID | N/A | YES | +| replyTo | uint64 | POST | Upload as a reply | N/A | NO | | upload | *multipart.FileHeader | POST | File to upload | N/A | YES | diff --git a/sam/docs/src/spec.json b/sam/docs/src/spec.json index 1f382f41d..e2f36495f 100644 --- a/sam/docs/src/spec.json +++ b/sam/docs/src/spec.json @@ -319,6 +319,7 @@ { "name": "channelID", "type": "uint64", "required": true, "title": "Channel ID" } ], "post": [ + { "name": "replyTo", "type": "uint64", "required": false, "title": "Upload as a reply" }, { "name": "upload", "type": "*multipart.FileHeader", "required": true, "title": "File to upload" } ] } diff --git a/sam/docs/src/spec/channel.json b/sam/docs/src/spec/channel.json index 4a1e0abf8..c32f9971f 100644 --- a/sam/docs/src/spec/channel.json +++ b/sam/docs/src/spec/channel.json @@ -241,6 +241,12 @@ } ], "post": [ + { + "name": "replyTo", + "required": false, + "title": "Upload as a reply", + "type": "uint64" + }, { "name": "upload", "required": true, diff --git a/sam/rest/channel.go b/sam/rest/channel.go index 5fb6891ae..a6bb4e1bd 100644 --- a/sam/rest/channel.go +++ b/sam/rest/channel.go @@ -90,10 +90,12 @@ func (ctrl *Channel) Attach(ctx context.Context, r *request.ChannelAttach) (inte defer file.Close() return ctrl.wrapAttachment(ctrl.svc.att.With(ctx).Create( - r.ChannelID, r.Upload.Filename, r.Upload.Size, - file)) + file, + r.ChannelID, + r.ReplyTo, + )) } func (ctrl *Channel) wrapAttachment(attachment *types.Attachment, err error) (*outgoing.Attachment, error) { diff --git a/sam/rest/request/channel.go b/sam/rest/request/channel.go index 34c4c4518..02f579c28 100644 --- a/sam/rest/request/channel.go +++ b/sam/rest/request/channel.go @@ -17,13 +17,15 @@ package request import ( "encoding/json" - "github.com/go-chi/chi" - "github.com/jmoiron/sqlx/types" - "github.com/pkg/errors" "io" "mime/multipart" "net/http" "strings" + + "github.com/davecgh/go-spew/spew" + "github.com/go-chi/chi" + "github.com/jmoiron/sqlx/types" + "github.com/pkg/errors" ) var _ = chi.URLParam @@ -458,6 +460,7 @@ var _ RequestFiller = NewChannelInvite() // Channel attach request parameters type ChannelAttach struct { ChannelID uint64 `json:",string"` + ReplyTo uint64 `json:",string"` Upload *multipart.FileHeader } @@ -491,7 +494,13 @@ func (c *ChannelAttach) Fill(r *http.Request) error { post[name] = string(param[0]) } + spew.Dump(post) + c.ChannelID = parseUInt64(chi.URLParam(r, "channelID")) + if val, ok := post["replyTo"]; ok { + + c.ReplyTo = parseUInt64(val) + } if _, c.Upload, err = r.FormFile("upload"); err != nil { return errors.Wrap(err, "error procesing uploaded file") } diff --git a/sam/service/attachment.go b/sam/service/attachment.go index 06e93c84a..11f4780cf 100644 --- a/sam/service/attachment.go +++ b/sam/service/attachment.go @@ -11,6 +11,7 @@ import ( "path" "strings" + "github.com/davecgh/go-spew/spew" "github.com/disintegration/imaging" "github.com/edwvee/exiffix" "github.com/pkg/errors" @@ -44,7 +45,7 @@ type ( With(ctx context.Context) AttachmentService FindByID(id uint64) (*types.Attachment, error) - Create(channelId uint64, name string, size int64, fh io.ReadSeeker) (*types.Attachment, error) + Create(name string, size int64, fh io.ReadSeeker, channelId, replyTo uint64) (*types.Attachment, error) OpenOriginal(att *types.Attachment) (io.ReadSeeker, error) OpenPreview(att *types.Attachment) (io.ReadSeeker, error) } @@ -93,7 +94,7 @@ func (svc *attachment) OpenPreview(att *types.Attachment) (io.ReadSeeker, error) return svc.store.Open(att.PreviewUrl) } -func (svc *attachment) Create(channelId uint64, name string, size int64, fh io.ReadSeeker) (att *types.Attachment, err error) { +func (svc *attachment) Create(name string, size int64, fh io.ReadSeeker, channelId, replyTo uint64) (att *types.Attachment, err error) { if svc.store == nil { return nil, errors.New("Can not create attachment: store handler not set") } @@ -140,10 +141,12 @@ func (svc *attachment) Create(channelId uint64, name string, size int64, fh io.R } msg := &types.Message{ - Message: name, - Type: types.MessageTypeAttachment, - ChannelID: channelId, - UserID: currentUserID, + Attachment: att, + Message: name, + Type: types.MessageTypeAttachment, + ChannelID: channelId, + ReplyTo: replyTo, + UserID: currentUserID, } if strings.HasPrefix(att.Meta.Original.Mimetype, "image/") { @@ -162,7 +165,7 @@ func (svc *attachment) Create(channelId uint64, name string, size int64, fh io.R log.Printf("File %s (id: %d) attached to message (id: %d)", att.Name, att.ID, msg.ID) - return svc.sendEvent(msg, att) + return svc.sendEvent(msg) }) } @@ -286,9 +289,7 @@ func (svc *attachment) processImage(original io.ReadSeeker, att *types.Attachmen // Sends message to event loop // // It also preloads user -func (svc *attachment) sendEvent(msg *types.Message, att *types.Attachment) (err error) { - msg.Attachment = att - +func (svc *attachment) sendEvent(msg *types.Message) (err error) { if msg.User == nil { // @todo pull user from cache if msg.User, err = svc.usr.FindByID(msg.UserID); err != nil { @@ -296,6 +297,7 @@ func (svc *attachment) sendEvent(msg *types.Message, att *types.Attachment) (err } } + spew.Dump(msg) return svc.evl.Message(msg) }