1package orgchannel
2
3import (
4	"fmt"
5	"strconv"
6	"strings"
7)
8
9// PrependOrgID prepends orgID to a channel.
10func PrependOrgID(orgID int64, channel string) string {
11	return strconv.FormatInt(orgID, 10) + "/" + channel
12}
13
14// StripOrgID strips organization ID from channel ID.
15// The reason why we strip orgID is because we need to maintain multi-tenancy.
16// Each organization can have the same channels which should not overlap. Due
17// to this every channel in Centrifuge has orgID prefix. Internally in Grafana
18// we strip this prefix since orgID is part of user identity and channel handlers
19// supposed to have the same business logic for all organizations.
20func StripOrgID(channel string) (int64, string, error) {
21	parts := strings.SplitN(channel, "/", 2)
22	if len(parts) != 2 {
23		return 0, "", fmt.Errorf("malformed channel: %s", channel)
24	}
25	orgID, err := strconv.ParseInt(parts[0], 10, 64)
26	if err != nil {
27		return 0, "", fmt.Errorf("invalid orgID part: %s", parts[0])
28	}
29	return orgID, parts[1], nil
30}
31