Refactor Database Connector, Implement Register
This commit is contained in:
parent
0cc882cff0
commit
c79d1f86e4
18 changed files with 1495 additions and 1234 deletions
127
entities/device/deviceDatabaseConnector.go
Normal file
127
entities/device/deviceDatabaseConnector.go
Normal file
|
@ -0,0 +1,127 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateDevice(device *Device, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO device
|
||||
(id, name, userId)
|
||||
VALUES
|
||||
(?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(device.Id, device.Name, userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadDevice(id string) (foundDevice *Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name
|
||||
FROM device
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundDevice = &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadDevicesForUser(userId string) (devices map[string]*Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name
|
||||
FROM device
|
||||
WHERE userId = '%s'`, userId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
devices = make(map[string]*Device)
|
||||
|
||||
for rows.Next() {
|
||||
foundDevice := &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
||||
devices[foundDevice.Id] = foundDevice
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateDevice(device *Device, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE device SET
|
||||
name = ?,
|
||||
userId = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(device.Name, userId, device.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteDevice(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM device
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
125
entities/device/keyDatabaseConnector.go
Normal file
125
entities/device/keyDatabaseConnector.go
Normal file
|
@ -0,0 +1,125 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateKey(key *Key, deviceId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO key
|
||||
(id, type, key, deviceId)
|
||||
VALUES
|
||||
(?, ?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(key.Id, key.Type, key.Key, deviceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadKey(id string) (foundKey *Key, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, type, key
|
||||
FROM key
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundKey = &Key{}
|
||||
err = rows.Scan(&foundKey.Id, &foundKey.Type, &foundKey.Key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadKeysForDevice(deviceId string) (keys map[string]*Key, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, type, key
|
||||
FROM key
|
||||
WHERE deviceId = '%s'`, deviceId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
keys = make(map[string]*Key)
|
||||
|
||||
for rows.Next() {
|
||||
foundKey := &Key{}
|
||||
err = rows.Scan(&foundKey.Id, &foundKey.Type, &foundKey.Key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
keys[foundKey.Id] = foundKey
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateKey(key *Key) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE key SET
|
||||
type = ?,
|
||||
key = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(key.Type, key.Key, key.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteKey(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM key
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
208
entities/event/eventDatabaseConnector.go
Normal file
208
entities/event/eventDatabaseConnector.go
Normal file
|
@ -0,0 +1,208 @@
|
|||
package event
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateEvent(event *Event, txnId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO event
|
||||
(id, roomId, txnId, eventType, content, parentId, depth)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(event.Id, event.RoomId, txnId, event.EventType, event.Content, event.ParentId, event.Depth)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func CreateEventsFromTransaction(txnId string, pdus map[string]*Event) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO event
|
||||
(id, roomId, txnId, eventType, content, parentId, depth)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, pdu := range pdus {
|
||||
_, err = stmt.Exec(pdu.Id, pdu.RoomId, txnId, pdu.EventType, pdu.Content, pdu.ParentId, pdu.Depth)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEvent(id string) (foundEvent *Event, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
|
||||
FROM event
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundEvent = &Event{}
|
||||
err = rows.Scan(&foundEvent.Id,
|
||||
&foundEvent.RoomId,
|
||||
&foundEvent.EventType,
|
||||
&foundEvent.Content,
|
||||
&foundEvent.ParentId,
|
||||
&foundEvent.Depth,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEventsFromRoom(roomId string) (events map[string]*Event, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
|
||||
FROM event
|
||||
WHERE roomId = '%s'`, roomId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
events = make(map[string]*Event)
|
||||
|
||||
for rows.Next() {
|
||||
foundEvent := &Event{}
|
||||
err = rows.Scan(&foundEvent.Id,
|
||||
&foundEvent.RoomId,
|
||||
&foundEvent.EventType,
|
||||
&foundEvent.Content,
|
||||
&foundEvent.ParentId,
|
||||
&foundEvent.Depth,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
events[foundEvent.Id] = foundEvent
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEventsFromTransaction(txnId string) (events map[string]*Event, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
|
||||
FROM event
|
||||
WHERE txnId = '%s'`, txnId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
events = make(map[string]*Event)
|
||||
|
||||
for rows.Next() {
|
||||
foundEvent := &Event{}
|
||||
err = rows.Scan(
|
||||
&foundEvent.Id,
|
||||
&foundEvent.RoomId,
|
||||
&foundEvent.EventType,
|
||||
&foundEvent.Content,
|
||||
&foundEvent.ParentId,
|
||||
&foundEvent.Depth,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
events[foundEvent.Id] = foundEvent
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateEvent(event *Event) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE event SET
|
||||
roomId = ?,
|
||||
eventType = ?,
|
||||
content = ?,
|
||||
parentId = ?,
|
||||
depth = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(
|
||||
event.RoomId,
|
||||
event.EventType,
|
||||
event.Content,
|
||||
event.ParentId,
|
||||
event.Depth,
|
||||
event.Id,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteEvent(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM event
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
215
entities/room/roomDatabaseConnector.go
Normal file
215
entities/room/roomDatabaseConnector.go
Normal file
|
@ -0,0 +1,215 @@
|
|||
package room
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/entities/event"
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateRoom(room *Room, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO room
|
||||
(id, version)
|
||||
VALUES
|
||||
(?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(room.Id, room.Version)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
err = CreateRoomMember(room.Id, userId)
|
||||
return
|
||||
}
|
||||
|
||||
func CreateRoomMember(roomId string, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO roomMember
|
||||
(roomId, userId)
|
||||
VALUES
|
||||
(?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(roomId, userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadRoom(id string) (foundRoom *Room, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, version
|
||||
FROM room
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundRoom = &Room{}
|
||||
err = rows.Scan(&foundRoom.Id, &foundRoom.Version)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundRoom.Messages, err = event.ReadEventsFromRoom(foundRoom.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundRoom.Members, err = ReadRoomMembers(foundRoom.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadRoomMembers(roomId string) (roomMembers []string, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT userId
|
||||
FROM roomMember
|
||||
WHERE roomId = '%s'`, roomId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
roomMembers = []string{}
|
||||
|
||||
for rows.Next() {
|
||||
var foundUser string
|
||||
err = rows.Scan(&foundUser)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
roomMembers = append(roomMembers, foundUser)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateRoom(room *Room) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE room SET
|
||||
version = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(room.Version, room.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteRoom(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM room
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = DeleteAllRoomMemberForRoom(id)
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteRoomMember(roomId string, userId string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
|
||||
WHERE userId = '%s' AND roomId = '%s'`, userId, roomId)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteAllRoomMemberForUser(userId string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
|
||||
WHERE userId = '%s'`, userId)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteAllRoomMemberForRoom(roomId string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
|
||||
WHERE roomId = '%s'`, roomId)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
101
entities/transaction/transactionDatabaseConnector.go
Normal file
101
entities/transaction/transactionDatabaseConnector.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package transaction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/entities/event"
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateTransaction(transaction *Transaction) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO txn
|
||||
(id, origin, timestamp)
|
||||
VALUES
|
||||
(?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(transaction.Id, transaction.Origin, transaction.Timestamp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadTransaction(id string) (foundTransaction *Transaction, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, origin, timestamp
|
||||
FROM txn
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundTransaction = &Transaction{}
|
||||
err = rows.Scan(&foundTransaction.Id, &foundTransaction.Origin, &foundTransaction.Timestamp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundTransaction.PDUS, err = event.ReadEventsFromTransaction(foundTransaction.Id)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateTransaction(transaction *Transaction) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE txn SET
|
||||
origin = ?,
|
||||
timestamp = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(transaction.Origin, transaction.Timestamp, transaction.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteTransaction(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM txn
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
|
@ -10,3 +10,83 @@ type User struct {
|
|||
Password string `json:"password,omitempty"`
|
||||
Devices map[string]*device.Device `json:"devices,omitempty"`
|
||||
}
|
||||
|
||||
type registerRequest struct {
|
||||
Auth authentificationData `json:"auth,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
DeviceId string `json:"device_id,omitempty"`
|
||||
DeviceName string `json:"initial_device_display_name,omitempty"`
|
||||
InhibitLogin bool `json:"inhibit_login,omitempty"`
|
||||
}
|
||||
|
||||
type registerResponse struct {
|
||||
UserId string `json:"user_id,omitempty"`
|
||||
AccessToken string `json:"access_token,omitempty"`
|
||||
HomeServer string `json:"home_server,omitempty"`
|
||||
DeviceId string `json:"device_id,omitempty"`
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
LoginType string `json:"type,omitempty"`
|
||||
Identifier identifier `json:"identifier,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
DeviceId string `json:"device_id,omitempty"`
|
||||
DeviceName string `json:"initial_device_display_name,omitempty"`
|
||||
}
|
||||
|
||||
type loginResponse struct {
|
||||
UserId string `json:"user_id,omitempty"`
|
||||
AccessToken string `json:"access_token,omitempty"`
|
||||
DeviceId string `json:"device_id,omitempty"`
|
||||
DiscoveryInfo discoveryInformation `json:"well_known,omitempty"`
|
||||
}
|
||||
|
||||
type deaktivateUserRequest struct {
|
||||
Auth authentificationData `json:"auth,omitempty"`
|
||||
IdentityServer string `json:"id_server,omitempty"`
|
||||
}
|
||||
|
||||
type deaktivateUserResponse struct {
|
||||
Unbind3PIDS string `json:"id_server_unbind_result,omitempty"` // success or no-support
|
||||
}
|
||||
|
||||
type changePasswordRequest struct {
|
||||
NewPassword string
|
||||
LogoutDevices bool
|
||||
Auth authentificationData
|
||||
}
|
||||
|
||||
type errorResponse struct {
|
||||
ErrorCode string `json:"errcode,omitempty"`
|
||||
ErrorMessage string `json:"error,omitempty"`
|
||||
RetryTime int `json:"retry_after_ms,omitempty"`
|
||||
}
|
||||
|
||||
type identifier struct {
|
||||
IdentifierType string `json:"type,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Medium string `json:"medium,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Phone string `json:"phone,omitempty"`
|
||||
}
|
||||
|
||||
type authentificationData struct {
|
||||
LoginType string `json:"type,omitempty"`
|
||||
Session string `json:"session,omitempty"`
|
||||
}
|
||||
|
||||
type discoveryInformation struct {
|
||||
Homeserver homeserverInformation `json:"m.homeserver,omitempty"`
|
||||
IdentityServer identityServerInformation `json:"m.identity_server,omitempty"`
|
||||
}
|
||||
|
||||
type homeserverInformation struct {
|
||||
BaseUrl string `json:"base_url,omitempty"`
|
||||
}
|
||||
|
||||
type identityServerInformation struct {
|
||||
BaseUrl string `json:"base_url,omitempty"`
|
||||
}
|
||||
|
|
|
@ -1,5 +1,160 @@
|
|||
package user
|
||||
|
||||
func New() (user *User) {
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"nutfactory.org/Matrix/entities/device"
|
||||
"nutfactory.org/Matrix/utils"
|
||||
)
|
||||
|
||||
func New(id string, name, string, password string, devices map[string]*device.Device) (newUser *User) {
|
||||
newUser = &User{
|
||||
Id: id,
|
||||
Name: name,
|
||||
Password: password,
|
||||
Devices: devices,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CheckUsernameAvailability(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Register(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
request := registerRequest{}
|
||||
errResponse := utils.CheckRequest(r, request)
|
||||
if errResponse != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&request)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Could not parse JSON"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
errResponse = checkAuthData(&request.Auth)
|
||||
if errResponse != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
// TODO: Use New Function
|
||||
newUser := &User{
|
||||
Id: request.Username,
|
||||
Name: request.Username,
|
||||
Password: request.Password,
|
||||
}
|
||||
foundUser, err := ReadUser(newUser.Id)
|
||||
if foundUser != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorCode: "M_USER_IN_USE", ErrorMessage: "Username already in use"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
err = CreateUser(newUser)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Database Error"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
userDevice, err := device.ReadDevice(request.DeviceId)
|
||||
if userDevice != nil {
|
||||
err = device.UpdateDevice(userDevice, newUser.Id)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Database Error"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// TODO: Use New Function
|
||||
userDevice = &device.Device{}
|
||||
err = device.CreateDevice(userDevice, newUser.Id)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Database Error"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
response := registerResponse{
|
||||
UserId: newUser.Id,
|
||||
// TODO: Create Funktion for Token Generation
|
||||
AccessToken: "TEST",
|
||||
DeviceId: userDevice.Id,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Logout(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Deactivate(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func ChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Check if necessary
|
||||
func Sync(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkAuthData(authData *authentificationData) (errResponse *utils.ErrorResponse) {
|
||||
if authData.LoginType != "m.login.password" {
|
||||
errResponse = &utils.ErrorResponse{ErrorCode: "M_FORBIDDEN", ErrorMessage: "Unsupported Auth Type"}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
101
entities/user/userDatabaseConnector.go
Normal file
101
entities/user/userDatabaseConnector.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/entities/device"
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateUser(user *User) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO user
|
||||
(id, name, password)
|
||||
VALUES
|
||||
(?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(user.Id, user.Name, user.Password)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadUser(id string) (foundUser *User, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name, password
|
||||
FROM user
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundUser = &User{}
|
||||
err = rows.Scan(&foundUser.Id, &foundUser.Name, &foundUser.Password)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundUser.Devices, err = device.ReadDevicesForUser(foundUser.Id)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateUser(user *User) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE user SET
|
||||
name = ?,
|
||||
password = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(user.Name, user.Password, user.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteUser(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM user
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue