mirror of
https://gitea.phreedom.club/localhost_frssoft/bloat.git
synced 2025-05-01 09:34:21 +00:00
Add attachments support
This commit is contained in:
parent
ea66bd539d
commit
e129ea922e
8 changed files with 86 additions and 11 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"io"
|
||||
"mastodon"
|
||||
"mime/multipart"
|
||||
"web/model"
|
||||
)
|
||||
|
||||
|
@ -142,10 +143,10 @@ func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *mastod
|
|||
return s.Service.UnRetweet(ctx, client, c, id)
|
||||
}
|
||||
|
||||
func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string) (id string, err error) {
|
||||
func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
|
||||
c, err = s.getClient(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return s.Service.PostTweet(ctx, client, c, content, replyToID)
|
||||
return s.Service.PostTweet(ctx, client, c, content, replyToID, files)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"io"
|
||||
"log"
|
||||
"mastodon"
|
||||
"mime/multipart"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -108,10 +109,10 @@ func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *mas
|
|||
return s.Service.UnRetweet(ctx, client, c, id)
|
||||
}
|
||||
|
||||
func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string) (id string, err error) {
|
||||
func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Printf("method=%v, content=%v, reply_to_id=%v, took=%v, err=%v\n",
|
||||
"PostTweet", content, replyToID, time.Since(begin), err)
|
||||
}(time.Now())
|
||||
return s.Service.PostTweet(ctx, client, c, content, replyToID)
|
||||
return s.Service.PostTweet(ctx, client, c, content, replyToID, files)
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
@ -36,7 +37,7 @@ type Service interface {
|
|||
UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
|
||||
Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
|
||||
UnRetweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
|
||||
PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string) (id string, err error)
|
||||
PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
|
@ -292,10 +293,20 @@ func (svc *service) UnRetweet(ctx context.Context, client io.Writer, c *mastodon
|
|||
return
|
||||
}
|
||||
|
||||
func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string) (id string, err error) {
|
||||
func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
|
||||
var mediaIds []string
|
||||
for _, f := range files {
|
||||
a, err := c.UploadMediaFromMultipartFileHeader(ctx, f)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
mediaIds = append(mediaIds, a.ID)
|
||||
}
|
||||
|
||||
tweet := &mastodon.Toot{
|
||||
Status: content,
|
||||
InReplyToID: replyToID,
|
||||
MediaIDs: mediaIds,
|
||||
}
|
||||
|
||||
s, err := c.PostStatus(ctx, tweet)
|
||||
|
|
|
@ -3,6 +3,7 @@ package service
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
|
@ -153,9 +154,18 @@ func NewHandler(s Service, staticDir string) http.Handler {
|
|||
|
||||
r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) {
|
||||
ctx := getContextWithSession(context.Background(), req)
|
||||
content := req.FormValue("content")
|
||||
replyToID := req.FormValue("reply_to_id")
|
||||
id, err := s.PostTweet(ctx, w, nil, content, replyToID)
|
||||
|
||||
err := req.ParseMultipartForm(4 << 20)
|
||||
if err != nil {
|
||||
s.ServeErrorPage(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
content := getMultipartFormValue(req.MultipartForm, "content")
|
||||
replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id")
|
||||
files := req.MultipartForm.File["attachments"]
|
||||
|
||||
id, err := s.PostTweet(ctx, w, nil, content, replyToID, files)
|
||||
if err != nil {
|
||||
s.ServeErrorPage(ctx, w, err)
|
||||
return
|
||||
|
@ -178,3 +188,14 @@ func NewHandler(s Service, staticDir string) http.Handler {
|
|||
|
||||
return r
|
||||
}
|
||||
|
||||
func getMultipartFormValue(mf *multipart.Form, key string) (val string) {
|
||||
vals, ok := mf.Value[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if len(vals) < 1 {
|
||||
return ""
|
||||
}
|
||||
return vals[0]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue