~gioverse/chat

ref: 84203ce5a460 chat/example/kitchen/gen/message.go -rw-r--r-- 4.2 KiB
84203ce5Jack Mordaunt widget/material: maintain size for empty images a month ago
                                                                                
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Package gen implements data generators for the chat kitchen.
package gen

import (
	"fmt"
	"image"
	"image/color"
	"math"
	"math/rand"
	"strings"
	"sync"
	"time"

	"git.sr.ht/~gioverse/chat/example/kitchen/model"
	matchat "git.sr.ht/~gioverse/chat/widget/material"
	lorem "github.com/drhodes/golorem"
	"github.com/lucasb-eyer/go-colorful"
)

// inflection point in the theoretical message timeline.
// Messages with serial before inflection are older, and messages after it
// are newer.
const inflection = math.MaxInt64 / 2

type Generator struct {
	// FetchImage callback fetches an image of the given size.
	FetchImage func(image.Point) image.Image
	// old is the serial counter for old messages.
	old syncInt
	// new is the serial counter for new messages.
	new syncInt
}

// GenUsers generates some random number of users between min and max.
func (g *Generator) GenUsers(min, max int) *model.Users {
	return GenUsers(min, max, g.FetchImage)
}

// GenRooms generates some random number of rooms between min and max.
func (g *Generator) GenRooms(min, max int) *model.Rooms {
	return GenRooms(min, max, g.FetchImage)
}

// GenHistoryMessage generates an old message that theoretically exists at
// some point in history.
func (g *Generator) GenHistoricMessage(user *model.User) model.Message {
	var (
		serial = g.old.Increment()
		at     = time.Now().Add(time.Hour * time.Duration(serial) * -1)
	)
	// If we generate an empty string, the message will render as an image
	// instead of an empty message.
	return GenMessage(user, lorem.Paragraph(0, 5), inflection-serial, at, g.FetchImage)
}

// GenNewMessage generates a new message ready to be sent to the data model.
func (g *Generator) GenNewMessage(user *model.User, content string) model.Message {
	return GenMessage(user, content, inflection+g.new.Increment(), time.Now(), nil)
}

// GenMessage generates a message with sensible defaults.
func GenMessage(
	user *model.User,
	content string,
	serial int,
	at time.Time,
	fetchImage func(image.Point) image.Image,
) model.Message {
	return model.Message{
		SerialID: fmt.Sprintf("%05d", serial),
		Sender:   user.Name,
		Content:  content,
		SentAt:   at,
		Avatar:   user.Avatar,
		Image: func() string {
			if fetchImage == nil {
				return ""
			}
			if content != "" {
				return ""
			}
			sizes := []image.Point{
				image.Pt(1792, 828),
				image.Pt(828, 1792),
				image.Pt(600, 600),
				image.Pt(300, 300),
			}
			sz := sizes[rand.Intn(len(sizes))]
			return fmt.Sprintf("https://source.unsplash.com/random/%dx%d?nature", sz.X, sz.Y)
		}(),
		Read: func() bool {
			return serial < inflection
		}(),
		Status: func() string {
			if rand.Int()%10 == 0 {
				return matchat.FailedToSend
			}
			return ""
		}(),
	}
}

// GenUsers will generate a random number of fake users.
func GenUsers(min, max int, fetchImage func(image.Point) image.Image) *model.Users {
	var (
		users model.Users
	)
	for ii := rand.Intn(max-min) + min; ii > 0; ii-- {
		users.Add(model.User{
			Name: lorem.Word(4, 15),
			Theme: func() model.Theme {
				if rand.Float32() > 0.7 {
					if rand.Float32() > 0.5 {
						return model.ThemePlatoCookie
					}
					return model.ThemeHotdog
				}
				return model.ThemeEmpty
			}(),
			Avatar: fmt.Sprintf("https://source.unsplash.com/random/%dx%d?nature", 64, 64),
			Color: func() color.NRGBA {
				return ToNRGBA(colorful.FastHappyColor().Clamped())
			}(),
		})
	}
	return &users
}

// GenRooms generates a random number of rooms between min and max.
func GenRooms(min, max int, fetchImage func(image.Point) image.Image) *model.Rooms {
	var rooms model.Rooms
	for ii := rand.Intn(max-min) + min; ii > 0; ii-- {
		rooms.Add(model.Room{
			Name: strings.Trim(lorem.Sentence(1, 5), "."),
			Image: func() image.Image {
				if fetchImage == nil {
					return nil
				}
				return fetchImage(image.Pt(64, 64))
			}(),
		})
	}
	return &rooms
}

// syncInt is a synchronized integer.
type syncInt struct {
	v int
	sync.Mutex
}

// Increment and return a copy of the underlying value.
func (si *syncInt) Increment() int {
	var v int
	si.Lock()
	si.v++
	v = si.v
	si.Unlock()
	return v
}

// ToNRGBA converts a colorful.Color to the nearest representable color.NRGBA.
func ToNRGBA(c colorful.Color) color.NRGBA {
	r, g, b, a := c.RGBA()
	return color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)}
}