// Tests are done func bodyLenCheck(body string) error { if len(body) < config.MustGet().Limits.MessageBodyMinLen { return fmt.Errorf("message body length should be greater than %d, yours is %d ", config.MustGet().Limits.MessageBodyMinLen, len(body)) } return nil }
// PrepareFileKey prepares a key for redis func PrepareFileKey(fileId string) string { return fmt.Sprintf( "%s:%s:%s", config.MustGet().Environment, KeyPrefix, fileId, ) }
// IsAdmin checks if the current requester is an admin or not, this part is just // a stub and temproray solution for moderation security, when we implement the // permission system fully, this should be the first function to remove. func (c *Context) IsAdmin() bool { if !c.IsLoggedIn() { return false } superAdmins := config.MustGet().DummyAdmins return IsIn(c.Client.Account.Nick, superAdmins...) }
func checkThrottle(channelId, requesterId int64) error { c, err := models.Cache.Channel.ById(channelId) if err != nil { return err } if c.TypeConstant != models.Channel_TYPE_GROUP { return nil } cm := models.NewChannelMessage() conf := config.MustGet() // if oit is defaul treturn early if conf.Limits.PostThrottleDuration == "" { return nil } // if throttle count is zero, it meands it is not set if conf.Limits.PostThrottleCount == 0 { return nil } dur, err := time.ParseDuration(conf.Limits.PostThrottleDuration) if err != nil { return err } // subtrack duration from current time prevTime := time.Now().UTC().Truncate(dur) // count sends positional parameters, no need to sanitize input count, err := bongo.B.Count( cm, "initial_channel_id = ? and "+ "account_id = ? and "+ "created_at > ?", channelId, requesterId, prevTime.Format(time.RFC3339Nano), ) if err != nil { return err } if count > conf.Limits.PostThrottleCount { return fmt.Errorf("reached to throttle, current post count %d for user %d", count, requesterId) } return nil }
// NewBadRequestWithLogger is creating a new http response with predifined http // response properties, it uses a special logger for outputting callstack // properly func NewBadRequestWithLogger(l logging.Logger, err error) (int, http.Header, interface{}, error) { if err == nil { err = errors.New("request is not valid") } // make sure errors are outputted l.Debug("Bad Request: %s", err) // do not expose errors to the client env := config.MustGet().Environment // do not expose errors to the client. if env != "dev" && env != "test" && socialApiEnv != "wercker" { err = genericError } return http.StatusBadRequest, nil, nil, BadRequest{err} }
func (mwc *Controller) migrateAllAccountsToAlgolia() { mwc.log.Notice("Account migration to Algolia started") successCount := 0 s := modelhelper.Selector{ "type": "registered", } c := config.MustGet() algolia := algoliasearch.NewClient(c.Algolia.AppId, c.Algolia.ApiSecretKey) // create message handler handler := algoliaconnector.New(mwc.log, algolia, c.Algolia.IndexSuffix) migrateAccount := func(account interface{}) error { oldAccount := account.(*mongomodels.Account) return handler.AccountCreated(&models.Account{ OldId: oldAccount.Id.Hex(), Nick: oldAccount.Profile.Nickname, }) } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = "jAccounts" iterOptions.F = migrateAccount iterOptions.Filter = s iterOptions.Result = &mongomodels.Account{} iterOptions.Limit = 10000000 iterOptions.Skip = 0 iterOptions.Sort = []string{"-_id"} helpers.Iter(modelhelper.Mongo, iterOptions) mwc.log.Notice("Account migration completed for %d account with %d errors", successCount, errAccountCount) }
func (nc *NotificationChannel) PrepareName() string { env := config.MustGet().Environment return fmt.Sprintf("notification-%s-%s", env, nc.Account.Nick) }