2020-10-01 15:45:57 +00:00
|
|
|
package database
|
2020-09-30 08:28:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-10-01 15:45:57 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-09-30 08:28:49 +00:00
|
|
|
|
|
|
|
"nutfactory.org/Matrix/entities/room"
|
|
|
|
)
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
func initRoomTable(db *sql.DB) (err error) {
|
|
|
|
log.Printf("Init Room Table")
|
|
|
|
statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS room (
|
|
|
|
id TEXT PRIMARY KEY,
|
|
|
|
version TEXT
|
|
|
|
)`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-09-30 08:28:49 +00:00
|
|
|
statement.Exec()
|
2020-10-01 15:45:57 +00:00
|
|
|
statement, err = db.Prepare(`CREATE TABLE IF NOT EXISTS roomMember (
|
|
|
|
userId TEXT,
|
|
|
|
roomId TEXT,
|
|
|
|
PRIMARY KEY (userId, roomId)
|
|
|
|
)`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
statement.Exec()
|
|
|
|
|
|
|
|
//TODO: Test Queries
|
|
|
|
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
func CreateRoom(db *sql.DB, room *room.Room, userId string) (err error) {
|
|
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO room
|
|
|
|
(id, version)
|
|
|
|
VALUES
|
|
|
|
(?, ?)`)
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
tx, err := 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(db, room.Id, userId)
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
func CreateRoomMember(db *sql.DB, roomId string, userId string) (err error) {
|
|
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO roomMember
|
|
|
|
(roomId, userId)
|
|
|
|
VALUES
|
|
|
|
(?, ?)`)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(roomId, userId)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
func ReadRoom(db *sql.DB, id string) (foundRoom *room.Room, err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`SELECT id, version
|
|
|
|
FROM room
|
|
|
|
WHERE id = '%s'`, id)
|
|
|
|
|
|
|
|
rows, err := db.Query(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
if rows.Next() {
|
|
|
|
foundRoom = &room.Room{}
|
|
|
|
err = rows.Scan(&foundRoom.Id, &foundRoom.Version)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundRoom.Messages, err = ReadEventsFromRoom(db, foundRoom.Id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundRoom.Members, err = ReadRoomMembers(db, foundRoom.Id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
func ReadRoomMembers(db *sql.DB, roomId string) (roomMembers []string, err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`SELECT userId
|
|
|
|
FROM roomMember
|
|
|
|
WHERE roomId = '%s'`, roomId)
|
|
|
|
|
|
|
|
rows, err := 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(db *sql.DB, room *room.Room) (err error) {
|
|
|
|
sqlStmt := fmt.Sprintf(`UPDATE room SET
|
|
|
|
version = ?
|
|
|
|
WHERE id = ?`)
|
|
|
|
|
|
|
|
tx, err := 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(db *sql.DB, id string) (err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`DELETE FROM room
|
|
|
|
WHERE id = '%s'`, id)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = DeleteAllRoomMemberForRoom(db, id)
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteRoomMember(db *sql.DB, roomId string, userId string) (err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
|
|
|
|
WHERE userId = '%s' AND roomId = '%s'`, userId, roomId)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteAllRoomMemberForUser(db *sql.DB, userId string) (err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
|
|
|
|
WHERE userId = '%s'`, userId)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteAllRoomMemberForRoom(db *sql.DB, roomId string) (err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
|
|
|
|
WHERE roomId = '%s'`, roomId)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
tx.Commit()
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|