package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

func main() {
	// 要删除的房间ID
	roomID := "chat"
	// Synapse服务器地址
	serverURL := "https://chihaya.one"
	// 管理员的访问令牌
	accessToken := "4TM40GH-FKOso5XJu6e5ku5cRcbRQsJwTaOgDVRL5MY"

	// 请求体
	requestBody := map[string]interface{}{
		"block": true,
		"purge": true,
	}

	// 将请求体转换为JSON格式
	requestBodyJson, err := json.Marshal(requestBody)
	if err != nil {
		fmt.Println("Error marshalling request body:", err)
		os.Exit(1)
	}

	// 构建请求URL
	requestURL := fmt.Sprintf("%s/_synapse/admin/v1/rooms/%s", serverURL, roomID)

	// 创建HTTP请求
	req, err := http.NewRequest("DELETE", requestURL, bytes.NewBuffer(requestBodyJson))
	if err != nil {
		fmt.Println("Error creating HTTP request:", err)
		os.Exit(1)
	}

	// 设置请求头
	req.Header.Set("Authorization", "Bearer "+accessToken)
	req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending HTTP request:", err)
		os.Exit(1)
	}
	defer resp.Body.Close()

	// 读取响应
	var response map[string]interface{}
	if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
		fmt.Println("Error decoding response:", err)
		os.Exit(1)
	}

	// 输出响应
	responseJson, err := json.MarshalIndent(response, "", "  ")
	if err != nil {
		fmt.Println("Error marshalling response:", err)
		os.Exit(1)
	}
	fmt.Println("Response:", string(responseJson))
}
