This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (106 loc) · 3.19 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
)
func main() {
// Read config
var cfg Config
configBytes, _ := os.ReadFile("config.json")
_ = json.Unmarshal(configBytes, &cfg)
log.Printf("%v", cfg)
// Get all users
log.Printf("Get all users...")
offset := 0
allUserCount := 0
emptyUserAlreadySuspendedCount := 0
emptyUserNewlySuspendedCount := 0
idSet := make(map[string]struct{})
isNotEnd := true
const limit = 100
for isNotEnd {
log.Printf("Grabbing users from offset %d", offset)
reqBody := ShowUserRequest{
I: cfg.Token,
Origin: "local",
Offset: offset,
Limit: limit,
}
reqBodyBytes, _ := json.Marshal(&reqBody)
req, _ := http.NewRequest("POST", cfg.Instance+"/api/admin/show-users", bytes.NewReader(reqBodyBytes))
res, _ := (&http.Client{}).Do(req)
var resU []User
_ = json.NewDecoder(res.Body).Decode(&resU)
offset += len(resU)
if len(resU) < limit {
log.Printf("Reach the end")
isNotEnd = false
}
for _, u := range resU {
// Check ID for duplicate
if _, ok := idSet[u.ID]; ok {
// Already analyzed
continue
} else {
// New
idSet[u.ID] = struct{}{}
}
allUserCount++
if u.Name == "" && // No name
u.Description == "" && // No description
u.AvatarUrl == cfg.Instance+"/identicon/"+u.ID && // Default generated avatar
u.BannerUrl == "" && // No banner
u.Location == "" && // No location
u.IsBot == false && // Default
u.IsCat == false && // Default
len(u.Fields) == 0 && // No fields
u.FollowersCount == 0 && // No follower
u.FollowingCount == 0 && // No following
len(u.PinnedNoteIDs) == 0 && // No pin notes
u.PinnedPageID == "" && // No pin page
u.TwoFactorEnabled == false { // No 2FA
//log.Printf("New empty user found: %v", u)
if u.IsSuspended {
emptyUserAlreadySuspendedCount++
} else {
// Check note & page
if CheckNothingButIDCount(cfg.Instance+"/api/users/notes", u.ID, 1) == 0 && // No note
CheckNothingButIDCount(cfg.Instance+"/api/users/pages", u.ID, 1) == 0 { // No page
// Absolutely nothing
log.Printf("New empty user found:\t%s ( %s )", u.ID, u.Username)
// Suspend
SuspendUser(cfg.Instance, cfg.Token, u.ID)
// Add counter
emptyUserNewlySuspendedCount++
}
}
}
}
}
// Log statics
log.Printf("%d total users, %d newly suspended empty users, %d already suspended empty users", allUserCount, emptyUserNewlySuspendedCount, emptyUserAlreadySuspendedCount)
}
func CheckNothingButIDCount(endpoint string, userId string, limit uint) int {
reqBody := UserIDAndLimit{
UserID: userId,
Limit: limit,
}
reqBodyBytes, _ := json.Marshal(&reqBody)
req, _ := http.NewRequest("POST", endpoint, bytes.NewReader(reqBodyBytes))
res, _ := (&http.Client{}).Do(req)
var resBody []NothingButID
_ = json.NewDecoder(res.Body).Decode(&resBody)
return len(resBody)
}
func SuspendUser(instance string, token string, userId string) {
reqBody := IAndUserID{
I: token,
UserID: userId,
}
reqBodyBytes, _ := json.Marshal(&reqBody)
req, _ := http.NewRequest("POST", instance+"/api/admin/suspend-user", bytes.NewReader(reqBodyBytes))
_, _ = (&http.Client{}).Do(req)
}