Skip to content

Commit

Permalink
Feature/v7 (#9)
Browse files Browse the repository at this point in the history
* Bump version

* Add dummy invite code feature (#10)

* Add "Unicode" button (#11)

* Use buildinfo, and fix slog usage (#12)
  • Loading branch information
kakakaya committed Apr 18, 2023
1 parent 72c7d16 commit 30321f0
Show file tree
Hide file tree
Showing 6 changed files with 1,088 additions and 36 deletions.
52 changes: 33 additions & 19 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strings"
"net/http"
"os"
"strings"
"time"

"github.com/adrg/xdg"
"github.com/bluesky-social/indigo/xrpc"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/exp/slog"
"github.com/gen2brain/beeep"
)

// App struct
type App struct {
config Config
ctx context.Context
xrpcc *xrpc.Client
logger *log.Logger
config Config
ctx context.Context
xrpcc *xrpc.Client
logger *slog.Logger
environment runtime.EnvironmentInfo
}

// dialogResults is used for casting Dialog's response to boolean. Note that keys are lowercase.
Expand All @@ -46,26 +49,32 @@ func NewApp() *App {
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.environment = runtime.Environment(ctx)
if a.environment.BuildType == "dev" {
// Use stdout in dev mode
a.logger = slog.New(slog.NewTextHandler(os.Stdout))
}
a.logger.Info("Startup", "environment", a.environment)

// Build xrpc.Client
var auth *xrpc.AuthInfo

cred := a.config.Credential
if cred.Host == "" || cred.Identifier == "" || cred.Password == "" {
a.logger.Println("Credentials not set!")
a.logger.Warn("Credentials not set!")
result, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "認証情報がないよ",
Message: "config.tomlにIDとパスワードを入力してね。\nこのまま沈没するけど、設定ファイルの場所を開く?",
DefaultButton: "Yes",
})
if err != nil {
a.logger.Fatalln("Error creds not set dialog: ", err)
a.logger.Warn("Error creds not set dialog", "error", err)
}
a.logger.Println(result)
a.logger.Info(result)
if dialogResults[strings.ToLower(result)] {
a.OpenConfigDirectory()
}
a.logger.Fatalln("Missing credentials")
a.logger.Warn("Missing credentials")
}

auth, err := createSession(cred.Host, cred.Identifier, cred.Password)
Expand All @@ -80,14 +89,14 @@ func (a *App) startup(ctx context.Context) {
})

if err != nil {
a.logger.Fatalln("Error creds not set dialog: ", err)
a.logger.Warn("Error creds not set dialog", "error", err)
}
a.logger.Println(result)
a.logger.Info(result)
if dialogResults[strings.ToLower(result)] {
a.OpenConfigDirectory()
}

a.logger.Fatalf("Failed creating session, bad identifier or password?: %v", err)
a.logger.Warn("Failed creating session, bad identifier or password?", "error", err)
panic(err)
}

Expand Down Expand Up @@ -150,14 +159,19 @@ func NewHttpClient() *http.Client {
}

func (a *App) Post(text string) string {
a.logger.Println("Post: ", text)
a.logger.Info("Post", "text", text)
err := beeep.Notify("まぜそば大陸", fmt.Sprintf("BskyFeedPost: %s", text), "")

if a.environment.BuildType == "dev" {
return "<MOCK URI>"
}
res, err := BskyFeedPost(a.xrpcc, text)
if err != nil {
errs := fmt.Sprintf("Posting error: %s", err.Error())
a.logger.Println(errs)
a.logger.Error("Post: Error on BskyFeedPost", "error", errs)
return errs
}
a.logger.Println("Post result: ", res)
a.logger.Info("Posted", "result", res)
return res
}

Expand All @@ -167,13 +181,13 @@ func (a *App) Chikuwa(text string) string {

func (a *App) OpenConfigDirectory() error {
f, _ := xdg.ConfigFile("mazesoba-continent")
a.logger.Println("OpenConfigDirectory: ", f)
a.logger.Info("OpenConfigDirectory", "path", f)
return openDirectory(f)
}

func (a *App) OpenLogDirectory() error {
f, _ := xdg.CacheFile("mazesoba-continent")
a.logger.Println("OpenLogDirectory: ", f)
a.logger.Info("OpenLogDirectory", "path", f)
return openDirectory(f)
}

Expand Down
8 changes: 0 additions & 8 deletions bluesky.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ import (
appbsky "github.com/bluesky-social/indigo/api/bsky"
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/xrpc"
"github.com/gen2brain/beeep"
)

func BskyFeedPost(xrpcc *xrpc.Client, text string) (string, error) {
err := beeep.Notify("まぜそば大陸", fmt.Sprintf("BskyFeedPost: %s", text), "")
if err != nil {
return "", err
}

// return "<MOCK URI>", nil // TEMP

resp, err := comatproto.RepoCreateRecord(context.TODO(), xrpcc, &comatproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: xrpcc.Auth.Did,
Expand Down
26 changes: 25 additions & 1 deletion frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
Quit
} from '../wailsjs/runtime/runtime.js'
import {
GenerateDummyInviteCode,
} from './topping/invite-code.js'
import {
ConvertRichUnicode
} from './topping/unicode.js'
let text
let charCounter = 0
let placeholder = Math.random() > 0.5 ? "最近どう?" : "どう最近?"
Expand All @@ -33,12 +41,22 @@
})
}
function getDummyInviteCode() {
if (text === undefined | !(typeof text === 'string' && text.startsWith('bsky-'))) {
text = GenerateDummyInviteCode()
} else {
text += "\n" + GenerateDummyInviteCode()
}
}
function earthquake() {
text = "地震だ!"
}
function version() {
GetVersion().then(version => text = "まぜそば大陸バージョン" + version + "が浮上中")
GetVersion().then(version => text = "まぜそば大陸 バージョン" + version + "が浮上中")
}
function chikuwa() {
Expand All @@ -51,6 +69,10 @@
})
}
function unicode() {
text = ConvertRichUnicode(text, 'sansBold')
}
function handleKeyDown(event) {
if ((event.ctrlKey || event.metaKey) && event.key == 'Enter') {
event.preventDefault()
Expand Down Expand Up @@ -80,7 +102,9 @@
<button class="btn" on:click={chikuwa}>ちくわ。</button>
<button class="btn" on:click={WindowCenter}>画面中央</button>
<button class="btn" on:click={earthquake}>地震だ!(F9)</button>
<button class="btn" on:click={getDummyInviteCode}>嘘招待コード生成</button>
<button class="btn" on:click={version}>バージョン</button>
<button class="btn" on:click={unicode}>Unicode</button>
<button class="btn" on:click={OpenConfigDirectory}>設定の場所を開く(Ctrl+,)</button>
<button class="btn" on:click={OpenLogDirectory}>ログの場所を開く</button>
</div>
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/topping/invite-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function GenerateDummyInviteCode() {
return "bsky-social-"+generateRandomString(7);
}

function generateRandomString(length) {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let result = '';

while (true) {
if (result.length === length) {
const regex = /\d/g;
const matches = result.match(regex);
if (matches && matches.length > 1) {
console.log(result);
result = ''; // reset
} else {
return result; // OK
}
}
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
}

0 comments on commit 30321f0

Please sign in to comment.