1 /// NOTE: These permissions are for UI cosmetics only!
2 /// They should be in sync with the backend Policies.
4 import * as Episode from './Episode';
5 import Round from './Round';
7 export const hasGlobalRole = (user, role) =>
8 user && role && user.global_roles && user.global_roles.includes(role);
10 export const isAdmin = user => user && user.role === 'admin';
12 export const isSameUser = (user, subject) => user && subject && user.id === subject.id;
16 export const isChannelAdmin = (user, channel) =>
17 user && channel && user.channel_crews &&
18 user.channel_crews.find(c => c.role === 'admin' && c.channel_id === channel.id);
22 export const mayEditContent = user =>
23 user && hasGlobalRole(user, 'content');
27 export const isCommentator = (user, episode) => {
28 if (!user || !episode || !episode.crew) return false;
29 return !!episode.crew.find(c => c.role === "commentary" && c.user_id === user.id);
32 export const isTracker = (user, episode) => {
33 if (!user || !episode || !episode.crew) return false;
34 return !!episode.crew.find(c => c.role === "tracking" && c.user_id === user.id);
37 export const episodeHasChannel = (episode, channel) =>
38 episode && channel && episode.channels && episode.channels.find(c => c.id === channel.id);
40 export const mayRestreamEpisodes = user =>
41 user && user.channel_crews && user.channel_crews.find(c => c.role === 'admin');
43 export const mayEditRestream = (user, episode, channel) =>
44 episodeHasChannel(episode, channel) && isChannelAdmin(user, channel);
46 export const canApplyForEpisode = (user, episode, as) => {
47 if (!user) return false;
48 if (as === 'commentary') return Episode.acceptsComms(episode) && !isCommentator(user, episode);
49 if (as === 'tracking') return Episode.acceptsTrackers(episode) && !isTracker(user, episode);
53 export const applicableChannels = (user, episode, as) => {
54 if (!user || !episode) return [];
55 const assigned_channels = (episode.crew || [])
56 .filter(c => c.user_id === user.id)
57 .map(c => c.channel_id);
58 const channels = episode.channels || [];
59 if (as === 'commentary') return channels.filter(c =>
60 c.pivot && c.pivot.accept_comms && !assigned_channels.includes(c.id));
61 if (as === 'tracking') return channels.filter(c =>
62 c.pivot && c.pivot.accept_tracker && !assigned_channels.includes(c.id));
66 export const canRestreamEpisode = (user, episode) => {
67 if (!user || !episode || !mayRestreamEpisodes(user)) return false;
68 const available_channels = user.channel_crews
69 .filter(c => c.role === 'admin')
70 .map(c => c.channel_id);
71 const claimed_channels = ((episode && episode.channels) || []).map(c => c.id);
72 const remaining_channels = available_channels.filter(id => !claimed_channels.includes(id));
73 return remaining_channels.length > 0;
78 export const isApplicant = (user, tournament) => {
79 if (!user || !tournament || !tournament.applications) {
82 return tournament.applications.find(p => p.user && p.user.id == user.id);
85 export const isDeniedApplicant = (user, tournament) => {
86 if (!user || !tournament || !tournament.applications) {
89 const applicant = tournament.applications.find(p => p.user && p.user.id == user.id);
90 return applicant && applicant.denied;
93 export const isParticipant = (user, tournament) =>
94 user && tournament && tournament.participants &&
95 tournament.participants.find(p => p.user && p.user.id == user.id);
97 export const isRunner = (user, tournament) => {
98 const p = isParticipant(user, tournament);
99 return p && p.roles && p.roles.includes('runner');
102 export const isTournamentAdmin = (user, tournament) => {
103 const p = isParticipant(user, tournament);
104 return p && p.roles && p.roles.includes('admin');
107 export const isTournamentCrew = (user, tournament) =>
108 isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
110 export const isTournamentMonitor = (user, tournament) => {
111 const p = isParticipant(user, tournament);
112 return p && p.roles && p.roles.includes('monitor');
115 export const hasFinished = (user, round) =>
116 user && round && round.results &&
117 round.results.find(r => r.user_id == user.id && r.has_finished);
119 export const mayAddRounds = (user, tournament) =>
120 !tournament.locked &&
121 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
123 export const mayApply = (user, tournament) =>
124 user && tournament && tournament.accept_applications &&
125 !isRunner(user, tournament) && !isApplicant(user, tournament);
127 export const mayHandleApplications = (user, tournament) =>
128 tournament && tournament.accept_applications && isTournamentAdmin(user, tournament);
130 export const mayReportResult = (user, tournament) => {
131 if (!user || !tournament) return false;
132 if (tournament.type === 'open-async') return true;
133 return isRunner(user, tournament);
136 export const mayEditRound = (user, tournament) =>
137 !tournament.locked && isTournamentAdmin(user, tournament);
139 export const mayLockRound = (user, tournament) =>
140 !tournament.locked && isTournamentAdmin(user, tournament);
142 export const maySetSeed = (user, tournament, round) =>
144 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
146 export const mayUpdateTournament = (user, tournament) =>
147 isAdmin(user) || isTournamentAdmin(user, tournament);
149 export const mayViewProtocol = (user, tournament) =>
150 isTournamentCrew(user, tournament);
152 export const maySeeResults = (user, tournament, round) =>
154 hasFinished(user, round) ||
155 isTournamentMonitor(user, tournament) ||
156 Round.isComplete(tournament, round);
160 export const mayEditNickname = (user, subject) =>
161 isSameUser(user, subject);
163 export const mayEditStreamLink = (user, subject) =>
164 isSameUser(user, subject);