From 78d51f70b8f7b158dca15725d2c72860f75ce9e7 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Wed, 19 Oct 2022 20:10:16 +1030 Subject: [PATCH] Refactor to use model based create / update / delete --- Backend/Api/Auth/AddProfileImage.go | 7 ++- Backend/Api/Auth/AddProfileImage_test.go | 3 +- Backend/Api/Auth/ChangePassword.go | 8 +-- Backend/Api/Auth/ChangeUserMessageExpiry.go | 8 +-- Backend/Api/Auth/Login.go | 9 ++-- Backend/Api/Auth/Login_test.go | 3 +- Backend/Api/Auth/Logout_test.go | 3 +- Backend/Api/Auth/Session.go | 13 +++-- Backend/Api/Auth/Signup.go | 5 +- Backend/Api/Auth/Signup_test.go | 3 +- Backend/Api/Friends/AcceptFriendRequest.go | 9 ++-- .../Api/Friends/AcceptFriendRequest_test.go | 9 ++-- Backend/Api/Friends/CreateFriendRequest.go | 9 ++-- .../Api/Friends/CreateFriendRequest_test.go | 13 +++-- Backend/Api/Friends/Friends.go | 5 +- Backend/Api/Friends/Friends_test.go | 7 ++- Backend/Api/Friends/RejectFriendRequest.go | 5 +- Backend/Api/Messages/AddConversationImage.go | 7 ++- .../ChangeConversationMessageExpiry.go | 8 +-- Backend/Api/Messages/Conversations.go | 9 ++-- Backend/Api/Messages/Conversations_test.go | 23 ++++---- Backend/Api/Messages/CreateConversation.go | 27 +++++----- .../Api/Messages/CreateConversation_test.go | 17 +++--- Backend/Api/Messages/CreateMessage.go | 11 ++-- Backend/Api/Messages/CreateMessage_test.go | 6 +-- Backend/Api/Messages/MessageThread.go | 5 +- Backend/Api/Messages/MessageThread_test.go | 17 +++--- Backend/Api/Messages/UpdateConversation.go | 19 ++++--- .../Api/Messages/UpdateConversation_test.go | 31 ++++++----- Backend/Api/Users/SearchUsers.go | 3 +- Backend/Api/Users/SearchUsers_test.go | 4 +- Backend/Database/Attachments.go | 24 ++++++--- Backend/{Models => Database}/Base.go | 2 +- Backend/Database/ConversationDetailUsers.go | 33 ++++++++---- Backend/Database/ConversationDetails.go | 31 ++++++++--- Backend/Database/FriendRequests.go | 39 ++++++++++---- Backend/Database/Init.go | 20 ++++--- Backend/Database/MessageData.go | 22 +++++--- Backend/{Models => Database}/MessageExpiry.go | 2 +- Backend/Database/Messages.go | 33 +++++++++--- Backend/Database/Seeder/FriendSeeder.go | 13 +++-- Backend/Database/Seeder/MessageSeeder.go | 53 +++++++++---------- Backend/Database/Seeder/UserSeeder.go | 11 ++-- Backend/Database/Sessions.go | 25 ++++++--- Backend/Database/UserConversations.go | 43 ++++++++++----- Backend/Database/Users.go | 51 +++++++++++++----- Backend/Models/Attachments.go | 11 ---- Backend/Models/Conversations.go | 45 ---------------- Backend/Models/Friends.go | 22 -------- Backend/Models/Messages.go | 31 ----------- Backend/Models/Sessions.go | 18 ------- Backend/Models/Users.go | 33 ------------ Backend/Tests/Init.go | 13 +++-- Backend/Util/UserHelper.go | 5 +- Backend/dev.sh | 2 +- 55 files changed, 408 insertions(+), 480 deletions(-) rename Backend/{Models => Database}/Base.go (96%) rename Backend/{Models => Database}/MessageExpiry.go (99%) delete mode 100644 Backend/Models/Attachments.go delete mode 100644 Backend/Models/Conversations.go delete mode 100644 Backend/Models/Friends.go delete mode 100644 Backend/Models/Messages.go delete mode 100644 Backend/Models/Sessions.go delete mode 100644 Backend/Models/Users.go diff --git a/Backend/Api/Auth/AddProfileImage.go b/Backend/Api/Auth/AddProfileImage.go index af2a8ce..b1cb514 100644 --- a/Backend/Api/Auth/AddProfileImage.go +++ b/Backend/Api/Auth/AddProfileImage.go @@ -6,15 +6,14 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" ) // AddProfileImage adds a profile image func AddProfileImage(w http.ResponseWriter, r *http.Request) { var ( - user Models.User - attachment Models.Attachment + user Database.User + attachment Database.Attachment decodedFile []byte fileName string err error @@ -50,7 +49,7 @@ func AddProfileImage(w http.ResponseWriter, r *http.Request) { user.Attachment = attachment - err = Database.UpdateUser(user.ID.String(), &user) + err = (&user).UpdateUser() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Auth/AddProfileImage_test.go b/Backend/Api/Auth/AddProfileImage_test.go index 02daea1..526a025 100644 --- a/Backend/Api/Auth/AddProfileImage_test.go +++ b/Backend/Api/Auth/AddProfileImage_test.go @@ -10,7 +10,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -40,7 +39,7 @@ func Test_AddProfileImage(t *testing.T) { return } - a := Models.Attachment{ + a := Database.Attachment{ Mimetype: "image/png", Extension: "png", Data: base64.StdEncoding.EncodeToString(encDat), diff --git a/Backend/Api/Auth/ChangePassword.go b/Backend/Api/Auth/ChangePassword.go index 5972a64..2ec7dff 100644 --- a/Backend/Api/Auth/ChangePassword.go +++ b/Backend/Api/Auth/ChangePassword.go @@ -6,7 +6,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) type rawChangePassword struct { @@ -19,7 +18,7 @@ type rawChangePassword struct { // ChangePassword handle change password action func ChangePassword(w http.ResponseWriter, r *http.Request) { var ( - user Models.User + user Database.User changePassword rawChangePassword requestBody []byte err error @@ -63,10 +62,7 @@ func ChangePassword(w http.ResponseWriter, r *http.Request) { // Private key doesn't change at this point, is just re-encrypted with the new password user.AsymmetricPrivateKey = changePassword.PrivateKey - err = Database.UpdateUser( - user.ID.String(), - &user, - ) + err = (&user).UpdateUser() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Auth/ChangeUserMessageExpiry.go b/Backend/Api/Auth/ChangeUserMessageExpiry.go index 1ae0fc6..818401e 100644 --- a/Backend/Api/Auth/ChangeUserMessageExpiry.go +++ b/Backend/Api/Auth/ChangeUserMessageExpiry.go @@ -6,7 +6,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) type rawChangeMessageExpiry struct { @@ -16,7 +15,7 @@ type rawChangeMessageExpiry struct { // ChangeUserMessageExpiry handles changing default message expiry for user func ChangeUserMessageExpiry(w http.ResponseWriter, r *http.Request) { var ( - user Models.User + user Database.User changeMessageExpiry rawChangeMessageExpiry requestBody []byte err error @@ -43,10 +42,7 @@ func ChangeUserMessageExpiry(w http.ResponseWriter, r *http.Request) { return } - err = Database.UpdateUser( - user.ID.String(), - &user, - ) + err = (&user).UpdateUser() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Auth/Login.go b/Backend/Api/Auth/Login.go index d217493..31a1527 100644 --- a/Backend/Api/Auth/Login.go +++ b/Backend/Api/Auth/Login.go @@ -7,7 +7,6 @@ import ( "time" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) type credentials struct { @@ -29,8 +28,8 @@ type loginResponse struct { func Login(w http.ResponseWriter, r *http.Request) { var ( creds credentials - user Models.User - session Models.Session + user Database.User + session Database.Session expiresAt time.Time messageExpiryRaw driver.Value messageExpiry string @@ -59,12 +58,12 @@ func Login(w http.ResponseWriter, r *http.Request) { // TODO: Revisit before production expiresAt = time.Now().Add(12 * time.Hour) - session = Models.Session{ + session = Database.Session{ UserID: user.ID, Expiry: expiresAt, } - err = Database.CreateSession(&session) + err = (&session).CreateSession() if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return diff --git a/Backend/Api/Auth/Login_test.go b/Backend/Api/Auth/Login_test.go index b6bb982..a19257d 100644 --- a/Backend/Api/Auth/Login_test.go +++ b/Backend/Api/Auth/Login_test.go @@ -7,7 +7,6 @@ import ( "testing" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -50,7 +49,7 @@ func Test_Login(t *testing.T) { return } - var session Models.Session + var session Database.Session err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error diff --git a/Backend/Api/Auth/Logout_test.go b/Backend/Api/Auth/Logout_test.go index b5d5150..b2e3fa0 100644 --- a/Backend/Api/Auth/Logout_test.go +++ b/Backend/Api/Auth/Logout_test.go @@ -5,7 +5,6 @@ import ( "testing" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -28,7 +27,7 @@ func Test_Logout(t *testing.T) { return } - var session Models.Session + var session Database.Session u, err := Database.GetUserByUsername("test") if err != nil { diff --git a/Backend/Api/Auth/Session.go b/Backend/Api/Auth/Session.go index 7334a6c..c237308 100644 --- a/Backend/Api/Auth/Session.go +++ b/Backend/Api/Auth/Session.go @@ -5,14 +5,13 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) -func CheckCookie(r *http.Request) (Models.Session, error) { +func CheckCookie(r *http.Request) (Database.Session, error) { var ( c *http.Cookie sessionToken string - userSession Models.Session + userSession Database.Session err error ) @@ -31,17 +30,17 @@ func CheckCookie(r *http.Request) (Models.Session, error) { // If the session is present, but has expired, we can delete the session, and return // an unauthorized status if userSession.IsExpired() { - Database.DeleteSession(&userSession) + (&userSession).DeleteSession() return userSession, errors.New("Cookie expired") } return userSession, nil } -func CheckCookieCurrentUser(w http.ResponseWriter, r *http.Request) (Models.User, error) { +func CheckCookieCurrentUser(w http.ResponseWriter, r *http.Request) (Database.User, error) { var ( - session Models.Session - userData Models.User + session Database.Session + userData Database.User err error ) diff --git a/Backend/Api/Auth/Signup.go b/Backend/Api/Auth/Signup.go index bfae9d0..44674a2 100644 --- a/Backend/Api/Auth/Signup.go +++ b/Backend/Api/Auth/Signup.go @@ -5,7 +5,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) type signup struct { @@ -20,7 +19,7 @@ type signup struct { // Signup to the platform func Signup(w http.ResponseWriter, r *http.Request) { var ( - user Models.User + user Database.User err error ) @@ -58,7 +57,7 @@ func Signup(w http.ResponseWriter, r *http.Request) { return } - err = Database.CreateUser(&user) + err = (&user).CreateUser() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Auth/Signup_test.go b/Backend/Api/Auth/Signup_test.go index 02016dd..4e74892 100644 --- a/Backend/Api/Auth/Signup_test.go +++ b/Backend/Api/Auth/Signup_test.go @@ -13,7 +13,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) @@ -65,7 +64,7 @@ func Test_Signup(t *testing.T) { return } - var user Models.User + var user Database.User err = Database.DB.First(&user, "username = ?", "test").Error diff --git a/Backend/Api/Friends/AcceptFriendRequest.go b/Backend/Api/Friends/AcceptFriendRequest.go index aa9e233..bf08039 100644 --- a/Backend/Api/Friends/AcceptFriendRequest.go +++ b/Backend/Api/Friends/AcceptFriendRequest.go @@ -7,15 +7,14 @@ import ( "time" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) // AcceptFriendRequest accepts friend requests func AcceptFriendRequest(w http.ResponseWriter, r *http.Request) { var ( - oldFriendRequest Models.FriendRequest - newFriendRequest Models.FriendRequest + oldFriendRequest Database.FriendRequest + newFriendRequest Database.FriendRequest urlVars map[string]string friendRequestID string requestBody []byte @@ -51,7 +50,7 @@ func AcceptFriendRequest(w http.ResponseWriter, r *http.Request) { return } - err = Database.UpdateFriendRequest(&oldFriendRequest) + err = (&oldFriendRequest).UpdateFriendRequest() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return @@ -60,7 +59,7 @@ func AcceptFriendRequest(w http.ResponseWriter, r *http.Request) { newFriendRequest.AcceptedAt.Time = time.Now() newFriendRequest.AcceptedAt.Valid = true - err = Database.CreateFriendRequest(&newFriendRequest) + err = (&newFriendRequest).CreateFriendRequest() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Friends/AcceptFriendRequest_test.go b/Backend/Api/Friends/AcceptFriendRequest_test.go index 6f44d19..d6aec77 100644 --- a/Backend/Api/Friends/AcceptFriendRequest_test.go +++ b/Backend/Api/Friends/AcceptFriendRequest_test.go @@ -10,7 +10,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -47,7 +46,7 @@ func Test_AcceptFriendRequest(t *testing.T) { return } - friendReq := Models.FriendRequest{ + friendReq := Database.FriendRequest{ UserID: u.ID, FriendID: base64.StdEncoding.EncodeToString( Seeder.EncryptWithPublicKey( @@ -69,13 +68,13 @@ func Test_AcceptFriendRequest(t *testing.T) { ), } - err = Database.CreateFriendRequest(&friendReq) + err = (&friendReq).CreateFriendRequest() if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) return } - friendReqResponse := Models.FriendRequest{ + friendReqResponse := Database.FriendRequest{ UserID: u2.ID, FriendID: base64.StdEncoding.EncodeToString( Seeder.EncryptWithPublicKey( @@ -120,7 +119,7 @@ func Test_AcceptFriendRequest(t *testing.T) { return } - var reqs []Models.FriendRequest + var reqs []Database.FriendRequest err = Database.DB.Find(&reqs).Error if err != nil { diff --git a/Backend/Api/Friends/CreateFriendRequest.go b/Backend/Api/Friends/CreateFriendRequest.go index a1db196..c351d15 100644 --- a/Backend/Api/Friends/CreateFriendRequest.go +++ b/Backend/Api/Friends/CreateFriendRequest.go @@ -7,13 +7,12 @@ import ( "time" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) // CreateFriendRequest creates a FriendRequest from post data func CreateFriendRequest(w http.ResponseWriter, r *http.Request) { var ( - friendRequest Models.FriendRequest + friendRequest Database.FriendRequest requestBody []byte returnJSON []byte err error @@ -33,7 +32,7 @@ func CreateFriendRequest(w http.ResponseWriter, r *http.Request) { friendRequest.AcceptedAt.Scan(nil) - err = Database.CreateFriendRequest(&friendRequest) + err = (&friendRequest).CreateFriendRequest() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return @@ -53,7 +52,7 @@ func CreateFriendRequest(w http.ResponseWriter, r *http.Request) { // CreateFriendRequestQrCode creates a FriendRequest from post data from qr code scan func CreateFriendRequestQrCode(w http.ResponseWriter, r *http.Request) { var ( - friendRequests []Models.FriendRequest + friendRequests Database.FriendRequestList requestBody []byte i int err error @@ -76,7 +75,7 @@ func CreateFriendRequestQrCode(w http.ResponseWriter, r *http.Request) { friendRequests[i].AcceptedAt.Valid = true } - err = Database.CreateFriendRequests(&friendRequests) + err = (&friendRequests).CreateFriendRequests() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Friends/CreateFriendRequest_test.go b/Backend/Api/Friends/CreateFriendRequest_test.go index 2367d60..85775b5 100644 --- a/Backend/Api/Friends/CreateFriendRequest_test.go +++ b/Backend/Api/Friends/CreateFriendRequest_test.go @@ -9,7 +9,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -46,7 +45,7 @@ func Test_CreateFriendRequest(t *testing.T) { return } - friendReq := Models.FriendRequest{ + friendReq := Database.FriendRequest{ UserID: u.ID, FriendID: base64.StdEncoding.EncodeToString( Seeder.EncryptWithPublicKey( @@ -83,7 +82,7 @@ func Test_CreateFriendRequest(t *testing.T) { return } - var r Models.FriendRequest + var r Database.FriendRequest err = Database.DB.First(&r).Error if err != nil { @@ -130,7 +129,7 @@ func Test_CreateFriendRequestQrCode(t *testing.T) { return } - friendReq := Models.FriendRequest{ + friendReq := Database.FriendRequest{ UserID: u.ID, FriendID: base64.StdEncoding.EncodeToString( Seeder.EncryptWithPublicKey( @@ -152,7 +151,7 @@ func Test_CreateFriendRequestQrCode(t *testing.T) { ), } - friendReq2 := Models.FriendRequest{ + friendReq2 := Database.FriendRequest{ UserID: u2.ID, FriendID: base64.StdEncoding.EncodeToString( Seeder.EncryptWithPublicKey( @@ -174,7 +173,7 @@ func Test_CreateFriendRequestQrCode(t *testing.T) { ), } - jsonStr, _ := json.Marshal([]Models.FriendRequest{friendReq, friendReq2}) + jsonStr, _ := json.Marshal([]Database.FriendRequest{friendReq, friendReq2}) req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/friend_request/qr_code", bytes.NewBuffer(jsonStr)) req.Header.Set("Content-Type", "application/json") @@ -189,7 +188,7 @@ func Test_CreateFriendRequestQrCode(t *testing.T) { return } - var r Models.FriendRequest + var r Database.FriendRequest err = Database.DB.First(&r).Error if err != nil { diff --git a/Backend/Api/Friends/Friends.go b/Backend/Api/Friends/Friends.go index 26850d3..7321902 100644 --- a/Backend/Api/Friends/Friends.go +++ b/Backend/Api/Friends/Friends.go @@ -8,14 +8,13 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) // FriendRequestList gets friend request list func FriendRequestList(w http.ResponseWriter, r *http.Request) { var ( - userSession Models.Session - friends []Models.FriendRequest + userSession Database.Session + friends Database.FriendRequestList values url.Values returnJSON []byte page int diff --git a/Backend/Api/Friends/Friends_test.go b/Backend/Api/Friends/Friends_test.go index c58d7f6..cfa52d1 100644 --- a/Backend/Api/Friends/Friends_test.go +++ b/Backend/Api/Friends/Friends_test.go @@ -11,7 +11,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -45,7 +44,7 @@ func Test_FriendRequestList(t *testing.T) { return } - friendReq := Models.FriendRequest{ + friendReq := Database.FriendRequest{ UserID: u.ID, FriendID: base64.StdEncoding.EncodeToString( Seeder.EncryptWithPublicKey( @@ -72,7 +71,7 @@ func Test_FriendRequestList(t *testing.T) { friendReq.AcceptedAt.Valid = true } - err = Database.CreateFriendRequest(&friendReq) + err = (&friendReq).CreateFriendRequest() if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) return @@ -98,7 +97,7 @@ func Test_FriendRequestList(t *testing.T) { return } - var users []Models.FriendRequest + var users Database.FriendRequestList json.Unmarshal(requestBody, &users) diff --git a/Backend/Api/Friends/RejectFriendRequest.go b/Backend/Api/Friends/RejectFriendRequest.go index e341858..203793b 100644 --- a/Backend/Api/Friends/RejectFriendRequest.go +++ b/Backend/Api/Friends/RejectFriendRequest.go @@ -4,7 +4,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) @@ -12,7 +11,7 @@ import ( // RejectFriendRequest rejects friend requests func RejectFriendRequest(w http.ResponseWriter, r *http.Request) { var ( - friendRequest Models.FriendRequest + friendRequest Database.FriendRequest urlVars map[string]string friendRequestID string ok bool @@ -32,7 +31,7 @@ func RejectFriendRequest(w http.ResponseWriter, r *http.Request) { return } - err = Database.DeleteFriendRequest(&friendRequest) + err = (&friendRequest).DeleteFriendRequest() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Messages/AddConversationImage.go b/Backend/Api/Messages/AddConversationImage.go index 1da2866..8c05134 100644 --- a/Backend/Api/Messages/AddConversationImage.go +++ b/Backend/Api/Messages/AddConversationImage.go @@ -6,7 +6,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" "github.com/gorilla/mux" ) @@ -14,8 +13,8 @@ import ( // AddConversationImage adds an image for a conversation icon func AddConversationImage(w http.ResponseWriter, r *http.Request) { var ( - attachment Models.Attachment - conversationDetail Models.ConversationDetail + attachment Database.Attachment + conversationDetail Database.ConversationDetail urlVars map[string]string detailID string decodedFile []byte @@ -54,7 +53,7 @@ func AddConversationImage(w http.ResponseWriter, r *http.Request) { conversationDetail.Attachment = attachment - err = Database.UpdateConversationDetail(&conversationDetail) + err = (&conversationDetail).UpdateConversationDetail() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Messages/ChangeConversationMessageExpiry.go b/Backend/Api/Messages/ChangeConversationMessageExpiry.go index 1dd4218..d3953ae 100644 --- a/Backend/Api/Messages/ChangeConversationMessageExpiry.go +++ b/Backend/Api/Messages/ChangeConversationMessageExpiry.go @@ -6,7 +6,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) @@ -17,9 +16,8 @@ type rawChangeMessageExpiry struct { // ChangeUserMessageExpiry handles changing default message expiry for user func ChangeConversationMessageExpiry(w http.ResponseWriter, r *http.Request) { var ( - // user Models.User changeMessageExpiry rawChangeMessageExpiry - conversationDetail Models.ConversationDetail + conversationDetail Database.ConversationDetail requestBody []byte urlVars map[string]string detailID string @@ -62,9 +60,7 @@ func ChangeConversationMessageExpiry(w http.ResponseWriter, r *http.Request) { return } - err = Database.UpdateConversationDetail( - &conversationDetail, - ) + err = (&conversationDetail).UpdateConversationDetail() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Messages/Conversations.go b/Backend/Api/Messages/Conversations.go index 47cebb5..74dbf8a 100644 --- a/Backend/Api/Messages/Conversations.go +++ b/Backend/Api/Messages/Conversations.go @@ -10,14 +10,13 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) // ConversationList returns an encrypted list of all Conversations func ConversationList(w http.ResponseWriter, r *http.Request) { var ( - conversationDetails []Models.UserConversation - userSession Models.Session + conversationDetails []Database.UserConversation + userSession Database.Session returnJSON []byte values url.Values page int @@ -55,8 +54,8 @@ func ConversationList(w http.ResponseWriter, r *http.Request) { // ConversationDetailsList returns an encrypted list of all ConversationDetails func ConversationDetailsList(w http.ResponseWriter, r *http.Request) { var ( - conversationDetails []Models.ConversationDetail - detail Models.ConversationDetail + conversationDetails []Database.ConversationDetail + detail Database.ConversationDetail query url.Values conversationIds []string messageExpiryRaw driver.Value diff --git a/Backend/Api/Messages/Conversations_test.go b/Backend/Api/Messages/Conversations_test.go index a289d70..8254e1d 100644 --- a/Backend/Api/Messages/Conversations_test.go +++ b/Backend/Api/Messages/Conversations_test.go @@ -10,7 +10,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -41,12 +40,12 @@ func Test_ConversationsList(t *testing.T) { return } - messageThread := Models.ConversationDetail{ + messageThread := Database.ConversationDetail{ Name: base64.StdEncoding.EncodeToString(nameCiphertext), TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext), } - err = Database.CreateConversationDetail(&messageThread) + err = (&messageThread).CreateConversationDetail() if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) return @@ -66,7 +65,7 @@ func Test_ConversationsList(t *testing.T) { pubKey := Seeder.GetPubKey() - messageThreadUser := Models.UserConversation{ + messageThreadUser := Database.UserConversation{ UserID: u.ID, ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext), Admin: base64.StdEncoding.EncodeToString(adminCiphertext), @@ -75,7 +74,7 @@ func Test_ConversationsList(t *testing.T) { ), } - err = Database.CreateUserConversation(&messageThreadUser) + err = (&messageThreadUser).CreateUserConversation() req, _ := http.NewRequest("GET", ts.URL+"/api/v1/auth/conversations", nil) @@ -96,7 +95,7 @@ func Test_ConversationsList(t *testing.T) { return } - var conversations []Models.UserConversation + var conversations Database.UserConversationList json.Unmarshal(requestBody, &conversations) @@ -130,7 +129,7 @@ func Test_ConversationsList(t *testing.T) { return } - var conversationDetails []Models.ConversationDetail + var conversationDetails []Database.ConversationDetail requestBody, err = ioutil.ReadAll(resp.Body) if err != nil { @@ -190,12 +189,12 @@ func Test_ConversationsListPagination(t *testing.T) { return } - messageThread := Models.ConversationDetail{ + messageThread := Database.ConversationDetail{ Name: base64.StdEncoding.EncodeToString(nameCiphertext), TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext), } - err = Database.CreateConversationDetail(&messageThread) + err = (&messageThread).CreateConversationDetail() if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) return @@ -215,7 +214,7 @@ func Test_ConversationsListPagination(t *testing.T) { pubKey := Seeder.GetPubKey() - messageThreadUser := Models.UserConversation{ + messageThreadUser := Database.UserConversation{ UserID: u.ID, ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext), Admin: base64.StdEncoding.EncodeToString(adminCiphertext), @@ -224,7 +223,7 @@ func Test_ConversationsListPagination(t *testing.T) { ), } - err = Database.CreateUserConversation(&messageThreadUser) + err = (&messageThreadUser).CreateUserConversation() } req, _ := http.NewRequest("GET", ts.URL+"/api/v1/auth/conversations?page=0", nil) @@ -246,7 +245,7 @@ func Test_ConversationsListPagination(t *testing.T) { return } - var conversations []Models.UserConversation + var conversations []Database.UserConversation json.Unmarshal(requestBody, &conversations) diff --git a/Backend/Api/Messages/CreateConversation.go b/Backend/Api/Messages/CreateConversation.go index 9fafdf9..76fe5e1 100644 --- a/Backend/Api/Messages/CreateConversation.go +++ b/Backend/Api/Messages/CreateConversation.go @@ -7,26 +7,25 @@ import ( "github.com/gofrs/uuid" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) // RawCreateConversationData for holding POST payload type RawCreateConversationData struct { - ID string `json:"id"` - Name string `json:"name"` - TwoUser string `json:"two_user"` - AdminAddMembers string `json:"admin_add_members"` - AdminEditInfo string `json:"admin_edit_info"` - AdminSendMessages string `json:"admin_send_messages"` - Users []Models.ConversationDetailUser `json:"users"` - UserConversations []Models.UserConversation `json:"user_conversations"` + ID string `json:"id"` + Name string `json:"name"` + TwoUser string `json:"two_user"` + AdminAddMembers string `json:"admin_add_members"` + AdminEditInfo string `json:"admin_edit_info"` + AdminSendMessages string `json:"admin_send_messages"` + Users []Database.ConversationDetailUser `json:"users"` + UserConversations Database.UserConversationList `json:"user_conversations"` } // CreateConversation creates ConversationDetail, ConversationDetailUsers and UserConversations func CreateConversation(w http.ResponseWriter, r *http.Request) { var ( rawConversationData RawCreateConversationData - messageThread Models.ConversationDetail + messageThread Database.ConversationDetail err error ) @@ -36,8 +35,8 @@ func CreateConversation(w http.ResponseWriter, r *http.Request) { return } - messageThread = Models.ConversationDetail{ - Base: Models.Base{ + messageThread = Database.ConversationDetail{ + Base: Database.Base{ ID: uuid.FromStringOrNil(rawConversationData.ID), }, Name: rawConversationData.Name, @@ -48,13 +47,13 @@ func CreateConversation(w http.ResponseWriter, r *http.Request) { Users: rawConversationData.Users, } - err = Database.CreateConversationDetail(&messageThread) + err = (&messageThread).CreateConversationDetail() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } - err = Database.CreateUserConversations(&rawConversationData.UserConversations) + err = (&rawConversationData.UserConversations).CreateUserConversations() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Messages/CreateConversation_test.go b/Backend/Api/Messages/CreateConversation_test.go index 04b30d1..6eb896e 100644 --- a/Backend/Api/Messages/CreateConversation_test.go +++ b/Backend/Api/Messages/CreateConversation_test.go @@ -9,7 +9,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" "github.com/gofrs/uuid" ) @@ -74,16 +73,16 @@ func Test_CreateConversation(t *testing.T) { pubKey := Seeder.GetPubKey() d := struct { - ID string `json:"id"` - Name string `json:"name"` - TwoUser string `json:"two_user"` - Users []Models.ConversationDetailUser `json:"users"` - UserConversations []Models.UserConversation `json:"user_conversations"` + ID string `json:"id"` + Name string `json:"name"` + TwoUser string `json:"two_user"` + Users []Database.ConversationDetailUser `json:"users"` + UserConversations []Database.UserConversation `json:"user_conversations"` }{ ID: id.String(), Name: base64.StdEncoding.EncodeToString(nameCiphertext), TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext), - Users: []Models.ConversationDetailUser{ + Users: []Database.ConversationDetailUser{ { ConversationDetailID: id, UserID: base64.StdEncoding.EncodeToString(userIDCiphertext), @@ -93,7 +92,7 @@ func Test_CreateConversation(t *testing.T) { Admin: base64.StdEncoding.EncodeToString(adminCiphertext), }, }, - UserConversations: []Models.UserConversation{ + UserConversations: []Database.UserConversation{ { UserID: u.ID, ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext), @@ -119,7 +118,7 @@ func Test_CreateConversation(t *testing.T) { t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode) } - var c Models.ConversationDetail + var c Database.ConversationDetail err = Database.DB.First(&c, "id = ?", id.String()).Error if err != nil { t.Errorf("Expected conversation detail record, received %s", err.Error()) diff --git a/Backend/Api/Messages/CreateMessage.go b/Backend/Api/Messages/CreateMessage.go index 552159b..18d1ea1 100644 --- a/Backend/Api/Messages/CreateMessage.go +++ b/Backend/Api/Messages/CreateMessage.go @@ -7,13 +7,12 @@ import ( "time" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" ) type rawMessageData struct { - MessageData Models.MessageData `json:"message_data"` - Messages []Models.Message `json:"message"` + MessageData Database.MessageData `json:"message_data"` + Messages Database.MessageList `json:"message"` } // CreateMessage sends a message @@ -21,7 +20,7 @@ func CreateMessage(w http.ResponseWriter, r *http.Request) { var ( messagesData []rawMessageData messageData rawMessageData - message Models.Message + message Database.Message t time.Time decodedFile []byte fileName string @@ -53,13 +52,13 @@ func CreateMessage(w http.ResponseWriter, r *http.Request) { } } - err = Database.CreateMessageData(&messageData.MessageData) + err = (&messageData.MessageData).CreateMessageData() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } - err = Database.CreateMessages(&messageData.Messages) + err = (&messageData.Messages).CreateMessages() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Messages/CreateMessage_test.go b/Backend/Api/Messages/CreateMessage_test.go index 74d694f..407bde3 100644 --- a/Backend/Api/Messages/CreateMessage_test.go +++ b/Backend/Api/Messages/CreateMessage_test.go @@ -10,13 +10,11 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" "github.com/gofrs/uuid" ) // TODO: Write test for message expiry - func Test_CreateMessage(t *testing.T) { client, ts, err := Tests.InitTestEnv() if err != nil { @@ -116,14 +114,14 @@ func Test_CreateMessage(t *testing.T) { return } - var m Models.Message + var m Database.Message err = Database.DB.First(&m).Error if err != nil { t.Errorf("Expected conversation detail record, received %s", err.Error()) return } - var md Models.MessageData + var md Database.MessageData err = Database.DB.First(&md).Error if err != nil { t.Errorf("Expected conversation detail record, received %s", err.Error()) diff --git a/Backend/Api/Messages/MessageThread.go b/Backend/Api/Messages/MessageThread.go index a7a8911..12409d0 100644 --- a/Backend/Api/Messages/MessageThread.go +++ b/Backend/Api/Messages/MessageThread.go @@ -7,7 +7,6 @@ import ( "strconv" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) @@ -15,8 +14,8 @@ import ( // Messages gets messages by the associationKey func Messages(w http.ResponseWriter, r *http.Request) { var ( - messages []Models.Message - message Models.Message + messages []Database.Message + message Database.Message urlVars map[string]string associationKey string values url.Values diff --git a/Backend/Api/Messages/MessageThread_test.go b/Backend/Api/Messages/MessageThread_test.go index 1b2463a..830fb99 100644 --- a/Backend/Api/Messages/MessageThread_test.go +++ b/Backend/Api/Messages/MessageThread_test.go @@ -9,7 +9,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -56,8 +55,8 @@ func Test_Messages(t *testing.T) { pubKey := Seeder.GetPubKey() - message := Models.Message{ - MessageData: Models.MessageData{ + message := Database.Message{ + MessageData: Database.MessageData{ Data: base64.StdEncoding.EncodeToString(dataCiphertext), SenderID: base64.StdEncoding.EncodeToString(senderIDCiphertext), SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext), @@ -68,7 +67,7 @@ func Test_Messages(t *testing.T) { AssociationKey: "AssociationKey", } - err = Database.CreateMessage(&message) + err = (&message).CreateMessage() if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) return @@ -91,7 +90,7 @@ func Test_Messages(t *testing.T) { return } - var m []Models.Message + var m Database.MessageList err = json.Unmarshal(requestBody, &m) if len(m) != 1 { @@ -160,8 +159,8 @@ func Test_MessagesPagination(t *testing.T) { pubKey := Seeder.GetPubKey() for i := 0; i < 50; i++ { - message := Models.Message{ - MessageData: Models.MessageData{ + message := Database.Message{ + MessageData: Database.MessageData{ Data: base64.StdEncoding.EncodeToString(dataCiphertext), SenderID: base64.StdEncoding.EncodeToString(senderIDCiphertext), SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext), @@ -172,7 +171,7 @@ func Test_MessagesPagination(t *testing.T) { AssociationKey: "AssociationKey", } - err = Database.CreateMessage(&message) + err = (&message).CreateMessage() if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) return @@ -196,7 +195,7 @@ func Test_MessagesPagination(t *testing.T) { return } - var m []Models.Message + var m Database.MessageList err = json.Unmarshal(requestBody, &m) if len(m) != 20 { diff --git a/Backend/Api/Messages/UpdateConversation.go b/Backend/Api/Messages/UpdateConversation.go index 4900ba8..78f6e70 100644 --- a/Backend/Api/Messages/UpdateConversation.go +++ b/Backend/Api/Messages/UpdateConversation.go @@ -7,21 +7,20 @@ import ( "github.com/gofrs/uuid" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) type rawUpdateConversationData struct { - ID string `json:"id"` - Name string `json:"name"` - Users []Models.ConversationDetailUser `json:"users"` - UserConversations []Models.UserConversation `json:"user_conversations"` + ID string `json:"id"` + Name string `json:"name"` + Users []Database.ConversationDetailUser `json:"users"` + UserConversations Database.UserConversationList `json:"user_conversations"` } // UpdateConversation updates the conversation data, such as title, users, etc func UpdateConversation(w http.ResponseWriter, r *http.Request) { var ( rawConversationData rawUpdateConversationData - messageThread Models.ConversationDetail + messageThread Database.ConversationDetail err error ) @@ -31,22 +30,22 @@ func UpdateConversation(w http.ResponseWriter, r *http.Request) { return } - messageThread = Models.ConversationDetail{ - Base: Models.Base{ + messageThread = Database.ConversationDetail{ + Base: Database.Base{ ID: uuid.FromStringOrNil(rawConversationData.ID), }, Name: rawConversationData.Name, Users: rawConversationData.Users, } - err = Database.UpdateConversationDetail(&messageThread) + err = (&messageThread).UpdateConversationDetail() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } if len(rawConversationData.UserConversations) > 0 { - err = Database.UpdateOrCreateUserConversations(&rawConversationData.UserConversations) + err = (&rawConversationData.UserConversations).UpdateOrCreateUserConversations() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return diff --git a/Backend/Api/Messages/UpdateConversation_test.go b/Backend/Api/Messages/UpdateConversation_test.go index 5f61bd8..19b1e8f 100644 --- a/Backend/Api/Messages/UpdateConversation_test.go +++ b/Backend/Api/Messages/UpdateConversation_test.go @@ -9,15 +9,14 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) -func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.UserConversation, Models.ConversationDetailUser, error) { +func createConversation(key Seeder.AesKey) (Database.ConversationDetail, Database.UserConversation, Database.ConversationDetailUser, error) { var ( - cd Models.ConversationDetail - uc Models.UserConversation - cdu Models.ConversationDetailUser + cd Database.ConversationDetail + uc Database.UserConversation + cdu Database.ConversationDetailUser ) u, err := Database.GetUserByUsername("test") @@ -32,12 +31,12 @@ func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.Us return cd, uc, cdu, err } - cd = Models.ConversationDetail{ + cd = Database.ConversationDetail{ Name: base64.StdEncoding.EncodeToString(nameCiphertext), TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext), } - err = Database.CreateConversationDetail(&cd) + err = (&cd).CreateConversationDetail() if err != nil { return cd, uc, cdu, err } @@ -54,7 +53,7 @@ func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.Us pubKey := Seeder.GetPubKey() - uc = Models.UserConversation{ + uc = Database.UserConversation{ UserID: u.ID, ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext), Admin: base64.StdEncoding.EncodeToString(adminCiphertext), @@ -63,7 +62,7 @@ func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.Us ), } - err = Database.CreateUserConversation(&uc) + err = (&uc).CreateUserConversation() if err != nil { return cd, uc, cdu, err } @@ -93,7 +92,7 @@ func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.Us return cd, uc, cdu, err } - cdu = Models.ConversationDetailUser{ + cdu = Database.ConversationDetailUser{ ConversationDetailID: cd.ID, UserID: base64.StdEncoding.EncodeToString(userIDCiphertext), Username: base64.StdEncoding.EncodeToString(usernameCiphertext), @@ -102,7 +101,7 @@ func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.Us PublicKey: base64.StdEncoding.EncodeToString(publicKeyCiphertext), } - err = Database.CreateConversationDetailUser(&cdu) + err = (&cdu).CreateConversationDetailUser() return cd, uc, cdu, err } @@ -131,15 +130,15 @@ func Test_UpdateConversation(t *testing.T) { d := struct { ID string `json:"id"` Name string `json:"name"` - Users []Models.ConversationDetailUser - UserConversations []Models.UserConversation + Users []Database.ConversationDetailUser + UserConversations []Database.UserConversation }{ ID: cd.ID.String(), Name: base64.StdEncoding.EncodeToString(nameCiphertext), - Users: []Models.ConversationDetailUser{ + Users: []Database.ConversationDetailUser{ cdu, }, - UserConversations: []Models.UserConversation{ + UserConversations: []Database.UserConversation{ uc, }, } @@ -158,7 +157,7 @@ func Test_UpdateConversation(t *testing.T) { t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode) } - var ncd Models.ConversationDetail + var ncd Database.ConversationDetail err = Database.DB.First(&ncd, "id = ?", cd.ID.String()).Error if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) diff --git a/Backend/Api/Users/SearchUsers.go b/Backend/Api/Users/SearchUsers.go index 0ee71db..eaf34cf 100644 --- a/Backend/Api/Users/SearchUsers.go +++ b/Backend/Api/Users/SearchUsers.go @@ -6,13 +6,12 @@ import ( "net/url" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) // SearchUsers searches a for a user by username func SearchUsers(w http.ResponseWriter, r *http.Request) { var ( - user Models.User + user Database.User query url.Values rawUsername []string username string diff --git a/Backend/Api/Users/SearchUsers_test.go b/Backend/Api/Users/SearchUsers_test.go index b54cae7..d2bda6b 100644 --- a/Backend/Api/Users/SearchUsers_test.go +++ b/Backend/Api/Users/SearchUsers_test.go @@ -7,7 +7,7 @@ import ( "net/http" "testing" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests" ) @@ -43,7 +43,7 @@ func Test_SearchUsers(t *testing.T) { return } - var user Models.User + var user Database.User json.Unmarshal(requestBody, &user) diff --git a/Backend/Database/Attachments.go b/Backend/Database/Attachments.go index 3097a04..2fc837b 100644 --- a/Backend/Database/Attachments.go +++ b/Backend/Database/Attachments.go @@ -1,16 +1,24 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - "gorm.io/gorm" "gorm.io/gorm/clause" ) +// Attachment holds the attachment data +type Attachment struct { + Base + FilePath string `gorm:"not null" json:"-"` + Mimetype string `gorm:"not null" json:"mimetype"` + Extension string `gorm:"not null" json:"extension"` + Data string `gorm:"-" json:"data"` + ImageLink string `gorm:"-" json:"image_link"` +} + // GetAttachmentByID gets the attachment record by the id -func GetAttachmentByID(id string) (Models.MessageData, error) { +func GetAttachmentByID(id string) (MessageData, error) { var ( - messageData Models.MessageData + messageData MessageData err error ) @@ -22,21 +30,21 @@ func GetAttachmentByID(id string) (Models.MessageData, error) { } // CreateAttachment creates the attachment record -func CreateAttachment(messageData *Models.MessageData) error { +func (attachment *Attachment) CreateAttachment() error { var ( err error ) err = DB.Session(&gorm.Session{FullSaveAssociations: true}). - Create(messageData). + Create(attachment). Error return err } // DeleteAttachment deletes the attachment record -func DeleteAttachment(messageData *Models.MessageData) error { +func (attachment *Attachment) DeleteAttachment() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). - Delete(messageData). + Delete(attachment). Error } diff --git a/Backend/Models/Base.go b/Backend/Database/Base.go similarity index 96% rename from Backend/Models/Base.go rename to Backend/Database/Base.go index 797bccc..b73432b 100644 --- a/Backend/Models/Base.go +++ b/Backend/Database/Base.go @@ -1,4 +1,4 @@ -package Models +package Database import ( "github.com/gofrs/uuid" diff --git a/Backend/Database/ConversationDetailUsers.go b/Backend/Database/ConversationDetailUsers.go index 6396acb..fafd22c 100644 --- a/Backend/Database/ConversationDetailUsers.go +++ b/Backend/Database/ConversationDetailUsers.go @@ -1,15 +1,26 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) -func GetConversationDetailUserById(id string) (Models.ConversationDetailUser, error) { +// ConversationDetailUser all users associated with a conversation +type ConversationDetailUser struct { + Base + ConversationDetailID uuid.UUID `gorm:"not null" json:"conversation_detail_id"` + ConversationDetail ConversationDetail `gorm:"not null" json:"conversation"` + UserID string `gorm:"not null" json:"user_id"` // Stored encrypted + Username string `gorm:"not null" json:"username"` // Stored encrypted + Admin string `gorm:"not null" json:"admin"` // Stored encrypted + AssociationKey string `gorm:"not null" json:"association_key"` // Stored encrypted + PublicKey string `gorm:"not null" json:"public_key"` // Stored encrypted +} + +func GetConversationDetailUserById(id string) (ConversationDetailUser, error) { var ( - messageThread Models.ConversationDetailUser + messageThread ConversationDetailUser err error ) @@ -21,21 +32,21 @@ func GetConversationDetailUserById(id string) (Models.ConversationDetailUser, er return messageThread, err } -func CreateConversationDetailUser(messageThread *Models.ConversationDetailUser) error { +func (detailUser *ConversationDetailUser) CreateConversationDetailUser() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). - Create(messageThread). + Create(detailUser). Error } -func UpdateConversationDetailUser(messageThread *Models.ConversationDetailUser) error { +func (detailUser *ConversationDetailUser) UpdateConversationDetailUser() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). - Where("id = ?", messageThread.ID). - Updates(messageThread). + Where("id = ?", detailUser.ID). + Updates(detailUser). Error } -func DeleteConversationDetailUser(messageThread *Models.ConversationDetailUser) error { +func (detailUser *ConversationDetailUser) DeleteConversationDetailUser() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). - Delete(messageThread). + Delete(detailUser). Error } diff --git a/Backend/Database/ConversationDetails.go b/Backend/Database/ConversationDetails.go index 269b9ef..ab6d664 100644 --- a/Backend/Database/ConversationDetails.go +++ b/Backend/Database/ConversationDetails.go @@ -1,16 +1,31 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) +// ConversationDetail stores the name for the conversation +type ConversationDetail struct { + Base + Name string `gorm:"not null" json:"name"` // Stored encrypted + Users []ConversationDetailUser ` json:"users"` + TwoUser string `gorm:"not null" json:"two_user"` + AttachmentID *uuid.UUID ` json:"attachment_id"` + Attachment Attachment ` json:"attachment"` + MessageExpiryDefault MessageExpiry `gorm:"default:no_expiry" json:"-" sql:"type:ENUM('fifteen_min', 'thirty_min', 'one_hour', 'three_hour', 'six_hour', 'twelve_hour', 'one_day', 'three_day', 'no_expiry')"` // Stored encrypted + MessageExpiry string `gorm:"-" json:"message_expiry"` // Stored encrypted + AdminAddMembers string ` json:"admin_add_members"` // Stored encrypted + AdminEditInfo string ` json:"admin_edit_info"` // Stored encrypted + AdminSendMessages string ` json:"admin_send_messages"` // Stored encrypted +} + // GetConversationDetailByID gets by id -func GetConversationDetailByID(id string) (Models.ConversationDetail, error) { +func GetConversationDetailByID(id string) (ConversationDetail, error) { var ( - conversationDetail Models.ConversationDetail + conversationDetail ConversationDetail err error ) @@ -23,9 +38,9 @@ func GetConversationDetailByID(id string) (Models.ConversationDetail, error) { } // GetConversationDetailsByIds gets by multiple ids -func GetConversationDetailsByIds(id []string) ([]Models.ConversationDetail, error) { +func GetConversationDetailsByIds(id []string) ([]ConversationDetail, error) { var ( - conversationDetail []Models.ConversationDetail + conversationDetail []ConversationDetail err error ) @@ -38,14 +53,14 @@ func GetConversationDetailsByIds(id []string) ([]Models.ConversationDetail, erro } // CreateConversationDetail creates a ConversationDetail record -func CreateConversationDetail(conversationDetail *Models.ConversationDetail) error { +func (conversationDetail *ConversationDetail) CreateConversationDetail() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Create(conversationDetail). Error } // UpdateConversationDetail updates a ConversationDetail record -func UpdateConversationDetail(conversationDetail *Models.ConversationDetail) error { +func (conversationDetail *ConversationDetail) UpdateConversationDetail() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Where("id = ?", conversationDetail.ID). Updates(conversationDetail). @@ -53,7 +68,7 @@ func UpdateConversationDetail(conversationDetail *Models.ConversationDetail) err } // DeleteConversationDetail deletes a ConversationDetail record -func DeleteConversationDetail(conversationDetail *Models.ConversationDetail) error { +func (conversationDetail *ConversationDetail) DeleteConversationDetail() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(conversationDetail). Error diff --git a/Backend/Database/FriendRequests.go b/Backend/Database/FriendRequests.go index f387f4b..716cb1a 100644 --- a/Backend/Database/FriendRequests.go +++ b/Backend/Database/FriendRequests.go @@ -1,16 +1,35 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "database/sql" + "time" + + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) +// FriendRequest Set with Friend being the requestee, and RequestFromID being the requester +type FriendRequest struct { + Base + UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"` + User User ` json:"user"` + FriendID string `gorm:"not null" json:"friend_id"` // Stored encrypted + FriendUsername string ` json:"friend_username"` // Stored encrypted + FriendImagePath string ` json:"friend_image_path"` + FriendPublicAsymmetricKey string ` json:"asymmetric_public_key"` // Stored encrypted + SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted + AcceptedAt sql.NullTime ` json:"accepted_at"` + CreatedAt time.Time `gorm:"not null" json:"created_at"` +} + +type FriendRequestList []FriendRequest + // GetFriendRequestByID gets friend request -func GetFriendRequestByID(id string) (Models.FriendRequest, error) { +func GetFriendRequestByID(id string) (FriendRequest, error) { var ( - friendRequest Models.FriendRequest + friendRequest FriendRequest err error ) @@ -22,16 +41,16 @@ func GetFriendRequestByID(id string) (Models.FriendRequest, error) { } // GetFriendRequestsByUserID gets friend request by user id -func GetFriendRequestsByUserID(userID string, page int) ([]Models.FriendRequest, error) { +func GetFriendRequestsByUserID(userID string, page int) ([]FriendRequest, error) { var ( - friends []Models.FriendRequest + friends []FriendRequest offset int err error ) offset = page * PageSize - err = DB.Model(Models.FriendRequest{}). + err = DB.Model(FriendRequest{}). Where("user_id = ?", userID). Offset(offset). Limit(PageSize). @@ -43,26 +62,26 @@ func GetFriendRequestsByUserID(userID string, page int) ([]Models.FriendRequest, } // CreateFriendRequest creates friend request -func CreateFriendRequest(friendRequest *Models.FriendRequest) error { +func (friendRequest *FriendRequest) CreateFriendRequest() error { return DB.Create(friendRequest). Error } // CreateFriendRequests creates multiple friend requests -func CreateFriendRequests(friendRequest *[]Models.FriendRequest) error { +func (friendRequest *FriendRequestList) CreateFriendRequests() error { return DB.Create(friendRequest). Error } // UpdateFriendRequest Updates friend request -func UpdateFriendRequest(friendRequest *Models.FriendRequest) error { +func (friendRequest *FriendRequest) UpdateFriendRequest() error { return DB.Where("id = ?", friendRequest.ID). Updates(friendRequest). Error } // DeleteFriendRequest deletes friend request -func DeleteFriendRequest(friendRequest *Models.FriendRequest) error { +func (friendRequest *FriendRequest) DeleteFriendRequest() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(friendRequest). Error diff --git a/Backend/Database/Init.go b/Backend/Database/Init.go index 27613c8..43fd0b9 100644 --- a/Backend/Database/Init.go +++ b/Backend/Database/Init.go @@ -3,8 +3,6 @@ package Database import ( "log" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -20,15 +18,15 @@ const ( var DB *gorm.DB var models = []interface{}{ - &Models.Session{}, - &Models.Attachment{}, - &Models.User{}, - &Models.FriendRequest{}, - &Models.MessageData{}, - &Models.Message{}, - &Models.ConversationDetail{}, - &Models.ConversationDetailUser{}, - &Models.UserConversation{}, + &Session{}, + &Attachment{}, + &User{}, + &FriendRequest{}, + &MessageData{}, + &Message{}, + &ConversationDetail{}, + &ConversationDetailUser{}, + &UserConversation{}, } // Init initializes the database connection diff --git a/Backend/Database/MessageData.go b/Backend/Database/MessageData.go index 80c6515..79ab149 100644 --- a/Backend/Database/MessageData.go +++ b/Backend/Database/MessageData.go @@ -1,15 +1,25 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) -func GetMessageDataById(id string) (Models.MessageData, error) { +// MessageData holds the content of the message +// encrypted through the Message.SymmetricKey +type MessageData struct { + Base + Data string ` json:"data"` // Stored encrypted + AttachmentID *uuid.UUID ` json:"attachment_id"` + Attachment Attachment ` json:"attachment"` + SenderID string `gorm:"not null" json:"sender_id"` // Stored encrypted + SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted +} + +func GetMessageDataById(id string) (MessageData, error) { var ( - messageData Models.MessageData + messageData MessageData err error ) @@ -20,7 +30,7 @@ func GetMessageDataById(id string) (Models.MessageData, error) { return messageData, err } -func CreateMessageData(messageData *Models.MessageData) error { +func (messageData *MessageData) CreateMessageData() error { var ( err error ) @@ -32,7 +42,7 @@ func CreateMessageData(messageData *Models.MessageData) error { return err } -func DeleteMessageData(messageData *Models.MessageData) error { +func (messageData *MessageData) DeleteMessageData() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(messageData). Error diff --git a/Backend/Models/MessageExpiry.go b/Backend/Database/MessageExpiry.go similarity index 99% rename from Backend/Models/MessageExpiry.go rename to Backend/Database/MessageExpiry.go index 4d25ccc..75d68aa 100644 --- a/Backend/Models/MessageExpiry.go +++ b/Backend/Database/MessageExpiry.go @@ -1,4 +1,4 @@ -package Models +package Database import ( "database/sql/driver" diff --git a/Backend/Database/Messages.go b/Backend/Database/Messages.go index 277948a..59f053e 100644 --- a/Backend/Database/Messages.go +++ b/Backend/Database/Messages.go @@ -1,16 +1,33 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "database/sql" + "time" + + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) +// Message holds data pertaining to each users' message +type Message struct { + Base + MessageDataID uuid.UUID ` json:"message_data_id"` + MessageData MessageData ` json:"message_data"` + SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted + AssociationKey string `gorm:"not null" json:"association_key"` // Stored encrypted + ExpiryRaw string ` json:"expiry"` + Expiry sql.NullTime ` json:"-"` + CreatedAt time.Time `gorm:"not null" json:"created_at"` +} + +type MessageList []Message + // GetMessageByID gets a message -func GetMessageByID(id string) (Models.Message, error) { +func GetMessageByID(id string) (Message, error) { var ( - message Models.Message + message Message err error ) @@ -22,9 +39,9 @@ func GetMessageByID(id string) (Models.Message, error) { } // GetMessagesByAssociationKey for getting whole thread -func GetMessagesByAssociationKey(associationKey string, page int) ([]Models.Message, error) { +func GetMessagesByAssociationKey(associationKey string, page int) ([]Message, error) { var ( - messages []Models.Message + messages []Message offset int err error ) @@ -43,7 +60,7 @@ func GetMessagesByAssociationKey(associationKey string, page int) ([]Models.Mess } // CreateMessage creates a message record -func CreateMessage(message *Models.Message) error { +func (message *Message) CreateMessage() error { var err error err = DB.Session(&gorm.Session{FullSaveAssociations: true}). @@ -54,7 +71,7 @@ func CreateMessage(message *Models.Message) error { } // CreateMessages creates multiple records -func CreateMessages(messages *[]Models.Message) error { +func (messages *MessageList) CreateMessages() error { var err error err = DB.Session(&gorm.Session{FullSaveAssociations: true}). @@ -65,7 +82,7 @@ func CreateMessages(messages *[]Models.Message) error { } // DeleteMessage deletes a message -func DeleteMessage(message *Models.Message) error { +func (message *Message) DeleteMessage() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(message). Error diff --git a/Backend/Database/Seeder/FriendSeeder.go b/Backend/Database/Seeder/FriendSeeder.go index 1ae87fe..5d3c97d 100644 --- a/Backend/Database/Seeder/FriendSeeder.go +++ b/Backend/Database/Seeder/FriendSeeder.go @@ -7,12 +7,11 @@ import ( "time" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) -func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error { +func seedFriend(userRequestTo, userRequestFrom Database.User, accepted bool) error { var ( - friendRequest Models.FriendRequest + friendRequest Database.FriendRequest symKey AesKey encPublicKey []byte err error @@ -28,7 +27,7 @@ func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error return err } - friendRequest = Models.FriendRequest{ + friendRequest = Database.FriendRequest{ UserID: userRequestTo.ID, FriendID: base64.StdEncoding.EncodeToString( EncryptWithPublicKey( @@ -55,7 +54,7 @@ func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error friendRequest.AcceptedAt.Valid = true } - return Database.CreateFriendRequest(&friendRequest) + return (&friendRequest).CreateFriendRequest() } func copyProfileImage() error { @@ -83,8 +82,8 @@ func copyProfileImage() error { // SeedFriends creates dummy friends for testing/development func SeedFriends() { var ( - primaryUser Models.User - secondaryUser Models.User + primaryUser Database.User + secondaryUser Database.User accepted bool i int err error diff --git a/Backend/Database/Seeder/MessageSeeder.go b/Backend/Database/Seeder/MessageSeeder.go index 00376f1..6010f82 100644 --- a/Backend/Database/Seeder/MessageSeeder.go +++ b/Backend/Database/Seeder/MessageSeeder.go @@ -4,19 +4,18 @@ import ( "encoding/base64" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gofrs/uuid" ) func seedMessage( - primaryUser, secondaryUser Models.User, + primaryUser, secondaryUser Database.User, primaryUserAssociationKey, secondaryUserAssociationKey string, i int, ) error { var ( - message Models.Message - messageData Models.MessageData + message Database.Message + messageData Database.MessageData key, userKey AesKey keyCiphertext []byte plaintext string @@ -61,13 +60,13 @@ func seedMessage( panic(err) } - messageData = Models.MessageData{ + messageData = Database.MessageData{ Data: base64.StdEncoding.EncodeToString(dataCiphertext), SenderID: base64.StdEncoding.EncodeToString(senderIDCiphertext), SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext), } - message = Models.Message{ + message = Database.Message{ MessageData: messageData, SymmetricKey: base64.StdEncoding.EncodeToString( EncryptWithPublicKey(userKey.Key, decodedPublicKey), @@ -75,12 +74,12 @@ func seedMessage( AssociationKey: primaryUserAssociationKey, } - err = Database.CreateMessage(&message) + err = (&message).CreateMessage() if err != nil { return err } - message = Models.Message{ + message = Database.Message{ MessageData: messageData, SymmetricKey: base64.StdEncoding.EncodeToString( EncryptWithPublicKey(userKey.Key, decodedPublicKey), @@ -88,12 +87,12 @@ func seedMessage( AssociationKey: secondaryUserAssociationKey, } - return Database.CreateMessage(&message) + return (&message).CreateMessage() } -func seedConversationDetail(key AesKey) (Models.ConversationDetail, error) { +func seedConversationDetail(key AesKey) (Database.ConversationDetail, error) { var ( - conversationDetail Models.ConversationDetail + conversationDetail Database.ConversationDetail name string nameCiphertext []byte falseCiphertext []byte @@ -118,7 +117,7 @@ func seedConversationDetail(key AesKey) (Models.ConversationDetail, error) { panic(err) } - conversationDetail = Models.ConversationDetail{ + conversationDetail = Database.ConversationDetail{ Name: base64.StdEncoding.EncodeToString(nameCiphertext), TwoUser: base64.StdEncoding.EncodeToString(falseCiphertext), AdminAddMembers: base64.StdEncoding.EncodeToString(trueCiphertext), @@ -126,17 +125,17 @@ func seedConversationDetail(key AesKey) (Models.ConversationDetail, error) { AdminSendMessages: base64.StdEncoding.EncodeToString(falseCiphertext), } - err = Database.CreateConversationDetail(&conversationDetail) + err = (&conversationDetail).CreateConversationDetail() return conversationDetail, err } func seedUserConversation( - user Models.User, + user Database.User, threadID uuid.UUID, key AesKey, -) (Models.UserConversation, error) { +) (Database.UserConversation, error) { var ( - messageThreadUser Models.UserConversation + messageThreadUser Database.UserConversation conversationDetailIDCiphertext []byte adminCiphertext []byte err error @@ -152,7 +151,7 @@ func seedUserConversation( return messageThreadUser, err } - messageThreadUser = Models.UserConversation{ + messageThreadUser = Database.UserConversation{ UserID: user.ID, ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext), Admin: base64.StdEncoding.EncodeToString(adminCiphertext), @@ -161,19 +160,19 @@ func seedUserConversation( ), } - err = Database.CreateUserConversation(&messageThreadUser) + err = (&messageThreadUser).CreateUserConversation() return messageThreadUser, err } func seedConversationDetailUser( - user Models.User, - conversationDetail Models.ConversationDetail, + user Database.User, + conversationDetail Database.ConversationDetail, associationKey uuid.UUID, admin bool, key AesKey, -) (Models.ConversationDetailUser, error) { +) (Database.ConversationDetailUser, error) { var ( - conversationDetailUser Models.ConversationDetailUser + conversationDetailUser Database.ConversationDetailUser userIDCiphertext []byte usernameCiphertext []byte @@ -215,7 +214,7 @@ func seedConversationDetailUser( return conversationDetailUser, err } - conversationDetailUser = Models.ConversationDetailUser{ + conversationDetailUser = Database.ConversationDetailUser{ ConversationDetailID: conversationDetail.ID, UserID: base64.StdEncoding.EncodeToString(userIDCiphertext), Username: base64.StdEncoding.EncodeToString(usernameCiphertext), @@ -224,7 +223,7 @@ func seedConversationDetailUser( PublicKey: base64.StdEncoding.EncodeToString(publicKeyCiphertext), } - err = Database.CreateConversationDetailUser(&conversationDetailUser) + err = (&conversationDetailUser).CreateConversationDetailUser() return conversationDetailUser, err } @@ -232,11 +231,11 @@ func seedConversationDetailUser( // SeedMessages seeds messages & conversations for testing func SeedMessages() { var ( - conversationDetail Models.ConversationDetail + conversationDetail Database.ConversationDetail key AesKey - primaryUser Models.User + primaryUser Database.User primaryUserAssociationKey uuid.UUID - secondaryUser Models.User + secondaryUser Database.User secondaryUserAssociationKey uuid.UUID i int err error diff --git a/Backend/Database/Seeder/UserSeeder.go b/Backend/Database/Seeder/UserSeeder.go index c4f8a40..2b9c9d9 100644 --- a/Backend/Database/Seeder/UserSeeder.go +++ b/Backend/Database/Seeder/UserSeeder.go @@ -5,7 +5,6 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" ) var userNames = []string{ @@ -22,9 +21,9 @@ var userNames = []string{ "pajamasenergy", } -func createUser(username string) (Models.User, error) { +func createUser(username string) (Database.User, error) { var ( - userData Models.User + userData Database.User userKey AesKey password string err error @@ -37,10 +36,10 @@ func createUser(username string) (Models.User, error) { password, err = Auth.HashPassword("password") if err != nil { - return Models.User{}, err + return Database.User{}, err } - userData = Models.User{ + userData = Database.User{ Username: username, Password: password, AsymmetricPrivateKey: EncryptedPrivateKey, @@ -50,7 +49,7 @@ func createUser(username string) (Models.User, error) { ), } - err = Database.CreateUser(&userData) + err = (&userData).CreateUser() return userData, err } diff --git a/Backend/Database/Sessions.go b/Backend/Database/Sessions.go index 877d11b..bc2a538 100644 --- a/Backend/Database/Sessions.go +++ b/Backend/Database/Sessions.go @@ -1,15 +1,28 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "time" + + "github.com/gofrs/uuid" "gorm.io/gorm/clause" ) +func (s Session) IsExpired() bool { + return s.Expiry.Before(time.Now()) +} + +type Session struct { + Base + UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;"` + User User + Expiry time.Time +} + // GetSessionByID Gets session -func GetSessionByID(id string) (Models.Session, error) { +func GetSessionByID(id string) (Session, error) { var ( - session Models.Session + session Session err error ) @@ -21,7 +34,7 @@ func GetSessionByID(id string) (Models.Session, error) { } // CreateSession creates session -func CreateSession(session *Models.Session) error { +func (session *Session) CreateSession() error { var ( err error ) @@ -32,14 +45,14 @@ func CreateSession(session *Models.Session) error { } // DeleteSession deletes session -func DeleteSession(session *Models.Session) error { +func (session *Session) DeleteSession() error { return DB.Delete(session).Error } // DeleteSessionByID deletes session func DeleteSessionByID(id string) error { return DB.Delete( - &Models.Session{}, + &Session{}, "id = ?", id, ).Error diff --git a/Backend/Database/UserConversations.go b/Backend/Database/UserConversations.go index 26a95d9..ee72c3f 100644 --- a/Backend/Database/UserConversations.go +++ b/Backend/Database/UserConversations.go @@ -1,15 +1,30 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "time" + + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) -func GetUserConversationById(id string) (Models.UserConversation, error) { +// UserConversation Used to link the current user to their conversations +type UserConversation struct { + Base + UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"` + User User ` json:"user"` + ConversationDetailID string `gorm:"not null" json:"conversation_detail_id"` // Stored encrypted + Admin string `gorm:"not null" json:"admin"` // Bool if user is admin of thread, stored encrypted + SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted + CreatedAt time.Time `gorm:"not null" json:"created_at"` +} + +type UserConversationList []UserConversation + +func GetUserConversationById(id string) (UserConversation, error) { var ( - message Models.UserConversation + message UserConversation err error ) @@ -19,9 +34,9 @@ func GetUserConversationById(id string) (Models.UserConversation, error) { return message, err } -func GetUserConversationsByUserId(id string, page int) ([]Models.UserConversation, error) { +func GetUserConversationsByUserId(id string, page int) ([]UserConversation, error) { var ( - conversations []Models.UserConversation + conversations []UserConversation offset int err error ) @@ -37,7 +52,7 @@ func GetUserConversationsByUserId(id string, page int) ([]Models.UserConversatio return conversations, err } -func CreateUserConversation(userConversation *Models.UserConversation) error { +func (userConversation *UserConversation) CreateUserConversation() error { var err error err = DB.Session(&gorm.Session{FullSaveAssociations: true}). @@ -47,7 +62,7 @@ func CreateUserConversation(userConversation *Models.UserConversation) error { return err } -func CreateUserConversations(userConversations *[]Models.UserConversation) error { +func (userConversations *UserConversationList) CreateUserConversations() error { var err error err = DB.Create(userConversations). @@ -56,30 +71,30 @@ func CreateUserConversations(userConversations *[]Models.UserConversation) error return err } -func UpdateUserConversation(userConversation *Models.UserConversation) error { +func (userConversation *UserConversation) UpdateUserConversation() error { var err error - err = DB.Model(Models.UserConversation{}). + err = DB.Model(UserConversation{}). Updates(userConversation). Error return err } -func UpdateUserConversations(userConversations *[]Models.UserConversation) error { +func (userConversations *UserConversationList) UpdateUserConversations() error { var err error - err = DB.Model(Models.UserConversation{}). + err = DB.Model(UserConversation{}). Updates(userConversations). Error return err } -func UpdateOrCreateUserConversations(userConversations *[]Models.UserConversation) error { +func (userConversations *UserConversationList) UpdateOrCreateUserConversations() error { var err error - err = DB.Model(Models.UserConversation{}). + err = DB.Model(UserConversation{}). Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, DoUpdates: clause.AssignmentColumns([]string{"admin"}), @@ -90,7 +105,7 @@ func UpdateOrCreateUserConversations(userConversations *[]Models.UserConversatio return err } -func DeleteUserConversation(userConversation *Models.UserConversation) error { +func (userConversation *UserConversation) DeleteUserConversation() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(userConversation). Error diff --git a/Backend/Database/Users.go b/Backend/Database/Users.go index 2df6a73..ac38afa 100644 --- a/Backend/Database/Users.go +++ b/Backend/Database/Users.go @@ -3,15 +3,42 @@ package Database import ( "errors" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) -func GetUserById(id string) (Models.User, error) { +// BeforeUpdate prevents updating the username or email if it has not changed +// This stops a unique constraint error +func (u *User) BeforeUpdate(tx *gorm.DB) (err error) { + if !tx.Statement.Changed("Username") { + tx.Statement.Omit("Username") + } + if !tx.Statement.Changed("Email") { + tx.Statement.Omit("Email") + } + return nil +} + +// User holds user data +type User struct { + Base + Username string `gorm:"not null;unique" json:"username"` + Password string `gorm:"not null" json:"password"` + ConfirmPassword string `gorm:"-" json:"confirm_password"` + Email string ` json:"email"` + AsymmetricPrivateKey string `gorm:"not null" json:"asymmetric_private_key"` // Stored encrypted + AsymmetricPublicKey string `gorm:"not null" json:"asymmetric_public_key"` + SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted + AttachmentID *uuid.UUID ` json:"attachment_id"` + Attachment Attachment ` json:"attachment"` + MessageExpiryDefault MessageExpiry `gorm:"default:no_expiry" json:"-" sql:"type:ENUM('fifteen_min', 'thirty_min', 'one_hour', 'three_hour', 'six_hour', 'twelve_hour', 'one_day', 'three_day', 'no_expiry')"` // Stored encrypted +} + +func GetUserById(id string) (User, error) { var ( - user Models.User + user User err error ) @@ -22,9 +49,9 @@ func GetUserById(id string) (Models.User, error) { return user, err } -func GetUserByUsername(username string) (Models.User, error) { +func GetUserByUsername(username string) (User, error) { var ( - user Models.User + user User err error ) @@ -41,7 +68,7 @@ func CheckUniqueUsername(username string) error { err error ) - err = DB.Model(Models.User{}). + err = DB.Model(User{}). Select("count(*) > 0"). Where("username = ?", username). Find(&exists). @@ -58,7 +85,7 @@ func CheckUniqueUsername(username string) error { return nil } -func CreateUser(user *Models.User) error { +func (user *User) CreateUser() error { var err error err = DB.Session(&gorm.Session{FullSaveAssociations: true}). @@ -68,11 +95,11 @@ func CreateUser(user *Models.User) error { return err } -func UpdateUser(id string, user *Models.User) error { +func (user *User) UpdateUser() error { var err error err = DB.Model(&user). Omit("id"). - Where("id = ?", id). + Where("id = ?", user.ID.String()). Updates(user). Error @@ -80,15 +107,15 @@ func UpdateUser(id string, user *Models.User) error { return err } - err = DB.Model(Models.User{}). - Where("id = ?", id). + err = DB.Model(User{}). + Where("id = ?", user.ID.String()). First(user). Error return err } -func DeleteUser(user *Models.User) error { +func (user *User) DeleteUser() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(user). Error diff --git a/Backend/Models/Attachments.go b/Backend/Models/Attachments.go deleted file mode 100644 index 739369e..0000000 --- a/Backend/Models/Attachments.go +++ /dev/null @@ -1,11 +0,0 @@ -package Models - -// Attachment holds the attachment data -type Attachment struct { - Base - FilePath string `gorm:"not null" json:"-"` - Mimetype string `gorm:"not null" json:"mimetype"` - Extension string `gorm:"not null" json:"extension"` - Data string `gorm:"-" json:"data"` - ImageLink string `gorm:"-" json:"image_link"` -} diff --git a/Backend/Models/Conversations.go b/Backend/Models/Conversations.go deleted file mode 100644 index 8e98e2d..0000000 --- a/Backend/Models/Conversations.go +++ /dev/null @@ -1,45 +0,0 @@ -package Models - -import ( - "time" - - "github.com/gofrs/uuid" -) - -// ConversationDetail stores the name for the conversation -type ConversationDetail struct { - Base - Name string `gorm:"not null" json:"name"` // Stored encrypted - Users []ConversationDetailUser ` json:"users"` - TwoUser string `gorm:"not null" json:"two_user"` - AttachmentID *uuid.UUID ` json:"attachment_id"` - Attachment Attachment ` json:"attachment"` - MessageExpiryDefault MessageExpiry `gorm:"default:no_expiry" json:"-" sql:"type:ENUM('fifteen_min', 'thirty_min', 'one_hour', 'three_hour', 'six_hour', 'twelve_hour', 'one_day', 'three_day', 'no_expiry')"` // Stored encrypted - MessageExpiry string `gorm:"-" json:"message_expiry"` // Stored encrypted - AdminAddMembers string ` json:"admin_add_members"` // Stored encrypted - AdminEditInfo string ` json:"admin_edit_info"` // Stored encrypted - AdminSendMessages string ` json:"admin_send_messages"` // Stored encrypted -} - -// ConversationDetailUser all users associated with a conversation -type ConversationDetailUser struct { - Base - ConversationDetailID uuid.UUID `gorm:"not null" json:"conversation_detail_id"` - ConversationDetail ConversationDetail `gorm:"not null" json:"conversation"` - UserID string `gorm:"not null" json:"user_id"` // Stored encrypted - Username string `gorm:"not null" json:"username"` // Stored encrypted - Admin string `gorm:"not null" json:"admin"` // Stored encrypted - AssociationKey string `gorm:"not null" json:"association_key"` // Stored encrypted - PublicKey string `gorm:"not null" json:"public_key"` // Stored encrypted -} - -// UserConversation Used to link the current user to their conversations -type UserConversation struct { - Base - UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"` - User User ` json:"user"` - ConversationDetailID string `gorm:"not null" json:"conversation_detail_id"` // Stored encrypted - Admin string `gorm:"not null" json:"admin"` // Bool if user is admin of thread, stored encrypted - SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted - CreatedAt time.Time `gorm:"not null" json:"created_at"` -} diff --git a/Backend/Models/Friends.go b/Backend/Models/Friends.go deleted file mode 100644 index 6438d97..0000000 --- a/Backend/Models/Friends.go +++ /dev/null @@ -1,22 +0,0 @@ -package Models - -import ( - "database/sql" - "time" - - "github.com/gofrs/uuid" -) - -// FriendRequest Set with Friend being the requestee, and RequestFromID being the requester -type FriendRequest struct { - Base - UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"` - User User ` json:"user"` - FriendID string `gorm:"not null" json:"friend_id"` // Stored encrypted - FriendUsername string ` json:"friend_username"` // Stored encrypted - FriendImagePath string ` json:"friend_image_path"` - FriendPublicAsymmetricKey string ` json:"asymmetric_public_key"` // Stored encrypted - SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted - AcceptedAt sql.NullTime ` json:"accepted_at"` - CreatedAt time.Time `gorm:"not null" json:"created_at"` -} diff --git a/Backend/Models/Messages.go b/Backend/Models/Messages.go deleted file mode 100644 index eafac22..0000000 --- a/Backend/Models/Messages.go +++ /dev/null @@ -1,31 +0,0 @@ -package Models - -import ( - "database/sql" - "time" - - "github.com/gofrs/uuid" -) - -// MessageData holds the content of the message -// encrypted through the Message.SymmetricKey -type MessageData struct { - Base - Data string ` json:"data"` // Stored encrypted - AttachmentID *uuid.UUID ` json:"attachment_id"` - Attachment Attachment ` json:"attachment"` - SenderID string `gorm:"not null" json:"sender_id"` // Stored encrypted - SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted -} - -// Message holds data pertaining to each users' message -type Message struct { - Base - MessageDataID uuid.UUID ` json:"message_data_id"` - MessageData MessageData ` json:"message_data"` - SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted - AssociationKey string `gorm:"not null" json:"association_key"` // Stored encrypted - ExpiryRaw string ` json:"expiry"` - Expiry sql.NullTime ` json:"-"` - CreatedAt time.Time `gorm:"not null" json:"created_at"` -} diff --git a/Backend/Models/Sessions.go b/Backend/Models/Sessions.go deleted file mode 100644 index 1f2e215..0000000 --- a/Backend/Models/Sessions.go +++ /dev/null @@ -1,18 +0,0 @@ -package Models - -import ( - "time" - - "github.com/gofrs/uuid" -) - -func (s Session) IsExpired() bool { - return s.Expiry.Before(time.Now()) -} - -type Session struct { - Base - UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;"` - User User - Expiry time.Time -} diff --git a/Backend/Models/Users.go b/Backend/Models/Users.go deleted file mode 100644 index fa587d4..0000000 --- a/Backend/Models/Users.go +++ /dev/null @@ -1,33 +0,0 @@ -package Models - -import ( - "github.com/gofrs/uuid" - "gorm.io/gorm" -) - -// BeforeUpdate prevents updating the username or email if it has not changed -// This stops a unique constraint error -func (u *User) BeforeUpdate(tx *gorm.DB) (err error) { - if !tx.Statement.Changed("Username") { - tx.Statement.Omit("Username") - } - if !tx.Statement.Changed("Email") { - tx.Statement.Omit("Email") - } - return nil -} - -// User holds user data -type User struct { - Base - Username string `gorm:"not null;unique" json:"username"` - Password string `gorm:"not null" json:"password"` - ConfirmPassword string `gorm:"-" json:"confirm_password"` - Email string ` json:"email"` - AsymmetricPrivateKey string `gorm:"not null" json:"asymmetric_private_key"` // Stored encrypted - AsymmetricPublicKey string `gorm:"not null" json:"asymmetric_public_key"` - SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted - AttachmentID *uuid.UUID ` json:"attachment_id"` - Attachment Attachment ` json:"attachment"` - MessageExpiryDefault MessageExpiry `gorm:"default:no_expiry" json:"-" sql:"type:ENUM('fifteen_min', 'thirty_min', 'one_hour', 'three_hour', 'six_hour', 'twelve_hour', 'one_day', 'three_day', 'no_expiry')"` // Stored encrypted -} diff --git a/Backend/Tests/Init.go b/Backend/Tests/Init.go index 457cf3f..566ff9a 100644 --- a/Backend/Tests/Init.go +++ b/Backend/Tests/Init.go @@ -14,21 +14,20 @@ import ( "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) -func InitTestCreateUser(username string) (Models.User, error) { +func InitTestCreateUser(username string) (Database.User, error) { userKey, err := Seeder.GenerateAesKey() if err != nil { - return Models.User{}, err + return Database.User{}, err } pubKey := Seeder.GetPubKey() p, _ := Auth.HashPassword("password") - u := Models.User{ + u := Database.User{ Username: username, Password: p, AsymmetricPublicKey: Seeder.PublicKey, @@ -38,7 +37,7 @@ func InitTestCreateUser(username string) (Models.User, error) { ), } - err = Database.CreateUser(&u) + err = (&u).CreateUser() return u, err } @@ -59,12 +58,12 @@ func InitTestEnv() (*http.Client, *httptest.Server, error) { return http.DefaultClient, ts, err } - session := Models.Session{ + session := Database.Session{ UserID: u.ID, Expiry: time.Now().Add(12 * time.Hour), } - err = Database.CreateSession(&session) + err = (&session).CreateSession() if err != nil { return http.DefaultClient, ts, err } diff --git a/Backend/Util/UserHelper.go b/Backend/Util/UserHelper.go index 32616a6..80638df 100644 --- a/Backend/Util/UserHelper.go +++ b/Backend/Util/UserHelper.go @@ -6,7 +6,6 @@ import ( "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "github.com/gorilla/mux" ) @@ -26,9 +25,9 @@ func GetUserId(r *http.Request) (string, error) { return id, nil } -func GetUserById(w http.ResponseWriter, r *http.Request) (Models.User, error) { +func GetUserById(w http.ResponseWriter, r *http.Request) (Database.User, error) { var ( - postData Models.User + postData Database.User id string err error ) diff --git a/Backend/dev.sh b/Backend/dev.sh index 7c1709a..c3d72c3 100644 --- a/Backend/dev.sh +++ b/Backend/dev.sh @@ -3,6 +3,6 @@ while true; do go build main.go ./main & PID=$! - inotifywait --exclude 'attachments|/\..+' -r -e modify . + inotifywait --exclude 'attachments|main|/\..+' -r -e modify . kill $PID done