thisnthat vor 4 Jahren
Ursprung
Commit
d62902c437
5 geänderte Dateien mit 72 neuen und 59 gelöschten Zeilen
  1. 5 4
      discourse.go
  2. 0 49
      session.go
  3. 2 2
      sso.go
  4. 60 0
      topics.go
  5. 5 4
      users.go

+ 5 - 4
discourse.go

@@ -1,7 +1,8 @@
 package discourse
 
-type ApiConfig struct {
-	Endpoint string
-	ApiKey   string
-	ApiUser  string
+// APIConfig - configuration to make an API call to discourse
+type APIConfig struct {
+	Endpoint    string
+	APIKey      string
+	APIUsername string
 }

+ 0 - 49
session.go

@@ -1,49 +0,0 @@
-package discourse
-
-import (
-	"bytes"
-	"encoding/json"
-	"fmt"
-	"net/http"
-	"strconv"
-	"strings"
-)
-
-type Session struct {
-	apiKey      string
-	apiUser     string
-	apiEndpoint string
-}
-
-func (session Session) SendPM(usernames []string, title string, body string) (string, error) {
-	targetUsernames := strings.Join(usernames, ",")
-
-	messageJSON := map[string]interface{}{
-		"title":            title,
-		"target_usernames": targetUsernames,
-		"archetype":        "private_message",
-		"raw":              body,
-	}
-
-	payload, err := json.Marshal(messageJSON)
-	if err != nil {
-		return "", err
-	}
-
-	url := fmt.Sprintf("%s/posts.json?api_key=%s&api_username=%s", session.apiEndpoint, session.apiKey, session.apiUser)
-
-	response, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
-
-	if err != nil {
-		return "", err
-	}
-
-	var result map[string]interface{}
-
-	json.NewDecoder(response.Body).Decode(&result)
-	topicIDDigit := result["topic_id"].(float64)
-
-	topicID := strconv.FormatFloat(topicIDDigit, 'f', -1, 64)
-
-	return topicID, nil
-}

+ 2 - 2
sso.go

@@ -16,7 +16,7 @@ type SsoConfig struct {
 	Endpoint       string
 	ReturnEndpoint string
 	Secret         string
-	Uuid           uuid.UUID
+	UUID           uuid.UUID
 }
 
 // GetSSORequestURL - gets a url for an sso request
@@ -35,7 +35,7 @@ func ValidateSsoResponse(sso, sig, secret string) bool {
 
 func generateSSORequestURL(config SsoConfig) string {
 	// Build the query string payload
-	payload := fmt.Sprintf("nonce=%s&return_sso_url=%s", config.Uuid, config.ReturnEndpoint)
+	payload := fmt.Sprintf("nonce=%s&return_sso_url=%s", config.UUID, config.ReturnEndpoint)
 
 	// Base64 encode the payload
 	base64Payload := base64.StdEncoding.EncodeToString([]byte(payload))

+ 60 - 0
topics.go

@@ -0,0 +1,60 @@
+package discourse
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"strings"
+)
+
+type topic struct {
+	Body            string `json:"raw"`
+	TargetUsernames string `json:"target_usernames"`
+	Title           string `json:"title"`
+	ArchType        string `json:"archetype"`
+}
+
+type errorResponse struct {
+	Action string   `json:"action"`
+	Errors []string `json:"errors"`
+}
+
+type newPMResponse struct {
+	ID int `json:"id"`
+}
+
+const pmArchtype = "private_message"
+
+// SendPM - Send a PM on the discouse forum
+func SendPM(apiConfig APIConfig, recipients []string, title, body string) (int, error) {
+	newPm := topic{
+		ArchType:        pmArchtype,
+		TargetUsernames: strings.Join(recipients, ","),
+		Title:           title,
+		Body:            body,
+	}
+
+	payload, _ := json.Marshal(newPm)
+	fmt.Println(string(payload))
+	url := fmt.Sprintf("%s/posts.json?api_key=%s&api_username=%s",
+		apiConfig.Endpoint, apiConfig.APIKey, apiConfig.APIUsername)
+
+	response, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
+	if err != nil {
+		return 0, err
+	}
+
+	if response.StatusCode != 200 {
+		var errorResult errorResponse
+		json.NewDecoder(response.Body).Decode(&errorResult)
+
+		return 0, fmt.Errorf("Failed to send PM. %s", strings.Join(errorResult.Errors, "; "))
+	}
+
+	var result newPMResponse
+
+	json.NewDecoder(response.Body).Decode(&result)
+
+	return result.ID, nil
+}

+ 5 - 4
users.go

@@ -6,7 +6,7 @@ import (
 	"net/http"
 )
 
-// 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"`
@@ -15,17 +15,18 @@ type UserResponse struct {
 
 // User - A discoruse User
 type User struct {
-	ID        int     `json:"id"`
+	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"`
 	Groups    []Group `json:"groups"`
+	GroupStr  string  `schema:"groups"`
 }
 
 // GetUser - Get a discourse user
-func GetUser(config ApiConfig, username string) User {
-	url := fmt.Sprintf("%s/users/%s.json?api_key=%s&api_username=%s", config.Endpoint, username, config.ApiKey, config.ApiUser)
+func GetUser(config APIConfig, username string) User {
+	url := fmt.Sprintf("%s/users/%s.json?api_key=%s&api_username=%s", config.Endpoint, username, config.APIKey, config.APIUsername)
 
 	response, _ := http.Get(url)