2023-09-06 01:07:05 +00:00
// @ts-check
const assert = require ( "assert" ) . strict
const fs = require ( "fs" )
const sqlite = require ( "better-sqlite3" )
const HeatSync = require ( "heatsync" )
const config = require ( "../config" )
const passthrough = require ( "../passthrough" )
const db = new sqlite ( "db/ooye.db" )
2023-10-02 01:00:14 +00:00
const migrate = require ( "../db/migrate" )
2023-09-06 01:07:05 +00:00
const sync = new HeatSync ( { watchFS : false } )
2023-10-07 08:57:09 +00:00
Object . assign ( passthrough , { sync , config , db } )
const orm = sync . require ( "../db/orm" )
passthrough . from = orm . from
passthrough . select = orm . select
const DiscordClient = require ( "../d2m/discord-client" )
const discord = new DiscordClient ( config . discordToken , "no" )
passthrough . discord = discord
2023-09-06 01:07:05 +00:00
const api = require ( "../matrix/api" )
const file = require ( "../matrix/file" )
const reg = require ( "../matrix/read-registration" )
const utils = require ( "../m2d/converters/utils" )
; ( async ( ) => {
const mxid = ` @ ${ reg . sender _localpart } : ${ reg . ooye . server _name } `
// ensure registration is correctly set...
2023-09-12 08:43:56 +00:00
assert ( reg . sender _localpart . startsWith ( reg . ooye . namespace _prefix ) ) // appservice's localpart must be in the namespace it controls
assert ( utils . eventSenderIsFromDiscord ( mxid ) ) // appservice's mxid must be in the namespace it controls
assert ( reg . ooye . server _origin . match ( /^https?:\/\// ) ) // must start with http or https
assert . notEqual ( reg . ooye . server _origin . slice ( - 1 ) , "/" ) // must not end in slash
2023-10-07 08:57:09 +00:00
console . log ( "✅ Configuration looks good..." )
2023-09-06 01:07:05 +00:00
2023-09-12 07:23:23 +00:00
// database ddl...
2023-10-02 01:00:14 +00:00
await migrate . migrate ( db )
2023-09-12 07:23:23 +00:00
2023-10-07 08:57:09 +00:00
// add initial rows to database, like adding the bot to sim...
db . prepare ( "INSERT OR IGNORE INTO sim (user_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)" ) . run ( "0" , reg . sender _localpart . slice ( reg . ooye . namespace _prefix . length ) , reg . sender _localpart , mxid )
console . log ( "✅ Database is ready..." )
2023-09-06 01:07:05 +00:00
// ensure homeserver well-known is valid and returns reg.ooye.server_name...
// upload initial images...
2023-10-07 08:57:09 +00:00
const avatarUrl = await file . uploadDiscordFileToMxc ( "https://cadence.moe/friends/out_of_your_element.png" )
// set profile data on discord...
const avatarImageBuffer = await fetch ( "https://cadence.moe/friends/out_of_your_element.png" ) . then ( res => res . arrayBuffer ( ) )
await discord . snow . user . updateSelf ( { avatar : "data:image/png;base64," + Buffer . from ( avatarImageBuffer ) . toString ( "base64" ) } )
await discord . snow . requestHandler . request ( ` /applications/@me ` , { } , "patch" , "json" , { description : "Powered by **Out Of Your Element**\nhttps://gitdab.com/cadence/out-of-your-element" } )
console . log ( "✅ Discord profile updated..." )
2023-09-06 01:07:05 +00:00
// set profile data on homeserver...
await api . profileSetDisplayname ( mxid , "Out Of Your Element" )
await api . profileSetAvatarUrl ( mxid , avatarUrl )
2023-10-07 08:57:09 +00:00
console . log ( "✅ Matrix profile updated..." )
2023-09-06 01:07:05 +00:00
2023-10-07 08:57:09 +00:00
console . log ( "Good to go. I hope you enjoy Out Of Your Element." )
process . exit ( )
2023-09-06 01:07:05 +00:00
} ) ( )