package Messages import ( "encoding/json" "net/http" "net/url" "strings" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) // EncryptedConversationList returns an encrypted list of all Conversations func EncryptedConversationList(w http.ResponseWriter, r *http.Request) { var ( conversationDetails []Models.UserConversation userSession Models.Session returnJSON []byte err error ) userSession, err = Auth.CheckCookie(r) if err != nil { http.Error(w, "Forbidden", http.StatusUnauthorized) return } conversationDetails, err = Database.GetUserConversationsByUserId( userSession.UserID.String(), ) if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } returnJSON, err = json.MarshalIndent(conversationDetails, "", " ") if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) w.Write(returnJSON) } // EncryptedConversationDetailsList returns an encrypted list of all ConversationDetails func EncryptedConversationDetailsList(w http.ResponseWriter, r *http.Request) { var ( conversationDetails []Models.ConversationDetail detail Models.ConversationDetail query url.Values conversationIds []string returnJSON []byte i int ok bool err error ) query = r.URL.Query() conversationIds, ok = query["conversation_detail_ids"] if !ok { http.Error(w, "Invalid Data", http.StatusBadGateway) return } conversationIds = strings.Split(conversationIds[0], ",") conversationDetails, err = Database.GetConversationDetailsByIds( conversationIds, ) if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } for i, detail = range conversationDetails { if detail.AttachmentID == nil { continue } conversationDetails[i].Attachment.ImageLink = detail.Attachment.FilePath } returnJSON, err = json.MarshalIndent(conversationDetails, "", " ") if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) w.Write(returnJSON) }