/
utils.go
156 lines (143 loc) · 4.13 KB
/
utils.go
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
package mastodon
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/mattn/go-mastodon"
"github.com/tomnomnom/linkheader"
"github.com/turbot/steampipe-plugin-sdk/v4/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin/transform"
)
func connect(_ context.Context, d *plugin.QueryData) (*mastodon.Client, error) {
config := GetConfig(d.Connection)
client := mastodon.NewClient(&mastodon.Config{
Server: *config.Server,
AccessToken: *config.AccessToken,
})
return client, nil
}
func accountColumns() []*plugin.Column {
return []*plugin.Column{
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "ID of the account.",
},
{
Name: "acct",
Type: proto.ColumnType_STRING,
Description: "username@server for the account.",
},
{
Name: "created_at",
Type: proto.ColumnType_TIMESTAMP,
Description: "Timestamp when the account was created.",
},
{
Name: "url",
Type: proto.ColumnType_STRING,
Description: "URL for the account.",
},
{
Name: "username",
Type: proto.ColumnType_STRING,
Description: "Username for the account.",
},
{
Name: "display_name",
Type: proto.ColumnType_STRING,
Description: "Display name for the account.",
},
{
Name: "followers_count",
Type: proto.ColumnType_INT,
Description: "Number of followers for the account.",
},
{
Name: "following_count",
Type: proto.ColumnType_INT,
Description: "Number of accounts this account follows.",
},
{
Name: "statuses_count",
Type: proto.ColumnType_INT,
Description: "Toots from this account.",
},
{
Name: "note",
Type: proto.ColumnType_STRING,
Description: "Description of the account.",
Transform: transform.FromValue().Transform(sanitizeNote),
},
{
Name: "query",
Type: proto.ColumnType_STRING,
Description: "Query used to search hashtags.",
Transform: transform.FromQual("query"),
},
}
}
// This is a workaround for the upstream SDK's doGet() method which intends to handle link-based pagination but seems to fail for:
//
// https://pkg.go.dev/github.com/mattn/go-mastodon#Client.GetAccountFollowers
// https://pkg.go.dev/github.com/mattn/go-mastodon#Client.GetAccountFollowing
//
// The workaround sacrifices the exponential backoff provided by the SDK's doGet().
func listFollows(ctx context.Context, category string, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
return nil, err
}
config := GetConfig(d.Connection)
token := *config.AccessToken
accountCurrentUser, err := client.GetAccountCurrentUser(ctx)
if err != nil {
return nil, err
}
url := fmt.Sprintf("https://mastodon.social/api/v1/accounts/%s/%s", accountCurrentUser.ID, category)
plugin.Logger(ctx).Debug("follow", "category", category, "initial url", url)
httpClient := &http.Client{}
for {
pageAccounts := []*mastodon.Account{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Authorization", "Bearer "+token)
res, err := httpClient.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&pageAccounts)
if err != nil {
fmt.Println(err)
}
plugin.Logger(ctx).Debug("follows", "category", category, "pageAccounts", len(pageAccounts))
for i, account := range pageAccounts {
plugin.Logger(ctx).Debug("followers", "i", i, "account", account)
d.StreamListItem(ctx, account)
}
header := res.Header
newUrl := ""
for _, link := range linkheader.Parse(header.Get("Link")) {
if link.Rel == "next" {
newUrl = link.URL
}
}
plugin.Logger(ctx).Debug("followers", "newUrl", newUrl)
if newUrl == "" {
break
} else {
url = newUrl
}
}
return nil, nil
}
func sanitizeNote(ctx context.Context, input *transform.TransformData) (interface{}, error) {
account := input.Value.(*mastodon.Account)
return sanitize(account.Note), nil
}