You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.4 KiB
Go

package mobilizon
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/hasura/go-graphql-client"
)
func (c MobilizonClient) GetEvents() ([]*Event, error) {
var q struct {
Events struct {
Elements []*Event
Total Int
}
}
err := c.client.Query(context.TODO(), &q, nil)
return q.Events.Elements, err
}
func (c MobilizonClient) GetEvent(uuid UUID) (*Event, error) {
var q struct {
Event Event `graphql:"event(uuid: $uuid)"`
}
vars := map[string]interface{}{"uuid": uuid}
err := c.client.Query(context.TODO(), &q, vars)
return &q.Event, err
}
func (c MobilizonClient) CreateEvent(e *EventInput, organizer Actor) (*Event, error) {
var q struct {
Event Event `graphql:"createEvent(organizerActorID: $actorID, title: $title, description: $desc, beginsOn: $begin, endsOn: $end, physicalAddress: $addr)"`
}
// if e.PhysicalAddress.Geom == "" {
// e.PhysicalAddress.Geom = "13.0;52.0"
// }
vars := map[string]interface{}{
"actorID": organizer.ID,
"title": e.Title,
"desc": String(strings.ReplaceAll(string(e.Description), "\n", "<br/>")),
"begin": DateTime{e.BeginsOn},
"end": DateTime{e.EndsOn},
"addr": e.PhysicalAddress,
}
err := c.client.Mutate(context.TODO(), &q, vars)
return &q.Event, err
}
// CreateEventWithImage doesn't use the standard graphql client, but does a
// multipart/form-data request to upload the picture.
func (c MobilizonClient) CreateEventWithImage(e *EventInput, organizer Actor, picture io.Reader) (*Event, error) {
var q struct {
Event Event `graphql:"createEvent(organizerActorID: $actorID, title: $title, description: $desc, beginsOn: $begin, endsOn: $end, physicalAddress: $addr, picture: $pic)"`
}
vars := map[string]interface{}{
"actorID": organizer.ID,
"title": e.Title,
"desc": String(strings.ReplaceAll(string(e.Description), "\n", "<br/>")),
"begin": DateTime{e.BeginsOn},
"end": DateTime{e.EndsOn},
"addr": e.PhysicalAddress,
"pic": e.Picture,
}
mutation, _ := graphql.ConstructMutation(q, vars)
files := map[string]io.Reader{}
files[string(e.Picture.Media.File)] = picture
req, err := c.makeMultipartRequest(mutation, vars, files)
if err != nil {
return nil, err
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
// FIXME: can't we use facilities from the graphql lib to do this?
var result struct {
Data struct{ CreateEvent Event }
Errors graphql.Errors
}
err = graphql.UnmarshalGraphQL(body, &result)
if len(result.Errors) != 0 {
return nil, fmt.Errorf("%v", result.Errors)
}
return &result.Data.CreateEvent, err
}
func (c MobilizonClient) UploadMedia(fileContent io.Reader, name string) (*Media, error) {
query := `mutation uploadMedia($file: Upload!, $name: String!, $alt: String) {
uploadMedia(file: $file, name: $name, alt: $alt) {
id
}
}`
vars := map[string]interface{}{"name": name, "alt": name, "file": "file"}
files := map[string]io.Reader{"file": fileContent}
req, err := c.makeMultipartRequest(query, vars, files)
if err != nil {
return nil, err
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var result struct{ Data struct{ UploadMedia Media } }
json.Unmarshal(content, &result)
return &result.Data.UploadMedia, nil
}