2020-10-03 12:22:29 +00:00
|
|
|
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-03 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func CreateRoom(room *Room) (err error) {
|
2020-10-03 12:22:29 +00:00
|
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO room
|
2020-10-11 21:11:30 +00:00
|
|
|
(id, version, visibility, name, topic, isDirect, federated)
|
2020-10-03 12:22:29 +00:00
|
|
|
VALUES
|
2020-10-11 21:11:30 +00:00
|
|
|
(?, ?, ?, ?, ?, ?, ?)`)
|
2020-10-03 12:22:29 +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,
|
|
|
|
)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tx.Commit()
|
2020-10-11 21:11:30 +00:00
|
|
|
for _, userId := range room.Members {
|
|
|
|
err = CreateRoomMember(room.Id, userId)
|
|
|
|
}
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateRoomMember(roomId string, userId string) (err error) {
|
2020-10-11 21:11:30 +00:00
|
|
|
return event.CreateRoomMember(roomId, userId)
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-10-03 12:22:29 +00:00
|
|
|
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,
|
|
|
|
)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
foundRoom.Events, err = event.ReadEventsFromRoom(foundRoom.Id)
|
2020-10-03 12:22:29 +00:00
|
|
|
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)
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = ?
|
2020-10-03 12:22:29 +00:00
|
|
|
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,
|
|
|
|
)
|
2020-10-03 12:22:29 +00:00
|
|
|
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
|
|
|
|
}
|