Fix more compiler errors, logic error in main
- Forgot to also update the undo handler for the inbox - Fixed main not ever returning if the debug server was not enabled
This commit is contained in:
parent
29eda04330
commit
27b7d342b7
2 changed files with 35 additions and 24 deletions
9
main.go
9
main.go
|
@ -110,13 +110,16 @@ func newServer() {
|
|||
}
|
||||
debugShutdownChan := make(chan *sync.WaitGroup, 1)
|
||||
interuptChan := make(chan os.Signal, 1)
|
||||
debugShutdownWaitgroup := sync.WaitGroup{}
|
||||
signal.Notify(interuptChan, os.Interrupt)
|
||||
|
||||
if *shared.FlagStartDebugServer {
|
||||
go func() {
|
||||
log.Info().Msg("Starting debug server")
|
||||
s := webdebug.New(*shared.FlagDebugPort)
|
||||
go func() {
|
||||
wg := <-debugShutdownChan
|
||||
wg.Add(1)
|
||||
if err := s.Stop(); err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to cleanly stop debug server")
|
||||
}
|
||||
|
@ -141,12 +144,10 @@ func newServer() {
|
|||
}()
|
||||
<-interuptChan
|
||||
log.Info().Msg("Received interrupt, shutting down")
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
debugShutdownChan <- &wg
|
||||
debugShutdownChan <- &debugShutdownWaitgroup
|
||||
if err = public.Stop(); err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to stop public server")
|
||||
}
|
||||
log.Info().Msg("Public server stopped")
|
||||
wg.Wait()
|
||||
debugShutdownWaitgroup.Wait()
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func handleUndo(w http.ResponseWriter, r *http.Request, object map[string]any) b
|
|||
other.IntoPointer(`Request data needs to contain a field "actor" with a string value`),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
return true
|
||||
}
|
||||
rawTarget, ok := object["object"]
|
||||
if !ok {
|
||||
|
@ -37,7 +37,7 @@ func handleUndo(w http.ResponseWriter, r *http.Request, object map[string]any) b
|
|||
other.IntoPointer(`Request data needs to contain a field "object"`),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
return true
|
||||
}
|
||||
// FIXME: Also handle other undo cases, such as follows
|
||||
var targetObjectId string
|
||||
|
@ -57,7 +57,7 @@ func handleUndo(w http.ResponseWriter, r *http.Request, object map[string]any) b
|
|||
other.IntoPointer(`Target object does not have a type`),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
return true
|
||||
}
|
||||
targetObjectType = objType
|
||||
targetObjectId, ok = target["id"].(string)
|
||||
|
@ -70,7 +70,7 @@ func handleUndo(w http.ResponseWriter, r *http.Request, object map[string]any) b
|
|||
other.IntoPointer(`Missing id in undone object`),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
return true
|
||||
}
|
||||
default:
|
||||
_ = webutils.ProblemDetails(
|
||||
|
@ -81,28 +81,29 @@ func handleUndo(w http.ResponseWriter, r *http.Request, object map[string]any) b
|
|||
other.IntoPointer(`Request data needs to contain a field "object" of type string or object`),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
return true
|
||||
}
|
||||
switch targetObjectType {
|
||||
case "Like":
|
||||
undoLike(w, r, object, targetObjectId)
|
||||
return undoLike(w, r, object, targetObjectId)
|
||||
case "Follow":
|
||||
undoFollow(w, r, object, targetObjectId)
|
||||
return undoFollow(w, r, object, targetObjectId)
|
||||
default:
|
||||
log.Error().
|
||||
Str("undo-target-type", targetObjectType).
|
||||
Msg("Unknown/unimplemented undo target type")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func undoLike(w http.ResponseWriter, r *http.Request, object map[string]any, targetId string) {
|
||||
func undoLike(w http.ResponseWriter, r *http.Request, object map[string]any, targetId string) bool {
|
||||
log := hlog.FromRequest(r)
|
||||
act, err := dbgen.Activity.Where(dbgen.Activity.Id.Eq(targetId), dbgen.Activity.Type.Eq("like")).
|
||||
First()
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
return
|
||||
return true
|
||||
case nil:
|
||||
default:
|
||||
log.Error().
|
||||
|
@ -110,13 +111,13 @@ func undoLike(w http.ResponseWriter, r *http.Request, object map[string]any, tar
|
|||
Str("activity-id", targetId).
|
||||
Msg("Error while looking for like activity")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
reactionId := uint(other.Must(strconv.ParseUint(act.ObjectId, 10, 64)))
|
||||
reaction, err := dbgen.Reaction.Where(dbgen.Reaction.ID.Eq(reactionId)).First()
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
return
|
||||
return true
|
||||
case nil:
|
||||
default:
|
||||
log.Error().
|
||||
|
@ -124,7 +125,7 @@ func undoLike(w http.ResponseWriter, r *http.Request, object map[string]any, tar
|
|||
Str("activity-id", targetId).
|
||||
Msg("Error while looking for find activity")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
tx := dbgen.Q.Begin()
|
||||
_, err = tx.Activity.Where(dbgen.Activity.Id.Eq(act.Id)).Delete()
|
||||
|
@ -132,7 +133,7 @@ func undoLike(w http.ResponseWriter, r *http.Request, object map[string]any, tar
|
|||
_ = tx.Rollback()
|
||||
log.Error().Err(err).Str("activity-id", act.Id).Msg("Failed to delete activity on undo")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
_, err = tx.Reaction.Where(dbgen.Reaction.ID.Eq(reaction.ID)).Delete()
|
||||
if err != nil {
|
||||
|
@ -142,22 +143,29 @@ func undoLike(w http.ResponseWriter, r *http.Request, object map[string]any, tar
|
|||
Uint("reaction-id", reaction.ID).
|
||||
Msg("Failed to delete reaction on undo")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to delete reaction and activity")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func undoFollow(w http.ResponseWriter, r *http.Request, object map[string]any, targetId string) {
|
||||
func undoFollow(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
object map[string]any,
|
||||
targetId string,
|
||||
) bool {
|
||||
log := hlog.FromRequest(r)
|
||||
act, err := dbgen.Activity.Where(dbgen.Activity.Id.Eq(targetId), dbgen.Activity.Type.Eq("follow")).
|
||||
First()
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
return
|
||||
return true
|
||||
case nil:
|
||||
default:
|
||||
log.Error().
|
||||
|
@ -165,7 +173,7 @@ func undoFollow(w http.ResponseWriter, r *http.Request, object map[string]any, t
|
|||
Str("activity-id", targetId).
|
||||
Msg("Error while looking for follow activity")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
relationId := other.Must(strconv.ParseUint(act.ObjectId, 10, 64))
|
||||
tx := dbgen.Q.Begin()
|
||||
|
@ -177,7 +185,7 @@ func undoFollow(w http.ResponseWriter, r *http.Request, object map[string]any, t
|
|||
Str("activity-id", act.Id).
|
||||
Msg("Failed to delete follow activity on undo")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
// Delete all activities that reference the follow activity (so accept/reject if exists)
|
||||
_, err = tx.Activity.Where(dbgen.Activity.ObjectId.Eq(act.Id)).Delete()
|
||||
|
@ -188,7 +196,7 @@ func undoFollow(w http.ResponseWriter, r *http.Request, object map[string]any, t
|
|||
Str("activity-id", act.Id).
|
||||
Msg("Failed to delete accept/reject activity for follow on undo")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
_, err = tx.UserToUserRelation.Where(dbgen.UserToUserRelation.ID.Eq(relationId)).Delete()
|
||||
if err != nil {
|
||||
|
@ -199,11 +207,13 @@ func undoFollow(w http.ResponseWriter, r *http.Request, object map[string]any, t
|
|||
Uint64("relation-id", relationId).
|
||||
Msg("Failed to delete user-to-user relation for follow on undo")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
return false
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to delete reaction and activity")
|
||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue