The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "time"
  6. )
  7. // A Change is a change in the timeline.
  8. type Change struct {
  9. ID string `json:"id"`
  10. Model string `json:"model"`
  11. Op string `json:"op"`
  12. Author string `json:"author"`
  13. Date time.Time `json:"date"`
  14. Keys []ChangeKey `json:"keys"`
  15. Objects []ChangeObject `json:"objects"`
  16. }
  17. // A ChangeKey describes a model directly or indirectly affected by the change.
  18. type ChangeKey struct {
  19. Model string `json:"model"`
  20. ID string `json:"id"`
  21. }
  22. // A ChangeObject is an object affected by the change.
  23. type ChangeObject struct {
  24. Data json.RawMessage `json:"-"`
  25. TypeName string `json:"-"`
  26. }
  27. type changeObjectHeader struct {
  28. TypeName string `json:"__typename"`
  29. }
  30. // Channel parses the ChangeObject as a channel.
  31. func (co *ChangeObject) Channel() (Channel, error) {
  32. if co.TypeName != "Channel" {
  33. return Channel{}, errors.New("Incorrect Type Name")
  34. }
  35. channel := Channel{}
  36. err := json.Unmarshal(co.Data, &channel)
  37. return channel, err
  38. }
  39. // UnmarshalJSON implements json.Unmarshaller
  40. func (co *ChangeObject) UnmarshalJSON(b []byte) error {
  41. data := make([]byte, len(b))
  42. copy(data, b)
  43. header := changeObjectHeader{}
  44. err := json.Unmarshal(data, &header)
  45. if err != nil {
  46. return err
  47. }
  48. co.Data = data
  49. co.TypeName = header.TypeName
  50. return nil
  51. }