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.

118 lines
2.7 KiB

package Messages
import (
"database/sql/driver"
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
)
// ConversationList returns an encrypted list of all Conversations
func ConversationList(w http.ResponseWriter, r *http.Request) {
var (
conversationDetails []Database.UserConversation
userSession Database.Session
returnJSON []byte
values url.Values
page int
err error
)
values = r.URL.Query()
page, err = strconv.Atoi(values.Get("page"))
if err != nil {
page = 0
}
userSession, _ = Auth.CheckCookie(r)
conversationDetails, err = Database.GetUserConversationsByUserId(
userSession.UserID.String(),
page,
)
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)
}
// ConversationDetailsList returns an encrypted list of all ConversationDetails
func ConversationDetailsList(w http.ResponseWriter, r *http.Request) {
var (
conversationDetails []Database.ConversationDetail
detail Database.ConversationDetail
query url.Values
conversationIds []string
updatedAtString []string
updatedAt time.Time
messageExpiryRaw driver.Value
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], ",")
updatedAtString, ok = query["updated_at"]
if ok {
updatedAt, err = time.Parse(time.RFC3339, updatedAtString[0])
if err != nil {
http.Error(w, "Invalid Data", http.StatusBadGateway)
return
}
}
conversationDetails, err = Database.GetConversationDetailsByIds(
conversationIds,
updatedAt,
)
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
for i, detail = range conversationDetails {
messageExpiryRaw, _ = detail.MessageExpiryDefault.Value()
conversationDetails[i].MessageExpiry, _ = messageExpiryRaw.(string)
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)
}