instance.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 06 Sep 2018 01:07:40 +0200
changeset 238 1c0042e76902
parent 221 f9228d1f4267
child 239 ca5639b4768e
permissions -rw-r--r--
Do not use a global API version Mastodon uses an API version per endpoint (cf. search v2) so we need to be able to specify the version for any API call.

/*
Copyright 2017-2018 Mikael Berthe

Licensed under the MIT license.  Please see the LICENSE file is this directory.
*/

package madon

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/sendgrid/rest"
)

// GetCurrentInstance returns current instance information
func (mc *Client) GetCurrentInstance() (*Instance, error) {
	var i Instance
	if err := mc.apiCall("v1/instance", rest.Get, nil, nil, nil, &i); err != nil {
		return nil, err
	}
	return &i, nil
}

// GetInstancePeers returns current instance peers
// The peers are defined as the domains of users the instance has previously
// resolved.
func (mc *Client) GetInstancePeers() ([]InstancePeer, error) {
	var peers []InstancePeer
	if err := mc.apiCall("v1/instance/peers", rest.Get, nil, nil, nil, &peers); err != nil {
		return nil, err
	}
	return peers, nil
}

// GetInstanceActivity returns current instance activity
// The activity contains the counts of active users, locally posted statuses,
// and new registrations in weekly buckets.
func (mc *Client) GetInstanceActivity() ([]WeekActivity, error) {
	var activity []WeekActivity
	if err := mc.apiCall("v1/instance/activity", rest.Get, nil, nil, nil, &activity); err != nil {
		return nil, err
	}
	return activity, nil
}

/* Activity time handling */

// UnmarshalJSON handles deserialization for custom ActivityTime type
func (act *ActivityTime) UnmarshalJSON(b []byte) error {
	s, err := strconv.ParseInt(strings.Trim(string(b), "\""), 10, 64)
	if err != nil {
		return err
	}
	if s == 0 {
		act.Time = time.Time{}
		return nil
	}
	act.Time = time.Unix(s, 0)
	return nil
}

// MarshalJSON handles serialization for custom ActivityTime type
func (act *ActivityTime) MarshalJSON() ([]byte, error) {
	return []byte(fmt.Sprintf("\"%d\"", act.Unix())), nil
}