setup.mjs (4474B)
1 import Olm from '@matrix-org/olm'; 2 global.Olm = Olm; 3 4 import inquirer from 'inquirer'; 5 import { 6 MemoryCryptoStore, 7 MemoryStore, 8 createClient, 9 Preset, 10 EventType, 11 RoomCreateTypeField, 12 RoomType, 13 UNSTABLE_MSC3088_PURPOSE, 14 UNSTABLE_MSC3089_TREE_SUBTYPE, 15 UNSTABLE_MSC3088_ENABLED 16 } from 'matrix-js-sdk'; 17 import { MEGOLM_ALGORITHM } from 'matrix-js-sdk/lib/crypto/olmlib.js'; 18 import { DEFAULT_TREE_POWER_LEVELS_TEMPLATE } from 'matrix-js-sdk/lib/models/MSC3089TreeSpace.js'; 19 import { AutoDiscovery } from 'matrix-js-sdk/lib/autodiscovery.js'; 20 import fs from 'fs'; 21 22 console.info(`Welcome to setting up your Matrix Art instance. 23 This will generate the required things for your instance. 24 It will generate the room directory and the config file. 25 You will need a user account that is the admin of your instance. We recommend to use a mjolnir. 26 You will need to provide the following information:`); 27 const replies = await inquirer.prompt([ 28 { 29 type: 'input', 30 name: 'instance_name', 31 message: 'What is the instance Name?', 32 default: "Matrix Art", 33 }, 34 { 35 type: 'input', 36 name: 'homeserver', 37 message: 'What is the instance Homeserver?', 38 default: undefined, 39 }, 40 { 41 type: 'input', 42 name: 'admin_user', 43 message: 'What is the admin user\'s full MXID?', 44 default: undefined, 45 }, 46 { 47 type: 'password', 48 name: 'admin_password', 49 message: 'What is the admin user\'s password? (not going to be stored)', 50 default: undefined, 51 }, 52 ]); 53 54 // Check inputs 55 console.info("Checking inputs..."); 56 if (replies.homeserver === undefined || replies.admin_user === undefined || replies.admin_password === undefined || replies.instance_name === undefined) { 57 console.error('Please fill in all fields'); 58 process.exit(1); 59 } 60 if (replies.homeserver.lastIndexOf('https://', 0) !== 0 && replies.homeserver.lastIndexOf('http://', 0) !== 0) { 61 console.error('Please enter a valid homeserver url'); 62 process.exit(1); 63 } 64 if (replies.admin_user.lastIndexOf('@', 0) !== 0) { 65 console.error('Please enter a valid admin user'); 66 process.exit(1); 67 } 68 69 await Olm.init(); 70 71 // Matrix stuff 72 console.info("Connecting to homeserver..."); 73 const client = createClient({ 74 useAuthorizationHeader: true, 75 baseUrl: replies.homeserver, 76 userId: replies.admin_user, 77 deviceId: "MatrixArtSetup", 78 sessionStore: new MemoryStore(), 79 cryptoStore: new MemoryCryptoStore() 80 }); 81 82 await client.loginWithPassword(replies.admin_user, replies.admin_password); 83 await client.initCrypto(); 84 await client.startClient(); 85 86 // Create room 87 console.info("Creating root room..."); 88 await client.createRoom({ 89 name: replies.instance_name, 90 preset: Preset.PublicChat, 91 power_level_content_override: { 92 ...DEFAULT_TREE_POWER_LEVELS_TEMPLATE, 93 events: { 94 [EventType.SpaceChild]: 0, 95 }, 96 users: { 97 [client.getUserId()]: 100, 98 }, 99 }, 100 creation_content: { 101 [RoomCreateTypeField]: RoomType.Space, 102 }, 103 room_alias_name: replies.instance_name.replace(" ", "_"), 104 initial_state: [ 105 { 106 type: UNSTABLE_MSC3088_PURPOSE.name, 107 state_key: UNSTABLE_MSC3089_TREE_SUBTYPE.name, 108 content: { 109 [UNSTABLE_MSC3088_ENABLED.name]: true, 110 }, 111 }, 112 { 113 type: EventType.RoomEncryption, 114 state_key: "", 115 content: { 116 algorithm: MEGOLM_ALGORITHM, 117 }, 118 }, 119 { 120 type: EventType.RoomGuestAccess, 121 state_key: "", 122 content: { 123 guest_access: "can_join", 124 }, 125 }, 126 { 127 type: EventType.RoomHistoryVisibility, 128 state_key: "", 129 content: { 130 history_visibility: "world_readable", 131 }, 132 } 133 ], 134 }); 135 136 const wellKnown = await AutoDiscovery.getRawClientConfig(replies.homeserver); 137 138 // Write config 139 console.info("Writing config..."); 140 fs.writeFileSync('./.env.local', `VITE_MATRIX_SERVER_URL="${replies.homeserver}" 141 VITE_MATRIX_INSTANCE_ADMIN="${replies.admin_user}" 142 VITE_MATRIX_ROOT_FOLDER="#${replies.instance_name.replace(" ", "_")}:${wellKnown['m.homeserver']}")}"`); 143 144 console.info("Your instance is ready! Have fun!"); 145 process.exit(0); 146 147 //TODO: Allow upgrading the room with this script if the matrix art requirements change