mirror of
https://github.com/pushbits/server.git
synced 2025-07-26 04:47:22 +02:00
Kick user when deleting an application
This commit is contained in:
parent
b89deeac3d
commit
3bdadd6029
4 changed files with 36 additions and 18 deletions
|
@ -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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -55,10 +55,10 @@ func (h *ApplicationHandler) createApplication(ctx *gin.Context, name string, u
|
|||
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)
|
||||
|
||||
err := h.DP.DeregisterApplication(a)
|
||||
err := h.DP.DeregisterApplication(a, u)
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
return err
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if err := h.deleteApplication(ctx, application); err != nil {
|
||||
if err := h.deleteApplication(ctx, application, authentication.GetUser(ctx)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ type Database interface {
|
|||
|
||||
// The Dispatcher interface for relaying notifications.
|
||||
type Dispatcher interface {
|
||||
RegisterApplication(name, user string) (string, error)
|
||||
DeregisterApplication(a *model.Application) error
|
||||
RegisterApplication(id uint, name, token, user string) (string, error)
|
||||
DeregisterApplication(a *model.Application, u *model.User) error
|
||||
}
|
||||
|
||||
// The CredentialsManager interface for updating credentials.
|
||||
|
|
|
@ -44,7 +44,7 @@ func (h *UserHandler) deleteApplications(ctx *gin.Context, u *model.User) error
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ func (h *UserHandler) updateChannels(ctx *gin.Context, u *model.User, matrixID s
|
|||
}
|
||||
|
||||
for _, application := range applications {
|
||||
err := h.DP.DeregisterApplication(&application)
|
||||
err := h.DP.DeregisterApplication(&application, u)
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/eikendev/pushbits/model"
|
||||
|
@ -9,15 +10,18 @@ import (
|
|||
)
|
||||
|
||||
// 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)
|
||||
|
||||
topic := fmt.Sprintf("Application %d, Token %s", id, token)
|
||||
|
||||
response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{
|
||||
Visibility: "private",
|
||||
Name: name,
|
||||
Invite: []string{user},
|
||||
Preset: "private_chat",
|
||||
IsDirect: true,
|
||||
Name: name,
|
||||
Preset: "private_chat",
|
||||
Topic: topic,
|
||||
Visibility: "private",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
@ -31,14 +35,28 @@ func (d *Dispatcher) RegisterApplication(name, user string) (string, error) {
|
|||
}
|
||||
|
||||
// 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)
|
||||
|
||||
_, err := d.client.LeaveRoom(a.MatrixID)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
kickUser := &gomatrix.ReqKickUser{
|
||||
Reason: "This application was deleted",
|
||||
UserID: u.MatrixID,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue