package room import ( "fmt" "git.nutfactory.org/hoernschen/Matrix/config" "git.nutfactory.org/hoernschen/Matrix/entities/event" "git.nutfactory.org/hoernschen/Matrix/utils/database" ) func CreateRoom(room *Room) (err error) { sqlStmt := fmt.Sprintf(`INSERT INTO room (id, version, visibility, name, topic, isDirect, federated) 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, room.Visibility, room.Name, room.Topic, room.IsDirect, room.Federated, ) if err != nil { tx.Rollback() return } tx.Commit() for _, userId := range room.Members { err = CreateRoomMember(room.Id, userId, config.Homeserver) } return } func CreateRoomMember(roomId string, userId string, server string) (err error) { return event.CreateRoomMember(roomId, userId, server) } func ReadRoom(id string) (foundRoom *Room, err error) { 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{} err = rows.Scan( &foundRoom.Id, &foundRoom.Version, &foundRoom.Visibility, &foundRoom.Name, &foundRoom.Topic, &foundRoom.IsDirect, &foundRoom.Federated, ) if err != nil { return } 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) { return event.ReadRoomMembers(roomId) } func UpdateRoom(room *Room) (err error) { sqlStmt := fmt.Sprintf(`UPDATE room SET 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() _, err = stmt.Exec( room.Version, room.Visibility, room.Name, room.Topic, room.IsDirect, room.Federated, room.Id, ) if err != nil { 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 } _, err = tx.Exec(queryStmt) if err != nil { 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 } _, err = tx.Exec(queryStmt) if err != nil { 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 } _, err = tx.Exec(queryStmt) if err != nil { 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 } _, err = tx.Exec(queryStmt) if err != nil { tx.Rollback() return } tx.Commit() return }