Extremely basic functionality, posting
This commit is contained in:
67
main.go
67
main.go
@@ -3,29 +3,33 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/app"
|
"fyne.io/fyne/v2/app"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/container"
|
||||||
"fyne.io/fyne/v2/dialog"
|
"fyne.io/fyne/v2/dialog"
|
||||||
"fyne.io/fyne/v2/layout"
|
"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/kirsle/configdir"
|
||||||
"github.com/mattn/go-mastodon"
|
"github.com/mattn/go-mastodon"
|
||||||
"github.com/google/uuid"
|
|
||||||
webview "github.com/webview/webview_go"
|
webview "github.com/webview/webview_go"
|
||||||
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"context"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var App fyne.App
|
var App fyne.App
|
||||||
var MainWindow fyne.Window
|
var MainWindow fyne.Window
|
||||||
|
|
||||||
|
// Client used for posting, getting posts, etc.
|
||||||
|
var Client *mastodon.Client
|
||||||
|
|
||||||
// Federale config settings apply to all profiles.
|
// Federale config settings apply to all profiles.
|
||||||
// The config stores the name of the profile to launch,
|
// The config stores the name of the profile to launch,
|
||||||
// as well as if the profile selection screen should
|
// 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
|
UserAuthorizationCode string // Authorization code of the user
|
||||||
|
|
||||||
|
|
||||||
Running bool // Whether the profile is currently running
|
Running bool // Whether the profile is currently running
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +142,9 @@ func ProfileLaunch() {
|
|||||||
AddProfileWindow.SetFixedSize(true)
|
AddProfileWindow.SetFixedSize(true)
|
||||||
|
|
||||||
GoButton := widget.NewButton("Go", func() {
|
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
|
Domain := InstanceEntry.Text
|
||||||
// Step one: register the application
|
// Step one: register the application
|
||||||
AppConfig := &mastodon.AppConfig{
|
AppConfig := &mastodon.AppConfig{
|
||||||
@@ -187,7 +193,20 @@ func ProfileLaunch() {
|
|||||||
|
|
||||||
dialog.ShowForm("Enter authorization code", "Continue", "Exit", FormItems, func(b bool) {
|
dialog.ShowForm("Enter authorization code", "Continue", "Exit", FormItems, func(b bool) {
|
||||||
if b {
|
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)
|
fmt.Println("Successfully created user profile:\n", NewProfile)
|
||||||
// Save profile to disk
|
// Save profile to disk
|
||||||
b, err := json.MarshalIndent(NewProfile, "", "\t")
|
b, err := json.MarshalIndent(NewProfile, "", "\t")
|
||||||
@@ -234,9 +253,6 @@ func ProfileLaunch() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Box.Add(ProfileSelection)
|
Box.Add(ProfileSelection)
|
||||||
RootBox := container.New(layout.NewCenterLayout(), Box)
|
RootBox := container.New(layout.NewCenterLayout(), Box)
|
||||||
|
|
||||||
@@ -267,8 +283,6 @@ func ProfileLaunch() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
log.Println("Checking for federale config")
|
log.Println("Checking for federale config")
|
||||||
@@ -331,22 +345,45 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ProfilePath := filepath.Join(ConfigPath, LoadedConfig.ProfileName+".json")
|
ProfilePath := filepath.Join(ConfigPath, LoadedConfig.ProfileName+".json")
|
||||||
log.Println("Reading profile from disk")
|
|
||||||
b, err = os.ReadFile(ProfilePath)
|
b, err = os.ReadFile(ProfilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tempprof := new(FederaleProfile)
|
||||||
log.Println("Unmarshalling config to RAM")
|
log.Println("Unmarshalling config to RAM")
|
||||||
err = json.Unmarshal(b, LoadedConfig)
|
err = json.Unmarshal(b, tempprof)
|
||||||
if err != nil {
|
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()
|
App = app.New()
|
||||||
MainWindow = App.NewWindow("Federalé")
|
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()
|
MainWindow.ShowAndRun()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user