# HG changeset patch # User Mikael Berthe # Date 1495905121 -7200 # Node ID e1b1f4a168b71bf4ef86ef0cc7cd64cc20934ea3 # Parent eb9e257ba50cc63ef7dd9d87f8e6af61c8d88fec Add GetDomainBlocks, DomainBlock and DomainUnblock diff -r eb9e257ba50c -r e1b1f4a168b7 domain.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/domain.go Sat May 27 19:12:01 2017 +0200 @@ -0,0 +1,52 @@ +/* +Copyright 2017 Mikael Berthe + +Licensed under the MIT license. Please see the LICENSE file is this directory. +*/ + +package madon + +import ( + "github.com/sendgrid/rest" +) + +// GetDomainBlocks returns the current user blocked domains +// If lopt.All is true, several requests will be made until the API server +// has nothing to return. +func (mc *Client) GetDomainBlocks(lopt *LimitParams) ([]DomainName, error) { + const endPoint = "domain_blocks" + var links apiLinks + var domains []DomainName + if err := mc.apiCall(endPoint, rest.Get, nil, lopt, &links, &domains); err != nil { + return nil, err + } + if lopt != nil { // Fetch more pages to reach our limit + var domainSlice []DomainName + for (lopt.All || lopt.Limit > len(domains)) && links.next != nil { + newlopt := links.next + links = apiLinks{} + if err := mc.apiCall(endPoint, rest.Get, nil, newlopt, &links, &domainSlice); err != nil { + return nil, err + } + domains = append(domains, domainSlice...) + domainSlice = domainSlice[:0] // Clear struct + } + } + return domains, nil +} + +// DomainBlock blocks the specified domain +func (mc *Client) DomainBlock(domain DomainName) error { + const endPoint = "domain_blocks" + params := make(apiCallParams) + params["domain"] = string(domain) + return mc.apiCall(endPoint, rest.Post, params, nil, nil, nil) +} + +// DomainUnblock unblocks the specified domain +func (mc *Client) DomainUnblock(domain DomainName) error { + const endPoint = "domain_blocks" + params := make(apiCallParams) + params["domain"] = string(domain) + return mc.apiCall(endPoint, rest.Delete, params, nil, nil, nil) +} diff -r eb9e257ba50c -r e1b1f4a168b7 types.go --- a/types.go Sat May 27 13:18:48 2017 +0200 +++ b/types.go Sat May 27 19:12:01 2017 +0200 @@ -26,6 +26,9 @@ Entities - Everything manipulated/returned by the API */ +// DomainName is a domain name string, as returned by the domain_blocks API +type DomainName string + // Account represents a Mastodon account entity type Account struct { ID int64 `json:"id"`