2022-08-14 21:40:57 +00:00
|
|
|
package deletion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-04-12 06:27:20 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/chat"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/photo"
|
2022-08-14 21:40:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteUser wipes a user and all associated data from the database.
|
|
|
|
func DeleteUser(user *models.User) error {
|
|
|
|
log.Error("BEGIN DeleteUser(%d, %s)", user.ID, user.Username)
|
|
|
|
|
2024-04-12 06:27:20 +00:00
|
|
|
// Clear their history on the chat room.
|
|
|
|
go func() {
|
2024-04-13 17:44:09 +00:00
|
|
|
i, err := chat.EraseChatHistory(user.Username)
|
2024-04-12 06:27:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("EraseChatHistory(%s): %s", user.Username, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error("DeleteUser(%s): Cleared chat DMs history for user (%d messages erased)", user.Username, i)
|
|
|
|
}()
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// Remove all linked tables and assets.
|
|
|
|
type remover struct {
|
|
|
|
Step string
|
|
|
|
Fn func(uint64) error
|
|
|
|
}
|
|
|
|
|
2022-10-21 04:51:53 +00:00
|
|
|
// Blank out the user's profile photo ID to avoid conflict removing their picture.
|
|
|
|
user.RemoveProfilePhoto()
|
|
|
|
|
2023-10-24 02:05:34 +00:00
|
|
|
// Tables to remove. In case of any unexpected DB errors, these tables are ordered
|
|
|
|
// to remove the "safest" fields first.
|
2022-08-14 21:40:57 +00:00
|
|
|
var todo = []remover{
|
2024-08-24 04:21:42 +00:00
|
|
|
{"Admin group memberships", DeleteAdminGroupUsers},
|
|
|
|
{"Disown User Forums", DisownForums},
|
2022-08-25 04:17:34 +00:00
|
|
|
{"Notifications", DeleteNotifications},
|
|
|
|
{"Likes", DeleteLikes},
|
|
|
|
{"Threads", DeleteForumThreads},
|
|
|
|
{"Comments", DeleteComments},
|
2022-08-27 18:42:48 +00:00
|
|
|
{"Subscriptions", DeleteSubscriptions},
|
2022-08-14 21:40:57 +00:00
|
|
|
{"Photos", DeleteUserPhotos},
|
2023-09-02 05:41:33 +00:00
|
|
|
{"Private Photo Grants", DeletePrivateGrants},
|
2022-08-14 21:40:57 +00:00
|
|
|
{"Certification Photo", DeleteCertification},
|
2023-08-20 02:11:33 +00:00
|
|
|
{"Who's Nearby Locations", DeleteUserLocation},
|
2022-10-21 04:51:53 +00:00
|
|
|
{"Comment Photos", DeleteUserCommentPhotos},
|
2022-08-14 21:40:57 +00:00
|
|
|
{"Messages", DeleteUserMessages},
|
|
|
|
{"Friends", DeleteFriends},
|
2023-10-24 02:05:34 +00:00
|
|
|
{"Blocks", DeleteBlocks},
|
|
|
|
{"Feedbacks", DeleteFeedbacks},
|
|
|
|
{"Two Factor", DeleteTwoFactor},
|
2022-08-14 21:40:57 +00:00
|
|
|
{"Profile Fields", DeleteProfile},
|
2023-09-16 20:46:26 +00:00
|
|
|
{"User Notes", DeleteUserNotes},
|
2024-02-26 01:28:40 +00:00
|
|
|
{"Change Logs", DeleteChangeLogs},
|
2024-04-26 01:55:02 +00:00
|
|
|
{"IP Addresses", DeleteIPAddresses},
|
2024-07-21 02:44:22 +00:00
|
|
|
{"Push Notifications", DeletePushNotifications},
|
2024-08-24 04:21:42 +00:00
|
|
|
{"Forum Memberships", DeleteForumMemberships},
|
2024-09-12 02:28:52 +00:00
|
|
|
{"Usage Statistics", DeleteUsageStatistics},
|
2022-08-14 21:40:57 +00:00
|
|
|
}
|
|
|
|
for _, item := range todo {
|
|
|
|
if err := item.Fn(user.ID); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", item.Step, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the user itself.
|
|
|
|
return user.Delete()
|
|
|
|
}
|
|
|
|
|
2024-08-24 04:21:42 +00:00
|
|
|
// DeleteAdminGroupUsers scrubs data for deleting a user.
|
|
|
|
func DeleteAdminGroupUsers(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteAdminGroupUsers(%d)", userID)
|
|
|
|
result := models.DB.Exec(
|
|
|
|
"DELETE FROM admin_group_users WHERE user_id = ?",
|
|
|
|
userID,
|
|
|
|
)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// DeleteUserPhotos scrubs data for deleting a user.
|
|
|
|
func DeleteUserPhotos(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: BEGIN DeleteUserPhotos(%d)", userID)
|
|
|
|
|
|
|
|
// Deeply scrub all user photos.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 20,
|
|
|
|
Sort: "photos.id",
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
photos, err := models.PaginateUserPhotos(
|
|
|
|
userID,
|
2023-10-22 23:03:17 +00:00
|
|
|
models.UserGallery{
|
|
|
|
Visibility: models.PhotoVisibilityAll,
|
|
|
|
},
|
2022-08-14 21:40:57 +00:00
|
|
|
pager,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(photos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range photos {
|
|
|
|
log.Warn("DeleteUserPhotos(%d): remove file %s", userID, item.Filename)
|
|
|
|
photo.Delete(item.Filename)
|
|
|
|
if item.CroppedFilename != "" {
|
|
|
|
log.Warn("DeleteUserPhotos(%d): remove file %s", userID, item.CroppedFilename)
|
|
|
|
photo.Delete(item.CroppedFilename)
|
|
|
|
}
|
|
|
|
if err := item.Delete(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error("DeleteUser: END DeleteUserPhotos(%d)", userID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-21 04:51:53 +00:00
|
|
|
// DeleteUserCommentPhotos scrubs data for deleting a user.
|
|
|
|
func DeleteUserCommentPhotos(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: BEGIN DeleteUserCommentPhotos(%d)", userID)
|
|
|
|
|
|
|
|
// Deeply scrub all user photos.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 20,
|
|
|
|
Sort: "comment_photos.id",
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
photos, err := models.PaginateUserCommentPhotos(
|
|
|
|
userID,
|
|
|
|
pager,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(photos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range photos {
|
|
|
|
log.Warn("DeleteUserCommentPhotos(%d): remove file %s", userID, item.Filename)
|
|
|
|
photo.Delete(item.Filename)
|
|
|
|
if err := item.Delete(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error("DeleteUser: END DeleteUserPhotos(%d)", userID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// DeleteCertification scrubs data for deleting a user.
|
|
|
|
func DeleteCertification(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteCertification(%d)", userID)
|
|
|
|
if cert, err := models.GetCertificationPhoto(userID); err == nil {
|
|
|
|
if cert.Filename != "" {
|
|
|
|
log.Warn("DeleteCertification(%d): remove file %s", userID, cert.Filename)
|
|
|
|
photo.Delete(cert.Filename)
|
|
|
|
}
|
|
|
|
return cert.Delete()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 02:05:34 +00:00
|
|
|
// DeleteTwoFactor scrubs data for deleting a user.
|
|
|
|
func DeleteTwoFactor(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteTwoFactor(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.TwoFactor{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2023-08-20 02:11:33 +00:00
|
|
|
// DeleteUserLocation scrubs data for deleting a user.
|
|
|
|
func DeleteUserLocation(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteUserLocation(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.UserLocation{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// DeleteUserMessages scrubs data for deleting a user.
|
|
|
|
func DeleteUserMessages(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteUserMessages(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"source_user_id = ? OR target_user_id = ?",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.Message{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFriends scrubs data for deleting a user.
|
|
|
|
func DeleteFriends(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteUserFriends(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"source_user_id = ? OR target_user_id = ?",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.Friend{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2023-10-24 02:05:34 +00:00
|
|
|
// DeleteBlocks scrubs data for deleting a user.
|
|
|
|
func DeleteBlocks(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteBlocks(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"source_user_id = ? OR target_user_id = ?",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.Block{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFeedbacks scrubs data for deleting a user.
|
|
|
|
func DeleteFeedbacks(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteFeedbacks(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ? OR (table_name='users' AND table_id=?)",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.Feedback{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2023-09-02 05:41:33 +00:00
|
|
|
// DeletePrivateGrants scrubs data for deleting a user.
|
|
|
|
func DeletePrivateGrants(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeletePrivateGrants(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"source_user_id = ? OR target_user_id = ?",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.PrivatePhoto{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// DeleteNotifications scrubs all notifications about a user.
|
|
|
|
func DeleteNotifications(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteNotifications(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ? OR about_user_id = ?",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.Notification{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-27 18:42:48 +00:00
|
|
|
// DeleteSubscriptions scrubs all notification subscriptions about a user.
|
|
|
|
func DeleteSubscriptions(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteSubscriptions(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.Subscription{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// DeleteLikes scrubs all Likes about a user.
|
|
|
|
func DeleteLikes(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteLikes(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ? OR (table_name='users' AND table_id=?)",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.Like{})
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// DeleteProfile scrubs data for deleting a user.
|
|
|
|
func DeleteProfile(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteProfile(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.ProfileField{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2022-08-25 04:17:34 +00:00
|
|
|
|
|
|
|
// DeleteForumThreads scrubs all forum threads started by the user.
|
|
|
|
func DeleteForumThreads(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteForumThreads(%d)", userID)
|
|
|
|
|
|
|
|
var threadIDs = []uint64{}
|
|
|
|
result := models.DB.Table(
|
|
|
|
"threads",
|
|
|
|
).Joins(
|
|
|
|
"JOIN comments ON (threads.comment_id = comments.id)",
|
|
|
|
).Select(
|
|
|
|
"distinct(threads.id) as id",
|
|
|
|
).Where(
|
|
|
|
"comments.user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Scan(&threadIDs)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return fmt.Errorf("Couldn't list thread IDs created by user: %s", result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("thread IDs to wipe: %+v", threadIDs)
|
|
|
|
|
|
|
|
// Wipe all these threads and their comments.
|
|
|
|
if len(threadIDs) > 0 {
|
2023-02-25 00:54:27 +00:00
|
|
|
// First, delete the threads - so they won't be referring to comment_ids that we
|
|
|
|
// delete next and causing errors to arise.
|
|
|
|
result = models.DB.Where(
|
|
|
|
"id IN ?",
|
|
|
|
threadIDs,
|
|
|
|
).Delete(&models.Thread{})
|
|
|
|
if result.Error != nil {
|
|
|
|
return fmt.Errorf("Couldn't delete your forum threads: %s", result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the comments.
|
2022-08-25 04:17:34 +00:00
|
|
|
result = models.DB.Where(
|
|
|
|
"table_name = ? AND table_id IN ?",
|
|
|
|
"threads", threadIDs,
|
|
|
|
).Delete(&models.Comment{})
|
|
|
|
if result.Error != nil {
|
|
|
|
return fmt.Errorf("Couldn't wipe threads of comments: %s", result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteComments deletes all comments by the user.
|
|
|
|
func DeleteComments(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteComments(%d)", userID)
|
|
|
|
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.Comment{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2023-09-16 20:46:26 +00:00
|
|
|
|
|
|
|
// DeleteUserNotes scrubs data for deleting a user.
|
|
|
|
func DeleteUserNotes(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteUserNotes(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ? OR about_user_id = ?",
|
|
|
|
userID, userID,
|
|
|
|
).Delete(&models.UserNote{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2024-02-26 01:28:40 +00:00
|
|
|
|
|
|
|
// DeleteChangeLogs scrubs data for deleting a user.
|
|
|
|
func DeleteChangeLogs(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteChangeLogs(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"about_user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.ChangeLog{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2024-04-26 01:55:02 +00:00
|
|
|
|
|
|
|
// DeleteIPAddresses scrubs data for deleting a user.
|
|
|
|
func DeleteIPAddresses(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteIPAddresses(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.IPAddress{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2024-07-21 02:44:22 +00:00
|
|
|
|
|
|
|
// DeletePushNotifications scrubs data for deleting a user.
|
|
|
|
func DeletePushNotifications(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeletePushNotifications(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.PushNotification{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2024-08-24 04:21:42 +00:00
|
|
|
|
|
|
|
// DisownForums unlinks the user from their owned forums.
|
|
|
|
func DisownForums(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DisownForums(%d)", userID)
|
|
|
|
result := models.DB.Exec(`
|
|
|
|
UPDATE forums
|
|
|
|
SET owner_id = NULL
|
|
|
|
WHERE owner_id = ?
|
|
|
|
`, userID)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteForumMemberships scrubs data for deleting a user.
|
|
|
|
func DeleteForumMemberships(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteForumMemberships(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.ForumMembership{})
|
|
|
|
return result.Error
|
|
|
|
}
|
2024-09-12 02:28:52 +00:00
|
|
|
|
|
|
|
// DeleteUsageStatistics scrubs data for deleting a user.
|
|
|
|
func DeleteUsageStatistics(userID uint64) error {
|
|
|
|
log.Error("DeleteUser: DeleteUsageStatistics(%d)", userID)
|
|
|
|
result := models.DB.Where(
|
|
|
|
"user_id = ?",
|
|
|
|
userID,
|
|
|
|
).Delete(&models.UsageStatistic{})
|
|
|
|
return result.Error
|
|
|
|
}
|