Matrix/entities/room/roomDatabaseConnector.go

211 lines
3.5 KiB
Go
Raw Normal View History

package room
import (
"fmt"
2020-10-17 10:07:39 +00:00
"git.nutfactory.org/hoernschen/Matrix/config"
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 {
2020-10-17 10:07:39 +00:00
tx.Rollback()
return
}
tx.Commit()
2020-10-11 21:11:30 +00:00
for _, userId := range room.Members {
2020-10-17 10:07:39 +00:00
err = CreateRoomMember(room.Id, userId, config.Homeserver)
2020-10-11 21:11:30 +00:00
}
return
}
2020-10-17 10:07:39 +00:00
func CreateRoomMember(roomId string, userId string, server string) (err error) {
return event.CreateRoomMember(roomId, userId, server)
}
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 {
2020-10-17 10:07:39 +00:00
tx.Rollback()
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
}
2020-10-17 10:07:39 +00:00
_, err = tx.Exec(queryStmt)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
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
}
2020-10-17 10:07:39 +00:00
_, err = tx.Exec(queryStmt)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
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
}
2020-10-17 10:07:39 +00:00
_, err = tx.Exec(queryStmt)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
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
}
2020-10-17 10:07:39 +00:00
_, err = tx.Exec(queryStmt)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
return
}
tx.Commit()
return
}