website/pkg/models/user_inner_circle.go
2024-04-13 15:10:15 -07:00

69 lines
1.8 KiB
Go

package models
import (
"errors"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log"
)
// Helper functions relating to the inner circle.
// IsInnerCircle returns whether the user is in the inner circle (including if the user is an admin, who is always in the inner circle).
func (u *User) IsInnerCircle() bool {
return u.InnerCircle || u.HasAdminScope(config.ScopeIsInnerCircle)
}
// AddToInnerCircle adds a user to the circle, sending them a notification in the process.
func AddToInnerCircle(u *User) error {
if u.InnerCircle {
return errors.New("already a part of the inner circle")
}
u.InnerCircle = true
if err := u.Save(); err != nil {
return err
}
// Send them a notification.
notif := &Notification{
UserID: u.ID,
AboutUserID: &u.ID,
Type: NotificationInnerCircle,
Link: "/inner-circle",
TableName: "__inner_circle",
TableID: u.ID,
}
if err := CreateNotification(notif); err != nil {
log.Error("AddToInnerCircle: couldn't create notification: %s", err)
}
return nil
}
// RemoveFromInnerCircle kicks a user from the inner circle. Any photo they
// had that was marked circle-only is updated to public.
func RemoveFromInnerCircle(u *User) error {
if !u.InnerCircle {
return errors.New("is not a part of the inner circle")
}
u.InnerCircle = false
if err := u.Save(); err != nil {
return err
}
// Update their circle-only photos to public.
if err := DB.Model(&Photo{}).Where(
"user_id = ? AND visibility = ?",
u.ID, PhotoInnerCircle,
).Update("visibility", PhotoPrivate); err != nil {
log.Error("RemoveFromInnerCircle: couldn't update photo visibility: %s", err.Error)
}
// Revoke any historic notification about the circle.
RemoveNotification("__inner_circle", u.ID)
return nil
}