Kick user when deleting an application

This commit is contained in:
eikendev 2020-08-06 17:56:58 +02:00
parent b89deeac3d
commit 3bdadd6029
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
4 changed files with 36 additions and 18 deletions

View file

@ -25,7 +25,7 @@ func (h *ApplicationHandler) applicationExists(token string) bool {
func (h *ApplicationHandler) registerApplication(ctx *gin.Context, a *model.Application, u *model.User) error { func (h *ApplicationHandler) registerApplication(ctx *gin.Context, a *model.Application, u *model.User) error {
log.Printf("Registering application %s.\n", a.Name) log.Printf("Registering application %s.\n", a.Name)
channelID, err := h.DP.RegisterApplication(a.Name, u.MatrixID) channelID, err := h.DP.RegisterApplication(a.ID, a.Name, a.Token, u.MatrixID)
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success { if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
return err return err
} }
@ -55,10 +55,10 @@ func (h *ApplicationHandler) createApplication(ctx *gin.Context, name string, u
return &application, nil return &application, nil
} }
func (h *ApplicationHandler) deleteApplication(ctx *gin.Context, a *model.Application) error { func (h *ApplicationHandler) deleteApplication(ctx *gin.Context, a *model.Application, u *model.User) error {
log.Printf("Deleting application %s.\n", a.Name) log.Printf("Deleting application %s.\n", a.Name)
err := h.DP.DeregisterApplication(a) err := h.DP.DeregisterApplication(a, u)
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success { if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
return err return err
} }
@ -154,7 +154,7 @@ func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
return return
} }
if err := h.deleteApplication(ctx, application); err != nil { if err := h.deleteApplication(ctx, application, authentication.GetUser(ctx)); err != nil {
return return
} }

View file

@ -24,8 +24,8 @@ type Database interface {
// The Dispatcher interface for relaying notifications. // The Dispatcher interface for relaying notifications.
type Dispatcher interface { type Dispatcher interface {
RegisterApplication(name, user string) (string, error) RegisterApplication(id uint, name, token, user string) (string, error)
DeregisterApplication(a *model.Application) error DeregisterApplication(a *model.Application, u *model.User) error
} }
// The CredentialsManager interface for updating credentials. // The CredentialsManager interface for updating credentials.

View file

@ -44,7 +44,7 @@ func (h *UserHandler) deleteApplications(ctx *gin.Context, u *model.User) error
} }
for _, application := range applications { for _, application := range applications {
if err := h.AH.deleteApplication(ctx, &application); err != nil { if err := h.AH.deleteApplication(ctx, &application, u); err != nil {
return err return err
} }
} }
@ -59,7 +59,7 @@ func (h *UserHandler) updateChannels(ctx *gin.Context, u *model.User, matrixID s
} }
for _, application := range applications { for _, application := range applications {
err := h.DP.DeregisterApplication(&application) err := h.DP.DeregisterApplication(&application, u)
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success { if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
return err return err
} }

View file

@ -1,6 +1,7 @@
package dispatcher package dispatcher
import ( import (
"fmt"
"log" "log"
"github.com/eikendev/pushbits/model" "github.com/eikendev/pushbits/model"
@ -9,15 +10,18 @@ import (
) )
// RegisterApplication creates a channel for an application. // RegisterApplication creates a channel for an application.
func (d *Dispatcher) RegisterApplication(name, user string) (string, error) { func (d *Dispatcher) RegisterApplication(id uint, name, token, user string) (string, error) {
log.Printf("Registering application %s, notifications will be relayed to user %s.\n", name, user) log.Printf("Registering application %s, notifications will be relayed to user %s.\n", name, user)
topic := fmt.Sprintf("Application %d, Token %s", id, token)
response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{ response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{
Visibility: "private",
Name: name,
Invite: []string{user}, Invite: []string{user},
Preset: "private_chat",
IsDirect: true, IsDirect: true,
Name: name,
Preset: "private_chat",
Topic: topic,
Visibility: "private",
}) })
if err != nil { if err != nil {
@ -31,14 +35,28 @@ func (d *Dispatcher) RegisterApplication(name, user string) (string, error) {
} }
// DeregisterApplication deletes a channel for an application. // DeregisterApplication deletes a channel for an application.
func (d *Dispatcher) DeregisterApplication(a *model.Application) error { func (d *Dispatcher) DeregisterApplication(a *model.Application, u *model.User) error {
log.Printf("Deregistering application with ID %s.\n", a.MatrixID) log.Printf("Deregistering application with ID %s.\n", a.MatrixID)
_, err := d.client.LeaveRoom(a.MatrixID) kickUser := &gomatrix.ReqKickUser{
Reason: "This application was deleted",
if err != nil { UserID: u.MatrixID,
log.Fatal(err)
} }
return err if _, err := d.client.KickUser(a.MatrixID, kickUser); err != nil {
log.Fatal(err)
return err
}
if _, err := d.client.LeaveRoom(a.MatrixID); err != nil {
log.Fatal(err)
return err
}
if _, err := d.client.ForgetRoom(a.MatrixID); err != nil {
log.Fatal(err)
return err
}
return nil
} }