honk/honk.go

267 lines
4.9 KiB
Go
Raw Permalink Normal View History

2019-04-09 13:59:33 +02:00
//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"html/template"
2019-10-21 08:28:35 +02:00
"strconv"
2019-10-03 06:11:02 +02:00
"strings"
2019-04-09 13:59:33 +02:00
"time"
2020-01-19 09:44:38 +01:00
"humungus.tedunangst.com/r/webs/httpsig"
2019-04-09 13:59:33 +02:00
)
type WhatAbout struct {
ID int64
Name string
Display string
About string
2020-09-26 01:02:26 +02:00
HTAbout template.HTML
2020-09-26 01:09:01 +02:00
Onts []string
Key string
URL string
Options UserOptions
2020-01-19 09:44:38 +01:00
SecKey httpsig.PrivateKey
}
type UserOptions struct {
2023-01-26 22:38:52 +01:00
SkinnyCSS bool `json:",omitempty"`
OmitImages bool `json:",omitempty"`
MentionAll bool `json:",omitempty"`
InlineQuotes bool `json:",omitempty"`
Avatar string `json:",omitempty"`
Banner string `json:",omitempty"`
MapLink string `json:",omitempty"`
Reaction string `json:",omitempty"`
MeCount int64
ChatCount int64
2019-04-09 13:59:33 +02:00
}
type KeyInfo struct {
keyname string
2020-01-19 09:44:38 +01:00
seckey httpsig.PrivateKey
}
2019-10-25 23:31:48 +02:00
const serverUID int64 = -2
const readyLuserOne int64 = 1
2019-10-25 23:31:48 +02:00
2019-04-13 19:58:42 +02:00
type Honk struct {
ID int64
UserID int64
Username string
What string
Honker string
Handle string
2020-08-05 21:33:27 +02:00
Handles string
Oonker string
2019-07-05 19:07:59 +02:00
Oondle string
2019-04-13 19:58:42 +02:00
XID string
RID string
Date time.Time
URL string
Noise string
Precis string
2019-09-11 22:20:22 +02:00
Format string
Convoy string
2019-04-13 19:58:42 +02:00
Audience []string
Public bool
Whofore int64
Replies []*Honk
2019-08-16 21:13:33 +02:00
Flags int64
2019-09-18 21:08:50 +02:00
HTPrecis template.HTML
2019-04-13 19:58:42 +02:00
HTML template.HTML
Style string
2019-07-10 20:36:14 +02:00
Open string
2019-04-13 19:58:42 +02:00
Donks []*Donk
2019-08-25 05:11:53 +02:00
Onts []string
2019-09-28 06:12:50 +02:00
Place *Place
Time *Time
2019-11-27 20:36:29 +01:00
Mentions []Mention
Badonks []Badonk
}
type Badonk struct {
Who string
What string
2019-11-27 20:36:29 +01:00
}
2020-05-13 23:15:29 +02:00
type Chonk struct {
ID int64
UserID int64
XID string
Who string
Target string
Date time.Time
Noise string
Format string
2020-05-15 05:51:42 +02:00
Donks []*Donk
2020-05-13 23:15:29 +02:00
Handle string
HTML template.HTML
}
type Chatter struct {
Target string
Chonks []*Chonk
}
2019-11-27 20:36:29 +01:00
type Mention struct {
Who string
Where string
2019-04-09 13:59:33 +02:00
}
2022-05-31 07:07:27 +02:00
func (mention *Mention) IsPresent(noise string) bool {
nick := strings.TrimLeft(mention.Who, "@")
idx := strings.IndexByte(nick, '@')
if idx != -1 {
nick = nick[:idx]
2022-05-31 06:24:02 +02:00
}
2022-05-31 07:07:27 +02:00
return strings.Contains(noise, ">@"+nick) || strings.Contains(noise, "@<span>"+nick)
2022-05-31 06:24:02 +02:00
}
2023-07-31 02:31:00 +02:00
func OntIsPresent(ont, noise string) bool {
2023-07-31 03:31:53 +02:00
ont = strings.ToLower(ont[1:])
idx := strings.IndexByte(noise, '#')
for idx >= 0 {
if strings.HasPrefix(noise[idx:], "#<span>") {
2023-07-31 03:31:53 +02:00
idx += 6
}
2023-07-31 03:31:53 +02:00
idx += 1
if idx+len(ont) >= len(noise) {
return false
}
test := noise[idx : idx+len(ont)]
test = strings.ToLower(test)
if test == ont {
return true
}
newidx := strings.IndexByte(noise[idx:], '#')
if newidx == -1 {
return false
}
idx += newidx
}
return false
2023-07-31 02:31:00 +02:00
}
type OldRevision struct {
Precis string
Noise string
}
2019-08-16 21:35:12 +02:00
const (
2019-10-30 00:14:41 +01:00
flagIsAcked = 1
flagIsBonked = 2
flagIsSaved = 4
flagIsUntagged = 8
2020-01-24 00:08:15 +01:00
flagIsReacted = 16
2019-08-16 21:35:12 +02:00
)
2019-08-19 06:01:00 +02:00
func (honk *Honk) IsAcked() bool {
return honk.Flags&flagIsAcked != 0
2019-08-16 21:35:12 +02:00
}
2019-08-24 02:43:30 +02:00
func (honk *Honk) IsBonked() bool {
return honk.Flags&flagIsBonked != 0
}
2019-10-11 22:03:17 +02:00
func (honk *Honk) IsSaved() bool {
return honk.Flags&flagIsSaved != 0
}
2019-10-30 00:14:41 +01:00
func (honk *Honk) IsUntagged() bool {
return honk.Flags&flagIsUntagged != 0
}
2020-01-24 00:08:15 +01:00
func (honk *Honk) IsReacted() bool {
return honk.Flags&flagIsReacted != 0
}
2019-04-13 19:58:42 +02:00
type Donk struct {
FileID int64
XID string
Name string
Desc string
URL string
Media string
Local bool
External bool
2019-04-09 13:59:33 +02:00
}
2019-09-28 06:12:50 +02:00
type Place struct {
Name string
Latitude float64
Longitude float64
2019-09-29 22:07:35 +02:00
Url string
2019-09-28 06:12:50 +02:00
}
2019-10-03 06:11:02 +02:00
type Duration int64
func (d Duration) String() string {
s := time.Duration(d).String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}
return s
}
2019-10-21 08:28:35 +02:00
func parseDuration(s string) time.Duration {
didx := strings.IndexByte(s, 'd')
if didx != -1 {
days, _ := strconv.ParseInt(s[:didx], 10, 0)
dur, _ := time.ParseDuration(s[didx:])
return dur + 24*time.Hour*time.Duration(days)
}
dur, _ := time.ParseDuration(s)
return dur
}
type Time struct {
StartTime time.Time
EndTime time.Time
2019-10-03 06:11:02 +02:00
Duration Duration
}
2019-04-13 19:58:42 +02:00
type Honker struct {
ID int64
UserID int64
Name string
XID string
Handle string
2019-04-13 19:58:42 +02:00
Flavor string
Combos []string
Meta HonkerMeta
2019-12-03 00:26:29 +01:00
}
type HonkerMeta struct {
Notes string
2019-04-09 13:59:33 +02:00
}
type SomeThing struct {
What int
XID string
Owner string
Name string
}
const (
SomeNothing int = iota
SomeActor
SomeCollection
)