telephant/chirp.go

193 lines
4.8 KiB
Go
Raw Normal View History

2017-08-29 05:09:17 +00:00
package main
import (
"fmt"
2017-08-29 14:05:51 +00:00
"log"
2017-08-29 05:09:17 +00:00
"os"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/quickcontrols2"
2019-05-01 15:15:56 +00:00
"github.com/muesli/chirp/accounts/mastodon"
2017-08-29 05:09:17 +00:00
)
var (
config Config
conversationModel = NewMessageModel(nil)
accountMessagesModel = NewMessageModel(nil)
)
func connectToInstance(instance string) {
var authURI string
var redirectURI string
var err error
tc, authURI, redirectURI, err = mastodon.RegisterAccount(instance)
fmt.Println("auth uri:", authURI)
fmt.Println("redirect uri:", redirectURI)
fmt.Println(err)
}
func authInstance(code string) {
instance, token, clientID, clientSecret, err := tc.Authenticate(code)
fmt.Println("authenticate:", err)
if err != nil {
return
}
config.Account[0].Instance = instance
config.Account[0].ClientID = clientID
config.Account[0].ClientSecret = clientSecret
config.Account[0].Token = token
setupMastodon(config.Account[0])
}
2019-05-02 08:37:04 +00:00
// reply is used to post a new message
2017-08-29 05:09:17 +00:00
// if replyid is > 0, it's send as a reply
func reply(replyid string, message string) {
var err error
if replyid != "" {
log.Println("Sending reply to:", replyid, message)
err = tc.Reply(replyid, message)
2017-08-29 05:09:17 +00:00
} else {
log.Println("Posting:", message)
err = tc.Post(message)
2017-08-29 05:09:17 +00:00
}
if err != nil {
2019-05-01 15:15:56 +00:00
log.Println("Error posting to Account:", err)
2017-08-29 05:09:17 +00:00
}
}
2019-05-06 20:51:31 +00:00
// share a post
func share(id string) {
log.Println("Sharing:", id)
if err := tc.Share(id); err != nil {
log.Println("Error posting to Account:", err)
}
2017-08-29 05:09:17 +00:00
}
2019-05-06 20:51:31 +00:00
// unshare a post
func unshare(id string) {
log.Println("Unsharing:", id)
if err := tc.Unshare(id); err != nil {
log.Println("Error posting to Account:", err)
}
}
// like a post
2017-08-29 05:09:17 +00:00
func like(id string) {
log.Println("Liking:", id)
if err := tc.Like(id); err != nil {
log.Println("Error posting to Account:", err)
}
2017-08-29 05:09:17 +00:00
}
2019-05-06 20:51:31 +00:00
// unlike a post
func unlike(id string) {
log.Println("Unliking:", id)
if err := tc.Unlike(id); err != nil {
log.Println("Error posting to Account:", err)
}
}
// loadConversation loads a message thread
func loadConversation(id string) {
log.Println("Loading conversation:", id)
messages, err := tc.LoadConversation(id)
if err != nil {
log.Println("Error loading conversation:", err)
return
}
fmt.Println("Found conversation posts:", len(messages))
conversationModel.Clear()
for _, m := range messages {
p := messageFromEvent(m)
conversationModel.AppendMessage(p)
}
}
// loadAccount loads an entire profile
func loadAccount(id string) {
log.Println("Loading account:", id)
profile, messages, err := tc.LoadAccount(id)
if err != nil {
log.Println("Error loading account:", err)
return
}
profileBridge.SetUsername(profile.Username)
profileBridge.SetName(profile.Name)
profileBridge.SetAvatar(profile.Avatar)
profileBridge.SetProfileURL(profile.ProfileURL)
profileBridge.SetProfileID(profile.ProfileID)
profileBridge.SetPosts(profile.Posts)
profileBridge.SetFollows(profile.Follows)
profileBridge.SetFollowers(profile.Followers)
fmt.Println("Found account posts:", len(messages))
accountMessagesModel.Clear()
for _, m := range messages {
p := messageFromEvent(m)
accountMessagesModel.AppendMessage(p)
}
}
2017-08-29 05:09:17 +00:00
// runApp loads and executes the QML UI
func runApp(config Config) {
quickcontrols2.QQuickStyle_SetStyle(config.Style)
app := qml.NewQQmlApplicationEngine(nil)
app.RootContext().SetContextProperty("uiBridge", uiBridge)
app.RootContext().SetContextProperty("accountBridge", accountBridge)
app.RootContext().SetContextProperty("profileBridge", profileBridge)
2017-08-29 05:09:17 +00:00
app.RootContext().SetContextProperty("settings", configBridge)
app.Load(core.NewQUrl3("qrc:/qml/chirp.qml", 0))
gui.QGuiApplication_Exec()
}
2019-05-01 15:15:56 +00:00
// setupMastodon starts a new Mastodon client and sets up event handling & models for it
func setupMastodon(config Account) {
tc = mastodon.NewAccount(config.Instance, config.Token, config.ClientID, config.ClientSecret)
2019-05-02 08:37:04 +00:00
postModel := NewMessageModel(nil)
2017-08-29 05:09:17 +00:00
notificationModel := NewMessageModel(nil)
accountBridge.SetUsername("Logging in...")
2019-05-02 08:37:04 +00:00
accountBridge.SetMessages(postModel)
2017-08-29 05:09:17 +00:00
accountBridge.SetNotifications(notificationModel)
accountBridge.SetConversation(conversationModel)
accountBridge.SetAccountMessages(accountMessagesModel)
2017-08-29 05:09:17 +00:00
evchan := make(chan interface{})
2019-05-02 08:37:04 +00:00
go handleEvents(evchan, postModel, notificationModel)
2017-08-29 05:09:17 +00:00
go func() {
tc.Run(evchan)
}()
}
func main() {
core.QCoreApplication_SetApplicationName("Chirp")
2019-05-01 15:15:56 +00:00
core.QCoreApplication_SetOrganizationName("fribbledom.com")
2017-08-29 05:09:17 +00:00
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
gui.NewQGuiApplication(len(os.Args), os.Args)
setupQmlBridges()
// load config
config = LoadConfig()
2017-08-29 05:09:17 +00:00
if config.Style == "" {
config.Style = "Material"
}
configBridge.SetStyle(config.Style)
2019-05-01 15:15:56 +00:00
setupMastodon(config.Account[0])
2017-08-29 05:09:17 +00:00
runApp(config)
// save config
config.Style = configBridge.Style()
SaveConfig(config)
}