Implement 'attach to thread'
This commit is contained in:
@@ -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 |
|
||||
|
||||
|
||||
|
||||
@@ -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" }
|
||||
]
|
||||
}
|
||||
|
||||
@@ -241,6 +241,12 @@
|
||||
}
|
||||
],
|
||||
"post": [
|
||||
{
|
||||
"name": "replyTo",
|
||||
"required": false,
|
||||
"title": "Upload as a reply",
|
||||
"type": "uint64"
|
||||
},
|
||||
{
|
||||
"name": "upload",
|
||||
"required": true,
|
||||
|
||||
+4
-2
@@ -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) {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
+12
-10
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user