2023-06-16 13:43:37 +00:00
|
|
|
package main
|
|
|
|
|
2023-06-18 18:38:09 +00:00
|
|
|
import (
|
2024-07-31 17:49:52 +00:00
|
|
|
"fmt"
|
2023-06-18 18:38:09 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/golang-jwt/jwt"
|
2024-07-30 19:55:45 +00:00
|
|
|
"github.com/hrfee/jfa-go/jellyseerr"
|
2024-07-31 17:49:52 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-10-19 17:56:35 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
2023-06-28 15:05:24 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-09-07 15:24:40 +00:00
|
|
|
REFERRAL_EXPIRY_DAYS = 90
|
2023-06-18 18:38:09 +00:00
|
|
|
)
|
2023-06-16 13:43:37 +00:00
|
|
|
|
2023-06-20 12:28:13 +00:00
|
|
|
// @Summary Returns the logged-in user's Jellyfin ID & Username, and other details.
|
2023-06-17 11:48:28 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} MyDetailsDTO
|
|
|
|
// @Router /my/details [get]
|
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) MyDetails(gc *gin.Context) {
|
|
|
|
resp := MyDetailsDTO{
|
|
|
|
Id: gc.GetString("jfId"),
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(resp.Id, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2023-06-17 11:48:28 +00:00
|
|
|
respond(500, "Failed to get user", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Username = user.Name
|
2023-06-17 16:26:36 +00:00
|
|
|
resp.Admin = user.Policy.IsAdministrator
|
2023-06-21 10:26:59 +00:00
|
|
|
resp.AccountsAdmin = false
|
|
|
|
if !app.config.Section("ui").Key("allow_all").MustBool(false) {
|
|
|
|
adminOnly := app.config.Section("ui").Key("admin_only").MustBool(true)
|
|
|
|
if emailStore, ok := app.storage.GetEmailsKey(resp.Id); ok {
|
|
|
|
resp.AccountsAdmin = emailStore.Admin
|
|
|
|
}
|
|
|
|
resp.AccountsAdmin = resp.AccountsAdmin || (adminOnly && resp.Admin)
|
|
|
|
}
|
2023-06-17 16:26:36 +00:00
|
|
|
resp.Disabled = user.Policy.IsDisabled
|
|
|
|
|
2023-06-24 18:13:05 +00:00
|
|
|
if exp, ok := app.storage.GetUserExpiryKey(user.ID); ok {
|
|
|
|
resp.Expiry = exp.Expiry.Unix()
|
2023-06-17 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if emailEnabled {
|
|
|
|
resp.Email = &MyDetailsContactMethodsDTO{}
|
2023-06-20 11:19:24 +00:00
|
|
|
if email, ok := app.storage.GetEmailsKey(user.ID); ok && email.Addr != "" {
|
2023-06-17 16:26:36 +00:00
|
|
|
resp.Email.Value = email.Addr
|
|
|
|
resp.Email.Enabled = email.Contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if discordEnabled {
|
|
|
|
resp.Discord = &MyDetailsContactMethodsDTO{}
|
2023-06-20 11:19:24 +00:00
|
|
|
if discord, ok := app.storage.GetDiscordKey(user.ID); ok {
|
2023-06-17 16:26:36 +00:00
|
|
|
resp.Discord.Value = RenderDiscordUsername(discord)
|
|
|
|
resp.Discord.Enabled = discord.Contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if telegramEnabled {
|
|
|
|
resp.Telegram = &MyDetailsContactMethodsDTO{}
|
2023-06-20 11:19:24 +00:00
|
|
|
if telegram, ok := app.storage.GetTelegramKey(user.ID); ok {
|
2023-06-17 16:26:36 +00:00
|
|
|
resp.Telegram.Value = telegram.Username
|
|
|
|
resp.Telegram.Enabled = telegram.Contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matrixEnabled {
|
|
|
|
resp.Matrix = &MyDetailsContactMethodsDTO{}
|
2023-06-20 11:19:24 +00:00
|
|
|
if matrix, ok := app.storage.GetMatrixKey(user.ID); ok {
|
2023-06-17 16:26:36 +00:00
|
|
|
resp.Matrix.Value = matrix.UserID
|
|
|
|
resp.Matrix.Enabled = matrix.Contact
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 11:48:28 +00:00
|
|
|
|
2023-09-07 15:24:40 +00:00
|
|
|
if app.config.Section("user_page").Key("referrals").MustBool(false) {
|
|
|
|
// 1. Look for existing template bound to this Jellyfin ID
|
|
|
|
// If one exists, that means its just for us and so we
|
|
|
|
// can use it directly.
|
|
|
|
inv := Invite{}
|
|
|
|
err := app.storage.db.FindOne(&inv, badgerhold.Where("ReferrerJellyfinID").Eq(resp.Id))
|
|
|
|
if err == nil {
|
|
|
|
resp.HasReferrals = true
|
|
|
|
} else {
|
|
|
|
// 2. Look for a template matching the key found in the user storage
|
|
|
|
// Since this key is shared between users in a profile, we make a copy.
|
|
|
|
user, ok := app.storage.GetEmailsKey(gc.GetString("jfId"))
|
|
|
|
err = app.storage.db.Get(user.ReferralTemplateKey, &inv)
|
|
|
|
if ok && err == nil {
|
|
|
|
resp.HasReferrals = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 11:48:28 +00:00
|
|
|
gc.JSON(200, resp)
|
2023-06-16 13:43:37 +00:00
|
|
|
}
|
2023-06-17 16:26:36 +00:00
|
|
|
|
|
|
|
// @Summary Sets whether to notify yourself through telegram/discord/matrix/email or not.
|
|
|
|
// @Produce json
|
|
|
|
// @Param SetContactMethodsDTO body SetContactMethodsDTO true "User's Jellyfin ID and whether or not to notify then through Telegram."
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Success 400 {object} boolResponse
|
|
|
|
// @Success 500 {object} boolResponse
|
|
|
|
// @Router /my/contact [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) SetMyContactMethods(gc *gin.Context) {
|
|
|
|
var req SetContactMethodsDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
req.ID = gc.GetString("jfId")
|
|
|
|
if req.ID == "" {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.setContactMethods(req, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Logout by deleting refresh token from cookies.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /my/logout [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) LogoutUser(gc *gin.Context) {
|
2023-06-18 11:30:23 +00:00
|
|
|
cookie, err := gc.Cookie("user-refresh")
|
2023-06-17 16:26:36 +00:00
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
msg := fmt.Sprintf(lm.FailedGetCookies, "user-refresh", err)
|
|
|
|
app.debug.Println(msg)
|
|
|
|
respond(500, msg, gc)
|
2023-06-17 16:26:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
app.invalidTokens = append(app.invalidTokens, cookie)
|
|
|
|
gc.SetCookie("refresh", "invalid", -1, "/my", gc.Request.URL.Hostname(), true, true)
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
2023-06-18 18:38:09 +00:00
|
|
|
|
|
|
|
// @Summary confirm an action (e.g. changing an email address.)
|
|
|
|
// @Produce json
|
|
|
|
// @Param jwt path string true "jwt confirmation code"
|
|
|
|
// @Router /my/confirm/{jwt} [post]
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Failure 404
|
|
|
|
// @Success 303
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) ConfirmMyAction(gc *gin.Context) {
|
|
|
|
app.confirmMyAction(gc, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) confirmMyAction(gc *gin.Context, key string) {
|
|
|
|
var claims jwt.MapClaims
|
|
|
|
var target ConfirmationTarget
|
|
|
|
var id string
|
|
|
|
fail := func() {
|
|
|
|
gcHTML(gc, 404, "404.html", gin.H{
|
|
|
|
"cssClass": app.cssClass,
|
|
|
|
"cssVersion": cssVersion,
|
|
|
|
"contactMessage": app.config.Section("ui").Key("contact_message").String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate key
|
|
|
|
if key == "" {
|
|
|
|
key = gc.Param("jwt")
|
|
|
|
}
|
|
|
|
token, err := jwt.Parse(key, checkToken)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedParseJWT, err)
|
2023-06-18 18:38:09 +00:00
|
|
|
fail()
|
|
|
|
// respond(500, "unknownError", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
|
|
if !ok {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Println(lm.FailedCastJWT)
|
2023-06-18 18:38:09 +00:00
|
|
|
fail()
|
|
|
|
// respond(500, "unknownError", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expiry := time.Unix(int64(claims["exp"].(float64)), 0)
|
|
|
|
if !(ok && token.Valid && claims["type"].(string) == "confirmation" && expiry.After(time.Now())) {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Println(lm.InvalidJWT)
|
2023-06-18 18:38:09 +00:00
|
|
|
fail()
|
|
|
|
// respond(400, "invalidKey", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
target = ConfirmationTarget(int(claims["target"].(float64)))
|
|
|
|
id = claims["id"].(string)
|
|
|
|
|
|
|
|
// Perform an Action
|
|
|
|
if target == NoOp {
|
|
|
|
gc.Redirect(http.StatusSeeOther, "/my/account")
|
|
|
|
return
|
|
|
|
} else if target == UserEmailChange {
|
2024-07-30 19:55:45 +00:00
|
|
|
app.modifyEmail(id, claims["email"].(string))
|
2023-10-19 20:13:00 +00:00
|
|
|
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactLinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "email",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2024-07-31 17:49:52 +00:00
|
|
|
app.info.Printf(lm.UserEmailAdjusted, gc.GetString("jfId"))
|
2023-06-18 18:38:09 +00:00
|
|
|
gc.Redirect(http.StatusSeeOther, "/my/account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Modify your email address.
|
|
|
|
// @Produce json
|
|
|
|
// @Param ModifyMyEmailDTO body ModifyMyEmailDTO true "New email address."
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Failure 401 {object} stringResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /my/email [post]
|
|
|
|
// @Security Bearer
|
2023-06-18 20:38:12 +00:00
|
|
|
// @tags User Page
|
2023-06-18 18:38:09 +00:00
|
|
|
func (app *appContext) ModifyMyEmail(gc *gin.Context) {
|
|
|
|
var req ModifyMyEmailDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
if !strings.ContainsRune(req.Email, '@') {
|
|
|
|
respond(400, "Invalid Email Address", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := gc.GetString("jfId")
|
|
|
|
|
|
|
|
// We'll use the ConfirmMyAction route to do the work, even if we don't need to confirm the address.
|
|
|
|
claims := jwt.MapClaims{
|
|
|
|
"valid": true,
|
|
|
|
"id": id,
|
|
|
|
"email": req.Email,
|
|
|
|
"type": "confirmation",
|
|
|
|
"target": UserEmailChange,
|
|
|
|
"exp": time.Now().Add(time.Hour).Unix(),
|
|
|
|
}
|
|
|
|
tk := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
key, err := tk.SignedString([]byte(os.Getenv("JFA_SECRET")))
|
|
|
|
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSignJWT, err)
|
2023-06-18 18:38:09 +00:00
|
|
|
respond(500, "errorUnknown", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if emailEnabled && app.config.Section("email_confirmation").Key("enabled").MustBool(false) {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(id, false)
|
2023-06-18 18:38:09 +00:00
|
|
|
name := ""
|
2024-08-06 13:48:31 +00:00
|
|
|
if err == nil {
|
2023-06-18 18:38:09 +00:00
|
|
|
name = user.Name
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.EmailConfirmationRequired, id)
|
2023-06-18 18:38:09 +00:00
|
|
|
respond(401, "confirmEmail", gc)
|
|
|
|
msg, err := app.email.constructConfirmation("", name, key, app, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructConfirmationEmail, id, err)
|
2023-06-18 18:38:09 +00:00
|
|
|
} else if err := app.email.send(msg, req.Email); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendConfirmationEmail, id, req.Email, err)
|
2023-06-18 18:38:09 +00:00
|
|
|
} else {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.SentConfirmationEmail, id, req.Email)
|
2023-06-18 18:38:09 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
app.confirmMyAction(gc, key)
|
|
|
|
return
|
|
|
|
}
|
2023-06-18 20:38:12 +00:00
|
|
|
|
|
|
|
// @Summary Returns a 10-minute, one-use Discord server invite
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} DiscordInviteDTO
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Param invCode path string true "invite Code"
|
|
|
|
// @Router /my/discord/invite [get]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
2023-06-18 20:38:12 +00:00
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) MyDiscordServerInvite(gc *gin.Context) {
|
2024-08-03 20:23:59 +00:00
|
|
|
if app.discord.InviteChannel.Name == "" {
|
2023-06-18 20:38:12 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
invURL, iconURL := app.discord.NewTempInvite(10*60, 1)
|
|
|
|
if invURL == "" {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc.JSON(200, DiscordInviteDTO{invURL, iconURL})
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Returns a linking PIN for discord/telegram
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} GetMyPINDTO
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// Param service path string true "discord/telegram"
|
|
|
|
// @Router /my/pin/{service} [get]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
2023-06-18 20:38:12 +00:00
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) GetMyPIN(gc *gin.Context) {
|
|
|
|
service := gc.Param("service")
|
|
|
|
resp := GetMyPINDTO{}
|
|
|
|
switch service {
|
|
|
|
case "discord":
|
2023-06-21 17:26:08 +00:00
|
|
|
resp.PIN = app.discord.NewAssignedAuthToken(gc.GetString("jfId"))
|
2023-06-18 20:38:12 +00:00
|
|
|
break
|
|
|
|
case "telegram":
|
2023-06-21 17:26:08 +00:00
|
|
|
resp.PIN = app.telegram.NewAssignedAuthToken(gc.GetString("jfId"))
|
2023-06-18 20:38:12 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
respond(400, "invalid service", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Returns true/false on whether or not your discord PIN was verified, and assigns the discord user to you.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Param pin path string true "PIN code to check"
|
|
|
|
// @Router /my/discord/verified/{pin} [get]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
2023-06-18 20:38:12 +00:00
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) MyDiscordVerifiedInvite(gc *gin.Context) {
|
|
|
|
pin := gc.Param("pin")
|
2023-06-21 17:26:08 +00:00
|
|
|
dcUser, ok := app.discord.AssignedUserVerified(pin, gc.GetString("jfId"))
|
2024-08-03 20:23:59 +00:00
|
|
|
app.discord.DeleteVerifiedToken(pin)
|
2023-06-18 20:38:12 +00:00
|
|
|
if !ok {
|
|
|
|
respondBool(200, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
if app.config.Section("discord").Key("require_unique").MustBool(false) && app.discord.UserExists(dcUser.ID) {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
2023-06-18 20:38:12 +00:00
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
existingUser, ok := app.storage.GetDiscordKey(gc.GetString("jfId"))
|
2023-06-19 21:11:35 +00:00
|
|
|
if ok {
|
|
|
|
dcUser.Lang = existingUser.Lang
|
|
|
|
dcUser.Contact = existingUser.Contact
|
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
app.storage.SetDiscordKey(gc.GetString("jfId"), dcUser)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2024-07-30 19:55:45 +00:00
|
|
|
if err := app.js.ModifyNotifications(gc.GetString("jfId"), map[jellyseerr.NotificationsField]any{
|
|
|
|
jellyseerr.FieldDiscord: dcUser.ID,
|
|
|
|
jellyseerr.FieldDiscordEnabled: dcUser.Contact,
|
|
|
|
}); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:13:00 +00:00
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactLinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "discord",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2023-06-18 20:38:12 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
2023-06-19 21:11:35 +00:00
|
|
|
|
|
|
|
// @Summary Returns true/false on whether or not your telegram PIN was verified, and assigns the telegram user to you.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Param pin path string true "PIN code to check"
|
|
|
|
// @Router /my/telegram/verified/{pin} [get]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
2023-06-19 21:11:35 +00:00
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) MyTelegramVerifiedInvite(gc *gin.Context) {
|
|
|
|
pin := gc.Param("pin")
|
2023-06-21 17:26:08 +00:00
|
|
|
token, ok := app.telegram.AssignedTokenVerified(pin, gc.GetString("jfId"))
|
2023-06-21 16:59:58 +00:00
|
|
|
app.telegram.DeleteVerifiedToken(pin)
|
|
|
|
if !ok {
|
2023-06-19 21:11:35 +00:00
|
|
|
respondBool(200, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
if app.config.Section("telegram").Key("require_unique").MustBool(false) && app.telegram.UserExists(token.Username) {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
2023-06-19 21:11:35 +00:00
|
|
|
}
|
|
|
|
tgUser := TelegramUser{
|
2023-06-21 16:59:58 +00:00
|
|
|
ChatID: token.ChatID,
|
|
|
|
Username: token.Username,
|
2023-06-19 21:11:35 +00:00
|
|
|
Contact: true,
|
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
if lang, ok := app.telegram.languages[tgUser.ChatID]; ok {
|
|
|
|
tgUser.Lang = lang
|
|
|
|
}
|
2023-06-19 21:11:35 +00:00
|
|
|
|
2023-06-20 11:19:24 +00:00
|
|
|
existingUser, ok := app.storage.GetTelegramKey(gc.GetString("jfId"))
|
2023-06-19 21:11:35 +00:00
|
|
|
if ok {
|
|
|
|
tgUser.Lang = existingUser.Lang
|
|
|
|
tgUser.Contact = existingUser.Contact
|
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
app.storage.SetTelegramKey(gc.GetString("jfId"), tgUser)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2024-07-30 19:55:45 +00:00
|
|
|
if err := app.js.ModifyNotifications(gc.GetString("jfId"), map[jellyseerr.NotificationsField]any{
|
|
|
|
jellyseerr.FieldTelegram: tgUser.ChatID,
|
|
|
|
jellyseerr.FieldTelegramEnabled: tgUser.Contact,
|
|
|
|
}); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:13:00 +00:00
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactLinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "telegram",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2023-06-19 21:11:35 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
2023-06-20 12:28:13 +00:00
|
|
|
|
|
|
|
// @Summary Generate and send a new PIN to your given matrix user.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Param MatrixSendPINDTO body MatrixSendPINDTO true "User's Matrix ID."
|
|
|
|
// @Router /my/matrix/user [post]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
2023-06-20 12:28:13 +00:00
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) MatrixSendMyPIN(gc *gin.Context) {
|
|
|
|
var req MatrixSendPINDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
if req.UserID == "" {
|
|
|
|
respond(400, "errorNoUserID", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.config.Section("matrix").Key("require_unique").MustBool(false) {
|
|
|
|
for _, u := range app.storage.GetMatrix() {
|
|
|
|
if req.UserID == u.UserID {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := app.matrix.SendStart(req.UserID)
|
|
|
|
if !ok {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Check whether your matrix PIN is valid, and link the account to yours if so.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Param pin path string true "PIN code to check"
|
|
|
|
// @Param invCode path string true "invite Code"
|
|
|
|
// @Param userID path string true "Matrix User ID"
|
|
|
|
// @Router /my/matrix/verified/{userID}/{pin} [get]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
2023-06-20 12:28:13 +00:00
|
|
|
// @tags User Page
|
|
|
|
func (app *appContext) MatrixCheckMyPIN(gc *gin.Context) {
|
|
|
|
userID := gc.Param("userID")
|
|
|
|
pin := gc.Param("pin")
|
|
|
|
user, ok := app.matrix.tokens[pin]
|
|
|
|
if !ok {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.InvalidPIN, pin)
|
2023-06-20 12:28:13 +00:00
|
|
|
respondBool(200, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if user.User.UserID != userID {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.UnauthorizedPIN, pin)
|
2023-06-20 12:28:13 +00:00
|
|
|
respondBool(200, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mxUser := *user.User
|
|
|
|
mxUser.Contact = true
|
|
|
|
existingUser, ok := app.storage.GetMatrixKey(gc.GetString("jfId"))
|
|
|
|
if ok {
|
|
|
|
mxUser.Lang = existingUser.Lang
|
|
|
|
mxUser.Contact = existingUser.Contact
|
|
|
|
}
|
|
|
|
|
|
|
|
app.storage.SetMatrixKey(gc.GetString("jfId"), mxUser)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactLinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "matrix",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2023-06-20 12:28:13 +00:00
|
|
|
delete(app.matrix.tokens, pin)
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
2023-06-20 15:44:12 +00:00
|
|
|
|
|
|
|
// @Summary unlink the Discord account from your Jellyfin user. Always succeeds.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /my/discord [delete]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
|
|
|
// @Tags User Page
|
2023-06-20 15:44:12 +00:00
|
|
|
func (app *appContext) UnlinkMyDiscord(gc *gin.Context) {
|
|
|
|
app.storage.DeleteDiscordKey(gc.GetString("jfId"))
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2024-07-30 19:55:45 +00:00
|
|
|
if err := app.js.ModifyNotifications(gc.GetString("jfId"), map[jellyseerr.NotificationsField]any{
|
|
|
|
jellyseerr.FieldDiscord: jellyseerr.BogusIdentifier,
|
|
|
|
jellyseerr.FieldDiscordEnabled: false,
|
|
|
|
}); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:13:00 +00:00
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactUnlinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "discord",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2023-06-20 15:44:12 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary unlink the Telegram account from your Jellyfin user. Always succeeds.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /my/telegram [delete]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
|
|
|
// @Tags User Page
|
2023-06-20 15:44:12 +00:00
|
|
|
func (app *appContext) UnlinkMyTelegram(gc *gin.Context) {
|
|
|
|
app.storage.DeleteTelegramKey(gc.GetString("jfId"))
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2024-07-30 19:55:45 +00:00
|
|
|
if err := app.js.ModifyNotifications(gc.GetString("jfId"), map[jellyseerr.NotificationsField]any{
|
|
|
|
jellyseerr.FieldTelegram: jellyseerr.BogusIdentifier,
|
|
|
|
jellyseerr.FieldTelegramEnabled: false,
|
|
|
|
}); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:13:00 +00:00
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactUnlinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "telegram",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2023-06-20 15:44:12 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary unlink the Matrix account from your Jellyfin user. Always succeeds.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /my/matrix [delete]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Security Bearer
|
|
|
|
// @Tags User Page
|
2023-06-20 15:44:12 +00:00
|
|
|
func (app *appContext) UnlinkMyMatrix(gc *gin.Context) {
|
|
|
|
app.storage.DeleteMatrixKey(gc.GetString("jfId"))
|
2023-10-19 20:13:00 +00:00
|
|
|
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityContactUnlinked,
|
|
|
|
UserID: gc.GetString("jfId"),
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "matrix",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2023-06-20 15:44:12 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
|
2023-06-22 11:33:40 +00:00
|
|
|
// @Summary Generate & send a password reset link if the given username/email/contact method exists. Doesn't give you any info about it's success.
|
2023-06-22 11:04:40 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Param address path string true "address/contact method associated w/ your account."
|
|
|
|
// @Success 204 {object} boolResponse
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /my/password/reset/{address} [post]
|
2023-06-22 19:54:52 +00:00
|
|
|
// @Tags User Page
|
2023-06-22 11:04:40 +00:00
|
|
|
func (app *appContext) ResetMyPassword(gc *gin.Context) {
|
2023-06-22 11:25:00 +00:00
|
|
|
// All requests should take 1 second, to make it harder to tell if a success occured or not.
|
|
|
|
timerWait := make(chan bool)
|
|
|
|
cancel := time.AfterFunc(1*time.Second, func() {
|
|
|
|
timerWait <- true
|
|
|
|
})
|
2023-12-20 18:18:39 +00:00
|
|
|
usernameAllowed := app.config.Section("user_page").Key("allow_pwr_username").MustBool(true)
|
|
|
|
emailAllowed := app.config.Section("user_page").Key("allow_pwr_email").MustBool(true)
|
|
|
|
contactMethodAllowed := app.config.Section("user_page").Key("allow_pwr_contact_method").MustBool(true)
|
2023-06-22 11:04:40 +00:00
|
|
|
address := gc.Param("address")
|
|
|
|
if address == "" {
|
2023-06-22 11:25:00 +00:00
|
|
|
cancel.Stop()
|
2023-06-22 11:04:40 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pwr InternalPWR
|
|
|
|
var err error
|
|
|
|
|
2023-12-20 18:18:39 +00:00
|
|
|
jfUser, ok := app.ReverseUserSearch(address, usernameAllowed, emailAllowed, contactMethodAllowed)
|
2023-06-24 16:01:52 +00:00
|
|
|
if !ok {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.FailedGetUsers, lm.Jellyfin, "no results")
|
2023-06-22 11:25:00 +00:00
|
|
|
|
|
|
|
for range timerWait {
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
pwr, err = app.GenInternalReset(jfUser.ID)
|
2023-06-22 11:04:40 +00:00
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2023-06-22 11:25:00 +00:00
|
|
|
for range timerWait {
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.internalPWRs == nil {
|
|
|
|
app.internalPWRs = map[string]InternalPWR{}
|
|
|
|
}
|
|
|
|
app.internalPWRs[pwr.PIN] = pwr
|
|
|
|
// FIXME: Send to all contact methods
|
|
|
|
msg, err := app.email.constructReset(
|
|
|
|
PasswordReset{
|
|
|
|
Pin: pwr.PIN,
|
|
|
|
Username: pwr.Username,
|
|
|
|
Expiry: pwr.Expiry,
|
|
|
|
Internal: true,
|
|
|
|
}, app, false,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructPWRMessage, pwr.Username, err)
|
2023-06-22 11:25:00 +00:00
|
|
|
for range timerWait {
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
return
|
2023-06-24 16:01:52 +00:00
|
|
|
} else if err := app.sendByID(msg, jfUser.ID); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendPWRMessage, pwr.Username, "?", err)
|
2023-06-22 11:04:40 +00:00
|
|
|
} else {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.info.Printf(lm.SentPWRMessage, pwr.Username, "?")
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
2023-06-22 11:25:00 +00:00
|
|
|
for range timerWait {
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
2023-06-22 19:54:52 +00:00
|
|
|
|
|
|
|
// @Summary Change your password, given the old one and the new one.
|
|
|
|
// @Produce json
|
|
|
|
// @Param ChangeMyPasswordDTO body ChangeMyPasswordDTO true "User's old & new passwords."
|
|
|
|
// @Success 204 {object} boolResponse
|
|
|
|
// @Failure 400 {object} PasswordValidation
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /my/password [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @Tags User Page
|
|
|
|
func (app *appContext) ChangeMyPassword(gc *gin.Context) {
|
|
|
|
var req ChangeMyPasswordDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
if req.Old == "" || req.New == "" {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
}
|
|
|
|
validation := app.validator.validate(req.New)
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
gc.JSON(400, validation)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(gc.GetString("jfId"), false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, gc.GetString("jfId"), lm.Jellyfin, err)
|
2023-06-22 19:54:52 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Authenticate as user to confirm old password.
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.authJf.Authenticate(user.Name, req.Old)
|
|
|
|
if err != nil {
|
2023-06-22 19:54:52 +00:00
|
|
|
respondBool(401, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.jf.SetPassword(gc.GetString("jfId"), req.Old, req.New)
|
|
|
|
if err != nil {
|
2023-06-22 19:54:52 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityChangePassword,
|
|
|
|
UserID: user.ID,
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: user.ID,
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2023-06-22 19:54:52 +00:00
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
2023-06-22 19:58:56 +00:00
|
|
|
func() {
|
2024-08-06 13:48:31 +00:00
|
|
|
ombiUser, err := app.getOmbiUser(gc.GetString("jfId"))
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, user.Name, lm.Ombi, err)
|
2023-06-22 19:58:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ombiUser["password"] = req.New
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.ombi.ModifyUser(ombiUser)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedChangePassword, lm.Ombi, ombiUser["userName"], err)
|
2023-06-22 19:58:56 +00:00
|
|
|
return
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.ChangePassword, lm.Ombi, ombiUser["userName"])
|
2023-06-22 19:58:56 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
cookie, err := gc.Cookie("user-refresh")
|
|
|
|
if err == nil {
|
|
|
|
app.invalidTokens = append(app.invalidTokens, cookie)
|
|
|
|
gc.SetCookie("refresh", "invalid", -1, "/my", gc.Request.URL.Hostname(), true, true)
|
|
|
|
} else {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.FailedGetCookies, "user-refresh", err)
|
2023-06-22 19:54:52 +00:00
|
|
|
}
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
}
|
2023-06-28 15:05:24 +00:00
|
|
|
|
|
|
|
// @Summary Get or generate a new referral code.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} GetMyReferralRespDTO
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 401 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /my/referral [get]
|
|
|
|
// @Security Bearer
|
|
|
|
// @Tags User Page
|
|
|
|
func (app *appContext) GetMyReferral(gc *gin.Context) {
|
|
|
|
// 1. Look for existing template bound to this Jellyfin ID
|
|
|
|
// If one exists, that means its just for us and so we
|
|
|
|
// can use it directly.
|
|
|
|
inv := Invite{}
|
2023-09-06 21:46:16 +00:00
|
|
|
err := app.storage.db.FindOne(&inv, badgerhold.Where("ReferrerJellyfinID").Eq(gc.GetString("jfId")))
|
2023-06-28 15:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
// 2. Look for a template matching the key found in the user storage
|
2023-09-06 21:00:44 +00:00
|
|
|
// Since this key is shared between users in a profile, we make a copy.
|
2023-06-28 15:05:24 +00:00
|
|
|
user, ok := app.storage.GetEmailsKey(gc.GetString("jfId"))
|
|
|
|
err = app.storage.db.Get(user.ReferralTemplateKey, &inv)
|
2023-11-10 15:07:29 +00:00
|
|
|
if !ok || err != nil || user.ReferralTemplateKey == "" {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.FailedGetReferralTemplate, user.ReferralTemplateKey, err)
|
2023-06-28 15:05:24 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-10-11 10:30:28 +00:00
|
|
|
inv.Code = GenerateInviteCode()
|
2023-11-10 15:07:29 +00:00
|
|
|
expiryDelta := inv.ValidTill.Sub(inv.Created)
|
2023-06-28 15:05:24 +00:00
|
|
|
inv.Created = time.Now()
|
2023-11-10 15:07:29 +00:00
|
|
|
if inv.UseReferralExpiry {
|
|
|
|
inv.ValidTill = inv.Created.Add(expiryDelta)
|
|
|
|
} else {
|
|
|
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
|
|
|
}
|
2023-06-28 15:05:24 +00:00
|
|
|
inv.IsReferral = true
|
2023-09-06 21:46:16 +00:00
|
|
|
inv.ReferrerJellyfinID = gc.GetString("jfId")
|
2023-06-28 15:05:24 +00:00
|
|
|
app.storage.SetInvitesKey(inv.Code, inv)
|
|
|
|
} else if time.Now().After(inv.ValidTill) {
|
|
|
|
// 3. We found an invite for us, but it's expired.
|
|
|
|
// We delete it from storage, and put it back with a fresh code and expiry.
|
2023-11-10 15:07:29 +00:00
|
|
|
// If UseReferralExpiry is enabled, we delete it and return nothing.
|
2023-06-28 15:05:24 +00:00
|
|
|
app.storage.DeleteInvitesKey(inv.Code)
|
2023-11-10 15:07:29 +00:00
|
|
|
if inv.UseReferralExpiry {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.DeleteOldReferral, inv.Code)
|
2023-11-10 15:07:29 +00:00
|
|
|
user, ok := app.storage.GetEmailsKey(gc.GetString("jfId"))
|
|
|
|
if ok {
|
|
|
|
user.ReferralTemplateKey = ""
|
|
|
|
app.storage.SetEmailsKey(gc.GetString("jfId"), user)
|
|
|
|
}
|
|
|
|
app.debug.Printf("Ignoring referral request, expired.")
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.RenewOldReferral, inv.Code)
|
2023-10-11 10:30:28 +00:00
|
|
|
inv.Code = GenerateInviteCode()
|
2023-06-28 15:05:24 +00:00
|
|
|
inv.Created = time.Now()
|
|
|
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
|
|
|
app.storage.SetInvitesKey(inv.Code, inv)
|
|
|
|
}
|
|
|
|
gc.JSON(200, GetMyReferralRespDTO{
|
|
|
|
Code: inv.Code,
|
|
|
|
RemainingUses: inv.RemainingUses,
|
|
|
|
NoLimit: inv.NoLimit,
|
2023-09-07 15:24:40 +00:00
|
|
|
Expiry: inv.ValidTill.Unix(),
|
2023-11-10 15:07:29 +00:00
|
|
|
UseExpiry: inv.UseReferralExpiry,
|
2023-06-28 15:05:24 +00:00
|
|
|
})
|
|
|
|
}
|