|
@@ -3,34 +3,96 @@ package discourse
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "io/ioutil"
|
|
"strings"
|
|
"strings"
|
|
|
|
+ "time"
|
|
|
|
+
|
|
|
|
+ "github.com/sirupsen/logrus"
|
|
)
|
|
)
|
|
|
|
|
|
// UserResponse - Structure of a discourse user api response// UserResponse - Structure of a discourse user api response
|
|
// UserResponse - Structure of a discourse user api response// UserResponse - Structure of a discourse user api response
|
|
-type UserResponse struct {
|
|
|
|
- User User `json:"user"`
|
|
|
|
- Errors []string `json:"errors"`
|
|
|
|
- ErrorType string `json:"error_type"`
|
|
|
|
-}
|
|
|
|
|
|
+// type UserResponse struct {
|
|
|
|
+// User User `json:"user"`
|
|
|
|
+// Errors []string `json:"errors"`
|
|
|
|
+// ErrorType string `json:"error_type"`
|
|
|
|
+// }
|
|
|
|
|
|
// User - A discoruse User
|
|
// User - A discoruse User
|
|
-type User struct {
|
|
|
|
- ID int `json:"id" schema:"external_id"`
|
|
|
|
- Username string `json:"username"`
|
|
|
|
- CanSendPM bool `json:"can_send_private_messages"`
|
|
|
|
- Moderator bool `json:"moderator"`
|
|
|
|
- Admin bool `json:"admin"`
|
|
|
|
- TrustLevel int `json:"trust_level"`
|
|
|
|
- Groups []Group `json:"groups"`
|
|
|
|
- GroupStr string `schema:"groups"`
|
|
|
|
-}
|
|
|
|
|
|
+// type User struct {
|
|
|
|
+// ID int `json:"id" schema:"external_id"`
|
|
|
|
+// Username string `json:"username"`
|
|
|
|
+// CanSendPM bool `json:"can_send_private_messages"`
|
|
|
|
+// Moderator bool `json:"moderator"`
|
|
|
|
+// Admin bool `json:"admin"`
|
|
|
|
+// TrustLevel int `json:"trust_level"`
|
|
|
|
+// Groups []Group `json:"groups"`
|
|
|
|
+// GroupStr string `schema:"groups"`
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// Generated by https://quicktype.io
|
|
|
|
+
|
|
|
|
+// type User struct {
|
|
|
|
+// ID int64 `json:"id"`
|
|
|
|
+// Username string `json:"username"`
|
|
|
|
+// Name interface{} `json:"name"`
|
|
|
|
+// AvatarTemplate string `json:"avatar_template"`
|
|
|
|
+// Email string `json:"email"`
|
|
|
|
+// SecondaryEmails []interface{} `json:"secondary_emails"`
|
|
|
|
+// Active bool `json:"active"`
|
|
|
|
+// Admin bool `json:"admin"`
|
|
|
|
+// Moderator bool `json:"moderator"`
|
|
|
|
+// LastSeenAt interface{} `json:"last_seen_at"`
|
|
|
|
+// LastEmailedAt interface{} `json:"last_emailed_at"`
|
|
|
|
+// CreatedAt string `json:"created_at"`
|
|
|
|
+// LastSeenAge interface{} `json:"last_seen_age"`
|
|
|
|
+// LastEmailedAge interface{} `json:"last_emailed_age"`
|
|
|
|
+// CreatedAtAge interface{} `json:"created_at_age"`
|
|
|
|
+// TrustLevel int64 `json:"trust_level"`
|
|
|
|
+// ManualLockedTrustLevel interface{} `json:"manual_locked_trust_level"`
|
|
|
|
+// FlagLevel int64 `json:"flag_level"`
|
|
|
|
+// Title interface{} `json:"title"`
|
|
|
|
+// TimeRead int64 `json:"time_read"`
|
|
|
|
+// Staged bool `json:"staged"`
|
|
|
|
+// DaysVisited int64 `json:"days_visited"`
|
|
|
|
+// PostsReadCount int64 `json:"posts_read_count"`
|
|
|
|
+// TopicsEntered int64 `json:"topics_entered"`
|
|
|
|
+// PostCount int64 `json:"post_count"`
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// Generated by https://quicktype.io
|
|
|
|
+
|
|
|
|
+// type GroupMembers struct {
|
|
|
|
+// Members []Member `json:"members"`
|
|
|
|
+// Owners []Member `json:"owners"`
|
|
|
|
+// Meta Meta `json:"meta"`
|
|
|
|
+// Errors []string `json:"errors"`
|
|
|
|
+// ErrorType string `json:"error_type"`
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// type Member struct {
|
|
|
|
+// ID int64 `json:"id"`
|
|
|
|
+// Username string `json:"username"`
|
|
|
|
+// Name interface{} `json:"name"`
|
|
|
|
+// AvatarTemplate string `json:"avatar_template"`
|
|
|
|
+// Title interface{} `json:"title"`
|
|
|
|
+// LastPostedAt string `json:"last_posted_at"`
|
|
|
|
+// LastSeenAt string `json:"last_seen_at"`
|
|
|
|
+// AddedAt string `json:"added_at"`
|
|
|
|
+// Timezone string `json:"timezone"`
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// type Meta struct {
|
|
|
|
+// Total int64 `json:"total"`
|
|
|
|
+// Limit int64 `json:"limit"`
|
|
|
|
+// Offset int64 `json:"offset"`
|
|
|
|
+// }
|
|
|
|
|
|
// GetUser - Get a discourse user
|
|
// GetUser - Get a discourse user
|
|
func GetUser(config APIConfig, username string) (User, error) {
|
|
func GetUser(config APIConfig, username string) (User, error) {
|
|
url := fmt.Sprintf("%s/users/%s.json", config.Endpoint, username)
|
|
url := fmt.Sprintf("%s/users/%s.json", config.Endpoint, username)
|
|
-
|
|
|
|
|
|
+ fmt.Println(url)
|
|
req, _ := newGetRequest(config, url)
|
|
req, _ := newGetRequest(config, url)
|
|
- client := getClient()
|
|
|
|
|
|
+ client := getClient(rl)
|
|
response, _ := client.Do(req)
|
|
response, _ := client.Do(req)
|
|
|
|
|
|
var result *UserResponse
|
|
var result *UserResponse
|
|
@@ -47,7 +109,7 @@ func GetUserByID(config APIConfig, userID int) (User, error) {
|
|
url := fmt.Sprintf("%s/admin/users/%d.json", config.Endpoint, userID)
|
|
url := fmt.Sprintf("%s/admin/users/%d.json", config.Endpoint, userID)
|
|
|
|
|
|
req, _ := newGetRequest(config, url)
|
|
req, _ := newGetRequest(config, url)
|
|
- client := getClient()
|
|
|
|
|
|
+ client := getClient(rl)
|
|
response, _ := client.Do(req)
|
|
response, _ := client.Do(req)
|
|
|
|
|
|
var result *UserResponse
|
|
var result *UserResponse
|
|
@@ -58,4 +120,51 @@ func GetUserByID(config APIConfig, userID int) (User, error) {
|
|
}
|
|
}
|
|
|
|
|
|
return result.User, nil
|
|
return result.User, nil
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetUsersByMinTrustLevel(config APIConfig, minTrustLevel int64) ([]User, error) {
|
|
|
|
+ page := 1
|
|
|
|
+ getMoreResults := true
|
|
|
|
+
|
|
|
|
+ var users []User
|
|
|
|
+
|
|
|
|
+ for getMoreResults {
|
|
|
|
+ var pageResults, _ = GetUsersPage(config, page)
|
|
|
|
+ if len(pageResults) > 0 {
|
|
|
|
+ for _, v := range pageResults {
|
|
|
|
+ if v.Staged == false && v.TrustLevel >= minTrustLevel {
|
|
|
|
+ u, err := GetUser(config, v.Username)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ users = append(users, u)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ page++
|
|
|
|
+ } else {
|
|
|
|
+ getMoreResults = false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return users, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetUsersPage(config APIConfig, page int) ([]User, error) {
|
|
|
|
+ url := fmt.Sprintf("%s/admin/users/list/active.json?page=%d&?filter=&show_emails=false", config.Endpoint, page)
|
|
|
|
+ fmt.Println(url)
|
|
|
|
+ req, _ := newGetRequest(config, url)
|
|
|
|
+ client := getClient(rl)
|
|
|
|
+ response, _ := client.Do(req)
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
+
|
|
|
|
+ bodyBytes, err := ioutil.ReadAll(response.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logrus.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var users []User
|
|
|
|
+
|
|
|
|
+ json.Unmarshal(bodyBytes, &users)
|
|
|
|
+
|
|
|
|
+ return users, nil
|
|
|
|
+}
|