From 59e5cfefd490d5ea8f7ca8a73c01a9c37d39f857 Mon Sep 17 00:00:00 2001 From: Michael David Kuckuk <8076094+LBBO@users.noreply.github.com> Date: Tue, 17 Jan 2023 22:20:29 +0100 Subject: [PATCH] Remove twitter notification configuration (#2598) --- controllers/admin/notifications.go | 25 -- controllers/admin/serverConfig.go | 2 - core/data/config.go | 22 -- docs/dependencies.md | 1 - go.mod | 1 - go.sum | 2 - models/notification.go | 11 - notifications/notifications.go | 41 ---- notifications/twitter/twitter.go | 78 ------- router/router.go | 1 - web/components/admin/notification/twitter.tsx | 220 ------------------ web/pages/admin/config-notify.tsx | 8 - web/types/config-section.ts | 11 - web/utils/config-constants.tsx | 45 ---- web/utils/server-status-context.tsx | 9 - 15 files changed, 477 deletions(-) delete mode 100644 notifications/twitter/twitter.go delete mode 100644 web/components/admin/notification/twitter.tsx diff --git a/controllers/admin/notifications.go b/controllers/admin/notifications.go index f537c0612..82bdb5f8c 100644 --- a/controllers/admin/notifications.go +++ b/controllers/admin/notifications.go @@ -58,28 +58,3 @@ func SetBrowserNotificationConfiguration(w http.ResponseWriter, r *http.Request) controllers.WriteSimpleResponse(w, true, "updated browser push config with provided values") } - -// SetTwitterConfiguration will set the browser notification configuration. -func SetTwitterConfiguration(w http.ResponseWriter, r *http.Request) { - if !requirePOST(w, r) { - return - } - - type request struct { - Value models.TwitterConfiguration `json:"value"` - } - - decoder := json.NewDecoder(r.Body) - var config request - if err := decoder.Decode(&config); err != nil { - controllers.WriteSimpleResponse(w, false, "unable to update twitter config with provided values") - return - } - - if err := data.SetTwitterConfiguration(config.Value); err != nil { - controllers.WriteSimpleResponse(w, false, "unable to update twitter config with provided values") - return - } - - controllers.WriteSimpleResponse(w, true, "updated twitter config with provided values") -} diff --git a/controllers/admin/serverConfig.go b/controllers/admin/serverConfig.go index 5785190f9..a86bd5a46 100644 --- a/controllers/admin/serverConfig.go +++ b/controllers/admin/serverConfig.go @@ -84,7 +84,6 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) { Notifications: notificationsConfigResponse{ Discord: data.GetDiscordConfig(), Browser: data.GetBrowserPushConfig(), - Twitter: data.GetTwitterConfiguration(), }, } @@ -160,5 +159,4 @@ type federationConfigResponse struct { type notificationsConfigResponse struct { Browser models.BrowserNotificationConfiguration `json:"browser"` Discord models.DiscordConfiguration `json:"discord"` - Twitter models.TwitterConfiguration `json:"twitter"` } diff --git a/core/data/config.go b/core/data/config.go index da2d9238c..fd4957584 100644 --- a/core/data/config.go +++ b/core/data/config.go @@ -63,7 +63,6 @@ const ( browserPushConfigurationKey = "browser_push_configuration" browserPushPublicKeyKey = "browser_push_public_key" browserPushPrivateKeyKey = "browser_push_private_key" - twitterConfigurationKey = "twitter_configuration" hasConfiguredInitialNotificationsKey = "has_configured_initial_notifications" hideViewerCountKey = "hide_viewer_count" customOfflineMessageKey = "custom_offline_message" @@ -880,27 +879,6 @@ func GetBrowserPushPrivateKey() (string, error) { return _datastore.GetString(browserPushPrivateKeyKey) } -// SetTwitterConfiguration will set the Twitter configuration. -func SetTwitterConfiguration(config models.TwitterConfiguration) error { - configEntry := ConfigEntry{Key: twitterConfigurationKey, Value: config} - return _datastore.Save(configEntry) -} - -// GetTwitterConfiguration will return the Twitter configuration. -func GetTwitterConfiguration() models.TwitterConfiguration { - configEntry, err := _datastore.Get(twitterConfigurationKey) - if err != nil { - return models.TwitterConfiguration{Enabled: false} - } - - var config models.TwitterConfiguration - if err := configEntry.getObject(&config); err != nil { - return models.TwitterConfiguration{Enabled: false} - } - - return config -} - // SetHasPerformedInitialNotificationsConfig sets when performed initial setup. func SetHasPerformedInitialNotificationsConfig(hasConfigured bool) error { return _datastore.SetBool(hasConfiguredInitialNotificationsKey, true) diff --git a/docs/dependencies.md b/docs/dependencies.md index 0ab51422c..4387381be 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -56,7 +56,6 @@ Owncast Dependencies - HTTP routing https://github.com/gorilla/mux - Mastodon API https://github.com/mattn/go-mastodon -- Twitter API https://github.com/ChimeraCoder/anaconda - Go ORM https://gorm.io/gorm - ID string generator https://github.com/teris-io/shortid - Slug generator https://github.com/metal3d/go-slugify diff --git a/go.mod b/go.mod index 5a019dc70..46f6f3579 100644 --- a/go.mod +++ b/go.mod @@ -77,7 +77,6 @@ require github.com/SherClockHolmes/webpush-go v1.2.0 require ( github.com/aymerick/douceur v0.2.0 // indirect github.com/dghubble/oauth1 v0.7.2 - github.com/g8rswimmer/go-twitter/v2 v2.1.5 github.com/go-test/deep v1.0.4 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/gorilla/css v1.0.0 // indirect diff --git a/go.sum b/go.sum index 994fe6fa2..2141fe858 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/g8rswimmer/go-twitter/v2 v2.1.5 h1:Uj9Yuof2UducrP4Xva7irnUJfB9354/VyUXKmc2D5gg= -github.com/g8rswimmer/go-twitter/v2 v2.1.5/go.mod h1:/55xWb313KQs25X7oZrNSEwLQNkYHhPsDwFstc45vhc= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM= diff --git a/models/notification.go b/models/notification.go index 01542cc98..7b4e659b1 100644 --- a/models/notification.go +++ b/models/notification.go @@ -14,14 +14,3 @@ type BrowserNotificationConfiguration struct { Enabled bool `json:"enabled"` GoLiveMessage string `json:"goLiveMessage,omitempty"` } - -// TwitterConfiguration represents the configuration for Twitter access. -type TwitterConfiguration struct { - Enabled bool `json:"enabled"` - APIKey string `json:"apiKey"` // aka consumer key - APISecret string `json:"apiSecret"` // aka consumer secret - AccessToken string `json:"accessToken"` - AccessTokenSecret string `json:"accessTokenSecret"` - BearerToken string `json:"bearerToken"` - GoLiveMessage string `json:"goLiveMessage,omitempty"` -} diff --git a/notifications/notifications.go b/notifications/notifications.go index d19b611c8..99c01aede 100644 --- a/notifications/notifications.go +++ b/notifications/notifications.go @@ -2,15 +2,12 @@ package notifications import ( "fmt" - "strings" "github.com/owncast/owncast/config" "github.com/owncast/owncast/core/data" "github.com/owncast/owncast/models" "github.com/owncast/owncast/notifications/browser" "github.com/owncast/owncast/notifications/discord" - "github.com/owncast/owncast/notifications/twitter" - "github.com/owncast/owncast/utils" "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -20,7 +17,6 @@ type Notifier struct { datastore *data.Datastore browser *browser.Browser discord *discord.Discord - twitter *twitter.Twitter } // Setup will perform any pre-use setup for the notifier. @@ -68,9 +64,6 @@ func New(datastore *data.Datastore) (*Notifier, error) { if err := notifier.setupDiscord(); err != nil { log.Error(err) } - if err := notifier.setupTwitter(); err != nil { - log.Errorln(err) - } return ¬ifier, nil } @@ -147,36 +140,6 @@ func (n *Notifier) notifyDiscord() { } } -func (n *Notifier) setupTwitter() error { - if twitterConfig := data.GetTwitterConfiguration(); twitterConfig.Enabled { - if t, err := twitter.New(twitterConfig.APIKey, twitterConfig.APISecret, twitterConfig.AccessToken, twitterConfig.AccessTokenSecret, twitterConfig.BearerToken); err == nil { - n.twitter = t - } else if err != nil { - return errors.Wrap(err, "error creating twitter notifier") - } - } - return nil -} - -func (n *Notifier) notifyTwitter() { - goLiveMessage := data.GetTwitterConfiguration().GoLiveMessage - streamTitle := data.GetStreamTitle() - if streamTitle != "" { - goLiveMessage += "\n" + streamTitle - } - tagString := "" - for _, tag := range utils.ShuffleStringSlice(data.GetServerMetadataTags()) { - tagString = fmt.Sprintf("%s #%s", tagString, tag) - } - tagString = strings.TrimSpace(tagString) - - message := fmt.Sprintf("%s\n%s\n\n%s", goLiveMessage, data.GetServerURL(), tagString) - - if err := n.twitter.Notify(message); err != nil { - log.Errorln("error sending twitter message", err) - } -} - // Notify will fire the different notification channels. func (n *Notifier) Notify() { if n.browser != nil { @@ -186,8 +149,4 @@ func (n *Notifier) Notify() { if n.discord != nil { n.notifyDiscord() } - - if n.twitter != nil { - n.notifyTwitter() - } } diff --git a/notifications/twitter/twitter.go b/notifications/twitter/twitter.go deleted file mode 100644 index 6ed7b83d9..000000000 --- a/notifications/twitter/twitter.go +++ /dev/null @@ -1,78 +0,0 @@ -package twitter - -import ( - "context" - "errors" - "fmt" - "net/http" - - "github.com/dghubble/oauth1" - "github.com/g8rswimmer/go-twitter/v2" -) - -/* -1. developer.twitter.com. Apply to be a developer if needed. -2. Projects and apps -> Your project name -3. Settings. -4. Scroll down to"User authentication settings" Edit -5. Enable OAuth 1.0a with Read/Write permissions. -6. Fill out the form with your information. Callback can be anything. -7. Go to your project "Keys and tokens" -8. Generate API key and secret. -9. Generate access token and secret. Verify it says "Read and write permissions." -10. Generate bearer token. -*/ - -type authorize struct { - Token string -} - -func (a authorize) Add(req *http.Request) { - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token)) -} - -// Twitter is an instance of the Twitter notifier. -type Twitter struct { - apiKey string - apiSecret string - accessToken string - accessTokenSecret string - bearerToken string -} - -// New returns a new instance of the Twitter notifier. -func New(apiKey, apiSecret, accessToken, accessTokenSecret, bearerToken string) (*Twitter, error) { - if apiKey == "" || apiSecret == "" || accessToken == "" || accessTokenSecret == "" || bearerToken == "" { - return nil, errors.New("missing some or all of the required twitter configuration values") - } - - return &Twitter{ - apiKey: apiKey, - apiSecret: apiSecret, - accessToken: accessToken, - accessTokenSecret: accessTokenSecret, - bearerToken: bearerToken, - }, nil -} - -// Notify will send a notification to Twitter with the supplied text. -func (t *Twitter) Notify(text string) error { - config := oauth1.NewConfig(t.apiKey, t.apiSecret) - token := oauth1.NewToken(t.accessToken, t.accessTokenSecret) - httpClient := config.Client(oauth1.NoContext, token) - - client := &twitter.Client{ - Authorizer: authorize{ - Token: t.bearerToken, - }, - Client: httpClient, - Host: "https://api.twitter.com", - } - - req := twitter.CreateTweetRequest{ - Text: text, - } - - _, err := client.CreateTweet(context.Background(), req) - return err -} diff --git a/router/router.go b/router/router.go index fbaa90480..c13ef1719 100644 --- a/router/router.go +++ b/router/router.go @@ -366,7 +366,6 @@ func Start() error { // Configure outbound notification channels. http.HandleFunc("/api/admin/config/notifications/discord", middleware.RequireAdminAuth(admin.SetDiscordNotificationConfiguration)) http.HandleFunc("/api/admin/config/notifications/browser", middleware.RequireAdminAuth(admin.SetBrowserNotificationConfiguration)) - http.HandleFunc("/api/admin/config/notifications/twitter", middleware.RequireAdminAuth(admin.SetTwitterConfiguration)) // Auth diff --git a/web/components/admin/notification/twitter.tsx b/web/components/admin/notification/twitter.tsx deleted file mode 100644 index 4dd850305..000000000 --- a/web/components/admin/notification/twitter.tsx +++ /dev/null @@ -1,220 +0,0 @@ -import { Button, Typography } from 'antd'; -import React, { useState, useContext, useEffect } from 'react'; -import { ServerStatusContext } from '../../../utils/server-status-context'; -import { TextField, TEXTFIELD_TYPE_PASSWORD } from '../TextField'; -import { FormStatusIndicator } from '../FormStatusIndicator'; -import { - postConfigUpdateToAPI, - RESET_TIMEOUT, - TWITTER_CONFIG_FIELDS, -} from '../../../utils/config-constants'; -import { ToggleSwitch } from '../ToggleSwitch'; -import { - createInputStatus, - StatusState, - STATUS_ERROR, - STATUS_SUCCESS, -} from '../../../utils/input-statuses'; -import { UpdateArgs } from '../../../types/config-section'; -import { TEXTFIELD_TYPE_TEXT } from '../TextFieldWithSubmit'; - -const { Title } = Typography; - -export const ConfigNotify = () => { - const serverStatusData = useContext(ServerStatusContext); - const { serverConfig, setFieldInConfigState } = serverStatusData || {}; - const { notifications } = serverConfig || {}; - const { twitter } = notifications || {}; - - const [formDataValues, setFormDataValues] = useState({}); - const [submitStatus, setSubmitStatus] = useState(null); - - const [enableSaveButton, setEnableSaveButton] = useState(false); - - useEffect(() => { - const { - enabled, - apiKey, - apiSecret, - accessToken, - accessTokenSecret, - bearerToken, - goLiveMessage, - } = twitter || {}; - setFormDataValues({ - enabled, - apiKey, - apiSecret, - accessToken, - accessTokenSecret, - bearerToken, - goLiveMessage, - }); - }, [twitter]); - - const canSave = (): boolean => { - const { apiKey, apiSecret, accessToken, accessTokenSecret, bearerToken, goLiveMessage } = - formDataValues; - - return ( - !!apiKey && - !!apiSecret && - !!accessToken && - !!accessTokenSecret && - !!bearerToken && - !!goLiveMessage - ); - }; - - useEffect(() => { - setEnableSaveButton(canSave()); - }, [formDataValues]); - - // update individual values in state - const handleFieldChange = ({ fieldName, value }: UpdateArgs) => { - setFormDataValues({ - ...formDataValues, - [fieldName]: value, - }); - }; - - // toggle switch. - const handleSwitchChange = (switchEnabled: boolean) => { - const previouslySaved = formDataValues.enabled; - - handleFieldChange({ fieldName: 'enabled', value: switchEnabled }); - - return switchEnabled !== previouslySaved; - }; - - let resetTimer = null; - const resetStates = () => { - setSubmitStatus(null); - resetTimer = null; - clearTimeout(resetTimer); - setEnableSaveButton(false); - }; - - const save = async () => { - const postValue = formDataValues; - - await postConfigUpdateToAPI({ - apiPath: '/notifications/twitter', - data: { value: postValue }, - onSuccess: () => { - setFieldInConfigState({ - fieldName: 'twitter', - value: postValue, - path: 'notifications', - }); - setSubmitStatus(createInputStatus(STATUS_SUCCESS, 'Updated.')); - resetTimer = setTimeout(resetStates, RESET_TIMEOUT); - }, - onError: (message: string) => { - setSubmitStatus(createInputStatus(STATUS_ERROR, message)); - resetTimer = setTimeout(resetStates, RESET_TIMEOUT); - }, - }); - }; - - return ( - <> - Twitter -

- Let your Twitter followers know each time you go live. -

-
-

- - Read how to configure your Twitter account - {' '} - to support posting from Owncast. -

-

- - And then get your Twitter developer credentials - {' '} - to fill in below. -

-
- - -
- -
-
- -
-
- -
-
- -
-
- -
-
- -
- - - - ); -}; -export default ConfigNotify; diff --git a/web/pages/admin/config-notify.tsx b/web/pages/admin/config-notify.tsx index 0d90ab922..20ba0254e 100644 --- a/web/pages/admin/config-notify.tsx +++ b/web/pages/admin/config-notify.tsx @@ -4,7 +4,6 @@ import Link from 'next/link'; import Discord from '../../components/admin/notification/discord'; import Browser from '../../components/admin/notification/browser'; -import Twitter from '../../components/admin/notification/twitter'; import Federation from '../../components/admin/notification/federation'; import { TextFieldWithSubmit, @@ -99,13 +98,6 @@ export default function ConfigNotify() { > - - -