Encrypted messaging app
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.

59 lines
1.2 KiB

package Messages
import (
"encoding/json"
"fmt"
"net/http"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
"github.com/gorilla/mux"
)
// Messages gets messages by the associationKey
func Messages(w http.ResponseWriter, r *http.Request) {
var (
messages []Models.Message
message Models.Message
urlVars map[string]string
associationKey string
returnJSON []byte
i int
ok bool
err error
)
urlVars = mux.Vars(r)
associationKey, ok = urlVars["associationKey"]
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
messages, err = Database.GetMessagesByAssociationKey(associationKey)
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
for i, message = range messages {
if message.MessageData.AttachmentID == nil {
continue
}
messages[i].MessageData.Attachment.ImageLink = fmt.Sprintf(
"http://192.168.1.5:8080/files/%s",
message.MessageData.Attachment.FilePath,
)
}
returnJSON, err = json.MarshalIndent(messages, "", " ")
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(returnJSON)
}