generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
361 additions
and
34,637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.