2023-05-24 03:04:17 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2023-08-02 03:39:48 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2023-05-24 03:04:17 +00:00
|
|
|
"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 {
|
2023-08-02 03:39:48 +00:00
|
|
|
return u.InnerCircle || u.HasAdminScope(config.ScopeIsInnerCircle)
|
2023-05-24 03:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2023-07-23 22:02:41 +00:00
|
|
|
).Update("visibility", PhotoFriends); err != nil {
|
2023-08-02 03:39:48 +00:00
|
|
|
log.Error("RemoveFromInnerCircle: couldn't update photo visibility: %s", err.Error)
|
2023-05-24 03:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke any historic notification about the circle.
|
|
|
|
RemoveNotification("__inner_circle", u.ID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|