From 189f92c54b5ca14ede317a30c5a0ceac9871e99a Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Mon, 16 Jul 2018 16:19:59 -0400 Subject: [PATCH 1/8] initial commit --- .gitignore | 1 + CHANGELOG.md | 9 + LICENSE | 201 + Makefile | 105 + README.md | 54 + build/.gitignore | 1 + build/manifest/go.mod | 17 + build/manifest/go.sum | 30 + build/manifest/main.go | 119 + plugin.json | 36 + server/README.md | 100 + server/activate_hooks.go | 70 + server/channel_hooks.go | 98 + server/command_hooks.go | 88 + server/configuration.go | 112 + server/go.mod | 26 + server/go.sum | 48 + server/http_hooks.go | 29 + server/main.go | 9 + server/message_hooks.go | 180 + server/plugin.go | 24 + server/plugin_id.go | 3 + server/team_hooks.go | 66 + webapp/.eslintrc.json | 636 ++ webapp/.gitignore | 1 + webapp/README.md | 105 + webapp/docs/bottom_team_sidebar.png | Bin 0 -> 13044 bytes webapp/docs/channel_header_button.png | Bin 0 -> 15452 bytes webapp/docs/left_sidebar_header.png | Bin 0 -> 23726 bytes webapp/docs/main_menu.png | Bin 0 -> 65997 bytes webapp/docs/post_type.png | Bin 0 -> 31941 bytes webapp/docs/root.png | Bin 0 -> 38564 bytes webapp/docs/user_actions.png | Bin 0 -> 50586 bytes webapp/docs/user_attributes.png | Bin 0 -> 50586 bytes webapp/package-lock.json | 6619 +++++++++++++++++ webapp/package.json | 33 + webapp/src/action_types.js | 7 + webapp/src/actions.js | 31 + webapp/src/components/bottom_team_sidebar.jsx | 10 + webapp/src/components/icons.jsx | 9 + .../components/left_sidebar_header/index.js | 11 + .../left_sidebar_header.jsx | 32 + webapp/src/components/post_type.jsx | 36 + webapp/src/components/root/index.js | 17 + webapp/src/components/root/root.jsx | 54 + webapp/src/components/user_actions/index.js | 12 + .../components/user_actions/user_actions.jsx | 36 + webapp/src/components/user_attributes.jsx | 10 + webapp/src/index.js | 4 + webapp/src/plugin.jsx | 69 + webapp/src/plugin_id.js | 1 + webapp/src/reducer.js | 30 + webapp/src/selectors.js | 7 + webapp/webpack.config.js | 42 + 54 files changed, 9238 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 build/.gitignore create mode 100644 build/manifest/go.mod create mode 100644 build/manifest/go.sum create mode 100644 build/manifest/main.go create mode 100644 plugin.json create mode 100644 server/README.md create mode 100644 server/activate_hooks.go create mode 100644 server/channel_hooks.go create mode 100644 server/command_hooks.go create mode 100644 server/configuration.go create mode 100644 server/go.mod create mode 100644 server/go.sum create mode 100644 server/http_hooks.go create mode 100644 server/main.go create mode 100644 server/message_hooks.go create mode 100644 server/plugin.go create mode 100644 server/plugin_id.go create mode 100644 server/team_hooks.go create mode 100644 webapp/.eslintrc.json create mode 100644 webapp/.gitignore create mode 100644 webapp/README.md create mode 100644 webapp/docs/bottom_team_sidebar.png create mode 100644 webapp/docs/channel_header_button.png create mode 100644 webapp/docs/left_sidebar_header.png create mode 100644 webapp/docs/main_menu.png create mode 100644 webapp/docs/post_type.png create mode 100644 webapp/docs/root.png create mode 100644 webapp/docs/user_actions.png create mode 100644 webapp/docs/user_attributes.png create mode 100644 webapp/package-lock.json create mode 100644 webapp/package.json create mode 100644 webapp/src/action_types.js create mode 100644 webapp/src/actions.js create mode 100644 webapp/src/components/bottom_team_sidebar.jsx create mode 100644 webapp/src/components/icons.jsx create mode 100644 webapp/src/components/left_sidebar_header/index.js create mode 100644 webapp/src/components/left_sidebar_header/left_sidebar_header.jsx create mode 100644 webapp/src/components/post_type.jsx create mode 100644 webapp/src/components/root/index.js create mode 100644 webapp/src/components/root/root.jsx create mode 100644 webapp/src/components/user_actions/index.js create mode 100644 webapp/src/components/user_actions/user_actions.jsx create mode 100644 webapp/src/components/user_attributes.jsx create mode 100644 webapp/src/index.js create mode 100644 webapp/src/plugin.jsx create mode 100644 webapp/src/plugin_id.js create mode 100644 webapp/src/reducer.js create mode 100644 webapp/src/selectors.js create mode 100644 webapp/webpack.config.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1521c8b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dist diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b682ae5 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## 0.0.1 - 2018-08-16 +### Added +- Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f9b120 --- /dev/null +++ b/Makefile @@ -0,0 +1,105 @@ +GO=$(shell go env GOPATH)/bin/vgo + +# Check that vgo is installed. This won't be necessary once Go 1.11 is released, but it will still +# be necessary to assert the Go version. +ifeq ($(GO),) + echo go get -u github.com/golang/vgo; \ + go get -u github.com/golang/vgo; +endif + +# Ensure that the build tools are compiled. Go's caching makes this quick. +$(shell cd build/manifest && $(GO) build -o ../bin/manifest) + +# Extract the plugin id from the manifest. +PLUGIN_ID=$(shell build/bin/manifest plugin_id) +ifeq ($(PLUGIN_ID),) + $(error Cannot parse id from plugin.json) +endif + +# Determine if a server is defined in plugin.json +HAS_SERVER=$(shell build/bin/manifest has_server) + +# Determine if a webapp is defined in plugin.json +HAS_WEBAPP=$(shell build/bin/manifest has_webapp) + +# all, the default target, builds and bundle the plugin. +all: dist + +# apply propagates the plugin id into the server/ and webapp/ folders as required. +.PHONY: apply +apply: + ./build/bin/manifest apply + +# server builds the server, if it exists, including support for multiple architectures +.PHONY: server +server: +ifneq ($(HAS_SERVER),) + mkdir -p server/dist; + cd server && env GOOS=linux GOARCH=amd64 $(GO) build -o dist/plugin-linux-amd64; + cd server && env GOOS=darwin GOARCH=amd64 $(GO) build -o dist/plugin-darwin-amd64; + cd server && env GOOS=windows GOARCH=amd64 $(GO) build -o dist/plugin-windows-amd64.exe; +endif + +# webapp builds the webapp, if it exists +.PHONY: webapp +webapp: +ifneq ($(HAS_WEBAPP),) + cd webapp && npm install; + cd webapp && npm run fix; + cd webapp && npm run build; +endif + +# bundle generates a tar bundle of the plugin for install +.PHONY: bundle +bundle: + rm -rf dist/ + mkdir -p dist/$(PLUGIN_ID) + cp plugin.json dist/$(PLUGIN_ID)/ +ifneq ($(HAS_SERVER),) + mkdir -p dist/$(PLUGIN_ID)/server/dist; + cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/; +endif +ifneq ($(HAS_WEBAPP),) + mkdir -p dist/$(PLUGIN_ID)/webapp/dist; + cp -r webapp/dist/* dist/$(PLUGIN_ID)/webapp/dist/; +endif + cd dist/$(PLUGIN_ID) && tar -zcvf ../$(PLUGIN_ID).tar.gz * + + @echo plugin built at: dist/$(PLUGIN_ID).tar.gz + +# dist builds and bundles the plugin +.PHONY: dist +dist: apply \ + server \ + webapp \ + bundle + +# deploy installs the plugin to a (development) server, using the API if appropriate environment +# variables are defined, or copying the files directly to a sibling mattermost-server directory +.PHONY: deploy +deploy: +ifneq ($(and $(MM_SERVICESETTINGS_SITEURL),$(MM_ADMIN_USERNAME),$(MM_ADMIN_PASSWORD)),) + @echo "Installing plugin via API" + http --print b --check-status $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/me || ( \ + TOKEN=`http --print h POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/login login_id=$(MM_ADMIN_USERNAME) password=$(MM_ADMIN_PASSWORD) | grep Token | cut -f2 -d' '` && \ + http --print b GET $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/me Authorization:"Bearer $$TOKEN" \ + ) + http --print b DELETE $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID) + http --print b --check-status --form POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins plugin@dist/$(PLUGIN_ID).tar.gz && \ + http --print b POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID)/enable +else ifneq ($(wildcard ../mattermost-server/.*),) + @echo "Installing plugin via filesystem. Server restart and manual plugin enabling required" + mkdir -p ../mattermost-server/plugins/$(PLUGIN_ID) + tar -C ../mattermost-server/plugins/$(PLUGIN_ID) -zxvf dist/$(PLUGIN_ID).tar.gz +else + @echo "No supported deployment method available. Install plugin manually." +endif + +# clean removes all build artifacts +.PHONY: clean +clean: + rm -fr dist/ + rm -fr server/dist + rm -fr webapp/dist + rm -fr webapp/node_modules + rm -fr build/bin/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..84b8622 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# Sample Plugin + +This plugin serves as a reference guide for best practices, build scripts and samples when writing Mattermost plugins. It also doubles as a testbed for verifying plugin functionality during release testing. See [server/README.md](server/README.md) and [webapp/README.md](webapp/README.md) for more details. + +The example implementations are primarily meant as illustrations to assist with developing your plugin. Feel free to base your own plugin off this repository, removing or modifying components as needed. + +## Getting Started +Shallow clone the repository to a directory matching your plugin name: +``` +git clone --depth 1 https://github.com/mattermost/mattermost-plugin-sample com.example.my-plugin +``` + +Edit `plugin.json` with your `id`, `name`, and `description`: +``` +{ + "id": "com.example.my-plugin", + "name": "My Plugin", + "description": "A plugin to enhance Mattermost." +} +``` + +Build your plugin: +``` +make +``` + +This will produce a single plugin file (with support for multiple architectures) for upload to your Mattermost server: + +``` +dist/com.example.my-plugin.tar.gz +``` + +There is a build target to automate deploying and enabling the plugin to your server, but it requires configuration and [http](https://httpie.org/) to be installed: +``` +export MM_SERVICESETTINGS_SITEURL=http://localhost:8065/ +export MM_ADMIN_USERNAME=admin +export MM_ADMIN_PASSWORD=password +make deploy +``` + +Alternatively, if you are running your `mattermost-server` out of a sibling directory by the same name, use the `deploy` target alone to unpack the files into the right directory. You will need to restart your server and manually enable your plugin. + +In production, deploy and upload your plugin via the [System Console](https://about.mattermost.com/default-plugin-uploads). + +## Q&A + +### How do I make a server-only or web app-only plugin? + +Simply delete the `server` or `webapp` folders and remove the corresponding sections from `plugin.json`. The build scripts will skip the missing portions automatically. + +### How do I remove unwanted hooks from the server? + +Simply delete the corresponding implementations (or files). The Mattermost server automatically +identifies which hooks have been implemented when the plugin is started. diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..ba077a4 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1 @@ +bin diff --git a/build/manifest/go.mod b/build/manifest/go.mod new file mode 100644 index 0000000..e8e890e --- /dev/null +++ b/build/manifest/go.mod @@ -0,0 +1,17 @@ +module github.com/mattermost/mattermost-plugin-sample/build/manifest + +require ( + github.com/gorilla/websocket v1.2.0 // indirect + github.com/hashicorp/go-hclog v0.0.0-20180402200405-69ff559dc25f // indirect + github.com/mattermost/mattermost-server v0.0.0-20180718195328-2edc5d4fe051 + github.com/nicksnyder/go-i18n v1.10.0 // indirect + github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3 // indirect + github.com/pelletier/go-toml v1.2.0 // indirect + github.com/pkg/errors v0.8.0 + go.uber.org/atomic v1.3.2 // indirect + go.uber.org/multierr v1.1.0 // indirect + go.uber.org/zap v1.8.0 // indirect + golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 // indirect + gopkg.in/yaml.v2 v2.2.1 // indirect +) diff --git a/build/manifest/go.sum b/build/manifest/go.sum new file mode 100644 index 0000000..12cbc0b --- /dev/null +++ b/build/manifest/go.sum @@ -0,0 +1,30 @@ +github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ= +github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/hashicorp/go-hclog v0.0.0-20180402200405-69ff559dc25f h1:t34t/ySFIGsPOLQ/dCcKeCoErlqhXlNLYvPn7mVogzo= +github.com/hashicorp/go-hclog v0.0.0-20180402200405-69ff559dc25f/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= +github.com/mattermost/mattermost-server v0.0.0-20180716205655-f2c180390599 h1:ULtKuAlmCaYC4UfTsGN7BFTGzWPXNmkEtvokk1aSusc= +github.com/mattermost/mattermost-server v0.0.0-20180716205655-f2c180390599/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y= +github.com/mattermost/mattermost-server v0.0.0-20180717201910-181fd3dd4d42 h1:dRfBO9L3Q1rjZJN7O1stQrkG/xVqfsi8SCRDwysoFKQ= +github.com/mattermost/mattermost-server v0.0.0-20180717201910-181fd3dd4d42/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y= +github.com/mattermost/mattermost-server v0.0.0-20180718195328-2edc5d4fe051 h1:vIs9B718aEEX3ECt9yjKOxIiQKq4KqVIZ5VQIcI08to= +github.com/mattermost/mattermost-server v0.0.0-20180718195328-2edc5d4fe051/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y= +github.com/nicksnyder/go-i18n v1.10.0 h1:5AzlPKvXBH4qBzmZ09Ua9Gipyruv6uApMcrNZdo96+Q= +github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q= +github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3 h1:9J0mOv1rXIBlRjQCiAGyx9C3dZZh5uIa3HU0oTV8v1E= +github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.8.0 h1:r6Za1Rii8+EGOYRDLvpooNOF6kP3iyDnkpzbw67gCQ8= +go.uber.org/zap v1.8.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8 h1:h7zdf0RiEvWbYBKIx4b+q41xoUVnMmvsGZnIVE5syG8= +golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 h1:AFxeG48hTWHhDTQDk/m2gorfVHUEa9vo3tp3D7TzwjI= +gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/build/manifest/main.go b/build/manifest/main.go new file mode 100644 index 0000000..62a19e1 --- /dev/null +++ b/build/manifest/main.go @@ -0,0 +1,119 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "regexp" + + "github.com/mattermost/mattermost-server/model" + "github.com/pkg/errors" +) + +const PluginIdGoFileTemplate = `package main + +const PluginId = "%s" +` + +const PluginIdJsFileTemplate = `export default '%s';` + +func main() { + if len(os.Args) <= 1 { + panic("no cmd specified") + } + + manifest, err := findManifest() + if err != nil { + panic("failed to find manifest: " + err.Error()) + } + + cmd := os.Args[1] + switch cmd { + case "plugin_id": + dumpPluginId(manifest) + + case "has_server": + if manifest.HasServer() { + fmt.Printf("true") + } + + case "has_webapp": + if manifest.HasWebapp() { + fmt.Printf("true") + } + + case "apply": + if err := applyManifest(manifest); err != nil { + panic("failed to apply manifest: " + err.Error()) + } + + default: + panic("unrecognized command: " + cmd) + } +} + +func findManifest() (*model.Manifest, error) { + _, manifestFilePath, err := model.FindManifest(".") + if err != nil { + return nil, errors.Wrap(err, "failed to find manifest in current working directory") + } + manifestFile, err := os.Open(manifestFilePath) + if err != nil { + return nil, errors.Wrapf(err, "failed to open %s", manifestFilePath) + } + defer manifestFile.Close() + + // Re-decode the manifest, disallowing unknown fields. When we write the manifest back out, + // we don't want to accidentally clobber anything we won't preserve. + var manifest model.Manifest + decoder := json.NewDecoder(manifestFile) + decoder.DisallowUnknownFields() + if err = decoder.Decode(&manifest); err != nil { + return nil, errors.Wrap(err, "failed to parse manifest") + } + + return &manifest, nil +} + +// dumpPluginId writes the plugin id from the given manifest to standard out +func dumpPluginId(manifest *model.Manifest) { + fmt.Printf("%s", manifest.Id) +} + +// applyManifest propagates the plugin_id into the server and webapp folders, as necessary +func applyManifest(manifest *model.Manifest) error { + if manifest.HasServer() { + if err := ioutil.WriteFile( + "server/plugin_id.go", + []byte(fmt.Sprintf(PluginIdGoFileTemplate, manifest.Id)), + 0644, + ); err != nil { + return errors.Wrap(err, "failed to write server/plugin_id.go") + } + + goMod, err := ioutil.ReadFile("server/go.mod") + if err != nil { + return errors.Wrap(err, "failed to read server/go.mod") + } + + moduleRe := regexp.MustCompile("module .+") + goMod = moduleRe.ReplaceAll(goMod, []byte(fmt.Sprintf("module %s", manifest.Id))) + + if err := ioutil.WriteFile("server/go.mod", goMod, 0644); err != nil { + return errors.Wrap(err, "failed to write server/go.mod") + } + } + + if manifest.HasWebapp() { + if err := ioutil.WriteFile( + "webapp/src/plugin_id.js", + []byte(fmt.Sprintf(PluginIdJsFileTemplate, manifest.Id)), + 0644, + ); err != nil { + return errors.Wrap(err, "failed to open webapp/src/plugin_id.js") + } + } + + return nil +} diff --git a/plugin.json b/plugin.json new file mode 100644 index 0000000..40199cd --- /dev/null +++ b/plugin.json @@ -0,0 +1,36 @@ +{ + "id": "com.mattermost.sample-plugin", + "name": "Sample Plugin", + "description": "This plugin serves as a reference guide for best practices and build scripts when writing Mattermost plugins.", + "version": "0.0.1", + "server": { + "executables": { + "linux-amd64": "server/dist/plugin-linux-amd64", + "darwin-amd64": "server/dist/plugin-darwin-amd64", + "windows-amd64": "server/dist/plugin-windows-amd64.exe" + }, + "executable": "" + }, + "webapp": { + "bundle_path": "webapp/dist/main.js" + }, + "settings_schema": { + "header": "", + "footer": "", + "settings": [{ + "key": "ChannelName", + "display_name": "Channel Name", + "type": "text", + "help_text": "The channel to use as part of the sample plugin, created for each team automatically if it does not exist.", + "placeholder": "sample_plugin", + "default": "sample_plugin" + }, { + "key": "Username", + "display_name": "Username", + "type": "text", + "help_text": "The user to use as part of the sample plugin, created automatically if it does not exist.", + "placeholder": "sample_plugin", + "default": "sample_plugin" + }] + } +} diff --git a/server/README.md b/server/README.md new file mode 100644 index 0000000..bd135b1 --- /dev/null +++ b/server/README.md @@ -0,0 +1,100 @@ +# Sample Plugin: Server + +The server component of this sample plugin is written in Go and [net/rpc](https://golang.org/pkg/net/rpc/). It relies on a configured `ChannelName` and `Username` in [plugin.json](../plugin.json) to implement each of the supported hooks. + +Each of the included files or folders is outlined below. + +## [go.mod](go.mod), [go.sum](go.sum) + +These are metadata files managed by [vgo](https://github.com/golang/vgo) for dependency management. While vgo is currently in beta, it will launch as part of the standard Go 1.11 tooling and stabilize in subsequent releases. It was preferred for this project over [dep](https://github.com/golang/dep) since it does not require locating your plugin in the `$GOPATH`. + +## [main.go](main.go) + +This is the entry point of your plugin binary, that in turn invokes [plugin.ClientMain](https://godoc.org/github.com/mattermost/mattermost-server/plugin#ClientMain) to wire up RPC communication between your plugin and the Mattermost Server. + +## [plugin\_id.go](plugin_id.go) + +This is a file generated by the [build/manifest](../build/manifest) tool that captures the plugin id from [plugin.json](../plugin.json). It simplifies the need to hard-code the plugin id in multiple places by exporting a constant for use instead. + +## [plugin.go](plugin.go) + +This file defines the `Plugin` struct, embedding [plugin.MattermostPlugin](https://godoc.org/github.com/mattermost/mattermost-server/plugin#MattermostPlugin) to automatically handle the wiring up the [API](https://godoc.org/github.com/mattermost/mattermost-server/plugin#API) when the plugin starts. It contains public fields that are automatically unmarshalled from [plugin.json](../plugin.json) as part of the `OnConfiguration` hook in [configuration.go](configuration.go). + +## [activate\_hooks.go](activate_hooks.go) + +### OnActivate + +This sample implementation logs a message to the sample channel whenever the plugin is activated. + +### OnDeactivate + +This sample implementation logs a debug message to the server logs whenever the plugin is activated. + +## [configuration.go](configuration.go) + +### OnConfigurationChange + +This sample implementation ensures the configured sample user and channel are created for use +by the plugin. + +## [channel\_hooks.go](channel_hooks.go) + +### ChannelHasBeenCreated + +This sample implementation logs a message to the sample channel whenever a channel is created. + +### UserHasJoinedChannel + +This sample implementation logs a message to the sample channel whenever a user joins a channel. + +### UserHasLeftChannel + +This sample implementation logs a message to the sample channel whenever a user leaves a channel. + +## [command\_hooks.go](command_hooks.go) + +### ExecuteCommand + +This sample implementation responds to a `/sample_plugin` command, allowing the user to enable +or disable the sample plugin's hooks functionality (but leave the command and webapp enabled). + +## [http\_hooks.go](http_hooks.go) + +### ServeHTTP + +This sample implementation sends back whether or not the plugin hooks are currently enabled. It +is used by the web app to recover from a network reconnection and synchronize the state of the +plugin's hooks. + +## [message\_hooks.go](message_hooks.go) + +### MessageWillBePosted + +This sample implementation rejects posts in the sample channel, as well as posts that @-mention +the sample plugin user. + +### MessageWillBeUpdated + +This sample implementation rejects posts that @-mention the sample plugin user. + +### MessageHasBeenPosted + +This sample implementation logs a message to the sample channel whenever a message is posted, +unless by the sample plugin user itself. + +### MessageHasBeenUpdated + +This sample implementation logs a message to the sample channel whenever a message is updated, +unless by the sample plugin user itself. + +## [team\_hooks.go](team.go) + +### UserHasJoinedTeam + +This sample implementation logs a message to the sample channel in the team whenever a user +joins the team. + +### UserHasLeftTeam + +This sample implementation logs a message to the sample channel in the team whenever a user +leaves the team. diff --git a/server/activate_hooks.go b/server/activate_hooks.go new file mode 100644 index 0000000..45befdf --- /dev/null +++ b/server/activate_hooks.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + + "github.com/mattermost/mattermost-server/model" +) + +// OnActivate is invoked when the plugin is activated. +// +// This sample implementation logs a message to the sample channel whenever the plugin is +// activated. +func (p *Plugin) OnActivate() error { + // It's necessary to do this asynchronously, so as to avoid CreatePost triggering a call + // to MessageWillBePosted and deadlocking the plugin. + // + // See https://mattermost.atlassian.net/browse/MM-11431 + go func() { + teams, err := p.API.GetTeams() + if err != nil { + p.API.LogError( + "failed to query teams OnActivate", + "error", err.Error(), + ) + } + + for _, team := range teams { + if _, err := p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[team.Id], + Message: fmt.Sprintf( + "OnActivate: %s", PluginId, + ), + Type: "custom_sample_plugin", + Props: map[string]interface{}{ + "username": p.Username, + "channel_name": p.ChannelName, + }, + }); err != nil { + p.API.LogError( + "failed to post OnActivate message", + "error", err.Error(), + ) + } + + if err := p.registerCommand(team.Id); err != nil { + p.API.LogError( + "failed to register command", + "error", err.Error(), + ) + } + } + + }() + + return nil +} + +// OnDeactivate is invoked when the plugin is deactivated. This is the plugin's last chance to use +// the API, and the plugin will be terminated shortly after this invocation. +// +// This sample implementation logs a debug message to the server logs whenever the plugin is +// activated. +func (p *Plugin) OnDeactivate() error { + // Ideally, we'd post an on deactivate message like in OnActivate, but this is hampered by + // https://mattermost.atlassian.net/browse/MM-11431?filter=15018 + p.API.LogDebug("OnDeactivate") + + return nil +} diff --git a/server/channel_hooks.go b/server/channel_hooks.go new file mode 100644 index 0000000..b1ee3f2 --- /dev/null +++ b/server/channel_hooks.go @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/plugin" +) + +// ChannelHasBeenCreated is invoked after the channel has been committed to the database. +// +// This sample implementation logs a message to the sample channel whenever a channel is created. +func (p *Plugin) ChannelHasBeenCreated(c *plugin.Context, channel *model.Channel) { + if p.disabled { + return + } + + if _, err := p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[channel.TeamId], + Message: fmt.Sprintf("ChannelHasBeenCreated: ~%s", channel.Name), + }); err != nil { + p.API.LogError( + "failed to post ChannelHasBeenCreated message", + "channel_id", channel.Id, + "error", err.Error(), + ) + } +} + +// UserHasJoinedChannel is invoked after the membership has been committed to the database. If +// actor is not nil, the user was invited to the channel by the actor. +// +// This sample implementation logs a message to the sample channel whenever a user joins a channel. +func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) { + if p.disabled { + return + } + + user, err := p.API.GetUser(channelMember.UserId) + if err != nil { + p.API.LogError("failed to query user", "user_id", channelMember.UserId) + return + } + + channel, err := p.API.GetChannel(channelMember.ChannelId) + if err != nil { + p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId) + return + } + + if _, err = p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[channel.TeamId], + Message: fmt.Sprintf("UserHasJoinedChannel: @%s, ~%s", user.Username, channel.Name), + }); err != nil { + p.API.LogError( + "failed to post UserHasJoinedChannel message", + "user_id", channelMember.UserId, + "error", err.Error(), + ) + } +} + +// UserHasLeftChannel is invoked after the membership has been removed from the database. If +// actor is not nil, the user was removed from the channel by the actor. +// +// This sample implementation logs a message to the sample channel whenever a user leaves a +// channel. +func (p *Plugin) UserHasLeftChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) { + if p.disabled { + return + } + + user, err := p.API.GetUser(channelMember.UserId) + if err != nil { + p.API.LogError("failed to query user", "user_id", channelMember.UserId) + return + } + + channel, err := p.API.GetChannel(channelMember.ChannelId) + if err != nil { + p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId) + return + } + + if _, err = p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[channel.TeamId], + Message: fmt.Sprintf("UserHasLeftChannel: @%s, ~%s", user.Username, channel.Name), + }); err != nil { + p.API.LogError( + "failed to post UserHasLeftChannel message", + "user_id", channelMember.UserId, + "error", err.Error(), + ) + } +} diff --git a/server/command_hooks.go b/server/command_hooks.go new file mode 100644 index 0000000..814395e --- /dev/null +++ b/server/command_hooks.go @@ -0,0 +1,88 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/plugin" +) + +const CommandTrigger = "sample_plugin" + +func (p *Plugin) registerCommand(teamId string) error { + if err := p.API.RegisterCommand(&model.Command{ + TeamId: teamId, + Trigger: CommandTrigger, + AutoComplete: true, + AutoCompleteHint: "(true|false)", + AutoCompleteDesc: "Enables or disables the sample plugin hooks.", + DisplayName: "Sample Plugin Command", + Description: "A command used to enable or disable the sample plugin hooks.", + }); err != nil { + p.API.LogError( + "failed to register command", + "error", err.Error(), + ) + } + + return nil +} + +func (p *Plugin) emitStatusChange() { + p.API.PublishWebSocketEvent("status_change", map[string]interface{}{ + "enabled": !p.disabled, + }, &model.WebsocketBroadcast{}) +} + +// ExecuteCommand executes a command that has been previously registered via the RegisterCommand +// API. +// +// This sample implementation responds to a /sample_plugin command, allowing the user to enable +// or disable the sample plugin's hooks functionality (but leave the command and webapp enabled). +func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { + if !strings.HasPrefix(args.Command, "/"+CommandTrigger) { + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: fmt.Sprintf("Unknown command: " + args.Command), + }, nil + } + + if strings.HasSuffix(args.Command, "true") { + if !p.disabled { + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: "The sample plugin hooks are already enabled.", + }, nil + } + + p.disabled = false + p.emitStatusChange() + + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: "Enabled sample plugin hooks.", + }, nil + + } else if strings.HasSuffix(args.Command, "false") { + if p.disabled { + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: "The sample plugin hooks are already disabled.", + }, nil + } + + p.disabled = true + p.emitStatusChange() + + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: "Disabled sample plugin hooks.", + }, nil + } + + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: fmt.Sprintf("Unknown command action: " + args.Command), + }, nil +} diff --git a/server/configuration.go b/server/configuration.go new file mode 100644 index 0000000..e7fbd8b --- /dev/null +++ b/server/configuration.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + + "github.com/mattermost/mattermost-server/model" +) + +// OnConfigurationChange is invoked when configuration changes may have been made. +// +// This sample implementation ensures the configured sample user and channel are created for use +// by the plugin. +func (p *Plugin) OnConfigurationChange() error { + // Leverage the default implementation on the embedded plugin.Mattermost. This + // automatically attempts to unmarshal the plugin config block of the server's + // configuration onto the public members of Plugin, such as Username and ChannelName. + // + // Feel free to skip this and implement your own handler if you have more complex needs. + if err := p.MattermostPlugin.OnConfigurationChange(); err != nil { + p.API.LogError(err.Error()) + return err + } + + if err := p.ensureSampleUser(); err != nil { + p.API.LogError(err.Error()) + return err + } + + if err := p.ensureSampleChannels(); err != nil { + p.API.LogError(err.Error()) + return err + } + + return nil +} + +func (p *Plugin) ensureSampleUser() *model.AppError { + var err *model.AppError + + // Check for the configured user. Ignore any error, since it's hard to distinguish runtime + // errors from a user simply not existing. + user, _ := p.API.GetUserByUsername(p.Username) + + // Ensure the configured user exists. + if user == nil { + user, err = p.API.CreateUser(&model.User{ + Username: p.Username, + Password: "sample", + // AuthData *string `json:"auth_data,omitempty"` + // AuthService string `json:"auth_service"` + Email: fmt.Sprintf("%s@example.com", p.Username), + Nickname: "Sam", + FirstName: "Sample", + LastName: "Plugin User", + Position: "Bot", + }) + + if err != nil { + return err + } + } + + teams, err := p.API.GetTeams() + if err != nil { + return err + } + + for _, team := range teams { + // Ignore any error. + p.API.CreateTeamMember(team.Id, p.sampleUserId) + } + + // Save the id for later use. + p.sampleUserId = user.Id + + return nil +} + +func (p *Plugin) ensureSampleChannels() *model.AppError { + teams, err := p.API.GetTeams() + if err != nil { + return err + } + + p.sampleChannelIds = make(map[string]string) + for _, team := range teams { + // Check for the configured channel. Ignore any error, since it's hard to + // distinguish runtime errors from a channel simply not existing. + channel, _ := p.API.GetChannelByNameForTeamName(team.Name, p.ChannelName) + + // Ensure the configured channel exists. + if channel == nil { + channel, err = p.API.CreateChannel(&model.Channel{ + TeamId: team.Id, + Type: model.CHANNEL_OPEN, + DisplayName: "Sample Plugin", + Name: p.ChannelName, + Header: "The channel used by the sample plugin.", + Purpose: "This channel was created by a plugin for testing.", + }) + + if err != nil { + return err + } + } + + // Save the ids for later use. + p.sampleChannelIds[team.Id] = channel.Id + } + + return nil +} diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..9de7bd2 --- /dev/null +++ b/server/go.mod @@ -0,0 +1,26 @@ +module com.mattermost.sample-plugin + +require ( + github.com/golang/protobuf v1.1.0 // indirect + github.com/gorilla/websocket v1.2.0 // indirect + github.com/hashicorp/go-hclog v0.0.0-20180402200405-69ff559dc25f // indirect + github.com/hashicorp/go-plugin v0.0.0-20180331002553-e8d22c780116 // indirect + github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect + github.com/mattermost/mattermost-server v0.0.0-20180719183001-d599a74f2cc6 + github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect + github.com/nicksnyder/go-i18n v1.10.0 // indirect + github.com/oklog/run v1.0.0 // indirect + github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3 // indirect + github.com/pelletier/go-toml v1.2.0 // indirect + github.com/pkg/errors v0.8.0 // indirect + go.uber.org/atomic v1.3.2 // indirect + go.uber.org/multierr v1.1.0 // indirect + go.uber.org/zap v1.8.0 // indirect + golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8 // indirect + golang.org/x/net v0.0.0-20180712202826-d0887baf81f4 // indirect + golang.org/x/text v0.3.0 // indirect + google.golang.org/genproto v0.0.0-20180716172848-2731d4fa720b // indirect + google.golang.org/grpc v1.13.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 // indirect + gopkg.in/yaml.v2 v2.2.1 // indirect +) diff --git a/server/go.sum b/server/go.sum new file mode 100644 index 0000000..76789be --- /dev/null +++ b/server/go.sum @@ -0,0 +1,48 @@ +github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc= +github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ= +github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/hashicorp/go-hclog v0.0.0-20180402200405-69ff559dc25f h1:t34t/ySFIGsPOLQ/dCcKeCoErlqhXlNLYvPn7mVogzo= +github.com/hashicorp/go-hclog v0.0.0-20180402200405-69ff559dc25f/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= +github.com/hashicorp/go-plugin v0.0.0-20180331002553-e8d22c780116 h1:Y4V/yReWjQo/Ngyc0w6C3EKXKincp4YgvXeo8lI4LrI= +github.com/hashicorp/go-plugin v0.0.0-20180331002553-e8d22c780116/go.mod h1:JSqWYsict+jzcj0+xElxyrBQRPNoiWQuddnxArJ7XHQ= +github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M= +github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/mattermost/mattermost-server v0.0.0-20180716205655-f2c180390599 h1:ULtKuAlmCaYC4UfTsGN7BFTGzWPXNmkEtvokk1aSusc= +github.com/mattermost/mattermost-server v0.0.0-20180716205655-f2c180390599/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y= +github.com/mattermost/mattermost-server v0.0.0-20180719180216-0f672ab36b0e h1:dzS1oPHnRzIQiwHGpN9HrZT825wSXe6tjwuAJ/BRYHc= +github.com/mattermost/mattermost-server v0.0.0-20180719180216-0f672ab36b0e/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y= +github.com/mattermost/mattermost-server v0.0.0-20180719183001-d599a74f2cc6 h1:cLxxyKbKCIqkiSPFm2+Kc8KT0k/zLmiie+P0bTPMIB8= +github.com/mattermost/mattermost-server v0.0.0-20180719183001-d599a74f2cc6/go.mod h1:5L6MjAec+XXQwMIt791Ganu45GKsSiM+I0tLR9wUj8Y= +github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 h1:7GoSOOW2jpsfkntVKaS2rAr1TJqfcxotyaUcuxoZSzg= +github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/nicksnyder/go-i18n v1.10.0 h1:5AzlPKvXBH4qBzmZ09Ua9Gipyruv6uApMcrNZdo96+Q= +github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q= +github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= +github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= +github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3 h1:9J0mOv1rXIBlRjQCiAGyx9C3dZZh5uIa3HU0oTV8v1E= +github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.8.0 h1:r6Za1Rii8+EGOYRDLvpooNOF6kP3iyDnkpzbw67gCQ8= +go.uber.org/zap v1.8.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8 h1:h7zdf0RiEvWbYBKIx4b+q41xoUVnMmvsGZnIVE5syG8= +golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20180712202826-d0887baf81f4 h1:KDF3PK6A+dkI7c4O8QbMtJqcXE3LdNJFGZECIlifQOg= +golang.org/x/net v0.0.0-20180712202826-d0887baf81f4/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +google.golang.org/genproto v0.0.0-20180716172848-2731d4fa720b h1:mXqBiicV0B+k8wzFNkKeNBRL7LyRV5xG0s+S6ffLb/E= +google.golang.org/genproto v0.0.0-20180716172848-2731d4fa720b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/grpc v1.13.0 h1:bHIbVsCwmvbArgCJmLdgOdHFXlKqTOVjbibbS19cXHc= +google.golang.org/grpc v1.13.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 h1:AFxeG48hTWHhDTQDk/m2gorfVHUEa9vo3tp3D7TzwjI= +gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/server/http_hooks.go b/server/http_hooks.go new file mode 100644 index 0000000..68c25c9 --- /dev/null +++ b/server/http_hooks.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "net/http" + + "github.com/mattermost/mattermost-server/plugin" +) + +// ServeHTTP allows the plugin to implement the http.Handler interface. Requests destined for the +// /plugins/{id} path will be routed to the plugin. +// +// The Mattermost-User-Id header will be present if (and only if) the request is by an +// authenticated user. +// +// This sample implementation sends back whether or not the plugin hooks are currently enabled. It +// is used by the web app to recover from a network reconnection and synchronize the state of the +// plugin's hooks. +func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) { + var response = struct { + Enabled bool `json:"enabled"` + }{ + Enabled: !p.disabled, + } + + responseJSON, _ := json.Marshal(response) + + w.Write(responseJSON) +} diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..69cc896 --- /dev/null +++ b/server/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/mattermost/mattermost-server/plugin" +) + +func main() { + plugin.ClientMain(&Plugin{}) +} diff --git a/server/message_hooks.go b/server/message_hooks.go new file mode 100644 index 0000000..2099b9c --- /dev/null +++ b/server/message_hooks.go @@ -0,0 +1,180 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/plugin" +) + +// MessageWillBePosted is invoked when a message is posted by a user before it is committed to the +// database. If you also want to act on edited posts, see MessageWillBeUpdated. Return values +// should be the modified post or nil if rejected and an explanation for the user. +// +// If you don't need to modify or reject posts, use MessageHasBeenPosted instead. +// +// Note that this method will be called for posts created by plugins, including the plugin that created the post. +// +// This sample implementation rejects posts in the sample channel, as well as posts that @-mention +// the sample plugin user. +func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { + if p.disabled { + return post, "" + } + + // Always allow posts by the sample plugin user. + if post.UserId == p.sampleUserId { + return post, "" + } + + // Reject posts by other users in the sample channels, effectively making it read-only. + for _, channelId := range p.sampleChannelIds { + if channelId == post.ChannelId { + p.API.SendEphemeralPost(post.UserId, &model.Post{ + UserId: p.sampleUserId, + ChannelId: channelId, + Message: "Posting is not allowed in this channel.", + }) + + return nil, "disallowing post in sample channel" + } + } + + // Reject posts mentioning the sample plugin user. + if strings.Contains(post.Message, fmt.Sprintf("@%s", p.Username)) { + p.API.SendEphemeralPost(post.UserId, &model.Post{ + UserId: p.sampleUserId, + ChannelId: post.ChannelId, + Message: "You must not talk about the sample plugin user.", + }) + + return nil, "disallowing mention of sample plugin user" + } + + // Otherwise, allow the post through. + return post, "" +} + +// MessageWillBeUpdated is invoked when a message is updated by a user before it is committed to +// the database. If you also want to act on new posts, see MessageWillBePosted. Return values +// should be the modified post or nil if rejected and an explanation for the user. On rejection, +// the post will be kept in its previous state. +// +// If you don't need to modify or rejected updated posts, use MessageHasBeenUpdated instead. +// +// Note that this method will be called for posts updated by plugins, including the plugin that +// updated the post. +// +// This sample implementation rejects posts that @-mention the sample plugin user. +func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, newPost, oldPost *model.Post) (*model.Post, string) { + if p.disabled { + return newPost, "" + } + + // Reject posts mentioning the sample plugin user. + if strings.Contains(newPost.Message, fmt.Sprintf("@%s", p.Username)) { + p.API.SendEphemeralPost(newPost.UserId, &model.Post{ + UserId: p.sampleUserId, + ChannelId: newPost.ChannelId, + Message: "You must not talk about the sample plugin user.", + }) + + return nil, "disallowing mention of sample plugin user" + } + + // Otherwise, allow the post through. + return newPost, "" +} + +// MessageHasBeenPosted is invoked after the message has been committed to the database. If you +// need to modify or reject the post, see MessageWillBePosted Note that this method will be called +// for posts created by plugins, including the plugin that created the post. +// +// This sample implementation logs a message to the sample channel whenever a message is posted, +// unless by the sample plugin user itself. +func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) { + if p.disabled { + return + } + + // Ignore posts by the sample plugin user. + if post.UserId == p.sampleUserId { + return + } + + user, err := p.API.GetUser(post.UserId) + if err != nil { + p.API.LogError("failed to query user", "user_id", post.UserId) + return + } + + channel, err := p.API.GetChannel(post.ChannelId) + if err != nil { + p.API.LogError("failed to query channel", "channel_id", post.ChannelId) + return + } + + if _, err := p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[channel.TeamId], + Message: fmt.Sprintf( + "MessageHasBeenPosted in ~%s by @%s", + channel.Name, + user.Username, + ), + }); err != nil { + p.API.LogError( + "failed to post MessageHasBeenPosted message", + "channel_id", channel.Id, + "user_id", user.Id, + "error", err.Error(), + ) + } +} + +// MessageHasBeenUpdated is invoked after a message is updated and has been updated in the +// database. If you need to modify or reject the post, see MessageWillBeUpdated Note that this +// method will be called for posts created by plugins, including the plugin that created the post. +// +// This sample implementation logs a message to the sample channel whenever a message is updated, +// unless by the sample plugin user itself. +func (p *Plugin) MessageHasBeenUpdated(c *plugin.Context, newPost, oldPost *model.Post) { + if p.disabled { + return + } + + // Ignore updates by the sample plugin user. + if newPost.UserId == p.sampleUserId { + return + } + + user, err := p.API.GetUser(newPost.UserId) + if err != nil { + p.API.LogError("failed to query user", "user_id", newPost.UserId) + return + } + + channel, err := p.API.GetChannel(newPost.ChannelId) + if err != nil { + p.API.LogError("failed to query channel", "channel_id", newPost.ChannelId) + return + } + + if _, err := p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[channel.TeamId], + Message: fmt.Sprintf( + "MessageHasBeenUpdated in ~%s by @%s", + channel.Name, + user.Username, + ), + }); err != nil { + p.API.LogError( + "failed to post MessageHasBeenUpdated message", + "channel_id", channel.Id, + "user_id", user.Id, + "error", err.Error(), + ) + } +} diff --git a/server/plugin.go b/server/plugin.go new file mode 100644 index 0000000..3181c8b --- /dev/null +++ b/server/plugin.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/mattermost/mattermost-server/plugin" +) + +type Plugin struct { + plugin.MattermostPlugin + + // The user to use as part of the sample plugin, created automatically if it does not exist. + Username string + + // The channel to use as part of the sample plugin, created for each team automatically if it does not exist. + ChannelName string + + // disabled tracks whether or not the plugin has been disabled after activation. It always starts enabled. + disabled bool + + // sampleUserId is the id of the user specified above. + sampleUserId string + + // sampleChannelIds maps team ids to the channels created for each using the channel name above. + sampleChannelIds map[string]string +} diff --git a/server/plugin_id.go b/server/plugin_id.go new file mode 100644 index 0000000..4a5e2e6 --- /dev/null +++ b/server/plugin_id.go @@ -0,0 +1,3 @@ +package main + +const PluginId = "com.mattermost.sample-plugin" diff --git a/server/team_hooks.go b/server/team_hooks.go new file mode 100644 index 0000000..3438f30 --- /dev/null +++ b/server/team_hooks.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/plugin" +) + +// UserHasJoinedTeam is invoked after the membership has been committed to the database. If +// actor is not nil, the user was added to the team by the actor. +// +// This sample implementation logs a message to the sample channel in the team whenever a user +// joins the team. +func (p *Plugin) UserHasJoinedTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) { + if p.disabled { + return + } + + user, err := p.API.GetUser(teamMember.UserId) + if err != nil { + p.API.LogError("failed to query user", "user_id", teamMember.UserId) + return + } + + if _, err = p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[teamMember.TeamId], + Message: fmt.Sprintf("UserHasJoinedTeam: @%s", user.Username), + }); err != nil { + p.API.LogError( + "failed to post UserHasJoinedTeam message", + "user_id", teamMember.UserId, + "error", err.Error(), + ) + } +} + +// UserHasLeftTeam is invoked after the membership has been removed from the database. If actor +// is not nil, the user was removed from the team by the actor. +// +// This sample implementation logs a message to the sample channel in the team whenever a user +// leaves the team. +func (p *Plugin) UserHasLeftTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) { + if p.disabled { + return + } + + user, err := p.API.GetUser(teamMember.UserId) + if err != nil { + p.API.LogError("failed to query user", "user_id", teamMember.UserId) + return + } + + if _, err = p.API.CreatePost(&model.Post{ + UserId: p.sampleUserId, + ChannelId: p.sampleChannelIds[teamMember.TeamId], + Message: fmt.Sprintf("UserHasLeftTeam: @%s", user.Username), + }); err != nil { + p.API.LogError( + "failed to post UserHasLeftTeam message", + "user_id", teamMember.UserId, + "error", err.Error(), + ) + } +} diff --git a/webapp/.eslintrc.json b/webapp/.eslintrc.json new file mode 100644 index 0000000..2c1e0b6 --- /dev/null +++ b/webapp/.eslintrc.json @@ -0,0 +1,636 @@ +{ + "extends": "eslint:recommended", + "parserOptions": { + "ecmaVersion": 8, + "sourceType": "module", + "ecmaFeatures": { + "jsx": true, + "impliedStrict": true, + "modules": true, + "experimentalObjectRestSpread": true + } + }, + "parser": "babel-eslint", + "plugins": [ + "react", + "import" + ], + "env": { + "browser": true, + "node": true, + "jquery": true, + "es6": true, + "jest": true + }, + "globals": { + "jest": true, + "describe": true, + "it": true, + "expect": true, + "before": true, + "after": true, + "beforeEach": true + }, + "settings": { + "import/resolver": "webpack" + }, + "rules": { + "array-bracket-spacing": [ + 2, + "never" + ], + "array-callback-return": 2, + "arrow-body-style": 0, + "arrow-parens": [ + 2, + "always" + ], + "arrow-spacing": [ + 2, + { + "before": true, + "after": true + } + ], + "block-scoped-var": 2, + "brace-style": [ + 2, + "1tbs", + { + "allowSingleLine": false + } + ], + "camelcase": [ + 2, + { + "properties": "never" + } + ], + "capitalized-comments": 0, + "class-methods-use-this": 0, + "comma-dangle": [ + 2, + "always-multiline" + ], + "comma-spacing": [ + 2, + { + "before": false, + "after": true + } + ], + "comma-style": [ + 2, + "last" + ], + "complexity": [ + 0, + 10 + ], + "computed-property-spacing": [ + 2, + "never" + ], + "consistent-return": 2, + "consistent-this": [ + 2, + "self" + ], + "constructor-super": 2, + "curly": [ + 2, + "all" + ], + "dot-location": [ + 2, + "object" + ], + "dot-notation": 2, + "eqeqeq": [ + 2, + "smart" + ], + "func-call-spacing": [ + 2, + "never" + ], + "func-name-matching": 0, + "func-names": 2, + "func-style": [ + 2, + "declaration", + { + "allowArrowFunctions": true + } + ], + "generator-star-spacing": [ + 2, + { + "before": false, + "after": true + } + ], + "global-require": 2, + "guard-for-in": 2, + "id-blacklist": 0, + "import/no-unresolved": 2, + "import/order": [ + "error", + { + "newlines-between": "always-and-inside-groups", + "groups": [ + "builtin", + "external", + [ + "internal", + "parent" + ], + "sibling", + "index" + ] + } + ], + "indent": [ + 2, + 4, + { + "SwitchCase": 0 + } + ], + "jsx-quotes": [ + 2, + "prefer-single" + ], + "key-spacing": [ + 2, + { + "beforeColon": false, + "afterColon": true, + "mode": "strict" + } + ], + "keyword-spacing": [ + 2, + { + "before": true, + "after": true, + "overrides": {} + } + ], + "line-comment-position": 0, + "linebreak-style": 2, + "lines-around-comment": [ + 2, + { + "beforeBlockComment": true, + "beforeLineComment": true, + "allowBlockStart": true, + "allowBlockEnd": true + } + ], + "max-lines": [ + 1, + { + "max": 450, + "skipBlankLines": true, + "skipComments": false + } + ], + "max-nested-callbacks": [ + 2, + { + "max": 2 + } + ], + "max-statements-per-line": [ + 2, + { + "max": 1 + } + ], + "multiline-ternary": [ + 1, + "never" + ], + "new-cap": 2, + "new-parens": 2, + "newline-before-return": 0, + "newline-per-chained-call": 0, + "no-alert": 2, + "no-array-constructor": 2, + "no-await-in-loop": 2, + "no-caller": 2, + "no-case-declarations": 2, + "no-class-assign": 2, + "no-compare-neg-zero": 2, + "no-cond-assign": [ + 2, + "except-parens" + ], + "no-confusing-arrow": 2, + "no-console": 2, + "no-const-assign": 2, + "no-constant-condition": 2, + "no-debugger": 2, + "no-div-regex": 2, + "no-dupe-args": 2, + "no-dupe-class-members": 2, + "no-dupe-keys": 2, + "no-duplicate-case": 2, + "no-duplicate-imports": [ + 2, + { + "includeExports": true + } + ], + "no-else-return": 2, + "no-empty": 2, + "no-empty-function": 2, + "no-empty-pattern": 2, + "no-eval": 2, + "no-ex-assign": 2, + "no-extend-native": 2, + "no-extra-bind": 2, + "no-extra-label": 2, + "no-extra-parens": 0, + "no-extra-semi": 2, + "no-fallthrough": 2, + "no-floating-decimal": 2, + "no-func-assign": 2, + "no-global-assign": 2, + "no-implicit-coercion": 2, + "no-implicit-globals": 0, + "no-implied-eval": 2, + "no-inner-declarations": 0, + "no-invalid-regexp": 2, + "no-irregular-whitespace": 2, + "no-iterator": 2, + "no-labels": 2, + "no-lone-blocks": 2, + "no-lonely-if": 2, + "no-loop-func": 2, + "no-magic-numbers": [ + 1, + { + "ignore": [ + -1, + 0, + 1, + 2 + ], + "enforceConst": true, + "detectObjects": true + } + ], + "no-mixed-operators": [ + 2, + { + "allowSamePrecedence": false + } + ], + "no-mixed-spaces-and-tabs": 2, + "no-multi-assign": 2, + "no-multi-spaces": [ + 2, + { + "exceptions": { + "Property": false + } + } + ], + "no-multi-str": 0, + "no-multiple-empty-lines": [ + 2, + { + "max": 1 + } + ], + "no-native-reassign": 2, + "no-negated-condition": 2, + "no-nested-ternary": 2, + "no-new": 2, + "no-new-func": 2, + "no-new-object": 2, + "no-new-symbol": 2, + "no-new-wrappers": 2, + "no-octal-escape": 2, + "no-param-reassign": 2, + "no-process-env": 2, + "no-process-exit": 2, + "no-proto": 2, + "no-redeclare": 2, + "no-return-assign": [ + 2, + "always" + ], + "no-return-await": 2, + "no-script-url": 2, + "no-self-assign": [ + 2, + { + "props": true + } + ], + "no-self-compare": 2, + "no-sequences": 2, + "no-shadow": [ + 2, + { + "hoist": "functions" + } + ], + "no-shadow-restricted-names": 2, + "no-spaced-func": 2, + "no-tabs": 0, + "no-template-curly-in-string": 2, + "no-ternary": 0, + "no-this-before-super": 2, + "no-throw-literal": 2, + "no-trailing-spaces": [ + 2, + { + "skipBlankLines": false + } + ], + "no-undef-init": 2, + "no-undefined": 2, + "no-underscore-dangle": 2, + "no-unexpected-multiline": 2, + "no-unmodified-loop-condition": 2, + "no-unneeded-ternary": [ + 2, + { + "defaultAssignment": false + } + ], + "no-unreachable": 2, + "no-unsafe-finally": 2, + "no-unsafe-negation": 2, + "no-unused-expressions": 2, + "no-unused-vars": [ + 2, + { + "vars": "all", + "args": "after-used" + } + ], + "no-use-before-define": [ + 2, + { + "classes": false, + "functions": false, + "variables": false + } + ], + "no-useless-computed-key": 2, + "no-useless-concat": 2, + "no-useless-constructor": 2, + "no-useless-escape": 2, + "no-useless-rename": 2, + "no-useless-return": 2, + "no-var": 0, + "no-void": 2, + "no-warning-comments": 1, + "no-whitespace-before-property": 2, + "no-with": 2, + "object-curly-newline": 0, + "object-curly-spacing": [ + 2, + "never" + ], + "object-property-newline": [ + 2, + { + "allowMultiplePropertiesPerLine": true + } + ], + "object-shorthand": [ + 2, + "always" + ], + "one-var": [ + 2, + "never" + ], + "one-var-declaration-per-line": 0, + "operator-assignment": [ + 2, + "always" + ], + "operator-linebreak": [ + 2, + "after" + ], + "padded-blocks": [ + 2, + "never" + ], + "prefer-arrow-callback": 2, + "prefer-const": 2, + "prefer-destructuring": 0, + "prefer-numeric-literals": 2, + "prefer-promise-reject-errors": 2, + "prefer-rest-params": 2, + "prefer-spread": 2, + "prefer-template": 0, + "quote-props": [ + 2, + "as-needed" + ], + "quotes": [ + 2, + "single", + "avoid-escape" + ], + "radix": 2, + "react/display-name": [ + 0, + { + "ignoreTranspilerName": false + } + ], + "react/forbid-component-props": 0, + "react/forbid-elements": [ + 2, + { + "forbid": [ + "embed" + ] + } + ], + "react/jsx-boolean-value": [ + 2, + "always" + ], + "react/jsx-closing-bracket-location": [ + 2, + { + "location": "tag-aligned" + } + ], + "react/jsx-curly-spacing": [ + 2, + "never" + ], + "react/jsx-equals-spacing": [ + 2, + "never" + ], + "react/jsx-filename-extension": 2, + "react/jsx-first-prop-new-line": [ + 2, + "multiline" + ], + "react/jsx-handler-names": 0, + "react/jsx-indent": [ + 2, + 4 + ], + "react/jsx-indent-props": [ + 2, + 4 + ], + "react/jsx-key": 2, + "react/jsx-max-props-per-line": [ + 2, + { + "maximum": 1 + } + ], + "react/jsx-no-bind": 0, + "react/jsx-no-comment-textnodes": 2, + "react/jsx-no-duplicate-props": [ + 2, + { + "ignoreCase": false + } + ], + "react/jsx-no-literals": 2, + "react/jsx-no-target-blank": 2, + "react/jsx-no-undef": 2, + "react/jsx-pascal-case": 2, + "react/jsx-tag-spacing": [ + 2, + { + "closingSlash": "never", + "beforeSelfClosing": "never", + "afterOpening": "never" + } + ], + "react/jsx-uses-react": 2, + "react/jsx-uses-vars": 2, + "react/jsx-wrap-multilines": 2, + "react/no-array-index-key": 1, + "react/no-children-prop": 2, + "react/no-danger": 0, + "react/no-danger-with-children": 2, + "react/no-deprecated": 1, + "react/no-did-mount-set-state": 2, + "react/no-did-update-set-state": 2, + "react/no-direct-mutation-state": 2, + "react/no-find-dom-node": 1, + "react/no-is-mounted": 2, + "react/no-multi-comp": [ + 2, + { + "ignoreStateless": true + } + ], + "react/no-render-return-value": 2, + "react/no-set-state": 0, + "react/no-string-refs": 0, + "react/no-unescaped-entities": 2, + "react/no-unknown-property": 2, + "react/no-unused-prop-types": [ + 1, + { + "skipShapeProps": true + } + ], + "react/prefer-es6-class": 2, + "react/prefer-stateless-function": 0, + "react/prop-types": [ + 2, + { + "ignore": [ + "location", + "history", + "component" + ] + } + ], + "react/require-default-props": 0, + "react/require-optimization": 1, + "react/require-render-return": 2, + "react/self-closing-comp": 2, + "react/sort-comp": 0, + "react/style-prop-object": 2, + "require-yield": 2, + "rest-spread-spacing": [ + 2, + "never" + ], + "semi": [ + 2, + "always" + ], + "semi-spacing": [ + 2, + { + "before": false, + "after": true + } + ], + "sort-imports": 0, + "sort-keys": 0, + "space-before-blocks": [ + 2, + "always" + ], + "space-before-function-paren": [ + 2, + { + "anonymous": "never", + "named": "never", + "asyncArrow": "always" + } + ], + "space-in-parens": [ + 2, + "never" + ], + "space-infix-ops": 2, + "space-unary-ops": [ + 2, + { + "words": true, + "nonwords": false + } + ], + "symbol-description": 2, + "template-curly-spacing": [ + 2, + "never" + ], + "valid-typeof": [ + 2, + { + "requireStringLiterals": false + } + ], + "vars-on-top": 0, + "wrap-iife": [ + 2, + "outside" + ], + "wrap-regex": 2, + "yoda": [ + 2, + "never", + { + "exceptRange": false, + "onlyEquality": false + } + ] + } +} diff --git a/webapp/.gitignore b/webapp/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/webapp/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/webapp/README.md b/webapp/README.md new file mode 100644 index 0000000..2a82977 --- /dev/null +++ b/webapp/README.md @@ -0,0 +1,105 @@ +# Sample Plugin: Web App + +The web app component of this sample plugin is written in Javascript, and leverages [React](https://reactjs.org/) and [Redux](https://redux.js.org/). It registers a component type for all of the supported registration calls, parses a custom webhook to detect when the server plugin's hooks status changes, and pings the server on network reconnect to synchronize state. + +Each of the included files or folders is outlined below. + +## [package.json](package.json) + +See the NPM documentation on [package.json](https://docs.npmjs.com/files/package.json). It defines a `build` script to invoke webpack and generate a bundle, a `lint` script to run the `src/` directory through the [eslint](https://eslint.org/) checker, and a `fix` script that both lints and automatically tries to fix issues. + +## [package-lock.json](package-lock.json) + +See the NPM documentation on [package-lock.json](https://docs.npmjs.com/files/package-lock.json). + +## [webpack.config.js](webpack.config.js) + +See the Webpack documentation on [configuration](https://webpack.js.org/configuration/). Notably, this configuration specifies external dependencies on React, Redux and React Redux to avoid bundling these libraries and duplicating the versions already part of the Mattermost Web App. + +## [.eslintrc.json](.eslintrc.json) + +This defines rules to configure [eslint](https://eslint.org/) as part of invoking the `lint` and `fix` scripts. The styles are based on the rules used by the Mattermost Webapp. + +## [node\_modules](node_modules) + +This is the [location](https://docs.npmjs.com/files/folders#node-modules) in which [npm](https://www.npmjs.com/) installs any necessary Javascript dependencies. + +## [src/index.js](src/index.js) + +This is the entry point of the web app. When the plugin is loaded, this file is executed, registering the plugin with the Mattermost Webapp. + +## [src/plugin\_id.js](src/plugin_id.js) + +This is a file generated by the [build/manifest](../build/manifest) tool that captures the plugin id from [plugin.json](../plugin.json). It simplifies the need to hard-code the plugin id in multiple places by exporting a constant for use instead. + +## [src/plugin.jsx](src/plugin.jsx) + +This defines the Plugin class requires by the Mattermost Webapp, registering all the components and callbacks used by the plugin on `initialize` and logging a console message on `uninitialize`. + +## [src/reducer.js](src/reducer.js) + +This exports a [reducer](https://redux.js.org/basics/reducers) tracking the plugin hook's status. It is part of the global state of the Mattermost Webapp, and accessible at `store['plugins' + PluginId]`. + +## [src/selectors.js](src/selectors.js) + +This defines selectors into the Redux state managed by the plugin to determine if the plugin is enabled or disabled. + +## [src/action\_types.js](src/action_types.js) + +This exports constants used by the Redux [actions](https://redux.js.org/basics/actions) in [action\_types.js](src/action_types.js). It's important to namespace any action types to avoid unintentional collisions with action types from the Mattermost Webapp or other plugins. + +## [src/actions.js](src/actions.js) + +This exports Redux [actions](https://redux.js.org/basics/actions) for triggering the root component, as well as querying the server for the current plugin hooks status and responding to websocket events emitted by the server for the plugin. + +## [components](components) + +This folder exports a number of components illustrating plugin functionality. + +## Root + +This plugin registers a modal-like root component that displays above all other components, and is triggered by interacting with other plugin components on the page: + +![root](docs/root.png) + +## User Attributes + +This plugin registers a user attributes components displaying a static string: + +![user attributes](docs/user_attributes.png) + +## User Actions + +This plugin registers a user actions components displaying a static string followed by a simple ` + + ); + } +} + +const getStyle = (theme) => ({ + button: { + color: theme.buttonColor, + backgroundColor: theme.buttonBg, + }, +}); diff --git a/webapp/src/components/user_attributes.jsx b/webapp/src/components/user_attributes.jsx new file mode 100644 index 0000000..deaeadb --- /dev/null +++ b/webapp/src/components/user_attributes.jsx @@ -0,0 +1,10 @@ +import React from 'react'; + +export default class UserAttributes extends React.PureComponent { + render() { + return ( +
{'Sample Plugin: User Attributes'}
+ ); + } +} + diff --git a/webapp/src/index.js b/webapp/src/index.js new file mode 100644 index 0000000..bc55496 --- /dev/null +++ b/webapp/src/index.js @@ -0,0 +1,4 @@ +import Plugin from './plugin'; +import PluginId from './plugin_id'; + +window.registerPlugin(PluginId, new Plugin()); diff --git a/webapp/src/plugin.jsx b/webapp/src/plugin.jsx new file mode 100644 index 0000000..75ab81f --- /dev/null +++ b/webapp/src/plugin.jsx @@ -0,0 +1,69 @@ +import React from 'react'; + +import PluginId from './plugin_id'; + +import Root from './components/root'; +import BottomTeamSidebar from './components/bottom_team_sidebar'; +import LeftSidebarHeader from './components/left_sidebar_header'; +import UserAttributes from './components/user_attributes'; +import UserActions from './components/user_actions'; +import PostType from './components/post_type'; +import { + MainMenuMobileIcon, + ChannelHeaderButtonIcon, +} from './components/icons'; +import { + mainMenuAction, + channelHeaderButtonAction, + websocketStatusChange, + getStatus, +} from './actions'; +import reducer from './reducer'; + +export default class SamplePlugin { + initialize(registry, store) { + registry.registerRootComponent(Root); + registry.registerPopoverUserAttributesComponent(UserAttributes); + registry.registerPopoverUserActionsComponent(UserActions); + registry.registerLeftSidebarHeaderComponent(LeftSidebarHeader); + registry.registerBottomTeamSidebarComponent( + BottomTeamSidebar, + ); + + registry.registerChannelHeaderButtonAction( + , + () => store.dispatch(channelHeaderButtonAction()), + 'Sample Plugin', + ); + + registry.registerPostTypeComponent('custom_sample_plugin', PostType); + + registry.registerMainMenuAction( + 'Sample Plugin', + () => store.dispatch(mainMenuAction()), + , + ); + + registry.registerWebSocketEventHandler( + 'custom_' + PluginId + '_status_change', + (message) => { + store.dispatch(websocketStatusChange(message)); + }, + ); + + registry.registerReducer(reducer); + + // Immediately fetch the current plugin status. + store.dispatch(getStatus()); + + // Fetch the current status whenever we recover an internet connection. + registry.registerReconnectHandler(() => { + store.dispatch(getStatus()); + }); + } + + uninitialize() { + //eslint-disable-next-line no-console + console.log(PluginId + '::uninitialize()'); + } +} diff --git a/webapp/src/plugin_id.js b/webapp/src/plugin_id.js new file mode 100644 index 0000000..a56bbb4 --- /dev/null +++ b/webapp/src/plugin_id.js @@ -0,0 +1 @@ +export default 'com.mattermost.sample-plugin'; \ No newline at end of file diff --git a/webapp/src/reducer.js b/webapp/src/reducer.js new file mode 100644 index 0000000..27bb7fb --- /dev/null +++ b/webapp/src/reducer.js @@ -0,0 +1,30 @@ +import {combineReducers} from 'redux'; + +import {STATUS_CHANGE, OPEN_ROOT_MODAL, CLOSE_ROOT_MODAL} from './action_types'; + +const enabled = (state = false, action) => { + switch (action.type) { + case STATUS_CHANGE: + return action.data; + + default: + return state; + } +}; + +const rootModalVisible = (state = false, action) => { + switch (action.type) { + case OPEN_ROOT_MODAL: + return true; + case CLOSE_ROOT_MODAL: + return false; + default: + return state; + } +}; + +export default combineReducers({ + enabled, + rootModalVisible, +}); + diff --git a/webapp/src/selectors.js b/webapp/src/selectors.js new file mode 100644 index 0000000..0b9e9dd --- /dev/null +++ b/webapp/src/selectors.js @@ -0,0 +1,7 @@ +import PluginId from './plugin_id'; + +const getPluginState = (state) => state['plugins-' + PluginId] || {}; + +export const isEnabled = (state) => getPluginState(state).enabled; + +export const isRootModalVisible = (state) => getPluginState(state).rootModalVisible; diff --git a/webapp/webpack.config.js b/webapp/webpack.config.js new file mode 100644 index 0000000..b72b0fd --- /dev/null +++ b/webapp/webpack.config.js @@ -0,0 +1,42 @@ +var path = require('path'); + +module.exports = { + entry: [ + './src/index.js', + ], + resolve: { + modules: [ + 'src', + 'node_modules', + ], + extensions: ['*', '.js', '.jsx'], + }, + module: { + rules: [ + { + test: /\.(js|jsx)$/, + exclude: /node_modules/, + use: { + loader: 'babel-loader', + options: { + presets: ['env', 'react'], + plugins: [ + 'transform-class-properties', + 'transform-object-rest-spread', + ], + }, + }, + }, + ], + }, + externals: { + react: 'react', + redux: 'redux', + 'react-redux': 'reactRedux', + }, + output: { + path: path.join(__dirname, '/dist'), + publicPath: '/', + filename: 'main.js', + }, +}; From bc26a8c5d5ca44c1c27494a150946c986fdc5058 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Mon, 23 Jul 2018 13:49:44 -0400 Subject: [PATCH 2/8] tweak webpack externals --- webapp/webpack.config.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webapp/webpack.config.js b/webapp/webpack.config.js index b72b0fd..6b3332f 100644 --- a/webapp/webpack.config.js +++ b/webapp/webpack.config.js @@ -30,9 +30,9 @@ module.exports = { ], }, externals: { - react: 'react', - redux: 'redux', - 'react-redux': 'reactRedux', + react: 'React', + redux: 'Redux', + 'react-redux': 'ReactRedux', }, output: { path: path.join(__dirname, '/dist'), From 33eb1385d235bb8b674c02a1c1accc8ed89c0e9d Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Mon, 23 Jul 2018 14:07:42 -0400 Subject: [PATCH 3/8] add webapp/.npminstall target to speed up rebuilds --- Makefile | 11 +++++++++-- webapp/.gitignore | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5f9b120..3551830 100644 --- a/Makefile +++ b/Makefile @@ -40,11 +40,17 @@ ifneq ($(HAS_SERVER),) cd server && env GOOS=windows GOARCH=amd64 $(GO) build -o dist/plugin-windows-amd64.exe; endif +# webapp/.npminstall ensures NPM dependencies are installed without having to run this all the time +webapp/.npminstall: +ifneq ($(HAS_WEBAPP),) + cd webapp && npm install + touch $@ +endif + # webapp builds the webapp, if it exists .PHONY: webapp -webapp: +webapp: webapp/.npminstall ifneq ($(HAS_WEBAPP),) - cd webapp && npm install; cd webapp && npm run fix; cd webapp && npm run build; endif @@ -100,6 +106,7 @@ endif clean: rm -fr dist/ rm -fr server/dist + rm -fr webapp/.npminstall rm -fr webapp/dist rm -fr webapp/node_modules rm -fr build/bin/ diff --git a/webapp/.gitignore b/webapp/.gitignore index 3c3629e..3c36a8f 100644 --- a/webapp/.gitignore +++ b/webapp/.gitignore @@ -1 +1,2 @@ node_modules +.npminstall From 390fab485d85b5148d891ca33ae04011683dc3a6 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Mon, 23 Jul 2018 14:50:12 -0400 Subject: [PATCH 4/8] access window.PostUtils --- webapp/src/components/post_type.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/src/components/post_type.jsx b/webapp/src/components/post_type.jsx index 41b7573..7d778fc 100644 --- a/webapp/src/components/post_type.jsx +++ b/webapp/src/components/post_type.jsx @@ -1,7 +1,7 @@ import React from 'react'; import PropTypes from 'prop-types'; -const {formatText, messageHtmlToComponent} = window['post-utils']; +const {formatText, messageHtmlToComponent} = window.PostUtils; export default class PostType extends React.PureComponent { static propTypes = { From 7641f7cf17fe9e729e013be71f3ffd7e7af5aaf1 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Tue, 24 Jul 2018 15:45:28 -0400 Subject: [PATCH 5/8] update README.md, description to reflect sample nature of repository --- README.md | 9 +-------- plugin.json | 18 ++---------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 84b8622..5620d97 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # Sample Plugin -This plugin serves as a reference guide for best practices, build scripts and samples when writing Mattermost plugins. It also doubles as a testbed for verifying plugin functionality during release testing. See [server/README.md](server/README.md) and [webapp/README.md](webapp/README.md) for more details. - -The example implementations are primarily meant as illustrations to assist with developing your plugin. Feel free to base your own plugin off this repository, removing or modifying components as needed. +This plugin serves as a starting point for writing a Mattermost plugin. Feel free to base your own plugin off this repository. ## Getting Started Shallow clone the repository to a directory matching your plugin name: @@ -47,8 +45,3 @@ In production, deploy and upload your plugin via the [System Console](https://ab ### How do I make a server-only or web app-only plugin? Simply delete the `server` or `webapp` folders and remove the corresponding sections from `plugin.json`. The build scripts will skip the missing portions automatically. - -### How do I remove unwanted hooks from the server? - -Simply delete the corresponding implementations (or files). The Mattermost server automatically -identifies which hooks have been implemented when the plugin is started. diff --git a/plugin.json b/plugin.json index 40199cd..d4790b5 100644 --- a/plugin.json +++ b/plugin.json @@ -1,7 +1,7 @@ { "id": "com.mattermost.sample-plugin", "name": "Sample Plugin", - "description": "This plugin serves as a reference guide for best practices and build scripts when writing Mattermost plugins.", + "description": "This plugin serves as a starting point for writing a Mattermost plugin.", "version": "0.0.1", "server": { "executables": { @@ -17,20 +17,6 @@ "settings_schema": { "header": "", "footer": "", - "settings": [{ - "key": "ChannelName", - "display_name": "Channel Name", - "type": "text", - "help_text": "The channel to use as part of the sample plugin, created for each team automatically if it does not exist.", - "placeholder": "sample_plugin", - "default": "sample_plugin" - }, { - "key": "Username", - "display_name": "Username", - "type": "text", - "help_text": "The user to use as part of the sample plugin, created automatically if it does not exist.", - "placeholder": "sample_plugin", - "default": "sample_plugin" - }] + "settings": [] } } From daaf7822ea70eb46fb796ea1dc719de2369c1c1e Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Tue, 24 Jul 2018 16:02:46 -0400 Subject: [PATCH 6/8] strip out sample bits (moved to demo repository) --- plugin.json | 3 +- server/README.md | 100 ---------- server/activate_hooks.go | 70 ------- server/channel_hooks.go | 98 ---------- server/command_hooks.go | 88 --------- server/configuration.go | 112 ----------- server/http_hooks.go | 29 --- server/message_hooks.go | 180 ------------------ server/plugin.go | 17 +- server/team_hooks.go | 66 ------- webapp/README.md | 105 ---------- webapp/docs/bottom_team_sidebar.png | Bin 13044 -> 0 bytes webapp/docs/channel_header_button.png | Bin 15452 -> 0 bytes webapp/docs/left_sidebar_header.png | Bin 23726 -> 0 bytes webapp/docs/main_menu.png | Bin 65997 -> 0 bytes webapp/docs/post_type.png | Bin 31941 -> 0 bytes webapp/docs/root.png | Bin 38564 -> 0 bytes webapp/docs/user_actions.png | Bin 50586 -> 0 bytes webapp/docs/user_attributes.png | Bin 50586 -> 0 bytes webapp/package.json | 4 +- webapp/src/action_types.js | 7 - webapp/src/actions.js | 31 --- webapp/src/components/bottom_team_sidebar.jsx | 10 - webapp/src/components/icons.jsx | 9 - .../components/left_sidebar_header/index.js | 11 -- .../left_sidebar_header.jsx | 32 ---- webapp/src/components/post_type.jsx | 36 ---- webapp/src/components/root/index.js | 17 -- webapp/src/components/root/root.jsx | 54 ------ webapp/src/components/user_actions/index.js | 12 -- .../components/user_actions/user_actions.jsx | 36 ---- webapp/src/components/user_attributes.jsx | 10 - webapp/src/index.js | 8 +- webapp/src/plugin.jsx | 69 ------- webapp/src/reducer.js | 30 --- webapp/src/selectors.js | 7 - 36 files changed, 12 insertions(+), 1239 deletions(-) delete mode 100644 server/README.md delete mode 100644 server/activate_hooks.go delete mode 100644 server/channel_hooks.go delete mode 100644 server/command_hooks.go delete mode 100644 server/configuration.go delete mode 100644 server/http_hooks.go delete mode 100644 server/message_hooks.go delete mode 100644 server/team_hooks.go delete mode 100644 webapp/README.md delete mode 100644 webapp/docs/bottom_team_sidebar.png delete mode 100644 webapp/docs/channel_header_button.png delete mode 100644 webapp/docs/left_sidebar_header.png delete mode 100644 webapp/docs/main_menu.png delete mode 100644 webapp/docs/post_type.png delete mode 100644 webapp/docs/root.png delete mode 100644 webapp/docs/user_actions.png delete mode 100644 webapp/docs/user_attributes.png delete mode 100644 webapp/src/action_types.js delete mode 100644 webapp/src/actions.js delete mode 100644 webapp/src/components/bottom_team_sidebar.jsx delete mode 100644 webapp/src/components/icons.jsx delete mode 100644 webapp/src/components/left_sidebar_header/index.js delete mode 100644 webapp/src/components/left_sidebar_header/left_sidebar_header.jsx delete mode 100644 webapp/src/components/post_type.jsx delete mode 100644 webapp/src/components/root/index.js delete mode 100644 webapp/src/components/root/root.jsx delete mode 100644 webapp/src/components/user_actions/index.js delete mode 100644 webapp/src/components/user_actions/user_actions.jsx delete mode 100644 webapp/src/components/user_attributes.jsx delete mode 100644 webapp/src/plugin.jsx delete mode 100644 webapp/src/reducer.js delete mode 100644 webapp/src/selectors.js diff --git a/plugin.json b/plugin.json index d4790b5..270d606 100644 --- a/plugin.json +++ b/plugin.json @@ -8,8 +8,7 @@ "linux-amd64": "server/dist/plugin-linux-amd64", "darwin-amd64": "server/dist/plugin-darwin-amd64", "windows-amd64": "server/dist/plugin-windows-amd64.exe" - }, - "executable": "" + } }, "webapp": { "bundle_path": "webapp/dist/main.js" diff --git a/server/README.md b/server/README.md deleted file mode 100644 index bd135b1..0000000 --- a/server/README.md +++ /dev/null @@ -1,100 +0,0 @@ -# Sample Plugin: Server - -The server component of this sample plugin is written in Go and [net/rpc](https://golang.org/pkg/net/rpc/). It relies on a configured `ChannelName` and `Username` in [plugin.json](../plugin.json) to implement each of the supported hooks. - -Each of the included files or folders is outlined below. - -## [go.mod](go.mod), [go.sum](go.sum) - -These are metadata files managed by [vgo](https://github.com/golang/vgo) for dependency management. While vgo is currently in beta, it will launch as part of the standard Go 1.11 tooling and stabilize in subsequent releases. It was preferred for this project over [dep](https://github.com/golang/dep) since it does not require locating your plugin in the `$GOPATH`. - -## [main.go](main.go) - -This is the entry point of your plugin binary, that in turn invokes [plugin.ClientMain](https://godoc.org/github.com/mattermost/mattermost-server/plugin#ClientMain) to wire up RPC communication between your plugin and the Mattermost Server. - -## [plugin\_id.go](plugin_id.go) - -This is a file generated by the [build/manifest](../build/manifest) tool that captures the plugin id from [plugin.json](../plugin.json). It simplifies the need to hard-code the plugin id in multiple places by exporting a constant for use instead. - -## [plugin.go](plugin.go) - -This file defines the `Plugin` struct, embedding [plugin.MattermostPlugin](https://godoc.org/github.com/mattermost/mattermost-server/plugin#MattermostPlugin) to automatically handle the wiring up the [API](https://godoc.org/github.com/mattermost/mattermost-server/plugin#API) when the plugin starts. It contains public fields that are automatically unmarshalled from [plugin.json](../plugin.json) as part of the `OnConfiguration` hook in [configuration.go](configuration.go). - -## [activate\_hooks.go](activate_hooks.go) - -### OnActivate - -This sample implementation logs a message to the sample channel whenever the plugin is activated. - -### OnDeactivate - -This sample implementation logs a debug message to the server logs whenever the plugin is activated. - -## [configuration.go](configuration.go) - -### OnConfigurationChange - -This sample implementation ensures the configured sample user and channel are created for use -by the plugin. - -## [channel\_hooks.go](channel_hooks.go) - -### ChannelHasBeenCreated - -This sample implementation logs a message to the sample channel whenever a channel is created. - -### UserHasJoinedChannel - -This sample implementation logs a message to the sample channel whenever a user joins a channel. - -### UserHasLeftChannel - -This sample implementation logs a message to the sample channel whenever a user leaves a channel. - -## [command\_hooks.go](command_hooks.go) - -### ExecuteCommand - -This sample implementation responds to a `/sample_plugin` command, allowing the user to enable -or disable the sample plugin's hooks functionality (but leave the command and webapp enabled). - -## [http\_hooks.go](http_hooks.go) - -### ServeHTTP - -This sample implementation sends back whether or not the plugin hooks are currently enabled. It -is used by the web app to recover from a network reconnection and synchronize the state of the -plugin's hooks. - -## [message\_hooks.go](message_hooks.go) - -### MessageWillBePosted - -This sample implementation rejects posts in the sample channel, as well as posts that @-mention -the sample plugin user. - -### MessageWillBeUpdated - -This sample implementation rejects posts that @-mention the sample plugin user. - -### MessageHasBeenPosted - -This sample implementation logs a message to the sample channel whenever a message is posted, -unless by the sample plugin user itself. - -### MessageHasBeenUpdated - -This sample implementation logs a message to the sample channel whenever a message is updated, -unless by the sample plugin user itself. - -## [team\_hooks.go](team.go) - -### UserHasJoinedTeam - -This sample implementation logs a message to the sample channel in the team whenever a user -joins the team. - -### UserHasLeftTeam - -This sample implementation logs a message to the sample channel in the team whenever a user -leaves the team. diff --git a/server/activate_hooks.go b/server/activate_hooks.go deleted file mode 100644 index 45befdf..0000000 --- a/server/activate_hooks.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/mattermost/mattermost-server/model" -) - -// OnActivate is invoked when the plugin is activated. -// -// This sample implementation logs a message to the sample channel whenever the plugin is -// activated. -func (p *Plugin) OnActivate() error { - // It's necessary to do this asynchronously, so as to avoid CreatePost triggering a call - // to MessageWillBePosted and deadlocking the plugin. - // - // See https://mattermost.atlassian.net/browse/MM-11431 - go func() { - teams, err := p.API.GetTeams() - if err != nil { - p.API.LogError( - "failed to query teams OnActivate", - "error", err.Error(), - ) - } - - for _, team := range teams { - if _, err := p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[team.Id], - Message: fmt.Sprintf( - "OnActivate: %s", PluginId, - ), - Type: "custom_sample_plugin", - Props: map[string]interface{}{ - "username": p.Username, - "channel_name": p.ChannelName, - }, - }); err != nil { - p.API.LogError( - "failed to post OnActivate message", - "error", err.Error(), - ) - } - - if err := p.registerCommand(team.Id); err != nil { - p.API.LogError( - "failed to register command", - "error", err.Error(), - ) - } - } - - }() - - return nil -} - -// OnDeactivate is invoked when the plugin is deactivated. This is the plugin's last chance to use -// the API, and the plugin will be terminated shortly after this invocation. -// -// This sample implementation logs a debug message to the server logs whenever the plugin is -// activated. -func (p *Plugin) OnDeactivate() error { - // Ideally, we'd post an on deactivate message like in OnActivate, but this is hampered by - // https://mattermost.atlassian.net/browse/MM-11431?filter=15018 - p.API.LogDebug("OnDeactivate") - - return nil -} diff --git a/server/channel_hooks.go b/server/channel_hooks.go deleted file mode 100644 index b1ee3f2..0000000 --- a/server/channel_hooks.go +++ /dev/null @@ -1,98 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/mattermost/mattermost-server/model" - "github.com/mattermost/mattermost-server/plugin" -) - -// ChannelHasBeenCreated is invoked after the channel has been committed to the database. -// -// This sample implementation logs a message to the sample channel whenever a channel is created. -func (p *Plugin) ChannelHasBeenCreated(c *plugin.Context, channel *model.Channel) { - if p.disabled { - return - } - - if _, err := p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[channel.TeamId], - Message: fmt.Sprintf("ChannelHasBeenCreated: ~%s", channel.Name), - }); err != nil { - p.API.LogError( - "failed to post ChannelHasBeenCreated message", - "channel_id", channel.Id, - "error", err.Error(), - ) - } -} - -// UserHasJoinedChannel is invoked after the membership has been committed to the database. If -// actor is not nil, the user was invited to the channel by the actor. -// -// This sample implementation logs a message to the sample channel whenever a user joins a channel. -func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) { - if p.disabled { - return - } - - user, err := p.API.GetUser(channelMember.UserId) - if err != nil { - p.API.LogError("failed to query user", "user_id", channelMember.UserId) - return - } - - channel, err := p.API.GetChannel(channelMember.ChannelId) - if err != nil { - p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId) - return - } - - if _, err = p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[channel.TeamId], - Message: fmt.Sprintf("UserHasJoinedChannel: @%s, ~%s", user.Username, channel.Name), - }); err != nil { - p.API.LogError( - "failed to post UserHasJoinedChannel message", - "user_id", channelMember.UserId, - "error", err.Error(), - ) - } -} - -// UserHasLeftChannel is invoked after the membership has been removed from the database. If -// actor is not nil, the user was removed from the channel by the actor. -// -// This sample implementation logs a message to the sample channel whenever a user leaves a -// channel. -func (p *Plugin) UserHasLeftChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) { - if p.disabled { - return - } - - user, err := p.API.GetUser(channelMember.UserId) - if err != nil { - p.API.LogError("failed to query user", "user_id", channelMember.UserId) - return - } - - channel, err := p.API.GetChannel(channelMember.ChannelId) - if err != nil { - p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId) - return - } - - if _, err = p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[channel.TeamId], - Message: fmt.Sprintf("UserHasLeftChannel: @%s, ~%s", user.Username, channel.Name), - }); err != nil { - p.API.LogError( - "failed to post UserHasLeftChannel message", - "user_id", channelMember.UserId, - "error", err.Error(), - ) - } -} diff --git a/server/command_hooks.go b/server/command_hooks.go deleted file mode 100644 index 814395e..0000000 --- a/server/command_hooks.go +++ /dev/null @@ -1,88 +0,0 @@ -package main - -import ( - "fmt" - "strings" - - "github.com/mattermost/mattermost-server/model" - "github.com/mattermost/mattermost-server/plugin" -) - -const CommandTrigger = "sample_plugin" - -func (p *Plugin) registerCommand(teamId string) error { - if err := p.API.RegisterCommand(&model.Command{ - TeamId: teamId, - Trigger: CommandTrigger, - AutoComplete: true, - AutoCompleteHint: "(true|false)", - AutoCompleteDesc: "Enables or disables the sample plugin hooks.", - DisplayName: "Sample Plugin Command", - Description: "A command used to enable or disable the sample plugin hooks.", - }); err != nil { - p.API.LogError( - "failed to register command", - "error", err.Error(), - ) - } - - return nil -} - -func (p *Plugin) emitStatusChange() { - p.API.PublishWebSocketEvent("status_change", map[string]interface{}{ - "enabled": !p.disabled, - }, &model.WebsocketBroadcast{}) -} - -// ExecuteCommand executes a command that has been previously registered via the RegisterCommand -// API. -// -// This sample implementation responds to a /sample_plugin command, allowing the user to enable -// or disable the sample plugin's hooks functionality (but leave the command and webapp enabled). -func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { - if !strings.HasPrefix(args.Command, "/"+CommandTrigger) { - return &model.CommandResponse{ - ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, - Text: fmt.Sprintf("Unknown command: " + args.Command), - }, nil - } - - if strings.HasSuffix(args.Command, "true") { - if !p.disabled { - return &model.CommandResponse{ - ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, - Text: "The sample plugin hooks are already enabled.", - }, nil - } - - p.disabled = false - p.emitStatusChange() - - return &model.CommandResponse{ - ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, - Text: "Enabled sample plugin hooks.", - }, nil - - } else if strings.HasSuffix(args.Command, "false") { - if p.disabled { - return &model.CommandResponse{ - ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, - Text: "The sample plugin hooks are already disabled.", - }, nil - } - - p.disabled = true - p.emitStatusChange() - - return &model.CommandResponse{ - ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, - Text: "Disabled sample plugin hooks.", - }, nil - } - - return &model.CommandResponse{ - ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, - Text: fmt.Sprintf("Unknown command action: " + args.Command), - }, nil -} diff --git a/server/configuration.go b/server/configuration.go deleted file mode 100644 index e7fbd8b..0000000 --- a/server/configuration.go +++ /dev/null @@ -1,112 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/mattermost/mattermost-server/model" -) - -// OnConfigurationChange is invoked when configuration changes may have been made. -// -// This sample implementation ensures the configured sample user and channel are created for use -// by the plugin. -func (p *Plugin) OnConfigurationChange() error { - // Leverage the default implementation on the embedded plugin.Mattermost. This - // automatically attempts to unmarshal the plugin config block of the server's - // configuration onto the public members of Plugin, such as Username and ChannelName. - // - // Feel free to skip this and implement your own handler if you have more complex needs. - if err := p.MattermostPlugin.OnConfigurationChange(); err != nil { - p.API.LogError(err.Error()) - return err - } - - if err := p.ensureSampleUser(); err != nil { - p.API.LogError(err.Error()) - return err - } - - if err := p.ensureSampleChannels(); err != nil { - p.API.LogError(err.Error()) - return err - } - - return nil -} - -func (p *Plugin) ensureSampleUser() *model.AppError { - var err *model.AppError - - // Check for the configured user. Ignore any error, since it's hard to distinguish runtime - // errors from a user simply not existing. - user, _ := p.API.GetUserByUsername(p.Username) - - // Ensure the configured user exists. - if user == nil { - user, err = p.API.CreateUser(&model.User{ - Username: p.Username, - Password: "sample", - // AuthData *string `json:"auth_data,omitempty"` - // AuthService string `json:"auth_service"` - Email: fmt.Sprintf("%s@example.com", p.Username), - Nickname: "Sam", - FirstName: "Sample", - LastName: "Plugin User", - Position: "Bot", - }) - - if err != nil { - return err - } - } - - teams, err := p.API.GetTeams() - if err != nil { - return err - } - - for _, team := range teams { - // Ignore any error. - p.API.CreateTeamMember(team.Id, p.sampleUserId) - } - - // Save the id for later use. - p.sampleUserId = user.Id - - return nil -} - -func (p *Plugin) ensureSampleChannels() *model.AppError { - teams, err := p.API.GetTeams() - if err != nil { - return err - } - - p.sampleChannelIds = make(map[string]string) - for _, team := range teams { - // Check for the configured channel. Ignore any error, since it's hard to - // distinguish runtime errors from a channel simply not existing. - channel, _ := p.API.GetChannelByNameForTeamName(team.Name, p.ChannelName) - - // Ensure the configured channel exists. - if channel == nil { - channel, err = p.API.CreateChannel(&model.Channel{ - TeamId: team.Id, - Type: model.CHANNEL_OPEN, - DisplayName: "Sample Plugin", - Name: p.ChannelName, - Header: "The channel used by the sample plugin.", - Purpose: "This channel was created by a plugin for testing.", - }) - - if err != nil { - return err - } - } - - // Save the ids for later use. - p.sampleChannelIds[team.Id] = channel.Id - } - - return nil -} diff --git a/server/http_hooks.go b/server/http_hooks.go deleted file mode 100644 index 68c25c9..0000000 --- a/server/http_hooks.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "encoding/json" - "net/http" - - "github.com/mattermost/mattermost-server/plugin" -) - -// ServeHTTP allows the plugin to implement the http.Handler interface. Requests destined for the -// /plugins/{id} path will be routed to the plugin. -// -// The Mattermost-User-Id header will be present if (and only if) the request is by an -// authenticated user. -// -// This sample implementation sends back whether or not the plugin hooks are currently enabled. It -// is used by the web app to recover from a network reconnection and synchronize the state of the -// plugin's hooks. -func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) { - var response = struct { - Enabled bool `json:"enabled"` - }{ - Enabled: !p.disabled, - } - - responseJSON, _ := json.Marshal(response) - - w.Write(responseJSON) -} diff --git a/server/message_hooks.go b/server/message_hooks.go deleted file mode 100644 index 2099b9c..0000000 --- a/server/message_hooks.go +++ /dev/null @@ -1,180 +0,0 @@ -package main - -import ( - "fmt" - "strings" - - "github.com/mattermost/mattermost-server/model" - "github.com/mattermost/mattermost-server/plugin" -) - -// MessageWillBePosted is invoked when a message is posted by a user before it is committed to the -// database. If you also want to act on edited posts, see MessageWillBeUpdated. Return values -// should be the modified post or nil if rejected and an explanation for the user. -// -// If you don't need to modify or reject posts, use MessageHasBeenPosted instead. -// -// Note that this method will be called for posts created by plugins, including the plugin that created the post. -// -// This sample implementation rejects posts in the sample channel, as well as posts that @-mention -// the sample plugin user. -func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { - if p.disabled { - return post, "" - } - - // Always allow posts by the sample plugin user. - if post.UserId == p.sampleUserId { - return post, "" - } - - // Reject posts by other users in the sample channels, effectively making it read-only. - for _, channelId := range p.sampleChannelIds { - if channelId == post.ChannelId { - p.API.SendEphemeralPost(post.UserId, &model.Post{ - UserId: p.sampleUserId, - ChannelId: channelId, - Message: "Posting is not allowed in this channel.", - }) - - return nil, "disallowing post in sample channel" - } - } - - // Reject posts mentioning the sample plugin user. - if strings.Contains(post.Message, fmt.Sprintf("@%s", p.Username)) { - p.API.SendEphemeralPost(post.UserId, &model.Post{ - UserId: p.sampleUserId, - ChannelId: post.ChannelId, - Message: "You must not talk about the sample plugin user.", - }) - - return nil, "disallowing mention of sample plugin user" - } - - // Otherwise, allow the post through. - return post, "" -} - -// MessageWillBeUpdated is invoked when a message is updated by a user before it is committed to -// the database. If you also want to act on new posts, see MessageWillBePosted. Return values -// should be the modified post or nil if rejected and an explanation for the user. On rejection, -// the post will be kept in its previous state. -// -// If you don't need to modify or rejected updated posts, use MessageHasBeenUpdated instead. -// -// Note that this method will be called for posts updated by plugins, including the plugin that -// updated the post. -// -// This sample implementation rejects posts that @-mention the sample plugin user. -func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, newPost, oldPost *model.Post) (*model.Post, string) { - if p.disabled { - return newPost, "" - } - - // Reject posts mentioning the sample plugin user. - if strings.Contains(newPost.Message, fmt.Sprintf("@%s", p.Username)) { - p.API.SendEphemeralPost(newPost.UserId, &model.Post{ - UserId: p.sampleUserId, - ChannelId: newPost.ChannelId, - Message: "You must not talk about the sample plugin user.", - }) - - return nil, "disallowing mention of sample plugin user" - } - - // Otherwise, allow the post through. - return newPost, "" -} - -// MessageHasBeenPosted is invoked after the message has been committed to the database. If you -// need to modify or reject the post, see MessageWillBePosted Note that this method will be called -// for posts created by plugins, including the plugin that created the post. -// -// This sample implementation logs a message to the sample channel whenever a message is posted, -// unless by the sample plugin user itself. -func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) { - if p.disabled { - return - } - - // Ignore posts by the sample plugin user. - if post.UserId == p.sampleUserId { - return - } - - user, err := p.API.GetUser(post.UserId) - if err != nil { - p.API.LogError("failed to query user", "user_id", post.UserId) - return - } - - channel, err := p.API.GetChannel(post.ChannelId) - if err != nil { - p.API.LogError("failed to query channel", "channel_id", post.ChannelId) - return - } - - if _, err := p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[channel.TeamId], - Message: fmt.Sprintf( - "MessageHasBeenPosted in ~%s by @%s", - channel.Name, - user.Username, - ), - }); err != nil { - p.API.LogError( - "failed to post MessageHasBeenPosted message", - "channel_id", channel.Id, - "user_id", user.Id, - "error", err.Error(), - ) - } -} - -// MessageHasBeenUpdated is invoked after a message is updated and has been updated in the -// database. If you need to modify or reject the post, see MessageWillBeUpdated Note that this -// method will be called for posts created by plugins, including the plugin that created the post. -// -// This sample implementation logs a message to the sample channel whenever a message is updated, -// unless by the sample plugin user itself. -func (p *Plugin) MessageHasBeenUpdated(c *plugin.Context, newPost, oldPost *model.Post) { - if p.disabled { - return - } - - // Ignore updates by the sample plugin user. - if newPost.UserId == p.sampleUserId { - return - } - - user, err := p.API.GetUser(newPost.UserId) - if err != nil { - p.API.LogError("failed to query user", "user_id", newPost.UserId) - return - } - - channel, err := p.API.GetChannel(newPost.ChannelId) - if err != nil { - p.API.LogError("failed to query channel", "channel_id", newPost.ChannelId) - return - } - - if _, err := p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[channel.TeamId], - Message: fmt.Sprintf( - "MessageHasBeenUpdated in ~%s by @%s", - channel.Name, - user.Username, - ), - }); err != nil { - p.API.LogError( - "failed to post MessageHasBeenUpdated message", - "channel_id", channel.Id, - "user_id", user.Id, - "error", err.Error(), - ) - } -} diff --git a/server/plugin.go b/server/plugin.go index 3181c8b..61df7b8 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -6,19 +6,6 @@ import ( type Plugin struct { plugin.MattermostPlugin - - // The user to use as part of the sample plugin, created automatically if it does not exist. - Username string - - // The channel to use as part of the sample plugin, created for each team automatically if it does not exist. - ChannelName string - - // disabled tracks whether or not the plugin has been disabled after activation. It always starts enabled. - disabled bool - - // sampleUserId is the id of the user specified above. - sampleUserId string - - // sampleChannelIds maps team ids to the channels created for each using the channel name above. - sampleChannelIds map[string]string } + +// See https://developers.mattermost.com/extend/plugins/server/reference/ diff --git a/server/team_hooks.go b/server/team_hooks.go deleted file mode 100644 index 3438f30..0000000 --- a/server/team_hooks.go +++ /dev/null @@ -1,66 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/mattermost/mattermost-server/model" - "github.com/mattermost/mattermost-server/plugin" -) - -// UserHasJoinedTeam is invoked after the membership has been committed to the database. If -// actor is not nil, the user was added to the team by the actor. -// -// This sample implementation logs a message to the sample channel in the team whenever a user -// joins the team. -func (p *Plugin) UserHasJoinedTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) { - if p.disabled { - return - } - - user, err := p.API.GetUser(teamMember.UserId) - if err != nil { - p.API.LogError("failed to query user", "user_id", teamMember.UserId) - return - } - - if _, err = p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[teamMember.TeamId], - Message: fmt.Sprintf("UserHasJoinedTeam: @%s", user.Username), - }); err != nil { - p.API.LogError( - "failed to post UserHasJoinedTeam message", - "user_id", teamMember.UserId, - "error", err.Error(), - ) - } -} - -// UserHasLeftTeam is invoked after the membership has been removed from the database. If actor -// is not nil, the user was removed from the team by the actor. -// -// This sample implementation logs a message to the sample channel in the team whenever a user -// leaves the team. -func (p *Plugin) UserHasLeftTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) { - if p.disabled { - return - } - - user, err := p.API.GetUser(teamMember.UserId) - if err != nil { - p.API.LogError("failed to query user", "user_id", teamMember.UserId) - return - } - - if _, err = p.API.CreatePost(&model.Post{ - UserId: p.sampleUserId, - ChannelId: p.sampleChannelIds[teamMember.TeamId], - Message: fmt.Sprintf("UserHasLeftTeam: @%s", user.Username), - }); err != nil { - p.API.LogError( - "failed to post UserHasLeftTeam message", - "user_id", teamMember.UserId, - "error", err.Error(), - ) - } -} diff --git a/webapp/README.md b/webapp/README.md deleted file mode 100644 index 2a82977..0000000 --- a/webapp/README.md +++ /dev/null @@ -1,105 +0,0 @@ -# Sample Plugin: Web App - -The web app component of this sample plugin is written in Javascript, and leverages [React](https://reactjs.org/) and [Redux](https://redux.js.org/). It registers a component type for all of the supported registration calls, parses a custom webhook to detect when the server plugin's hooks status changes, and pings the server on network reconnect to synchronize state. - -Each of the included files or folders is outlined below. - -## [package.json](package.json) - -See the NPM documentation on [package.json](https://docs.npmjs.com/files/package.json). It defines a `build` script to invoke webpack and generate a bundle, a `lint` script to run the `src/` directory through the [eslint](https://eslint.org/) checker, and a `fix` script that both lints and automatically tries to fix issues. - -## [package-lock.json](package-lock.json) - -See the NPM documentation on [package-lock.json](https://docs.npmjs.com/files/package-lock.json). - -## [webpack.config.js](webpack.config.js) - -See the Webpack documentation on [configuration](https://webpack.js.org/configuration/). Notably, this configuration specifies external dependencies on React, Redux and React Redux to avoid bundling these libraries and duplicating the versions already part of the Mattermost Web App. - -## [.eslintrc.json](.eslintrc.json) - -This defines rules to configure [eslint](https://eslint.org/) as part of invoking the `lint` and `fix` scripts. The styles are based on the rules used by the Mattermost Webapp. - -## [node\_modules](node_modules) - -This is the [location](https://docs.npmjs.com/files/folders#node-modules) in which [npm](https://www.npmjs.com/) installs any necessary Javascript dependencies. - -## [src/index.js](src/index.js) - -This is the entry point of the web app. When the plugin is loaded, this file is executed, registering the plugin with the Mattermost Webapp. - -## [src/plugin\_id.js](src/plugin_id.js) - -This is a file generated by the [build/manifest](../build/manifest) tool that captures the plugin id from [plugin.json](../plugin.json). It simplifies the need to hard-code the plugin id in multiple places by exporting a constant for use instead. - -## [src/plugin.jsx](src/plugin.jsx) - -This defines the Plugin class requires by the Mattermost Webapp, registering all the components and callbacks used by the plugin on `initialize` and logging a console message on `uninitialize`. - -## [src/reducer.js](src/reducer.js) - -This exports a [reducer](https://redux.js.org/basics/reducers) tracking the plugin hook's status. It is part of the global state of the Mattermost Webapp, and accessible at `store['plugins' + PluginId]`. - -## [src/selectors.js](src/selectors.js) - -This defines selectors into the Redux state managed by the plugin to determine if the plugin is enabled or disabled. - -## [src/action\_types.js](src/action_types.js) - -This exports constants used by the Redux [actions](https://redux.js.org/basics/actions) in [action\_types.js](src/action_types.js). It's important to namespace any action types to avoid unintentional collisions with action types from the Mattermost Webapp or other plugins. - -## [src/actions.js](src/actions.js) - -This exports Redux [actions](https://redux.js.org/basics/actions) for triggering the root component, as well as querying the server for the current plugin hooks status and responding to websocket events emitted by the server for the plugin. - -## [components](components) - -This folder exports a number of components illustrating plugin functionality. - -## Root - -This plugin registers a modal-like root component that displays above all other components, and is triggered by interacting with other plugin components on the page: - -![root](docs/root.png) - -## User Attributes - -This plugin registers a user attributes components displaying a static string: - -![user attributes](docs/user_attributes.png) - -## User Actions - -This plugin registers a user actions components displaying a static string followed by a simple ` - - ); - } -} - -const getStyle = (theme) => ({ - button: { - color: theme.buttonColor, - backgroundColor: theme.buttonBg, - }, -}); diff --git a/webapp/src/components/user_attributes.jsx b/webapp/src/components/user_attributes.jsx deleted file mode 100644 index deaeadb..0000000 --- a/webapp/src/components/user_attributes.jsx +++ /dev/null @@ -1,10 +0,0 @@ -import React from 'react'; - -export default class UserAttributes extends React.PureComponent { - render() { - return ( -
{'Sample Plugin: User Attributes'}
- ); - } -} - diff --git a/webapp/src/index.js b/webapp/src/index.js index bc55496..7b7c624 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -1,4 +1,10 @@ -import Plugin from './plugin'; import PluginId from './plugin_id'; +export default class Plugin { + // eslint-disable-next-line no-unused-vars + initialize(registry, store) { + // @see https://developers.mattermost.com/extend/plugins/webapp/reference/ + } +} + window.registerPlugin(PluginId, new Plugin()); diff --git a/webapp/src/plugin.jsx b/webapp/src/plugin.jsx deleted file mode 100644 index 75ab81f..0000000 --- a/webapp/src/plugin.jsx +++ /dev/null @@ -1,69 +0,0 @@ -import React from 'react'; - -import PluginId from './plugin_id'; - -import Root from './components/root'; -import BottomTeamSidebar from './components/bottom_team_sidebar'; -import LeftSidebarHeader from './components/left_sidebar_header'; -import UserAttributes from './components/user_attributes'; -import UserActions from './components/user_actions'; -import PostType from './components/post_type'; -import { - MainMenuMobileIcon, - ChannelHeaderButtonIcon, -} from './components/icons'; -import { - mainMenuAction, - channelHeaderButtonAction, - websocketStatusChange, - getStatus, -} from './actions'; -import reducer from './reducer'; - -export default class SamplePlugin { - initialize(registry, store) { - registry.registerRootComponent(Root); - registry.registerPopoverUserAttributesComponent(UserAttributes); - registry.registerPopoverUserActionsComponent(UserActions); - registry.registerLeftSidebarHeaderComponent(LeftSidebarHeader); - registry.registerBottomTeamSidebarComponent( - BottomTeamSidebar, - ); - - registry.registerChannelHeaderButtonAction( - , - () => store.dispatch(channelHeaderButtonAction()), - 'Sample Plugin', - ); - - registry.registerPostTypeComponent('custom_sample_plugin', PostType); - - registry.registerMainMenuAction( - 'Sample Plugin', - () => store.dispatch(mainMenuAction()), - , - ); - - registry.registerWebSocketEventHandler( - 'custom_' + PluginId + '_status_change', - (message) => { - store.dispatch(websocketStatusChange(message)); - }, - ); - - registry.registerReducer(reducer); - - // Immediately fetch the current plugin status. - store.dispatch(getStatus()); - - // Fetch the current status whenever we recover an internet connection. - registry.registerReconnectHandler(() => { - store.dispatch(getStatus()); - }); - } - - uninitialize() { - //eslint-disable-next-line no-console - console.log(PluginId + '::uninitialize()'); - } -} diff --git a/webapp/src/reducer.js b/webapp/src/reducer.js deleted file mode 100644 index 27bb7fb..0000000 --- a/webapp/src/reducer.js +++ /dev/null @@ -1,30 +0,0 @@ -import {combineReducers} from 'redux'; - -import {STATUS_CHANGE, OPEN_ROOT_MODAL, CLOSE_ROOT_MODAL} from './action_types'; - -const enabled = (state = false, action) => { - switch (action.type) { - case STATUS_CHANGE: - return action.data; - - default: - return state; - } -}; - -const rootModalVisible = (state = false, action) => { - switch (action.type) { - case OPEN_ROOT_MODAL: - return true; - case CLOSE_ROOT_MODAL: - return false; - default: - return state; - } -}; - -export default combineReducers({ - enabled, - rootModalVisible, -}); - diff --git a/webapp/src/selectors.js b/webapp/src/selectors.js deleted file mode 100644 index 0b9e9dd..0000000 --- a/webapp/src/selectors.js +++ /dev/null @@ -1,7 +0,0 @@ -import PluginId from './plugin_id'; - -const getPluginState = (state) => state['plugins-' + PluginId] || {}; - -export const isEnabled = (state) => getPluginState(state).enabled; - -export const isRootModalVisible = (state) => getPluginState(state).rootModalVisible; From b245410cbe6d30ab450f4b7c21ddba88dd7c400f Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Tue, 24 Jul 2018 16:03:48 -0400 Subject: [PATCH 7/8] pin same react/redux dependencies as webapp --- webapp/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webapp/package.json b/webapp/package.json index be94196..7a26255 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -26,8 +26,8 @@ "webpack-cli": "^3.0.8" }, "dependencies": { - "react": "^16.4.1", - "react-redux": "^5.0.7", - "redux": "^4.0.0" + "react": "16.4.1", + "react-redux": "5.0.7", + "redux": "4.0.0" } } From 6d10b915ca12c5013fca7021e640e92b4bfd71e8 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Tue, 24 Jul 2018 16:06:31 -0400 Subject: [PATCH 8/8] tweak plugin_id.js generation --- build/manifest/main.go | 3 ++- webapp/src/plugin_id.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/build/manifest/main.go b/build/manifest/main.go index 62a19e1..d3c7322 100644 --- a/build/manifest/main.go +++ b/build/manifest/main.go @@ -16,7 +16,8 @@ const PluginIdGoFileTemplate = `package main const PluginId = "%s" ` -const PluginIdJsFileTemplate = `export default '%s';` +const PluginIdJsFileTemplate = `export default '%s'; +` func main() { if len(os.Args) <= 1 { diff --git a/webapp/src/plugin_id.js b/webapp/src/plugin_id.js index a56bbb4..c7b1591 100644 --- a/webapp/src/plugin_id.js +++ b/webapp/src/plugin_id.js @@ -1 +1 @@ -export default 'com.mattermost.sample-plugin'; \ No newline at end of file +export default 'com.mattermost.sample-plugin';