dia_entity_post.go 4.97 KB
Newer Older
Ghost User's avatar
Ghost User committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
package federation
//
// GangGo Federation Library
// Copyright (C) 2017-2018 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "net/http"
  "bytes"
  "crypto/rsa"
  "github.com/Zauberstuhl/go-xml"
  helpers "git.feneas.org/ganggo/federation/helpers"
  "time"
)

type DiasporaStatusMessage struct {
  XMLName xml.Name `xml:"status_message"`
  EntityAuthor string `xml:"author"`
  EntityGuid string `xml:"guid"`
  EntityCreatedAt helpers.Time `xml:"created_at"`
  EntityProviderName string `xml:"provider_display_name"`
  EntityText string `xml:"text,omitempty"`
  EntityPhotos *DiasporaPhotos `xml:"photo,omitempty"`
  EntityLocation *DiasporaLocation `xml:"location,omitempty"`
  EntityPoll *DiasporaPoll `xml:"poll,omitempty"`
  EntityPublic bool `xml:"public"`
  EntityEvent *DiasporaEvent `xml:"event,omitempty"`
}

type DiasporaPhoto struct {
  Guid string `xml:"guid"`
  Author string `xml:"author"`
  Public bool `xml:"public"`
  CreatedAt helpers.Time `xml:"created_at"`
  RemotePhotoPath string `xml:"remote_photo_path"`
  RemotePhotoName string `xml:"remote_photo_name"`
  Text string `xml:"text"`
  StatusMessageGuid string `xml:"status_message_guid"`
  Height int `xml:"height"`
  Width int `xml:"width"`
}

type DiasporaPhotos []DiasporaPhoto

type DiasporaLocation struct {
  Address string `xml:"address"`
  Lat string `xml:"lat"`
  Lng string `xml:"lng"`
}

type DiasporaPoll struct {
  Guid string `xml:"guid"`
  Question string `xml:"question"`
  PollAnswers []DiasporaPollAnswer `xml:"poll_answers"`
}

type DiasporaPollAnswer struct {
  Guid string `xml:"guid"`
  Answer string `xml:"answer"`
}

type DiasporaEvent struct {
  Author string `xml:"author"`
  Guid string `xml:"guid"`
  Summary string `xml:"summary"`
  Start helpers.Time `xml:"start"`
  End helpers.Time `xml:"end"`
  AllDay bool `xml:"all_day"`
  Timezone string `xml:"timezone"`
  Description string `xml:"description"`
  Location *DiasporaLocation `xml:"location,omitempty"`
}

func (e *DiasporaStatusMessage) Author() string {
  return e.EntityAuthor
}

func (e *DiasporaStatusMessage) SetAuthor(author string) {
  (*e).EntityAuthor = author
}

func (e *DiasporaStatusMessage) Send(inbox string, priv *rsa.PrivateKey, pub *rsa.PublicKey) error {
  payload, err := e.Marshal(priv, pub)
  if err != nil {
    return err
  }

  client := HttpClient{}
  if e.EntityPublic {
    return client.Push(inbox, http.Header{
        "Content-Type": []string{CONTENT_TYPE_ENVELOPE},
      }, bytes.NewBuffer(payload))
  }
  return client.Push(inbox, http.Header{
      "Content-Type": []string{CONTENT_TYPE_JSON},
    }, bytes.NewBuffer(payload))
}

func (e *DiasporaStatusMessage) Marshal(priv *rsa.PrivateKey, pub *rsa.PublicKey) ([]byte, error) {
  payload, err := xml.MarshalIndent(e, "", "  ")
  if err != nil {
    return payload, err
  }
  if e.EntityPublic {
    return DiasporaMagicEnvelope(priv, e.Author(), payload)
  }
  return DiasporaEncryptedMagicEnvelope(priv, pub, e.Author(), payload)
}

func (e *DiasporaStatusMessage) Unmarshal(b []byte) error {
  return xml.Unmarshal(b, &e)
}

func (e *DiasporaStatusMessage) Type() MessageType {
  return MessageType{
    Proto: DiasporaProtocol,
    Entity: StatusMessage,
  }
}

func (e *DiasporaStatusMessage) Recipients() []string { return []string{} }

func (e *DiasporaStatusMessage) SetRecipients(recipient []string) {}

func (e *DiasporaStatusMessage) Guid() string {
  return e.EntityGuid
}

func (e *DiasporaStatusMessage) SetGuid(guid string) {
  (*e).EntityGuid = guid
}

func (e *DiasporaStatusMessage) CreatedAt() helpers.Time {
  return e.EntityCreatedAt
}

func (e *DiasporaStatusMessage) SetCreatedAt(createdAt time.Time) {
  e.EntityCreatedAt.New(createdAt)
}

func (e *DiasporaStatusMessage) Provider() string {
  return e.EntityProviderName
}

func (e *DiasporaStatusMessage) SetProvider(provider string) {
  (*e).EntityProviderName = provider
}

func (e *DiasporaStatusMessage) Text() string {
  return e.EntityText
}

func (e *DiasporaStatusMessage) SetText(text string) {
  (*e).EntityText = text
}

func (e *DiasporaStatusMessage) Public() bool {
  return e.EntityPublic
}

func (e *DiasporaStatusMessage) SetPublic(public bool) {
  (*e).EntityPublic = public
}


func (e *DiasporaStatusMessage) FilePaths() (result []string) {
  if e.EntityPhotos != nil {
    for _, photo := range *e.EntityPhotos {
      result = append(result, photo.RemotePhotoPath + photo.RemotePhotoName)
    }
  }
  return
}