all repos — honk @ v1.0.0

my fork of honk

honk.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"html/template"
 20	"strconv"
 21	"strings"
 22	"time"
 23
 24	"humungus.tedunangst.com/r/webs/httpsig"
 25)
 26
 27type WhatAbout struct {
 28	ID      int64
 29	Name    string
 30	Display string
 31	About   string
 32	HTAbout template.HTML
 33	Onts    []string
 34	Key     string
 35	URL     string
 36	Options UserOptions
 37	SecKey  httpsig.PrivateKey
 38}
 39
 40type UserOptions struct {
 41	SkinnyCSS    bool   `json:",omitempty"`
 42	OmitImages   bool   `json:",omitempty"`
 43	MentionAll   bool   `json:",omitempty"`
 44	InlineQuotes bool   `json:",omitempty"`
 45	Avatar       string `json:",omitempty"`
 46	Banner       string `json:",omitempty"`
 47	MapLink      string `json:",omitempty"`
 48	Reaction     string `json:",omitempty"`
 49	MeCount      int64
 50	ChatCount    int64
 51}
 52
 53type KeyInfo struct {
 54	keyname string
 55	seckey  httpsig.PrivateKey
 56}
 57
 58const serverUID int64 = -2
 59const readyLuserOne int64 = 1
 60
 61type Honk struct {
 62	ID       int64
 63	UserID   int64
 64	Username string
 65	What     string
 66	Honker   string
 67	Handle   string
 68	Handles  string
 69	Oonker   string
 70	Oondle   string
 71	XID      string
 72	RID      string
 73	Date     time.Time
 74	URL      string
 75	Noise    string
 76	Precis   string
 77	Format   string
 78	Convoy   string
 79	Audience []string
 80	Public   bool
 81	Whofore  int64
 82	Replies  []*Honk
 83	Flags    int64
 84	HTPrecis template.HTML
 85	HTML     template.HTML
 86	Style    string
 87	Open     string
 88	Donks    []*Donk
 89	Onts     []string
 90	Place    *Place
 91	Time     *Time
 92	Mentions []Mention
 93	Badonks  []Badonk
 94}
 95
 96type Badonk struct {
 97	Who  string
 98	What string
 99}
100
101type Chonk struct {
102	ID     int64
103	UserID int64
104	XID    string
105	Who    string
106	Target string
107	Date   time.Time
108	Noise  string
109	Format string
110	Donks  []*Donk
111	Handle string
112	HTML   template.HTML
113}
114
115type Chatter struct {
116	Target string
117	Chonks []*Chonk
118}
119
120type Mention struct {
121	Who   string
122	Where string
123}
124
125func (mention *Mention) IsPresent(noise string) bool {
126	nick := strings.TrimLeft(mention.Who, "@")
127	idx := strings.IndexByte(nick, '@')
128	if idx != -1 {
129		nick = nick[:idx]
130	}
131	return strings.Contains(noise, ">@"+nick) || strings.Contains(noise, "@<span>"+nick)
132}
133
134func OntIsPresent(ont, noise string) bool {
135	ont = strings.ToLower(ont[1:])
136	idx := strings.IndexByte(noise, '#')
137	for idx >= 0 {
138		if strings.HasPrefix(noise[idx:], "#<span>") {
139			idx += 6
140		}
141		idx += 1
142		if idx+len(ont) >= len(noise) {
143			return false
144		}
145		test := noise[idx : idx+len(ont)]
146		test = strings.ToLower(test)
147		if test == ont {
148			return true
149		}
150		newidx := strings.IndexByte(noise[idx:], '#')
151		if newidx == -1 {
152			return false
153		}
154		idx += newidx
155	}
156	return false
157}
158
159type OldRevision struct {
160	Precis string
161	Noise  string
162}
163
164const (
165	flagIsAcked    = 1
166	flagIsBonked   = 2
167	flagIsSaved    = 4
168	flagIsUntagged = 8
169	flagIsReacted  = 16
170)
171
172func (honk *Honk) IsAcked() bool {
173	return honk.Flags&flagIsAcked != 0
174}
175
176func (honk *Honk) IsBonked() bool {
177	return honk.Flags&flagIsBonked != 0
178}
179
180func (honk *Honk) IsSaved() bool {
181	return honk.Flags&flagIsSaved != 0
182}
183
184func (honk *Honk) IsUntagged() bool {
185	return honk.Flags&flagIsUntagged != 0
186}
187
188func (honk *Honk) IsReacted() bool {
189	return honk.Flags&flagIsReacted != 0
190}
191
192type Donk struct {
193	FileID   int64
194	XID      string
195	Name     string
196	Desc     string
197	URL      string
198	Media    string
199	Local    bool
200	External bool
201}
202
203type Place struct {
204	Name      string
205	Latitude  float64
206	Longitude float64
207	Url       string
208}
209
210type Duration int64
211
212func (d Duration) String() string {
213	s := time.Duration(d).String()
214	if strings.HasSuffix(s, "m0s") {
215		s = s[:len(s)-2]
216	}
217	if strings.HasSuffix(s, "h0m") {
218		s = s[:len(s)-2]
219	}
220	return s
221}
222
223func parseDuration(s string) time.Duration {
224	didx := strings.IndexByte(s, 'd')
225	if didx != -1 {
226		days, _ := strconv.ParseInt(s[:didx], 10, 0)
227		dur, _ := time.ParseDuration(s[didx:])
228		return dur + 24*time.Hour*time.Duration(days)
229	}
230	dur, _ := time.ParseDuration(s)
231	return dur
232}
233
234type Time struct {
235	StartTime time.Time
236	EndTime   time.Time
237	Duration  Duration
238}
239
240type Honker struct {
241	ID     int64
242	UserID int64
243	Name   string
244	XID    string
245	Handle string
246	Flavor string
247	Combos []string
248	Meta   HonkerMeta
249}
250
251type HonkerMeta struct {
252	Notes string
253}
254
255type SomeThing struct {
256	What  int
257	XID   string
258	Owner string
259	Name  string
260}
261
262const (
263	SomeNothing int = iota
264	SomeActor
265	SomeCollection
266)