2020-02-18 21:43:30 +00:00
|
|
|
//
|
|
|
|
// User.swift
|
2022-08-07 13:31:15 +01:00
|
|
|
// Mixonomer
|
2020-02-18 21:43:30 +00:00
|
|
|
//
|
|
|
|
// Created by Andy Pack on 18/02/2020.
|
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import SwiftyJSON
|
2022-11-23 23:39:58 +00:00
|
|
|
import OSLog
|
2020-02-18 21:43:30 +00:00
|
|
|
|
2020-03-06 23:36:51 +00:00
|
|
|
enum UserType: String, Decodable {
|
2020-02-18 21:43:30 +00:00
|
|
|
case user = "user"
|
|
|
|
case admin = "admin"
|
|
|
|
}
|
|
|
|
|
2022-12-09 08:51:42 +00:00
|
|
|
class User: Identifiable, Decodable, ObservableObject {
|
2020-02-18 21:43:30 +00:00
|
|
|
|
|
|
|
//MARK: Properties
|
|
|
|
|
|
|
|
var username: String
|
|
|
|
var email: String?
|
|
|
|
var type: UserType
|
|
|
|
|
2022-08-15 19:24:56 +01:00
|
|
|
var locked: Bool
|
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
var last_login: String
|
2022-08-11 20:15:21 +01:00
|
|
|
var last_keygen: String
|
2022-08-15 19:24:56 +01:00
|
|
|
var last_refreshed: String
|
2022-08-11 20:15:21 +01:00
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
var spotify_linked: Bool
|
2022-12-09 08:51:42 +00:00
|
|
|
|
|
|
|
@Published var notify: Bool {
|
|
|
|
didSet {
|
|
|
|
self.updateUser(updates: JSON(["notify": self.notify]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Published var notify_playlist_updates: Bool {
|
|
|
|
didSet {
|
|
|
|
self.updateUser(updates: JSON(["notify_playlist_updates": self.notify_playlist_updates]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Published var notify_tag_updates: Bool {
|
|
|
|
didSet {
|
|
|
|
self.updateUser(updates: JSON(["notify_tag_updates": self.notify_tag_updates]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Published var notify_admins: Bool {
|
|
|
|
didSet {
|
|
|
|
self.updateUser(updates: JSON(["notify_admins": self.notify_admins]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Published var lastfm_username: String {
|
2022-08-16 18:01:17 +01:00
|
|
|
didSet {
|
|
|
|
self.updateUser(updates: JSON(["lastfm_username": self.lastfm_username]))
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 21:43:30 +00:00
|
|
|
|
|
|
|
//MARK: Initialization
|
|
|
|
|
2022-08-15 19:24:56 +01:00
|
|
|
init(username: String = "user",
|
|
|
|
email: String? = nil,
|
2020-02-18 21:43:30 +00:00
|
|
|
type: UserType = .user,
|
2022-08-15 19:24:56 +01:00
|
|
|
|
|
|
|
locked: Bool = false,
|
|
|
|
|
|
|
|
last_login: String = "",
|
|
|
|
last_keygen: String = "",
|
|
|
|
last_refreshed: String = "",
|
|
|
|
spotify_linked: Bool = true,
|
2022-12-09 08:51:42 +00:00
|
|
|
lastfm_username: String = "",
|
|
|
|
|
|
|
|
notify: Bool = false,
|
|
|
|
notify_playlist_updates: Bool = false,
|
|
|
|
notify_tag_updates: Bool = false,
|
|
|
|
notify_admins: Bool = false){
|
2020-02-18 21:43:30 +00:00
|
|
|
|
|
|
|
self.username = username
|
|
|
|
self.email = email
|
|
|
|
self.type = type
|
|
|
|
|
2022-08-15 19:24:56 +01:00
|
|
|
self.locked = locked
|
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
self.last_login = last_login
|
2022-08-11 20:15:21 +01:00
|
|
|
self.last_keygen = last_keygen
|
2022-08-15 19:24:56 +01:00
|
|
|
self.last_refreshed = last_refreshed
|
2020-02-18 21:43:30 +00:00
|
|
|
self.spotify_linked = spotify_linked
|
|
|
|
self.lastfm_username = lastfm_username
|
2022-12-09 08:51:42 +00:00
|
|
|
|
|
|
|
self.notify = notify
|
|
|
|
self.notify_playlist_updates = notify_playlist_updates
|
|
|
|
self.notify_tag_updates = notify_tag_updates
|
|
|
|
self.notify_admins = notify_admins
|
2022-08-13 22:07:59 +01:00
|
|
|
}
|
2022-12-09 08:51:42 +00:00
|
|
|
|
2022-08-16 18:01:17 +01:00
|
|
|
func updateUser(updates: JSON) {
|
|
|
|
let api = UserApi.updateUser(updates: updates)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
2022-12-09 08:51:42 +00:00
|
|
|
|
|
|
|
if !NetworkHelper.check_network_response(response: response) {
|
2022-11-23 23:39:58 +00:00
|
|
|
Logger.net.error("error while updating user: \(updates)")
|
2022-08-16 18:01:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case username
|
|
|
|
case email
|
|
|
|
case type
|
|
|
|
|
2022-08-15 19:24:56 +01:00
|
|
|
case locked
|
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
case last_login
|
|
|
|
case last_keygen
|
2022-08-15 19:24:56 +01:00
|
|
|
case last_refreshed
|
2022-08-13 22:07:59 +01:00
|
|
|
|
|
|
|
case spotify_linked
|
|
|
|
case lastfm_username
|
2022-12-09 08:51:42 +00:00
|
|
|
|
|
|
|
case notify
|
|
|
|
case notify_playlist_updates
|
|
|
|
case notify_tag_updates
|
|
|
|
case notify_admins
|
2022-08-13 22:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
required init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
|
|
|
username = try container.decode(String.self, forKey: .username)
|
|
|
|
do{
|
|
|
|
email = try container.decode(String.self, forKey: .email)
|
|
|
|
}catch {
|
|
|
|
email = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type = try container.decode(UserType.self, forKey: .type)
|
2022-08-15 19:24:56 +01:00
|
|
|
locked = try container.decode(Bool.self, forKey: .locked)
|
2022-08-13 22:07:59 +01:00
|
|
|
|
|
|
|
last_login = try container.decode(String.self, forKey: .last_login)
|
2022-08-14 13:14:21 +01:00
|
|
|
do{
|
|
|
|
last_keygen = try container.decode(String.self, forKey: .last_keygen)
|
|
|
|
}catch {
|
|
|
|
last_keygen = ""
|
|
|
|
}
|
2022-08-15 19:24:56 +01:00
|
|
|
do{
|
|
|
|
last_refreshed = try container.decode(String.self, forKey: .last_refreshed)
|
|
|
|
}catch {
|
|
|
|
last_refreshed = ""
|
|
|
|
}
|
2022-08-13 22:07:59 +01:00
|
|
|
|
|
|
|
spotify_linked = try container.decode(Bool.self, forKey: .spotify_linked)
|
2022-08-14 13:14:21 +01:00
|
|
|
do{
|
|
|
|
lastfm_username = try container.decode(String.self, forKey: .lastfm_username)
|
|
|
|
}catch {
|
2022-12-09 08:51:42 +00:00
|
|
|
lastfm_username = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
notify = try container.decode(Bool.self, forKey: .notify)
|
|
|
|
}catch {
|
|
|
|
notify = false
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
notify_playlist_updates = try container.decode(Bool.self, forKey: .notify_playlist_updates)
|
|
|
|
}catch {
|
|
|
|
notify_playlist_updates = false
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
notify_tag_updates = try container.decode(Bool.self, forKey: .notify_tag_updates)
|
|
|
|
}catch {
|
|
|
|
notify_tag_updates = false
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
notify_admins = try container.decode(Bool.self, forKey: .notify_admins)
|
|
|
|
}catch {
|
|
|
|
notify_admins = false
|
2022-08-14 13:14:21 +01:00
|
|
|
}
|
2022-08-13 22:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
|
|
|
try container.encode(self.username, forKey: .username)
|
|
|
|
try container.encode(self.email, forKey: .email)
|
|
|
|
try container.encode(self.type.rawValue, forKey: .type)
|
|
|
|
|
2022-08-15 19:24:56 +01:00
|
|
|
try container.encode(self.locked, forKey: .locked)
|
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
try container.encode(self.last_login, forKey: .last_login)
|
|
|
|
try container.encode(self.last_keygen, forKey: .last_keygen)
|
2022-08-15 19:24:56 +01:00
|
|
|
try container.encode(self.last_refreshed, forKey: .last_refreshed)
|
2022-08-13 22:07:59 +01:00
|
|
|
|
|
|
|
try container.encode(self.spotify_linked, forKey: .spotify_linked)
|
|
|
|
try container.encode(self.lastfm_username, forKey: .lastfm_username)
|
2022-12-09 08:51:42 +00:00
|
|
|
|
|
|
|
try container.encode(self.notify, forKey: .notify)
|
|
|
|
try container.encode(self.notify_playlist_updates, forKey: .notify_playlist_updates)
|
|
|
|
try container.encode(self.notify_tag_updates, forKey: .notify_tag_updates)
|
|
|
|
try container.encode(self.notify_admins, forKey: .notify_admins)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func get_null_user() -> User {
|
|
|
|
return User()
|
2022-08-13 22:07:59 +01:00
|
|
|
}
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
|
|
|
|