Extremely basic functionality, posting

This commit is contained in:
2025-11-22 09:15:52 +00:00
parent c3ebbe6e7b
commit 01776f0ec0

67
main.go
View File

@@ -3,29 +3,33 @@ package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/google/uuid"
"github.com/kirsle/configdir"
"github.com/mattn/go-mastodon"
"github.com/google/uuid"
webview "github.com/webview/webview_go"
"encoding/json"
"path/filepath"
"context"
"errors"
"fmt"
"log"
"os"
"context"
"strings"
)
var App fyne.App
var MainWindow fyne.Window
// Client used for posting, getting posts, etc.
var Client *mastodon.Client
// Federale config settings apply to all profiles.
// The config stores the name of the profile to launch,
// as well as if the profile selection screen should
@@ -48,7 +52,6 @@ type FederaleProfile struct { // Blueprint for a Federale profile
UserAuthorizationCode string // Authorization code of the user
Running bool // Whether the profile is currently running
}
@@ -139,6 +142,9 @@ func ProfileLaunch() {
AddProfileWindow.SetFixedSize(true)
GoButton := widget.NewButton("Go", func() {
if !strings.HasPrefix(InstanceEntry.Text, "https://") {
InstanceEntry.SetText("https://" + InstanceEntry.Text) // FIXME: This may not work with darknet instances?
}
Domain := InstanceEntry.Text
// Step one: register the application
AppConfig := &mastodon.AppConfig{
@@ -187,7 +193,20 @@ func ProfileLaunch() {
dialog.ShowForm("Enter authorization code", "Continue", "Exit", FormItems, func(b bool) {
if b {
NewProfile.UserAuthorizationCode = AuthPasswordWidget.Text
config := &mastodon.Config{
Server: NewProfile.Server,
ClientID: NewProfile.ClientID,
ClientSecret: NewProfile.ClientSecret,
}
// Create the client
c := mastodon.NewClient(config)
err = c.GetUserAccessToken(context.Background(), AuthPasswordWidget.Text, app.RedirectURI)
if err != nil {
panic(err)
}
NewProfile.UserAuthorizationCode = c.Config.AccessToken
fmt.Println("Successfully created user profile:\n", NewProfile)
// Save profile to disk
b, err := json.MarshalIndent(NewProfile, "", "\t")
@@ -234,9 +253,6 @@ func ProfileLaunch() {
)
}
Box.Add(ProfileSelection)
RootBox := container.New(layout.NewCenterLayout(), Box)
@@ -267,8 +283,6 @@ func ProfileLaunch() {
}
}
func main() {
log.Println("Checking for federale config")
@@ -331,22 +345,45 @@ func main() {
}
ProfilePath := filepath.Join(ConfigPath, LoadedConfig.ProfileName+".json")
log.Println("Reading profile from disk")
b, err = os.ReadFile(ProfilePath)
if err != nil {
panic(err)
}
tempprof := new(FederaleProfile)
log.Println("Unmarshalling config to RAM")
err = json.Unmarshal(b, LoadedConfig)
err = json.Unmarshal(b, tempprof)
if err != nil {
return
panic(err)
}
LoadedProfile = tempprof
////////////////////////////////////////////////////
config := &mastodon.Config{
Server: LoadedProfile.Server,
ClientID: LoadedProfile.ClientID,
ClientSecret: LoadedProfile.ClientSecret,
AccessToken: LoadedProfile.UserAuthorizationCode,
}
Client = mastodon.NewClient(config)
log.Println(Client)
App = app.New()
MainWindow = App.NewWindow("Federalé")
MainWindow.SetContent(widget.NewLabel("Hello World!"))
TootEntry := widget.NewEntry()
MainWindow.SetContent(container.NewVBox(TootEntry, widget.NewButton("Post", func() {
toot := mastodon.Toot{
Status: TootEntry.Text,
Visibility: "public",
}
_, err := Client.PostStatus(context.Background(), &toot)
if err != nil {
dialog.ShowError(err, MainWindow)
}
})))
MainWindow.ShowAndRun()
}