all repos — honk @ 82fa46dc677b91efc12c54114dd1f7dc05c01ea2

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	ChatPubKey boxPubKey
 39	ChatSecKey boxSecKey
 40}
 41
 42type UserOptions struct {
 43	SkinnyCSS    bool   `json:",omitempty"`
 44	OmitImages   bool   `json:",omitempty"`
 45	MentionAll   bool   `json:",omitempty"`
 46	InlineQuotes bool   `json:",omitempty"`
 47	Avatar       string `json:",omitempty"`
 48	Banner       string `json:",omitempty"`
 49	MapLink      string `json:",omitempty"`
 50	Reaction     string `json:",omitempty"`
 51	MeCount      int64
 52	ChatCount    int64
 53	ChatPubKey   string
 54	ChatSecKey   string
 55}
 56
 57type KeyInfo struct {
 58	keyname string
 59	seckey  httpsig.PrivateKey
 60}
 61
 62const serverUID int64 = -2
 63const readyLuserOne int64 = 1
 64
 65type Honk struct {
 66	ID        int64
 67	UserID    int64
 68	Username  string
 69	What      string
 70	Honker    string
 71	Handle    string
 72	Handles   string
 73	Oonker    string
 74	Oondle    string
 75	XID       string
 76	RID       string
 77	Date      time.Time
 78	URL       string
 79	Noise     string
 80	Precis    string
 81	Format    string
 82	Convoy    string
 83	Audience  []string
 84	Public    bool
 85	Whofore   int64
 86	Replies   []*Honk
 87	Flags     int64
 88	HTPrecis  template.HTML
 89	HTML      template.HTML
 90	Style     string
 91	Open      string
 92	Donks     []*Donk
 93	Onts      []string
 94	Place     *Place
 95	Time      *Time
 96	Link      string
 97	Mentions  []Mention
 98	Badonks   []Badonk
 99	SeeAlso   string
100	Onties    string
101	LegalName string
102}
103
104type Badonk struct {
105	Who  string
106	What string
107}
108
109type Chonk struct {
110	ID     int64
111	UserID int64
112	XID    string
113	Who    string
114	Target string
115	Date   time.Time
116	Noise  string
117	Format string
118	Donks  []*Donk
119	Handle string
120	HTML   template.HTML
121}
122
123type Chatter struct {
124	Target string
125	Chonks []*Chonk
126}
127
128type Mention struct {
129	Who   string
130	Where string
131}
132
133func (mention *Mention) IsPresent(noise string) bool {
134	nick := strings.TrimLeft(mention.Who, "@")
135	idx := strings.IndexByte(nick, '@')
136	if idx != -1 {
137		nick = nick[:idx]
138	}
139	return strings.Contains(noise, ">@"+nick) || strings.Contains(noise, "@<span>"+nick)
140}
141
142func OntIsPresent(ont, noise string) bool {
143	ont = strings.ToLower(ont[1:])
144	idx := strings.IndexByte(noise, '#')
145	for idx >= 0 {
146		if strings.HasPrefix(noise[idx:], "#<span>") {
147			idx += 6
148		}
149		idx += 1
150		if idx+len(ont) >= len(noise) {
151			return false
152		}
153		test := noise[idx : idx+len(ont)]
154		test = strings.ToLower(test)
155		if test == ont {
156			return true
157		}
158		newidx := strings.IndexByte(noise[idx:], '#')
159		if newidx == -1 {
160			return false
161		}
162		idx += newidx
163	}
164	return false
165}
166
167type OldRevision struct {
168	Precis string
169	Noise  string
170}
171
172const (
173	flagIsAcked    = 1
174	flagIsBonked   = 2
175	flagIsSaved    = 4
176	flagIsUntagged = 8
177	flagIsReacted  = 16
178)
179
180func (honk *Honk) IsAcked() bool {
181	return honk.Flags&flagIsAcked != 0
182}
183
184func (honk *Honk) IsBonked() bool {
185	return honk.Flags&flagIsBonked != 0
186}
187
188func (honk *Honk) IsSaved() bool {
189	return honk.Flags&flagIsSaved != 0
190}
191
192func (honk *Honk) IsUntagged() bool {
193	return honk.Flags&flagIsUntagged != 0
194}
195
196func (honk *Honk) IsReacted() bool {
197	return honk.Flags&flagIsReacted != 0
198}
199
200type Donk struct {
201	FileID   int64
202	XID      string
203	Name     string
204	Desc     string
205	URL      string
206	Media    string
207	Local    bool
208	External bool
209}
210
211type Place struct {
212	Name      string
213	Latitude  float64
214	Longitude float64
215	Url       string
216}
217
218type Duration int64
219
220func (d Duration) String() string {
221	s := time.Duration(d).String()
222	if strings.HasSuffix(s, "m0s") {
223		s = s[:len(s)-2]
224	}
225	if strings.HasSuffix(s, "h0m") {
226		s = s[:len(s)-2]
227	}
228	return s
229}
230
231func parseDuration(s string) time.Duration {
232	didx := strings.IndexByte(s, 'd')
233	if didx != -1 {
234		days, _ := strconv.ParseInt(s[:didx], 10, 0)
235		dur, _ := time.ParseDuration(s[didx:])
236		return dur + 24*time.Hour*time.Duration(days)
237	}
238	dur, _ := time.ParseDuration(s)
239	return dur
240}
241
242type Time struct {
243	StartTime time.Time
244	EndTime   time.Time
245	Duration  Duration
246}
247
248type Honker struct {
249	ID     int64
250	UserID int64
251	Name   string
252	XID    string
253	Handle string
254	Flavor string
255	Combos []string
256	Meta   HonkerMeta
257}
258
259type HonkerMeta struct {
260	Notes string
261}
262
263type SomeThing struct {
264	What  int
265	XID   string
266	Owner string
267	Name  string
268}
269
270const (
271	SomeNothing int = iota
272	SomeActor
273	SomeCollection
274)