Merge branch 'Emotes'
This commit is contained in:
commit
54377c65fb
|
@ -255,15 +255,10 @@ func (cl *Client) Host() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *Client) setName(s string) error {
|
func (cl *Client) setName(s string) error {
|
||||||
// Case-insensitive search. Match whole words only (`\b` is word boundary).
|
|
||||||
regex, err := regexp.Compile(fmt.Sprintf(`(?i)\b(%s|@%s)\b`, s, s))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not compile regex: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cl.name = s
|
cl.name = s
|
||||||
cl.regexName = regex
|
if cl.conn != nil {
|
||||||
cl.conn.clientName = s
|
cl.conn.clientName = s
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,7 +269,18 @@ func (cl *Client) setColor(s string) error {
|
||||||
|
|
||||||
func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData {
|
func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData {
|
||||||
data := chatData.Data.(common.DataMessage)
|
data := chatData.Data.(common.DataMessage)
|
||||||
data.Message = cl.regexName.ReplaceAllString(data.Message, `<span class="mention">$1</span>`)
|
words := strings.Split(data.Message, " ")
|
||||||
|
newWords := []string{}
|
||||||
|
|
||||||
|
for _, word := range words {
|
||||||
|
if strings.ToLower(word) == strings.ToLower(cl.name) || strings.ToLower(word) == strings.ToLower("@"+cl.name) {
|
||||||
|
newWords = append(newWords, `<span class="mention">`+word+`</span>`)
|
||||||
|
} else {
|
||||||
|
newWords = append(newWords, word)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data.Message = strings.Join(newWords, " ")
|
||||||
chatData.Data = data
|
chatData.Data = data
|
||||||
return chatData
|
return chatData
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
|
)
|
||||||
|
|
||||||
func TestClient_addSpoilerTag(t *testing.T) {
|
func TestClient_addSpoilerTag(t *testing.T) {
|
||||||
data := [][]string{
|
data := [][]string{
|
||||||
|
@ -21,3 +25,31 @@ func TestClient_addSpoilerTag(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name highlighting should not interfere with emotes
|
||||||
|
func TestClient_emoteHighlight(t *testing.T) {
|
||||||
|
data := [][]string{
|
||||||
|
{"zorchenhimer", `<span class="mention">zorchenhimer</span>`},
|
||||||
|
{"@zorchenhimer", `<span class="mention">@zorchenhimer</span>`},
|
||||||
|
{"Zorchenhimer", `<span class="mention">Zorchenhimer</span>`},
|
||||||
|
{"@Zorchenhimer", `<span class="mention">@Zorchenhimer</span>`},
|
||||||
|
{"hello zorchenhimer", `hello <span class="mention">zorchenhimer</span>`},
|
||||||
|
{"hello zorchenhimer ass", `hello <span class="mention">zorchenhimer</span> ass`},
|
||||||
|
{`<img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`, `<img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`},
|
||||||
|
{`zorchenhimer <img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`, `<span class="mention">zorchenhimer</span> <img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`},
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := NewClient(nil, nil, "Zorchenhimer", "#9547ff")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Client init error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range data {
|
||||||
|
chatData := client.replaceColorizedName(common.NewChatMessage(client.name, client.color, d[0], common.CmdlUser, common.MsgChat))
|
||||||
|
if chatData.Data.(common.DataMessage).Message != d[1] {
|
||||||
|
t.Errorf("\nExpected:\n\t%s\nReceived\n\t%s", d[1], chatData.Data.(common.DataMessage).Message)
|
||||||
|
} else {
|
||||||
|
t.Logf("Passed %s", d[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -438,17 +438,8 @@ var commands = &CommandControl{
|
||||||
common.CNReloadEmotes.String(): Command{
|
common.CNReloadEmotes.String(): Command{
|
||||||
HelpText: "Reload the emotes on the server.",
|
HelpText: "Reload the emotes on the server.",
|
||||||
Function: func(cl *Client, args []string) (string, error) {
|
Function: func(cl *Client, args []string) (string, error) {
|
||||||
cl.SendServerMessage("Reloading emotes")
|
go commandReloadEmotes(cl)
|
||||||
num, err := common.LoadEmotes()
|
return "Reloading emotes...", nil
|
||||||
if err != nil {
|
|
||||||
common.LogErrorf("Unbale to reload emotes: %s\n", err)
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes))
|
|
||||||
cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes")
|
|
||||||
common.LogInfof("Loaded %d emotes\n", num)
|
|
||||||
return fmt.Sprintf("Emotes loaded: %d", num), nil
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -525,7 +516,7 @@ var commands = &CommandControl{
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
// Pretty sure this breaks on partial downloads (eg, one good channel and one non-existent)
|
// Pretty sure this breaks on partial downloads (eg, one good channel and one non-existent)
|
||||||
_, err := GetEmotes(args)
|
err := getEmotes(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cl.SendChatData(common.NewChatMessage("", "",
|
cl.SendChatData(common.NewChatMessage("", "",
|
||||||
err.Error(),
|
err.Error(),
|
||||||
|
@ -533,8 +524,13 @@ var commands = &CommandControl{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the emotes were able to be downloaded, add the channels to settings
|
||||||
|
settingsMtx.Lock()
|
||||||
|
settings.ApprovedEmotes = append(settings.ApprovedEmotes, args...)
|
||||||
|
settingsMtx.Unlock()
|
||||||
|
|
||||||
// reload emotes now that new ones were added
|
// reload emotes now that new ones were added
|
||||||
_, err = common.LoadEmotes()
|
err = loadEmotes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cl.SendChatData(common.NewChatMessage("", "",
|
cl.SendChatData(common.NewChatMessage("", "",
|
||||||
err.Error(),
|
err.Error(),
|
||||||
|
@ -543,6 +539,8 @@ var commands = &CommandControl{
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.belongsTo.AddModNotice(cl.name + " has added emotes from the following channels: " + strings.Join(args, ", "))
|
cl.belongsTo.AddModNotice(cl.name + " has added emotes from the following channels: " + strings.Join(args, ", "))
|
||||||
|
|
||||||
|
commandReloadEmotes(cl)
|
||||||
}()
|
}()
|
||||||
return "Emote download initiated for the following channels: " + strings.Join(args, ", "), nil
|
return "Emote download initiated for the following channels: " + strings.Join(args, ", "), nil
|
||||||
},
|
},
|
||||||
|
@ -618,3 +616,24 @@ func getHelp(lvl common.CommandLevel) map[string]string {
|
||||||
}
|
}
|
||||||
return helptext
|
return helptext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commandReloadEmotes(cl *Client) {
|
||||||
|
cl.SendServerMessage("Reloading emotes")
|
||||||
|
err := loadEmotes()
|
||||||
|
if err != nil {
|
||||||
|
common.LogErrorf("Unbale to reload emotes: %s\n", err)
|
||||||
|
//return "", err
|
||||||
|
|
||||||
|
cl.SendChatData(common.NewChatMessage("", "",
|
||||||
|
err.Error(),
|
||||||
|
common.CmdlUser, common.MsgCommandResponse))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes))
|
||||||
|
cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes")
|
||||||
|
|
||||||
|
num := len(common.Emotes)
|
||||||
|
common.LogInfof("Loaded %d emotes\n", num)
|
||||||
|
cl.belongsTo.AddModNotice(fmt.Sprintf("%s reloaded %d emotes.", cl.name, num))
|
||||||
|
}
|
||||||
|
|
|
@ -36,11 +36,11 @@ func newChatRoom() (*ChatRoom, error) {
|
||||||
clients: []*Client{},
|
clients: []*Client{},
|
||||||
}
|
}
|
||||||
|
|
||||||
num, err := common.LoadEmotes()
|
err := loadEmotes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error loading emotes: %s", err)
|
return nil, fmt.Errorf("error loading emotes: %s", err)
|
||||||
}
|
}
|
||||||
common.LogInfof("Loaded %d emotes\n", num)
|
common.LogInfof("Loaded %d emotes\n", len(common.Emotes))
|
||||||
|
|
||||||
//the "heartbeat" for broadcasting messages
|
//the "heartbeat" for broadcasting messages
|
||||||
go cr.Broadcast()
|
go cr.Broadcast()
|
||||||
|
|
|
@ -22,6 +22,7 @@ var (
|
||||||
CNUsers ChatCommandNames = []string{"users"}
|
CNUsers ChatCommandNames = []string{"users"}
|
||||||
CNNick ChatCommandNames = []string{"nick", "name"}
|
CNNick ChatCommandNames = []string{"nick", "name"}
|
||||||
CNStats ChatCommandNames = []string{"stats"}
|
CNStats ChatCommandNames = []string{"stats"}
|
||||||
|
CNPin ChatCommandNames = []string{"pin", "password"}
|
||||||
// Mod Commands
|
// Mod Commands
|
||||||
CNSv ChatCommandNames = []string{"sv"}
|
CNSv ChatCommandNames = []string{"sv"}
|
||||||
CNPlaying ChatCommandNames = []string{"playing"}
|
CNPlaying ChatCommandNames = []string{"playing"}
|
||||||
|
@ -30,7 +31,6 @@ var (
|
||||||
CNBan ChatCommandNames = []string{"ban"}
|
CNBan ChatCommandNames = []string{"ban"}
|
||||||
CNUnban ChatCommandNames = []string{"unban"}
|
CNUnban ChatCommandNames = []string{"unban"}
|
||||||
CNPurge ChatCommandNames = []string{"purge"}
|
CNPurge ChatCommandNames = []string{"purge"}
|
||||||
CNPin ChatCommandNames = []string{"pin", "password"}
|
|
||||||
// Admin Commands
|
// Admin Commands
|
||||||
CNMod ChatCommandNames = []string{"mod"}
|
CNMod ChatCommandNames = []string{"mod"}
|
||||||
CNReloadPlayer ChatCommandNames = []string{"reloadplayer"}
|
CNReloadPlayer ChatCommandNames = []string{"reloadplayer"}
|
||||||
|
@ -52,9 +52,9 @@ var ChatCommands = []ChatCommandNames{
|
||||||
CNUsers,
|
CNUsers,
|
||||||
CNNick,
|
CNNick,
|
||||||
CNStats,
|
CNStats,
|
||||||
|
CNPin,
|
||||||
|
|
||||||
// Mod
|
// Mod
|
||||||
CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin,
|
|
||||||
CNSv,
|
CNSv,
|
||||||
CNPlaying,
|
CNPlaying,
|
||||||
CNUnmod,
|
CNUnmod,
|
||||||
|
@ -64,13 +64,13 @@ var ChatCommands = []ChatCommandNames{
|
||||||
CNPurge,
|
CNPurge,
|
||||||
|
|
||||||
// Admin
|
// Admin
|
||||||
CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, CNRoomAccess, CNIP,
|
|
||||||
CNMod,
|
CNMod,
|
||||||
CNReloadPlayer,
|
CNReloadPlayer,
|
||||||
CNReloadEmotes,
|
CNReloadEmotes,
|
||||||
CNModpass,
|
CNModpass,
|
||||||
CNIP,
|
CNIP,
|
||||||
CNAddEmotes,
|
CNAddEmotes,
|
||||||
|
CNRoomAccess,
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFullChatCommand(c string) string {
|
func GetFullChatCommand(c string) string {
|
||||||
|
|
|
@ -3,13 +3,49 @@ package common
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Emotes map[string]string
|
type EmotesMap map[string]string
|
||||||
|
|
||||||
|
var Emotes EmotesMap
|
||||||
|
|
||||||
|
var reStripStatic = regexp.MustCompile(`^(\\|/)?static`)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Emotes = NewEmotesMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEmotesMap() EmotesMap {
|
||||||
|
return map[string]string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (em EmotesMap) Add(fullpath string) EmotesMap {
|
||||||
|
fullpath = reStripStatic.ReplaceAllLiteralString(fullpath, "")
|
||||||
|
|
||||||
|
base := filepath.Base(fullpath)
|
||||||
|
code := base[0 : len(base)-len(filepath.Ext(base))]
|
||||||
|
|
||||||
|
_, exists := em[code]
|
||||||
|
|
||||||
|
num := 0
|
||||||
|
for exists {
|
||||||
|
num += 1
|
||||||
|
_, exists = em[fmt.Sprintf("%s-%d", code, num)]
|
||||||
|
}
|
||||||
|
|
||||||
|
if num > 0 {
|
||||||
|
code = fmt.Sprintf("%s-%d", code, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
em[code] = fullpath
|
||||||
|
//fmt.Printf("Added emote %s at path %q\n", code, fullpath)
|
||||||
|
return em
|
||||||
|
}
|
||||||
|
|
||||||
func EmoteToHtml(file, title string) string {
|
func EmoteToHtml(file, title string) string {
|
||||||
return fmt.Sprintf(`<img src="/emotes/%s" height="28px" title="%s" />`, file, title)
|
return fmt.Sprintf(`<img src="%s" height="28px" title="%s" />`, file, title)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseEmotesArray(words []string) []string {
|
func ParseEmotesArray(words []string) []string {
|
||||||
|
@ -36,31 +72,3 @@ func ParseEmotes(msg string) string {
|
||||||
words := ParseEmotesArray(strings.Split(msg, " "))
|
words := ParseEmotesArray(strings.Split(msg, " "))
|
||||||
return strings.Join(words, " ")
|
return strings.Join(words, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadEmotes() (int, error) {
|
|
||||||
newEmotes := map[string]string{}
|
|
||||||
|
|
||||||
emotePNGs, err := filepath.Glob("./static/emotes/*.png")
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to glob emote directory: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
emoteGIFs, err := filepath.Glob("./static/emotes/*.gif")
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to glob emote directory: %s\n", err)
|
|
||||||
}
|
|
||||||
globbed_files := []string(emotePNGs)
|
|
||||||
globbed_files = append(globbed_files, emoteGIFs...)
|
|
||||||
|
|
||||||
LogInfoln("Loading emotes...")
|
|
||||||
emInfo := []string{}
|
|
||||||
for _, file := range globbed_files {
|
|
||||||
file = filepath.Base(file)
|
|
||||||
key := file[0 : len(file)-4]
|
|
||||||
newEmotes[key] = file
|
|
||||||
emInfo = append(emInfo, key)
|
|
||||||
}
|
|
||||||
Emotes = newEmotes
|
|
||||||
LogInfoln(strings.Join(emInfo, " "))
|
|
||||||
return len(Emotes), nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -27,9 +27,9 @@ var data_good = map[string]string{
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
Emotes = map[string]string{
|
Emotes = map[string]string{
|
||||||
"one": "one.png",
|
"one": "/emotes/one.png",
|
||||||
"two": "two.png",
|
"two": "/emotes/two.png",
|
||||||
"three": "three.gif",
|
"three": "/emotes/three.gif",
|
||||||
}
|
}
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
328
emotes.go
328
emotes.go
|
@ -4,164 +4,236 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/zorchenhimer/MovieNight/common"
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type twitchChannel struct {
|
const emoteDir = "./static/emotes/"
|
||||||
ChannelName string `json:"channel_name"`
|
|
||||||
DisplayName string `json:"display_name"`
|
type TwitchUser struct {
|
||||||
ChannelId string `json:"channel_id"`
|
ID string
|
||||||
BroadcasterType string `json:"broadcaster_type"`
|
Login string
|
||||||
Plans map[string]string `json:"plans"`
|
|
||||||
Emotes []struct {
|
|
||||||
Code string `json:"code"`
|
|
||||||
Set int `json:"emoticon_set"`
|
|
||||||
Id int `json:"id"`
|
|
||||||
} `json:"emotes"`
|
|
||||||
BaseSetId string `json:"base_set_id"`
|
|
||||||
GeneratedAt string `json:"generated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used in settings
|
type EmoteInfo struct {
|
||||||
type EmoteSet struct {
|
ID int
|
||||||
Channel string // channel name
|
Code string
|
||||||
Prefix string // emote prefix
|
|
||||||
Found bool `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscriberJson string = `subscribers.json`
|
func loadEmotes() error {
|
||||||
|
//fmt.Println(processEmoteDir(emoteDir))
|
||||||
|
newEmotes, err := processEmoteDir(emoteDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Download a single channel's emote set
|
common.Emotes = newEmotes
|
||||||
func (tc *twitchChannel) downloadEmotes() (*EmoteSet, error) {
|
|
||||||
es := &EmoteSet{Channel: strings.ToLower(tc.ChannelName)}
|
|
||||||
for _, emote := range tc.Emotes {
|
|
||||||
url := fmt.Sprintf(`https://static-cdn.jtvnw.net/emoticons/v1/%d/1.0`, emote.Id)
|
|
||||||
png := `static/emotes/` + emote.Code + `.png`
|
|
||||||
|
|
||||||
if len(es.Prefix) == 0 {
|
return nil
|
||||||
// For each letter
|
}
|
||||||
for i := 0; i < len(emote.Code); i++ {
|
|
||||||
// Find the first capital
|
func processEmoteDir(path string) (common.EmotesMap, error) {
|
||||||
b := emote.Code[i]
|
dirInfo, err := ioutil.ReadDir(path)
|
||||||
if b >= 'A' && b <= 'Z' {
|
if err != nil {
|
||||||
es.Prefix = emote.Code[0 : i-1]
|
return nil, errors.Wrap(err, "could not open emoteDir:")
|
||||||
common.LogDebugf("Found prefix for channel %q: %q (%q)\n", es.Channel, es.Prefix, emote)
|
}
|
||||||
break
|
|
||||||
|
subDirs := []string{}
|
||||||
|
|
||||||
|
for _, item := range dirInfo {
|
||||||
|
// Get first level subdirs (eg, "twitch", "discord", etc)
|
||||||
|
if item.IsDir() {
|
||||||
|
subDirs = append(subDirs, item.Name())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
em := common.NewEmotesMap()
|
||||||
|
// Find top level emotes
|
||||||
|
em, err = findEmotes(path, em)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "could not findEmotes() in top level directory:")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get second level subdirs (eg, "twitch", "zorchenhimer", etc)
|
||||||
|
for _, dir := range subDirs {
|
||||||
|
subd, err := ioutil.ReadDir(filepath.Join(path, dir))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error reading dir %q: %v\n", subd, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, d := range subd {
|
||||||
|
if d.IsDir() {
|
||||||
|
//emotes = append(emotes, findEmotes(filepath.Join(path, dir, d.Name()))...)
|
||||||
|
p := filepath.Join(path, dir, d.Name())
|
||||||
|
em, err = findEmotes(p, em)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error finding emotes in %q: %v\n", p, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("processEmoteDir: %d\n", len(em))
|
||||||
|
return em, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) {
|
||||||
|
//em := NewEmotesMap()
|
||||||
|
|
||||||
|
fmt.Printf("finding emotes in %q\n", dir)
|
||||||
|
emotePNGs, err := filepath.Glob(filepath.Join(dir, "*.png"))
|
||||||
|
if err != nil {
|
||||||
|
return em, fmt.Errorf("unable to glob emote directory: %s\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%d emotePNGs\n", len(emotePNGs))
|
||||||
|
|
||||||
|
emoteGIFs, err := filepath.Glob(filepath.Join(dir, "*.gif"))
|
||||||
|
if err != nil {
|
||||||
|
return em, errors.Wrap(err, "unable to glob emote directory:")
|
||||||
|
}
|
||||||
|
fmt.Printf("%d emoteGIFs\n", len(emoteGIFs))
|
||||||
|
|
||||||
|
for _, file := range emotePNGs {
|
||||||
|
em = em.Add(file)
|
||||||
|
//emotes = append(emotes, common.Emote{FullPath: dir, Code: file})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range emoteGIFs {
|
||||||
|
em = em.Add(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
return em, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEmotes(names []string) error {
|
||||||
|
users := getUserIDs(names)
|
||||||
|
users = append(users, TwitchUser{ID: "0", Login: "twitch"})
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
emotes, cheers, err := getChannelEmotes(user.ID)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "could not get emote data for \"%s\"", user.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
emoteUserDir := filepath.Join(emoteDir, "twitch", user.Login)
|
||||||
|
if _, err := os.Stat(emoteUserDir); os.IsNotExist(err) {
|
||||||
|
os.MkdirAll(emoteUserDir, os.ModePerm)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, emote := range emotes {
|
||||||
|
if !strings.ContainsAny(emote.Code, `:;\[]|?&`) {
|
||||||
|
filePath := filepath.Join(emoteUserDir, emote.Code+".png")
|
||||||
|
file, err := os.Create(filePath)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
return errors.Wrapf(err, "could not create emote file in path \"%s\":", filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = downloadEmote(emote.ID, file)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "could not download emote %s:", emote.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
for amount, sizes := range cheers {
|
||||||
if err != nil {
|
name := fmt.Sprintf("%sCheer%s.gif", user.Login, amount)
|
||||||
return nil, err
|
filePath := filepath.Join(emoteUserDir, name)
|
||||||
}
|
file, err := os.Create(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "could not create emote file in path \"%s\":", filePath)
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Create(png)
|
err = downloadCheerEmote(sizes["4"], file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return errors.Wrapf(err, "could not download emote %s:", name)
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(f, resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return es, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetEmotes(names []string) ([]*EmoteSet, error) {
|
|
||||||
// Do this up-front
|
|
||||||
for i := 0; i < len(names); i++ {
|
|
||||||
names[i] = strings.ToLower(names[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
channels, err := findChannels(names)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error reading %q: %v", subscriberJson, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
emoteSets := []*EmoteSet{}
|
|
||||||
for _, c := range channels {
|
|
||||||
es, err := c.downloadEmotes()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error downloading emotes: %v", err)
|
|
||||||
}
|
|
||||||
emoteSets = append(emoteSets, es)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, es := range emoteSets {
|
|
||||||
found := false
|
|
||||||
for _, name := range names {
|
|
||||||
if es.Channel == name {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
|
||||||
es.Found = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return emoteSets, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findChannels(names []string) ([]twitchChannel, error) {
|
func getUserIDs(names []string) []TwitchUser {
|
||||||
file, err := os.Open(subscriberJson)
|
logins := strings.Join(names, "&login=")
|
||||||
|
request, err := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", logins), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
log.Fatalln("Error generating new request:", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
request.Header.Set("Client-ID", settings.TwitchClientID)
|
||||||
|
|
||||||
data := []twitchChannel{}
|
client := http.Client{}
|
||||||
dec := json.NewDecoder(file)
|
resp, err := client.Do(request)
|
||||||
|
|
||||||
// Open bracket
|
|
||||||
_, err = dec.Token()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
log.Fatalln("Error sending request:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
done := false
|
decoder := json.NewDecoder(resp.Body)
|
||||||
for dec.More() && !done {
|
type userResponse struct {
|
||||||
// opening bracket of channel
|
Data []TwitchUser
|
||||||
_, err = dec.Token()
|
}
|
||||||
if err != nil {
|
var data userResponse
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode the channel stuff
|
err = decoder.Decode(&data)
|
||||||
var c twitchChannel
|
if err != nil {
|
||||||
err = dec.Decode(&c)
|
log.Fatalln("Error decoding data:", err)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is this a channel we are looking for?
|
|
||||||
found := false
|
|
||||||
for _, search := range names {
|
|
||||||
if strings.ToLower(c.ChannelName) == search {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Yes it is. Add it to the data
|
|
||||||
if found {
|
|
||||||
data = append(data, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for completion. Don't bother parsing the rest of
|
|
||||||
// the json file if we've already found everything that we're
|
|
||||||
// looking for.
|
|
||||||
if len(data) == len(names) {
|
|
||||||
done = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func getChannelEmotes(ID string) ([]EmoteInfo, map[string]map[string]string, error) {
|
||||||
|
resp, err := http.Get("https://api.twitchemotes.com/api/v4/channels/" + ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, errors.Wrap(err, "could not get emotes")
|
||||||
|
}
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
|
||||||
|
type EmoteResponse struct {
|
||||||
|
Emotes []EmoteInfo
|
||||||
|
Cheermotes map[string]map[string]string
|
||||||
|
}
|
||||||
|
var data EmoteResponse
|
||||||
|
|
||||||
|
err = decoder.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, errors.Wrap(err, "could not decode emotes")
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.Emotes, data.Cheermotes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadEmote(ID int, file *os.File) error {
|
||||||
|
resp, err := http.Get(fmt.Sprintf("https://static-cdn.jtvnw.net/emoticons/v1/%d/3.0", ID))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Errorf("could not download emote file %s: %v", file.Name(), err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(file, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Errorf("could not save emote: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadCheerEmote(url string, file *os.File) error {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Errorf("could not download cheer file %s: %v", file.Name(), err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(file, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Errorf("could not save cheer: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
3
go.mod
3
go.mod
|
@ -7,14 +7,13 @@ require (
|
||||||
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
|
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
|
||||||
github.com/chromedp/cdproto v0.0.0-20190412020601-c4267f5c421a // indirect
|
github.com/chromedp/cdproto v0.0.0-20190412020601-c4267f5c421a // indirect
|
||||||
github.com/containerd/continuity v0.0.0-20181203112020-004b46473808 // indirect
|
github.com/containerd/continuity v0.0.0-20181203112020-004b46473808 // indirect
|
||||||
github.com/dennwc/dom v0.3.0
|
|
||||||
github.com/gorilla/sessions v1.1.3
|
github.com/gorilla/sessions v1.1.3
|
||||||
github.com/gorilla/websocket v1.4.0
|
github.com/gorilla/websocket v1.4.0
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||||
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 // indirect
|
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 // indirect
|
||||||
github.com/nareix/joy4 v0.0.0-20181022032202-3ddbc8f9d431
|
github.com/nareix/joy4 v0.0.0-20181022032202-3ddbc8f9d431
|
||||||
github.com/ory/dockertest v3.3.4+incompatible // indirect
|
github.com/ory/dockertest v3.3.4+incompatible // indirect
|
||||||
github.com/pkg/errors v0.8.1 // indirect
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/sirupsen/logrus v1.4.1 // indirect
|
github.com/sirupsen/logrus v1.4.1 // indirect
|
||||||
github.com/stretchr/objx v0.2.0 // indirect
|
github.com/stretchr/objx v0.2.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect
|
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect
|
||||||
|
|
1
go.sum
1
go.sum
|
@ -65,6 +65,7 @@ github.com/ory/dockertest v3.3.2+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnh
|
||||||
github.com/ory/dockertest v3.3.4+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
|
github.com/ory/dockertest v3.3.4+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
|
||||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -78,8 +79,7 @@ func wsImages(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func wsEmotes(w http.ResponseWriter, r *http.Request) {
|
func wsEmotes(w http.ResponseWriter, r *http.Request) {
|
||||||
emotefile := filepath.Base(r.URL.Path)
|
http.ServeFile(w, r, path.Join("./static/", r.URL.Path))
|
||||||
http.ServeFile(w, r, "./static/emotes/"+emotefile)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handling the websocket
|
// Handling the websocket
|
||||||
|
|
21
main.go
21
main.go
|
@ -14,9 +14,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
addr string
|
pullEmotes bool
|
||||||
sKey string
|
addr string
|
||||||
stats = newStreamStats()
|
sKey string
|
||||||
|
stats = newStreamStats()
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupSettings() error {
|
func setupSettings() error {
|
||||||
|
@ -42,6 +43,7 @@ func setupSettings() error {
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&addr, "l", "", "host:port of the MovieNight")
|
flag.StringVar(&addr, "l", "", "host:port of the MovieNight")
|
||||||
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
|
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
|
||||||
|
flag.BoolVar(&pullEmotes, "e", false, "Pull emotes")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
format.RegisterAll()
|
format.RegisterAll()
|
||||||
|
@ -51,6 +53,16 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pullEmotes {
|
||||||
|
common.LogInfoln("Pulling emotes")
|
||||||
|
err := getEmotes(settings.ApprovedEmotes)
|
||||||
|
if err != nil {
|
||||||
|
common.LogErrorf("Error downloading emotes: %+v\n", err)
|
||||||
|
common.LogErrorf("Error downloading emotes: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := common.InitTemplates(); err != nil {
|
if err := common.InitTemplates(); err != nil {
|
||||||
common.LogErrorln(err)
|
common.LogErrorln(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -61,7 +73,8 @@ func main() {
|
||||||
|
|
||||||
// Load emotes before starting server.
|
// Load emotes before starting server.
|
||||||
var err error
|
var err error
|
||||||
if chat, err = newChatRoom(); err != nil {
|
chat, err = newChatRoom()
|
||||||
|
if err != nil {
|
||||||
common.LogErrorln(err)
|
common.LogErrorln(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,8 +30,9 @@ type Settings struct {
|
||||||
AdminPassword string
|
AdminPassword string
|
||||||
StreamKey string
|
StreamKey string
|
||||||
ListenAddress string
|
ListenAddress string
|
||||||
ApprovedEmotes []EmoteSet // list of channels that have been approved for emote use. Global emotes are always "approved".
|
ApprovedEmotes []string // list of channels that have been approved for emote use. Global emotes are always "approved".
|
||||||
SessionKey string // key for session data
|
TwitchClientID string // client id from twitch developers portal
|
||||||
|
SessionKey string // key for session data
|
||||||
Bans []BanInfo
|
Bans []BanInfo
|
||||||
LogLevel common.LogLevel
|
LogLevel common.LogLevel
|
||||||
LogFile string
|
LogFile string
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"MaxMessageCount": 300,
|
"MaxMessageCount": 300,
|
||||||
"TitleLength": 50,
|
"TitleLength": 50,
|
||||||
"AdminPassword": "",
|
"AdminPassword": "",
|
||||||
"Bans": [],
|
"Bans": [],
|
||||||
"StreamKey": "ALongStreamKey",
|
"StreamKey": "ALongStreamKey",
|
||||||
|
|
|
@ -3,6 +3,15 @@
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
|
// Map multiple JavaScript environments to a single common API,
|
||||||
|
// preferring web standards over Node.js API.
|
||||||
|
//
|
||||||
|
// Environments considered:
|
||||||
|
// - Browsers
|
||||||
|
// - Node.js
|
||||||
|
// - Electron
|
||||||
|
// - Parcel
|
||||||
|
|
||||||
if (typeof global !== "undefined") {
|
if (typeof global !== "undefined") {
|
||||||
// global already exists
|
// global already exists
|
||||||
} else if (typeof window !== "undefined") {
|
} else if (typeof window !== "undefined") {
|
||||||
|
@ -13,30 +22,15 @@
|
||||||
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map web browser API and Node.js API to a single common API (preferring web standards over Node.js API).
|
if (!global.require && typeof require !== "undefined") {
|
||||||
const isNodeJS = global.process && global.process.title === "node";
|
|
||||||
if (isNodeJS) {
|
|
||||||
global.require = require;
|
global.require = require;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!global.fs && global.require) {
|
||||||
global.fs = require("fs");
|
global.fs = require("fs");
|
||||||
|
}
|
||||||
|
|
||||||
const nodeCrypto = require("crypto");
|
if (!global.fs) {
|
||||||
global.crypto = {
|
|
||||||
getRandomValues(b) {
|
|
||||||
nodeCrypto.randomFillSync(b);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
global.performance = {
|
|
||||||
now() {
|
|
||||||
const [sec, nsec] = process.hrtime();
|
|
||||||
return sec * 1000 + nsec / 1000000;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const util = require("util");
|
|
||||||
global.TextEncoder = util.TextEncoder;
|
|
||||||
global.TextDecoder = util.TextDecoder;
|
|
||||||
} else {
|
|
||||||
let outputBuf = "";
|
let outputBuf = "";
|
||||||
global.fs = {
|
global.fs = {
|
||||||
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
||||||
|
@ -72,6 +66,34 @@
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!global.crypto) {
|
||||||
|
const nodeCrypto = require("crypto");
|
||||||
|
global.crypto = {
|
||||||
|
getRandomValues(b) {
|
||||||
|
nodeCrypto.randomFillSync(b);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!global.performance) {
|
||||||
|
global.performance = {
|
||||||
|
now() {
|
||||||
|
const [sec, nsec] = process.hrtime();
|
||||||
|
return sec * 1000 + nsec / 1000000;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!global.TextEncoder) {
|
||||||
|
global.TextEncoder = require("util").TextEncoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!global.TextDecoder) {
|
||||||
|
global.TextDecoder = require("util").TextDecoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of polyfills for common API.
|
||||||
|
|
||||||
const encoder = new TextEncoder("utf-8");
|
const encoder = new TextEncoder("utf-8");
|
||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
|
||||||
|
@ -243,7 +265,15 @@
|
||||||
const id = this._nextCallbackTimeoutID;
|
const id = this._nextCallbackTimeoutID;
|
||||||
this._nextCallbackTimeoutID++;
|
this._nextCallbackTimeoutID++;
|
||||||
this._scheduledTimeouts.set(id, setTimeout(
|
this._scheduledTimeouts.set(id, setTimeout(
|
||||||
() => { this._resume(); },
|
() => {
|
||||||
|
this._resume();
|
||||||
|
while (this._scheduledTimeouts.has(id)) {
|
||||||
|
// for some reason Go failed to register the timeout event, log and try again
|
||||||
|
// (temporary workaround for https://github.com/golang/go/issues/28975)
|
||||||
|
console.warn("scheduleTimeoutEvent: missed timeout event");
|
||||||
|
this._resume();
|
||||||
|
}
|
||||||
|
},
|
||||||
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
||||||
));
|
));
|
||||||
mem().setInt32(sp + 16, id, true);
|
mem().setInt32(sp + 16, id, true);
|
||||||
|
@ -357,6 +387,34 @@
|
||||||
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
|
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
||||||
|
"syscall/js.copyBytesToGo": (sp) => {
|
||||||
|
const dst = loadSlice(sp + 8);
|
||||||
|
const src = loadValue(sp + 32);
|
||||||
|
if (!(src instanceof Uint8Array)) {
|
||||||
|
mem().setUint8(sp + 48, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const toCopy = src.subarray(0, dst.length);
|
||||||
|
dst.set(toCopy);
|
||||||
|
setInt64(sp + 40, toCopy.length);
|
||||||
|
mem().setUint8(sp + 48, 1);
|
||||||
|
},
|
||||||
|
|
||||||
|
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
||||||
|
"syscall/js.copyBytesToJS": (sp) => {
|
||||||
|
const dst = loadValue(sp + 8);
|
||||||
|
const src = loadSlice(sp + 16);
|
||||||
|
if (!(dst instanceof Uint8Array)) {
|
||||||
|
mem().setUint8(sp + 48, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const toCopy = src.subarray(0, dst.length);
|
||||||
|
dst.set(toCopy);
|
||||||
|
setInt64(sp + 40, toCopy.length);
|
||||||
|
mem().setUint8(sp + 48, 1);
|
||||||
|
},
|
||||||
|
|
||||||
"debug": (value) => {
|
"debug": (value) => {
|
||||||
console.log(value);
|
console.log(value);
|
||||||
},
|
},
|
||||||
|
@ -373,7 +431,6 @@
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
global,
|
global,
|
||||||
this._inst.exports.mem,
|
|
||||||
this,
|
this,
|
||||||
];
|
];
|
||||||
this._refs = new Map();
|
this._refs = new Map();
|
||||||
|
@ -385,9 +442,13 @@
|
||||||
let offset = 4096;
|
let offset = 4096;
|
||||||
|
|
||||||
const strPtr = (str) => {
|
const strPtr = (str) => {
|
||||||
let ptr = offset;
|
const ptr = offset;
|
||||||
new Uint8Array(mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0"));
|
const bytes = encoder.encode(str + "\0");
|
||||||
offset += str.length + (8 - (str.length % 8));
|
new Uint8Array(mem.buffer, offset, bytes.length).set(bytes);
|
||||||
|
offset += bytes.length;
|
||||||
|
if (offset % 8 !== 0) {
|
||||||
|
offset += 8 - (offset % 8);
|
||||||
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -439,9 +500,15 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNodeJS) {
|
if (
|
||||||
|
global.require &&
|
||||||
|
global.require.main === module &&
|
||||||
|
global.process &&
|
||||||
|
global.process.versions &&
|
||||||
|
!global.process.versions.electron
|
||||||
|
) {
|
||||||
if (process.argv.length < 3) {
|
if (process.argv.length < 3) {
|
||||||
process.stderr.write("usage: go_js_wasm_exec [wasm binary] [arguments]\n");
|
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,7 +526,8 @@
|
||||||
});
|
});
|
||||||
return go.run(result.instance);
|
return go.run(result.instance);
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
throw err;
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
71
wasm/main.go
71
wasm/main.go
|
@ -9,7 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dennwc/dom/js"
|
"syscall/js"
|
||||||
|
|
||||||
"github.com/zorchenhimer/MovieNight/common"
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,21 +18,22 @@ var (
|
||||||
timestamp bool
|
timestamp bool
|
||||||
color string
|
color string
|
||||||
auth common.CommandLevel
|
auth common.CommandLevel
|
||||||
|
global js.Value
|
||||||
)
|
)
|
||||||
|
|
||||||
func getElement(s string) js.Value {
|
func getElement(s string) js.Value {
|
||||||
return js.Get("document").Call("getElementById", s)
|
return global.Get("document").Call("getElementById", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func join(v []js.Value) {
|
func join(v []js.Value) {
|
||||||
color := js.Call("getCookie", "color").String()
|
color := global.Call("getCookie", "color").String()
|
||||||
if color == "" {
|
if color == "" {
|
||||||
// If a color is not set, do a random color
|
// If a color is not set, do a random color
|
||||||
color = common.RandomColor()
|
color = common.RandomColor()
|
||||||
} else if !common.IsValidColor(color) {
|
} else if !common.IsValidColor(color) {
|
||||||
// Don't show the user the error, just clear the cookie
|
// Don't show the user the error, just clear the cookie
|
||||||
common.LogInfof("%#v is not a valid color, clearing cookie", color)
|
common.LogInfof("%#v is not a valid color, clearing cookie", color)
|
||||||
js.Call("deleteCookie", "color")
|
global.Call("deleteCookie", "color")
|
||||||
}
|
}
|
||||||
|
|
||||||
joinData, err := json.Marshal(common.JoinData{
|
joinData, err := json.Marshal(common.JoinData{
|
||||||
|
@ -51,7 +53,7 @@ func join(v []js.Value) {
|
||||||
common.LogErrorf("Could not marshal data: %v", err)
|
common.LogErrorf("Could not marshal data: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
js.Call("websocketSend", string(data))
|
global.Call("websocketSend", string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func recieve(v []js.Value) {
|
func recieve(v []js.Value) {
|
||||||
|
@ -63,7 +65,7 @@ func recieve(v []js.Value) {
|
||||||
chatJSON, err := common.DecodeData(v[0].String())
|
chatJSON, err := common.DecodeData(v[0].String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error decoding data: %s\n", err)
|
fmt.Printf("Error decoding data: %s\n", err)
|
||||||
js.Call("appendMessages", fmt.Sprintf("<div>%v</div>", v))
|
global.Call("appendMessages", fmt.Sprintf("<div>%v</div>", v))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +88,7 @@ func recieve(v []js.Value) {
|
||||||
auth = h.Data.(common.CommandLevel)
|
auth = h.Data.(common.CommandLevel)
|
||||||
case common.CdColor:
|
case common.CdColor:
|
||||||
color = h.Data.(string)
|
color = h.Data.(string)
|
||||||
js.Get("document").Set("cookie", fmt.Sprintf("color=%s; expires=Fri, 31 Dec 9999 23:59:59 GMT", color))
|
global.Get("document").Set("cookie", fmt.Sprintf("color=%s; expires=Fri, 31 Dec 9999 23:59:59 GMT", color))
|
||||||
case common.CdEmote:
|
case common.CdEmote:
|
||||||
data := h.Data.(map[string]interface{})
|
data := h.Data.(map[string]interface{})
|
||||||
emoteNames = make([]string, 0, len(data))
|
emoteNames = make([]string, 0, len(data))
|
||||||
|
@ -98,7 +100,7 @@ func recieve(v []js.Value) {
|
||||||
sort.Strings(emoteNames)
|
sort.Strings(emoteNames)
|
||||||
case common.CdJoin:
|
case common.CdJoin:
|
||||||
notify("")
|
notify("")
|
||||||
js.Call("openChat")
|
global.Call("openChat")
|
||||||
case common.CdNotify:
|
case common.CdNotify:
|
||||||
notify(h.Data.(string))
|
notify(h.Data.(string))
|
||||||
}
|
}
|
||||||
|
@ -126,18 +128,18 @@ func recieve(v []js.Value) {
|
||||||
switch d.Command {
|
switch d.Command {
|
||||||
case common.CmdPlaying:
|
case common.CmdPlaying:
|
||||||
if d.Arguments == nil || len(d.Arguments) == 0 {
|
if d.Arguments == nil || len(d.Arguments) == 0 {
|
||||||
js.Call("setPlaying", "", "")
|
global.Call("setPlaying", "", "")
|
||||||
|
|
||||||
} else if len(d.Arguments) == 1 {
|
} else if len(d.Arguments) == 1 {
|
||||||
js.Call("setPlaying", d.Arguments[0], "")
|
global.Call("setPlaying", d.Arguments[0], "")
|
||||||
|
|
||||||
} else if len(d.Arguments) == 2 {
|
} else if len(d.Arguments) == 2 {
|
||||||
js.Call("setPlaying", d.Arguments[0], d.Arguments[1])
|
global.Call("setPlaying", d.Arguments[0], d.Arguments[1])
|
||||||
}
|
}
|
||||||
case common.CmdRefreshPlayer:
|
case common.CmdRefreshPlayer:
|
||||||
js.Call("initPlayer", nil)
|
global.Call("initPlayer", nil)
|
||||||
case common.CmdPurgeChat:
|
case common.CmdPurgeChat:
|
||||||
js.Call("purgeChat", nil)
|
global.Call("purgeChat", nil)
|
||||||
appendMessage(d.HTML())
|
appendMessage(d.HTML())
|
||||||
case common.CmdHelp:
|
case common.CmdHelp:
|
||||||
url := "/help"
|
url := "/help"
|
||||||
|
@ -145,13 +147,13 @@ func recieve(v []js.Value) {
|
||||||
url = d.Arguments[0]
|
url = d.Arguments[0]
|
||||||
}
|
}
|
||||||
appendMessage(d.HTML())
|
appendMessage(d.HTML())
|
||||||
js.Get("window").Call("open", url, "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")
|
global.Get("window").Call("open", url, "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendMessage(msg string) {
|
func appendMessage(msg string) {
|
||||||
js.Call("appendMessages", "<div>"+msg+"</div>")
|
global.Call("appendMessages", "<div>"+msg+"</div>")
|
||||||
}
|
}
|
||||||
|
|
||||||
func websocketSend(msg string, dataType common.ClientDataType) error {
|
func websocketSend(msg string, dataType common.ClientDataType) error {
|
||||||
|
@ -167,7 +169,7 @@ func websocketSend(msg string, dataType common.ClientDataType) error {
|
||||||
return fmt.Errorf("could not marshal data: %v", err)
|
return fmt.Errorf("could not marshal data: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
js.Call("websocketSend", string(data))
|
global.Call("websocketSend", string(data))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,12 +190,12 @@ func send(this js.Value, v []js.Value) interface{} {
|
||||||
func showChatError(err error) {
|
func showChatError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Could not send: %v\n", err)
|
fmt.Printf("Could not send: %v\n", err)
|
||||||
js.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`)
|
global.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func notify(msg string) {
|
func notify(msg string) {
|
||||||
js.Call("setNotifyBox", msg)
|
global.Call("setNotifyBox", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func showTimestamp(v []js.Value) {
|
func showTimestamp(v []js.Value) {
|
||||||
|
@ -227,17 +229,19 @@ func debugValues(v []js.Value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
global = js.Global()
|
||||||
|
|
||||||
common.SetupLogging(common.LLDebug, "")
|
common.SetupLogging(common.LLDebug, "")
|
||||||
|
|
||||||
js.Set("processMessageKey", js.FuncOf(processMessageKey))
|
global.Set("processMessageKey", js.FuncOf(processMessageKey))
|
||||||
js.Set("sendMessage", js.FuncOf(send))
|
global.Set("sendMessage", js.FuncOf(send))
|
||||||
js.Set("isValidColor", js.FuncOf(isValidColor))
|
global.Set("isValidColor", js.FuncOf(isValidColor))
|
||||||
|
|
||||||
js.Set("recieveMessage", js.CallbackOf(recieve))
|
global.Set("recieveMessage", jsCallbackOf(recieve))
|
||||||
js.Set("processMessage", js.CallbackOf(processMessage))
|
global.Set("processMessage", jsCallbackOf(processMessage))
|
||||||
js.Set("debugValues", js.CallbackOf(debugValues))
|
global.Set("debugValues", jsCallbackOf(debugValues))
|
||||||
js.Set("showTimestamp", js.CallbackOf(showTimestamp))
|
global.Set("showTimestamp", jsCallbackOf(showTimestamp))
|
||||||
js.Set("join", js.CallbackOf(join))
|
global.Set("join", jsCallbackOf(join))
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 1)
|
||||||
|
@ -246,15 +250,26 @@ func main() {
|
||||||
inner += fmt.Sprintf(`<option value="%s">%s</option>\n`, c, c)
|
inner += fmt.Sprintf(`<option value="%s">%s</option>\n`, c, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
js.Get("colorSelect").Set("innerHTML", inner)
|
global.Get("colorSelect").Set("innerHTML", inner)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// This is needed so the goroutine does not end
|
// This is needed so the goroutine does not end
|
||||||
for {
|
for {
|
||||||
// heatbeat to keep connection alive to deal with nginx
|
// heatbeat to keep connection alive to deal with nginx
|
||||||
if js.Get("inChat").Bool() {
|
if global.Get("inChat").Bool() {
|
||||||
websocketSend("", common.CdPing)
|
websocketSend("", common.CdPing)
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second * 10)
|
time.Sleep(time.Second * 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsCallbackOf(fnc func(v []js.Value)) js.Func {
|
||||||
|
return js.FuncOf(func(this js.Value, refs []js.Value) interface{} {
|
||||||
|
vals := make([]js.Value, 0, len(refs))
|
||||||
|
for _, ref := range refs {
|
||||||
|
vals = append(vals, ref)
|
||||||
|
}
|
||||||
|
fnc(vals)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dennwc/dom/js"
|
"syscall/js"
|
||||||
|
|
||||||
"github.com/zorchenhimer/MovieNight/common"
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ func processMessageKey(this js.Value, v []js.Value) interface{} {
|
||||||
}
|
}
|
||||||
currentSug = filteredSug[newidx]
|
currentSug = filteredSug[newidx]
|
||||||
case keyTab, keyEnter:
|
case keyTab, keyEnter:
|
||||||
msg := js.Get("msg")
|
msg := global.Get("msg")
|
||||||
val := msg.Get("value").String()
|
val := msg.Get("value").String()
|
||||||
newval := val[:startIdx]
|
newval := val[:startIdx]
|
||||||
|
|
||||||
|
@ -104,7 +105,7 @@ func processMessageKey(this js.Value, v []js.Value) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
func processMessage(v []js.Value) {
|
func processMessage(v []js.Value) {
|
||||||
msg := js.Get("msg")
|
msg := global.Get("msg")
|
||||||
text := strings.ToLower(msg.Get("value").String())
|
text := strings.ToLower(msg.Get("value").String())
|
||||||
startIdx := msg.Get("selectionStart").Int()
|
startIdx := msg.Get("selectionStart").Int()
|
||||||
|
|
||||||
|
@ -189,6 +190,6 @@ func updateSuggestionDiv() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The \n is so it's easier to read th source in web browsers for the dev
|
// The \n is so it's easier to read th source in web browsers for the dev
|
||||||
js.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n"))
|
global.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n"))
|
||||||
js.Call("updateSuggestionScroll")
|
global.Call("updateSuggestionScroll")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue