<commit_before>package gdrive2slack

import (
	"encoding/json"
	"github.com/optionfactory/gdrive2slack/google/drive"
	"github.com/optionfactory/gdrive2slack/google/userinfo"
	"github.com/optionfactory/gdrive2slack/slack"
	"os"
)

type Subscription struct {
	Channel            string             `json:"channel"`
	SlackAccessToken   string             `json:"slack_access_token"`
	GoogleRefreshToken string             `json:"google_refresh_token"`
	GoogleUserInfo     *userinfo.UserInfo `json:"guser"`
	SlackUserInfo      *slack.UserInfo    `json:"suser"`
}

type UserState struct {
	Gdrive            *drive.State
	GoogleAccessToken string
}

type SubscriptionAndAccessToken struct {
	Subscription      *Subscription
	GoogleAccessToken string
}

type Subscriptions struct {
	Source string
	Info   map[string]*Subscription
	States map[string]*UserState
}

func LoadSubscriptions(filename string) (*Subscriptions, error) {
	var subscriptions = &Subscriptions{
		Source: filename,
		Info:   make(map[string]*Subscription),
		States: make(map[string]*UserState),
	}
	file, err := os.Open(filename)
	if err != nil {
		return subscriptions, nil
	}
	defer file.Close()
	err = json.NewDecoder(file).Decode(&subscriptions.Info)
	if err != nil {
		return nil, err
	}
	for k := range subscriptions.Info {
		subscriptions.States[k] = &UserState{
			Gdrive:            drive.NewState(),
			GoogleAccessToken: "",
		}
	}
	return subscriptions, nil
}

func (subscriptions *Subscriptions) save() error {
	file, err := os.Create(subscriptions.Source)
	if err != nil {
		return err
	}
	defer file.Close()
	return json.NewEncoder(file).Encode(subscriptions)
}

func (subscriptions *Subscriptions) Add(subscription *Subscription, googleAccessToken string) {
	subscriptions.Info[subscription.GoogleUserInfo.Email] = subscription
	subscriptions.States[subscription.GoogleUserInfo.Email] = &UserState{
		Gdrive:            drive.NewState(),
		GoogleAccessToken: googleAccessToken,
	}
	subscriptions.save()
}

func (subscriptions *Subscriptions) Remove(email string) *Subscription {
	s := subscriptions.Info[email]
	delete(subscriptions.States, email)
	delete(subscriptions.Info, email)
	subscriptions.save()<commit_msg>:bug: wrong subscription serialization<commit_after>package gdrive2slack

import (
	"encoding/json"
	"github.com/optionfactory/gdrive2slack/google/drive"
	"github.com/optionfactory/gdrive2slack/google/userinfo"
	"github.com/optionfactory/gdrive2slack/slack"
	"os"
)

type Subscription struct {
	Channel            string             `json:"channel"`
	SlackAccessToken   string             `json:"slack_access_token"`
	GoogleRefreshToken string             `json:"google_refresh_token"`
	GoogleUserInfo     *userinfo.UserInfo `json:"guser"`
	SlackUserInfo      *slack.UserInfo    `json:"suser"`
}

type UserState struct {
	Gdrive            *drive.State
	GoogleAccessToken string
}

type SubscriptionAndAccessToken struct {
	Subscription      *Subscription
	GoogleAccessToken string
}

type Subscriptions struct {
	Source string
	Info   map[string]*Subscription
	States map[string]*UserState
}

func LoadSubscriptions(filename string) (*Subscriptions, error) {
	var subscriptions = &Subscriptions{
		Source: filename,
		Info:   make(map[string]*Subscription),
		States: make(map[string]*UserState),
	}
	file, err := os.Open(filename)
	if err != nil {
		return subscriptions, nil
	}
	defer file.Close()
	err = json.NewDecoder(file).Decode(&subscriptions.Info)
	if err != nil {
		return nil, err
	}
	for k := range subscriptions.Info {
		subscriptions.States[k] = &UserState{
			Gdrive:            drive.NewState(),
			GoogleAccessToken: "",
		}
	}
	return subscriptions, nil
}

func (subscriptions *Subscriptions) save() error {
	file, err := os.Create(subscriptions.Source)
	if err != nil {
		return err
	}
	defer file.Close()
	return json.NewEncoder(file).Encode(subscriptions.Info)
}

func (subscriptions *Subscriptions) Add(subscription *Subscription, googleAccessToken string) {
	subscriptions.Info[subscription.GoogleUserInfo.Email] = subscription
	subscriptions.States[subscription.GoogleUserInfo.Email] = &UserState{
		Gdrive:            drive.NewState(),
		GoogleAccessToken: googleAccessToken,
	}
	subscriptions.save()
}

func (subscriptions *Subscriptions) Remove(email string) *Subscription {
	s := subscriptions.Info[email]
	delete(subscriptions.States, email)
	delete(subscriptions.Info, email)
	subscriptions.save()
{'fraction_non_alphanumeric': 0.10822413014008134, 'fraction_numerical': 0.0018075011296882061, 'mean_word_length': 4.458692971639951, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4376527', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1367, 'n_words': 282}