You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							85 lines
						
					
					
						
							2.2 KiB
						
					
					
				
			
		
		
	
	
							85 lines
						
					
					
						
							2.2 KiB
						
					
					
				package controllers | 
						|
 | 
						|
import ( | 
						|
	"encoding/json" | 
						|
	"net/http" | 
						|
 | 
						|
	"github.com/owncast/owncast/core/chat" | 
						|
	"github.com/owncast/owncast/core/user" | 
						|
	"github.com/owncast/owncast/router/middleware" | 
						|
	log "github.com/sirupsen/logrus" | 
						|
) | 
						|
 | 
						|
// ExternalGetChatMessages gets all of the chat messages. | 
						|
func ExternalGetChatMessages(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) { | 
						|
	middleware.EnableCors(w) | 
						|
	getChatMessages(w, r) | 
						|
} | 
						|
 | 
						|
// GetChatMessages gets all of the chat messages. | 
						|
func GetChatMessages(u user.User, w http.ResponseWriter, r *http.Request) { | 
						|
	getChatMessages(w, r) | 
						|
} | 
						|
 | 
						|
func getChatMessages(w http.ResponseWriter, r *http.Request) { | 
						|
	w.Header().Set("Content-Type", "application/json") | 
						|
 | 
						|
	switch r.Method { | 
						|
	case http.MethodGet: | 
						|
		messages := chat.GetChatHistory() | 
						|
 | 
						|
		if err := json.NewEncoder(w).Encode(messages); err != nil { | 
						|
			log.Debugln(err) | 
						|
		} | 
						|
	default: | 
						|
		w.WriteHeader(http.StatusNotImplemented) | 
						|
		if err := json.NewEncoder(w).Encode(j{"error": "method not implemented (PRs are accepted)"}); err != nil { | 
						|
			InternalErrorHandler(w, err) | 
						|
		} | 
						|
	} | 
						|
} | 
						|
 | 
						|
// RegisterAnonymousChatUser will register a new user. | 
						|
func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) { | 
						|
	if r.Method != POST { | 
						|
		WriteSimpleResponse(w, false, r.Method+" not supported") | 
						|
		return | 
						|
	} | 
						|
 | 
						|
	type registerAnonymousUserRequest struct { | 
						|
		DisplayName string `json:"displayName"` | 
						|
	} | 
						|
 | 
						|
	type registerAnonymousUserResponse struct { | 
						|
		ID          string `json:"id"` | 
						|
		AccessToken string `json:"accessToken"` | 
						|
		DisplayName string `json:"displayName"` | 
						|
	} | 
						|
 | 
						|
	decoder := json.NewDecoder(r.Body) | 
						|
	var request registerAnonymousUserRequest | 
						|
	if err := decoder.Decode(&request); err != nil { //nolint | 
						|
		// this is fine. register a new user anyway. | 
						|
	} | 
						|
 | 
						|
	if request.DisplayName == "" { | 
						|
		request.DisplayName = r.Header.Get("X-Forwarded-User") | 
						|
	} | 
						|
 | 
						|
	newUser, accessToken, err := user.CreateAnonymousUser(request.DisplayName) | 
						|
	if err != nil { | 
						|
		WriteSimpleResponse(w, false, err.Error()) | 
						|
		return | 
						|
	} | 
						|
 | 
						|
	response := registerAnonymousUserResponse{ | 
						|
		ID:          newUser.ID, | 
						|
		AccessToken: accessToken, | 
						|
		DisplayName: newUser.DisplayName, | 
						|
	} | 
						|
 | 
						|
	w.Header().Set("Content-Type", "application/json") | 
						|
	middleware.DisableCache(w) | 
						|
 | 
						|
	WriteResponse(w, response) | 
						|
}
 | 
						|
 |