Skip to content

Commit

Permalink
delete figure content of a post on post/user deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
krustowski committed Jul 23, 2024
1 parent ef00400 commit 5ed8391
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#

APP_NAME=litter-go
APP_VERSION=0.36.3
APP_VERSION=0.36.4
GOLANG_VERSION=1.22
51 changes: 50 additions & 1 deletion api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "MIT",
"url": "https://github.com/krustowski/litter-go/blob/master/LICENSE"
},
"version": "0.36.3"
"version": "0.36.4"
},
"host": "littr.eu",
"basePath": "/api/v1",
Expand Down Expand Up @@ -1026,6 +1026,47 @@
}
}
},
"/users/{nickname}/localtime": {
"patch": {
"description": "toggle the local time mode",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"users"
],
"summary": "Toggle the local time mode",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.Response"
}
}
}
}
},
"/users/{nickname}/posts": {
"get": {
"description": "get user posts",
Expand Down Expand Up @@ -1488,6 +1529,14 @@
"description": "LastLoginTime is an UNIX timestamp of the last user's successful log-in.",
"type": "string"
},
"live_mode": {
"description": "LiveMode is a feature allowing to show notifications about new posts",
"type": "boolean"
},
"local_time_mode": {
"description": "LocalTimeMode is a feature to show any post's datetime in the local time according to the client's/user's device setting.",
"type": "boolean"
},
"nickname": {
"description": "Nickname is a login name of such user.",
"type": "string"
Expand Down
23 changes: 23 additions & 0 deletions pkg/backend/posts/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,29 @@ func deletePost(w http.ResponseWriter, r *http.Request) {
return
}

// delete associated image and thumbnail
if post.Figure != "" {
err := os.Remove("/opt/pix/thumb_" + post.Figure)
if err != nil {
resp.Message = "cannot remove thumbnail associated to the post"
resp.Code = http.StatusInternalServerError

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}

err = os.Remove("/opt/pix/" + post.Figure)
if err != nil {
resp.Message = "cannot remove image associated to the post"
resp.Code = http.StatusInternalServerError

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}
}

resp.Message = "ok, post removed"
resp.Code = http.StatusOK

Expand Down
2 changes: 1 addition & 1 deletion pkg/backend/router.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @title litter-go
// @version 0.36.3
// @version 0.36.4
// @description nanoblogging platform as PWA built on go-app framework
// @termsOfService https://littr.eu/tos

Expand Down
42 changes: 40 additions & 2 deletions pkg/backend/users/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,31 +324,69 @@ func deleteUser(w http.ResponseWriter, r *http.Request) {
return
}

// delete user
if deleted := db.DeleteOne(db.UserCache, key); !deleted {
resp.Message = "error deleting:" + key
resp.Message = "error deleting from user cache:" + key
resp.Code = http.StatusInternalServerError

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}

// delete all user's posts and polls
// delete user's subscriptions
if deleted := db.DeleteOne(db.SubscriptionCache, key); !deleted {
resp.Message = "error deleting from subscription cache:" + key
resp.Code = http.StatusInternalServerError

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}

void := ""

// delete all user's posts and polls, and tokens
posts, _ := db.GetAll(db.FlowCache, models.Post{})
polls, _ := db.GetAll(db.PollCache, models.Poll{})
tokens, _ := db.GetAll(db.TokenCache, void)

// loop over posts and delete authored ones + linked fungures
for id, post := range posts {
if post.Nickname == key {
db.DeleteOne(db.FlowCache, id)

// delete associated image and thumbnail
if post.Figure != "" {
err := os.Remove("/opt/pix/thumb_" + post.Figure)
if err != nil {
// nasty bypass
continue
}

err = os.Remove("/opt/pix/" + post.Figure)
if err != nil {
// nasty bypass
continue
}
}
}
}

// loop over polls and delete authored ones
for id, poll := range polls {
if poll.Author == key {
db.DeleteOne(db.PollCache, id)
}
}

// loop over tokens and delete matching ones
for id, tok := range tokens {
if tok == key {
db.DeleteOne(db.TokenCache, id)
}
}

resp.Message = "ok, deleting user: " + key
resp.Code = http.StatusOK

Expand Down

0 comments on commit 5ed8391

Please sign in to comment.