Matrix/entities/room/roomDatabaseConnector.go

204 lines
3.4 KiB
Go
Raw Normal View History

package room
import (
"fmt"
2020-10-12 14:16:28 +00:00
"git.nutfactory.org/hoernschen/Matrix/entities/event"
"git.nutfactory.org/hoernschen/Matrix/utils/database"
)
2020-10-11 21:11:30 +00:00
func CreateRoom(room *Room) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO room
2020-10-11 21:11:30 +00:00
(id, version, visibility, name, topic, isDirect, federated)
VALUES
2020-10-11 21:11:30 +00:00
(?, ?, ?, ?, ?, ?, ?)`)
tx, err := database.DB.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
2020-10-11 21:11:30 +00:00
_, err = stmt.Exec(
room.Id,
room.Version,
room.Visibility,
room.Name,
room.Topic,
room.IsDirect,
room.Federated,
)
if err != nil {
return
}
tx.Commit()
2020-10-11 21:11:30 +00:00
for _, userId := range room.Members {
err = CreateRoomMember(room.Id, userId)
}
return
}
func CreateRoomMember(roomId string, userId string) (err error) {
2020-10-11 21:11:30 +00:00
return event.CreateRoomMember(roomId, userId)
}
func ReadRoom(id string) (foundRoom *Room, err error) {
2020-10-11 21:11:30 +00:00
queryStmt := fmt.Sprintf(`SELECT id, version, visibility, name, topic, isDirect, federated
FROM room
WHERE id = '%s'`, id)
rows, err := database.DB.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundRoom = &Room{}
2020-10-11 21:11:30 +00:00
err = rows.Scan(
&foundRoom.Id,
&foundRoom.Version,
&foundRoom.Visibility,
&foundRoom.Name,
&foundRoom.Topic,
&foundRoom.IsDirect,
&foundRoom.Federated,
)
if err != nil {
return
}
2020-10-11 21:11:30 +00:00
foundRoom.Events, 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) {
2020-10-11 21:11:30 +00:00
return event.ReadRoomMembers(roomId)
}
func UpdateRoom(room *Room) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE room SET
2020-10-11 21:11:30 +00:00
version = ?,
visibility = ?,
name = ?,
topic = ?,
isDirect = ?,
federated = ?
WHERE id = ?`)
tx, err := database.DB.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
2020-10-11 21:11:30 +00:00
_, err = stmt.Exec(
room.Version,
room.Visibility,
room.Name,
room.Topic,
room.IsDirect,
room.Federated,
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
}