summaryrefslogtreecommitdiff
path: root/lookup.go
blob: 96aea43194be517ba780276719786e61b6b2d441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
	"github.com/bwmarrin/snowflake"
	"log"
	"random.chars.jp/git/image-board/store"
	"time"
)

func init() {
	snowflake.Epoch = 0
}

var lookupMenu = &fyne.MenuItem{
	ChildMenu: &fyne.Menu{
		Label: "Lookup",
		Items: []*fyne.MenuItem{
			{
				Label: "By snowflake",
				Action: func() {
					entry := widget.NewEntry()
					dialog.ShowForm("Lookup by snowflake", "Lookup", "Cancel",
						[]*widget.FormItem{widget.NewFormItem("Snowflake", entry)}, func(submit bool) {
							if !submit {
								return
							}
							if id, err := snowflake.ParseString(entry.Text); err != nil {
								go dialog.ShowError(err, window)
								return
							} else {
								go func(id snowflake.ID) {
									defer loading("Fetching", "Waiting for server...")()
									s := time.Now()
									switch id.Node() {
									case store.UserSnowflakeNodeID:
										log.Printf("Looking up user snowflake %s.", id.String())
										if user, err := remote.User(id.String()); err != nil {
											log.Printf("Error lookup user snowflake %s, %s", id.String(), err)
											dialog.ShowError(err, window)
										} else {
											if user.ID == "" {
												log.Printf("User snowflake %s not found in %s.",
													id.String(), time.Now().Sub(s))
												dialog.ShowInformation("No such user",
													"No user exists with specified snowflake.", window)
												return
											}
											log.Printf("User snowflake %s queried in %s.",
												id.String(), time.Now().Sub(s))
											displayUserInfo(user)
										}
									case store.ImageSnowflakeNodeID:
										log.Printf("Looking up image snowflake %s.", id.String())
										if image, err := remote.Image(id.String()); err != nil {
											log.Printf("Error lookup image snowflake %s, %s", id.String(), err)
											dialog.ShowError(err, window)
										} else {
											if image.Snowflake == "" {
												log.Printf("Image snowflake %s not found in %s.",
													id.String(), time.Now().Sub(s))
												dialog.ShowInformation("No such image",
													"No image exists with specified snowflake.", window)
												return
											}
											setImage(image)
										}
									default:
										dialog.ShowInformation("Impossible snowflake",
											"Snowflake has impossible node ID.", window)
									}
								}(id)
							}
						}, window)
				},
			},
			{
				Label: "By username",
				Action: func() {
					entry := widget.NewEntry()
					dialog.ShowForm("Lookup user by username", "Lookup", "Cancel",
						[]*widget.FormItem{widget.NewFormItem("Username", entry)}, func(submit bool) {
							if !submit {
								return
							}
							if entry.Text == "" || !store.MatchName(entry.Text) {
								go dialog.ShowInformation("Invalid input", "Not a valid username.", window)
								return
							}
							go func() {
								s := time.Now()
								defer loading("Fetching", "Waiting for server...")()
								if user, err := remote.Username(entry.Text); err != nil {
									log.Printf("Error lookup username %s, %s.", entry.Text, err)
									dialog.ShowError(err, window)
									return
								} else {
									f := time.Now().Sub(s)
									if user.ID == "" {
										log.Printf("Username %s not found in %s.", entry.Text, f)
										dialog.ShowInformation("No such user",
											"No user exists with specified username.", window)
										return
									}
									log.Printf("Fetched user by username %s in %s.", entry.Text, f)
									displayUserInfo(user)
								}
							}()
						}, window)
				},
			},
		},
	},
	Label: "Lookup",
}