Skip to content

Commit

Permalink
Initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
gravufo committed Apr 21, 2023
1 parent d117258 commit 6b1410a
Show file tree
Hide file tree
Showing 23 changed files with 361 additions and 34,637 deletions.
58 changes: 56 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
module github.com/mattermost/mattermost-plugin-starter-template
module github.com/opendwellers/partyparrotssync

go 1.16
go 1.20

require (
github.com/mattermost/mattermost-server/v6 v6.2.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.3 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/go-hclog v1.0.0 // indirect
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/hashicorp/yamux v0.0.0-20210826001029-26ff87cf9493 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/lib/pq v1.10.3 // indirect
github.com/mattermost/go-i18n v1.11.1-0.20211013152124-5c415071e404 // indirect
github.com/mattermost/ldap v0.0.0-20201202150706-ee0e6284187d // indirect
github.com/mattermost/logr/v2 v2.0.15 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.14 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/xid v1.3.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
github.com/wiggin77/merror v1.0.3 // indirect
github.com/wiggin77/srslog v1.0.1 // indirect
github.com/yuin/goldmark v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20211006190231-62292e806868 // indirect
golang.org/x/sys v0.0.0-20211006225509-1a26e0398eed // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211005153810-c76a74d43a8e // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
26 changes: 16 additions & 10 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "com.mattermost.plugin-starter-template",
"name": "Plugin Starter Template",
"description": "This plugin serves as a starting point for writing a Mattermost plugin.",
"homepage_url": "https://github.com/mattermost/mattermost-plugin-starter-template",
"support_url": "https://github.com/mattermost/mattermost-plugin-starter-template/issues",
"release_notes_url": "https://github.com/mattermost/mattermost-plugin-starter-template/releases/tag/v0.1.0",
"id": "org.polycancer.mattermost-partyparrots-sync",
"name": "Party Parrots Sync Plugin",
"description": "This plugin allows syncing party parrot emojis into your Mattermost server.",
"homepage_url": "https://github.com/opendwellers/mattermost-partyparrots-sync",
"support_url": "https://github.com/opendwellers/mattermost-partyparrots-sync/issues",
"release_notes_url": "https://github.com/opendwellers/mattermost-partyparrots-sync/releases/tag/v0.1.0",
"icon_path": "assets/starter-template-icon.svg",
"version": "0.1.0",
"min_server_version": "6.2.1",
Expand All @@ -17,12 +17,18 @@
"windows-amd64": "server/dist/plugin-windows-amd64.exe"
}
},
"webapp": {
"bundle_path": "webapp/dist/main.js"
},
"settings_schema": {
"header": "",
"footer": "",
"settings": []
"settings": [
{
"key": "AccessToken",
"display_name": "Mattermost Access Token",
"type": "text",
"help_text": "Mattermost Personal Access Token for executing CreateEmoji API",
"placeholder": "",
"default": null
}
]
}
}
79 changes: 79 additions & 0 deletions server/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"net/http"

"github.com/mattermost/mattermost-server/v6/model"
)

type MattermostClient struct {
client *model.Client4
}

func Login(url, token string) (*MattermostClient, error) {
c := model.NewAPIv4Client(url)
c.AuthToken = token
c.AuthType = model.HeaderBearer
return &MattermostClient{
client: c,
}, nil
}

func (p *Plugin) ensureConnected() error {
if p.client == nil || p.UserID == "" {
return p.initializeMattermostClient()
}
return nil
}

func (p *Plugin) initializeMattermostClient() error {
p.API.LogDebug("Initializing client")
configuration := p.getConfiguration()
if configuration == nil || configuration.AccessToken == "" {
p.API.LogInfo("Not configured yet, skipping login.")
return nil
}
config := p.API.GetConfig()
p.siteURL = *config.ServiceSettings.SiteURL

p.API.LogDebug(fmt.Sprintf("Login to %s using token %s", p.siteURL, configuration.AccessToken))
c, err := Login(p.siteURL, configuration.AccessToken)
if err != nil {
return nil
}
id, err := c.getUserID()
if err != nil {
return err
}
p.API.LogDebug(fmt.Sprintf("Got user id %s", id))

p.client = c
p.UserID = id

return nil
}

func (c *MattermostClient) getUserID() (string, error) {
user, resp, err := c.client.GetMe("")
if err != nil {
return "", fmt.Errorf("failed to get user id: %w", err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to get user id: %d", resp.StatusCode)
}
return user.Id, nil
}

// RegisterNewEmoji send a request for creating emoji
func (c *MattermostClient) RegisterNewEmoji(b []byte, name, userID string) error {
_, resp, err := c.client.CreateEmoji(&model.Emoji{
CreatorId: userID,
Name: name,
}, b, name)

if err != nil || resp.StatusCode != http.StatusOK {
return fmt.Errorf(err.Error())
}
return nil
}
3 changes: 2 additions & 1 deletion server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// If you add non-reference types to your configuration struct, be sure to rewrite Clone as a deep
// copy appropriate for your types.
type configuration struct {
AccessToken string
}

// Clone shallow copies the configuration. Your implementation may require a deep copy if
Expand Down Expand Up @@ -79,5 +80,5 @@ func (p *Plugin) OnConfigurationChange() error {

p.setConfiguration(configuration)

return nil
return p.initializeMattermostClient()
}
71 changes: 71 additions & 0 deletions server/parrots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"io"
"net/http"
"strings"

"gopkg.in/yaml.v3"
)

const (
baseUrl = "https://raw.githubusercontent.com/jmhobbs/cultofthepartyparrot.com/main/"
)

var parrotTypes = [...]string{
"parrots",
"flags",
"guests",
}

type list map[string]string

type Parrot struct {
name string
file string
gif []byte
}

func fetchParrotList(parrotType string) (parrots []Parrot, err error) {
resp, err := http.Get(baseUrl + parrotType + ".yaml")
if err != nil {
return
}
defer resp.Body.Close()

var parrotList []list
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
err = yaml.Unmarshal(body, &parrotList)
if err != nil {
return
}

for i := 0; i < len(parrotList); i++ {
var parrot Parrot
if parrotList[i]["gif"] != "" {
parrot.file = parrotList[i]["gif"]
} else if parrotList[i]["hd"] != "" {
parrot.file = parrotList[i]["hd"]
}
split := strings.SplitAfter(parrot.file, "/")
parrot.name, _ = strings.CutSuffix(split[len(split)-1], ".gif")
parrots = append(parrots, parrot)
}
return
}

func fetchParrotGif(parrot *Parrot, parrotType string) (err error) {
// Fetch the gif from GitHub
resp, err := http.Get(baseUrl + parrotType + "/" + parrot.file)
if err != nil {
return
}
parrot.gif, err = io.ReadAll(resp.Body)
if err != nil {
return
}
return
}
26 changes: 26 additions & 0 deletions server/parrots_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFetchParrots(t *testing.T) {
assert := assert.New(t)
for _, parrotType := range parrotTypes {
list, err := fetchParrotList(parrotType)
assert.Nil(err)
assert.Greater(len(list), 0)
}
}

func TestFetchParrotGif(t *testing.T) {
assert := assert.New(t)
parrot := Parrot{
name: "parrot",
file: "hd/parrot.gif",
}
fetchParrotGif(&parrot, "parrots")
assert.NotEmpty(parrot.gif)
}
Loading

0 comments on commit 6b1410a

Please sign in to comment.