|
| 1 | +package spaces |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/fancyinnovations/fancyspaces/integrations/idp-go-sdk/idp" |
| 7 | +) |
| 8 | + |
| 9 | +type Space struct { |
| 10 | + ID string `json:"id"` |
| 11 | + Slug string `json:"slug"` |
| 12 | + Title string `json:"title"` |
| 13 | + Summary string `json:"summary"` |
| 14 | + Description string `json:"description"` |
| 15 | + Categories []Category `json:"categories"` |
| 16 | + Links []Link `json:"links"` |
| 17 | + IconURL string `json:"icon_url"` |
| 18 | + Status Status `json:"status"` |
| 19 | + CreatedAt time.Time `json:"created_at"` |
| 20 | + Creator string `json:"creator"` // UserID of the creator |
| 21 | + Members []Member `json:"members"` |
| 22 | + |
| 23 | + IssueSettings IssueSettings `json:"issue_settings"` |
| 24 | + ReleaseSettings ReleaseSettings `json:"release_settings"` |
| 25 | + MavenRepositorySettings MavenRepositorySettings `json:"maven_repository_settings"` |
| 26 | + StorageSettings StorageSettings `json:"storage_settings"` |
| 27 | + AnalyticsSettings AnalyticsSettings `json:"analytics_settings"` |
| 28 | +} |
| 29 | + |
| 30 | +// InternalSpace is the internal representation of a Space, containing additional fields that are not exposed to clients. |
| 31 | +type InternalSpace struct { |
| 32 | + Space |
| 33 | + |
| 34 | + AnalyticsWriteKey string `json:"analytics_write_key"` |
| 35 | +} |
| 36 | + |
| 37 | +type IssueSettings struct { |
| 38 | + Enabled bool `json:"enabled"` |
| 39 | + |
| 40 | + GitHubSync bool `json:"github_sync"` |
| 41 | + GitHubSyncOwner string `json:"github_sync_owner"` // GitHub username or organization (required if GitHubSync is true) |
| 42 | + GitHubSyncRepo string `json:"github_sync_repo"` // GitHub repository name (required if GitHubSync is true) |
| 43 | + GitHubSyncLabel string `json:"github_sync_label"` // only sync issues with this label (optional) |
| 44 | +} |
| 45 | + |
| 46 | +type ReleaseSettings struct { |
| 47 | + Enabled bool `json:"enabled"` |
| 48 | + |
| 49 | + // TODO: Implement these settings in the future |
| 50 | + //DiscordNotifications bool `json:"discord_notifications"` |
| 51 | + //DiscordNotificationWebhookURL string `json:"discord_notification_webhook_url"` |
| 52 | +} |
| 53 | + |
| 54 | +type MavenRepositorySettings struct { |
| 55 | + Enabled bool `json:"enabled"` |
| 56 | +} |
| 57 | + |
| 58 | +type StorageSettings struct { |
| 59 | + Enabled bool `json:"enabled"` |
| 60 | +} |
| 61 | + |
| 62 | +type AnalyticsSettings struct { |
| 63 | + Enabled bool `json:"enabled"` |
| 64 | + RequireWriteKey bool `json:"require_write_key"` |
| 65 | + WriteKey string `json:"-"` |
| 66 | +} |
| 67 | + |
| 68 | +type Link struct { |
| 69 | + Name string `json:"name"` |
| 70 | + URL string `json:"url"` |
| 71 | +} |
| 72 | + |
| 73 | +type Member struct { |
| 74 | + UserID string `json:"user_id"` |
| 75 | + Role Role `json:"role"` |
| 76 | +} |
| 77 | + |
| 78 | +type Role string |
| 79 | + |
| 80 | +const ( |
| 81 | + RoleAdmin Role = "admin" |
| 82 | + RoleMember Role = "member" |
| 83 | +) |
| 84 | + |
| 85 | +type Status string |
| 86 | + |
| 87 | +const ( |
| 88 | + StatusDraft Status = "draft" |
| 89 | + StatusReview Status = "review" |
| 90 | + StatusApproved Status = "approved" |
| 91 | + StatusPrivate Status = "private" |
| 92 | + StatusArchived Status = "archived" |
| 93 | + StatusRejected Status = "rejected" |
| 94 | + StatusBanned Status = "banned" |
| 95 | +) |
| 96 | + |
| 97 | +type Category string |
| 98 | + |
| 99 | +const ( |
| 100 | + CategoryMinecraftPlugin Category = "minecraft_plugin" |
| 101 | + CategoryMinecraftServer Category = "minecraft_server" |
| 102 | + CategoryMinecraftMod Category = "minecraft_mod" |
| 103 | + CategoryHytalePlugin Category = "hytale_plugin" |
| 104 | + CategoryWebApp Category = "web_app" |
| 105 | + CategoryMobileApp Category = "mobile_app" |
| 106 | + CategoryOther Category = "other" |
| 107 | +) |
| 108 | + |
| 109 | +func (s *Space) IsMember(u *idp.User) bool { |
| 110 | + if !idp.IsUserValid(u) { |
| 111 | + return false |
| 112 | + } |
| 113 | + |
| 114 | + if s.Creator == u.ID { |
| 115 | + return true |
| 116 | + } |
| 117 | + |
| 118 | + for _, m := range s.Members { |
| 119 | + if m.UserID == u.ID { |
| 120 | + return true |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return false |
| 125 | +} |
| 126 | + |
| 127 | +func (s *Space) IsOwner(u *idp.User) bool { |
| 128 | + if !idp.IsUserValid(u) { |
| 129 | + return false |
| 130 | + } |
| 131 | + |
| 132 | + return s.Creator == u.ID |
| 133 | +} |
| 134 | + |
| 135 | +func (s *Space) HasFullAccess(u *idp.User) bool { |
| 136 | + if !idp.IsUserValid(u) { |
| 137 | + return false |
| 138 | + } |
| 139 | + |
| 140 | + if s.Creator == u.ID { |
| 141 | + return true |
| 142 | + } |
| 143 | + |
| 144 | + for _, m := range s.Members { |
| 145 | + if m.UserID == u.ID { |
| 146 | + return m.Role == RoleAdmin |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return false |
| 151 | +} |
| 152 | + |
| 153 | +func (s *Space) HasWriteAccess(u *idp.User) bool { |
| 154 | + if !idp.IsUserValid(u) { |
| 155 | + return false |
| 156 | + } |
| 157 | + |
| 158 | + if s.Creator == u.ID { |
| 159 | + return true |
| 160 | + } |
| 161 | + |
| 162 | + for _, m := range s.Members { |
| 163 | + if m.UserID == u.ID { |
| 164 | + return m.Role == RoleAdmin || m.Role == RoleMember |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return false |
| 169 | +} |
| 170 | + |
| 171 | +func (s *Space) Validate() error { |
| 172 | + if len(s.Slug) < 3 { |
| 173 | + return ErrSlugTooShort |
| 174 | + } |
| 175 | + if len(s.Slug) > 20 { |
| 176 | + return ErrSlugTooLong |
| 177 | + } |
| 178 | + |
| 179 | + if len(s.Title) > 100 { |
| 180 | + return ErrTitleTooLong |
| 181 | + } |
| 182 | + if len(s.Title) < 3 { |
| 183 | + return ErrTitleTooShort |
| 184 | + } |
| 185 | + |
| 186 | + if len(s.Description) > 500 { |
| 187 | + return ErrDescriptionTooLong |
| 188 | + } |
| 189 | + |
| 190 | + return nil |
| 191 | +} |
0 commit comments