Skip to content

Commit

Permalink
edit server side unit tests, add code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowsith committed Mar 18, 2023
1 parent 2f69b65 commit 668b30a
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 104 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
all:
make test
make prepare
make linux
make macos
Expand Down Expand Up @@ -31,3 +32,6 @@ pack:
cp -r dist/main.js mattermost-pr0gramm-plugin/client
cp plugin.json mattermost-pr0gramm-plugin/
tar -czvf mattermost-pr0gramm-plugin.tar.gz mattermost-pr0gramm-plugin

test:
go test ./server -v
8 changes: 4 additions & 4 deletions client/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PostWillRenderEmbed extends React.Component {

handleTagResult(uid, data) {
const info = new Pr0grammInfo(data);
if (info.clientSettings.tags == false) {
if (PostWillRenderEmbed.settings.tags == false) {
return;
}
/**
Expand All @@ -149,7 +149,7 @@ class PostWillRenderEmbed extends React.Component {

handleRating(uid, data, isFileUrl = false) {
const get = new Pr0grammGet(data);
if (get.clientSettings.rating == false) {
if (PostWillRenderEmbed.settings.rating == false) {
return;
}
const file = get.items[0];
Expand Down Expand Up @@ -183,7 +183,7 @@ class PostWillRenderEmbed extends React.Component {
*/
const fileElement = document.getElementById(uid + '_file');
const f = file.items[0].image;
const maxHeight = file.clientSettings.maxHeight;
const maxHeight = PostWillRenderEmbed.settings.maxHeight;
if (f.includes('.mp4')) {
const fileUrl = `https://vid.pr0gramm.com/` + f;
fileElement.innerHTML = this.getVideoElement(fileUrl, maxHeight, 'video/mp4');
Expand Down Expand Up @@ -263,7 +263,7 @@ class Pr0grammPlugin {
this.registerPlugin(registry);
})
.catch(err => {
PostWillRenderEmbed.settings = new ClientSettings(res.data);
PostWillRenderEmbed.settings = new ClientSettings();
this.registerPlugin(registry);
});
}
Expand Down
8 changes: 0 additions & 8 deletions client/types.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ export class Pr0grammInfo {
* @type {number}
*/
this.ts = data.ts;
/**
* @type {ClientSettings}
*/
this.clientSettings = new ClientSettings(data.clientSettings);
}
}

Expand Down Expand Up @@ -261,9 +257,5 @@ export class Pr0grammGet {
* @type {number}
*/
this.qc = data.qc;
/**
* @type {ClientSettings}
*/
this.clientSettings = new ClientSettings(data.clientSettings);
}
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mattermost-pr0gramm",
"version": "1.2.0",
"version": "1.2.1",
"description": "",
"main": "webpack.config.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "This plugin serves as a starting point for writing a Mattermost plugin.",
"homepage_url": "https://github.com/Shadowsith/mattermost-plugin-pr0gramm",
"support_url": "https://github.com/Shadowsith/mattermost-plugin-pr0gramm/issues",
"release_notes_url": "https://github.com/Shadowsith/mattermost-plugin-pr0gramm/releases/tag/1.2.0",
"version": "1.2.0",
"release_notes_url": "https://github.com/Shadowsith/mattermost-plugin-pr0gramm/releases/tag/1.2.1",
"version": "1.2.1",
"min_server_version": "6.2.1",
"server": {
"executables": {
Expand Down
150 changes: 69 additions & 81 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"log"
"net/http"
"strconv"
"strings"

"github.com/mattermost/mattermost-server/v6/plugin"
)
Expand Down Expand Up @@ -40,132 +38,122 @@ const (
)

// ServeHTTP demonstrates a plugin that handles HTTP requests by greeting the world.

func (p *Pr0grammPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
w.Header().Set("Content-Type", "application/json")

switch path {
case routeSettings:
clientSettings := p.getClientSettings(w)
// fmt.Fprint(w, clientSettings.maxHeightStr)
p.handleClientSettingsResult(w, clientSettings)
break

case routeInfo:
resp, err := p.itemsInfo(w, r)
p.handleRequestResult(w, resp, err)
break

case routeGet:
resp, err := p.itemsGet(w, r)
p.handleRequestResult(w, resp, err)
break

case routeReverse:
resp, err := p.itemsReverse(w, r)
p.handleRequestResult(w, resp, err)
break

default:
resp, err := p.any(w, r)
p.handleRequestResult(w, resp, err)
break
}
}

func (p *Pr0grammPlugin) getSettings() *PluginSettings {
func (p *Pr0grammPlugin) getSettings() PluginSettings {
pluginSettings, ok := p.API.GetConfig().PluginSettings.Plugins["pr0gramm"]
settings := new(PluginSettings)
if ok {
for k, v := range pluginSettings {
if k == "username" {
settings.Username = v.(string)
} else if k == "password" {
settings.Password = v.(string)
} else if k == "maxheight" {
maxHeight, err := strconv.Atoi(
fmt.Sprintf("%v", v))
if err != nil {
log.Fatal(err)
}
settings.MaxHeight = maxHeight
} else if k == "tags" {
val, ok := v.(bool)
if !ok {
val = false
}
settings.Tags = val
} else if k == "rating" {
val, ok := v.(bool)
if !ok {
val = false
}
settings.Rating = val
}
}
if !ok {
return p.getDefaultSettings()
}

settings := PluginSettings{
Username: p.getStrVal(pluginSettings["username"]),
Password: p.getStrVal(pluginSettings["password"]),
MaxHeight: p.getIntVal(pluginSettings["maxheight"]),
Tags: p.getBoolVal(pluginSettings["tags"]),
Rating: p.getBoolVal(pluginSettings["rating"]),
}

return settings
}

func (p *Pr0grammPlugin) getClientSettings(w http.ResponseWriter) *ClientSettings {
func (p *Pr0grammPlugin) getClientSettings(w http.ResponseWriter) ClientSettings {
pluginSettings, ok := p.API.GetConfig().PluginSettings.Plugins["pr0gramm"]
settings := new(ClientSettings)
if ok {
for k, v := range pluginSettings {
if k == "maxheight" {
maxHeight, err := strconv.Atoi(
fmt.Sprintf("%v", v))
if err != nil {
log.Fatal(err)
}
settings.MaxHeight = maxHeight
} else if k == "tags" {
val, ok := v.(bool)
if !ok {
val = false
}
settings.Tags = val
} else if k == "rating" {
val, ok := v.(bool)
if !ok {
val = false
}
settings.Rating = val
}
}
if !ok {
return p.getDefaultClientSettings()
}

settings := ClientSettings{
MaxHeight: p.getIntVal(pluginSettings["maxheight"]),
Tags: p.getBoolVal(pluginSettings["tags"]),
Rating: p.getBoolVal(pluginSettings["rating"]),
}

return settings
}

func (p *Pr0grammPlugin) handleRequestResult(w http.ResponseWriter, resp *http.Response, err error) {
if err != nil {
fmt.Fprint(w, "{ \"error\":\""+err.Error()+"\"}")
} else {
response := p.handleResponse(resp)
if response == "error" {
fmt.Fprint(w, "{ \"error\":\"pr0gramm http request error \"}")
} else {
clientSettings := p.getClientSettings(w)
response = strings.TrimRight(response, "}")
fmt.Fprint(w, p.addClientSettingsToResult(w, response, clientSettings))
}
func (p *Pr0grammPlugin) getDefaultSettings() PluginSettings {
return PluginSettings{
MaxHeight: 400,
Tags: true,
Rating: true,
Username: "",
Password: "",
}
}

func (p *Pr0grammPlugin) addClientSettingsToResult(w http.ResponseWriter, response string, settings *ClientSettings) string {
json, err := json.Marshal(&settings)
func (p *Pr0grammPlugin) getDefaultClientSettings() ClientSettings {
return ClientSettings{
MaxHeight: 400,
Tags: true,
Rating: true,
}
}

func (p *Pr0grammPlugin) getStrVal(v interface{}) string {
val, ok := v.(string)
if !ok {
val = ""
}
return val
}

func (p *Pr0grammPlugin) getBoolVal(v interface{}) bool {
val, ok := v.(bool)
if !ok {
val = true
}
return val
}

func (p *Pr0grammPlugin) getIntVal(v interface{}) int {
val, ok := v.(int)
if !ok {
val = 400
}
return val
}

func (p *Pr0grammPlugin) handleRequestResult(w http.ResponseWriter, resp *http.Response, err error) {
if err != nil {
return response
fmt.Fprintf(w, `{"error": "%s"}`, err.Error())
} else {
if string(json) != "{}" {
return strings.TrimRight(response, "}") + ", \"clientSettings\": " + string(json) + "}"
response := p.handleResponse(resp)
if response == "error" {
fmt.Fprint(w, `{ "error": "pr0gramm http request error"}`)
} else {
return response
fmt.Fprint(w, response)
}
}
}

func (p *Pr0grammPlugin) handleClientSettingsResult(w http.ResponseWriter, settings *ClientSettings) {
func (p *Pr0grammPlugin) handleClientSettingsResult(w http.ResponseWriter, settings ClientSettings) {
json, err := json.Marshal(&settings)
if err != nil {
fmt.Fprint(w, "{\"error\": \"serialization error\"}")
Expand Down
Loading

0 comments on commit 668b30a

Please sign in to comment.