func SaveOrUpdateMessage(entity models.MessageStruct, enc Encoder, db gorp.SqlExecutor, user sessionauth.User) (int, string) {

	u := UserById(user.UniqueId().(int), db)

	if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN {

		entity.Last_modified_by = user.UniqueId().(int)

		if entity.Id < 1 {
			err := db.Insert(&entity)
			if err != nil {
				checkErr(err, "insert failed")
				return http.StatusBadRequest, ""
			}
		} else {
			obj, _ := db.Get(models.MessageStruct{}, entity.Id)
			if obj == nil {
				// Invalid id, or does not exist
				return http.StatusBadRequest, ""
			}

			_, err := db.Update(&entity)
			if err != nil {
				checkErr(err, "update failed")
				return http.StatusBadRequest, ""
			}
		}

		return http.StatusOK, Must(enc.EncodeOne(entity))

	}

	return http.StatusUnauthorized, ""
}
func AddContact_type(entity models.DefaultStruct, w http.ResponseWriter, enc Encoder, db gorp.SqlExecutor) (int, string) {
	err := db.Insert(&entity)
	if err != nil {
		checkErr(err, "INSERT CONTACT TYPE FAILED")
		return http.StatusBadRequest, ""
	}
	w.Header().Set("Location", fmt.Sprintf("/warnabroda/contact_types/%d", entity.Id))
	return http.StatusCreated, Must(enc.EncodeOne(entity))
}
func AddMessage(entity models.DefaultStruct, w http.ResponseWriter, enc Encoder, db gorp.SqlExecutor) (int, string) {
	err := db.Insert(&entity)
	if err != nil {
		checkErr(err, "insert failed")
		return http.StatusBadRequest, ""
	}
	w.Header().Set("Location", fmt.Sprintf("/warnabroda/messages/%d", entity.Id))
	return http.StatusCreated, Must(enc.EncodeOne(entity))
}
Example #4
0
func (m *Member) updateCategoires(s gorp.SqlExecutor) error {
	// delete existing categories
	format := "delete from " + TableNameCategoryMember + " where memberid = ?"
	if _, err := s.Exec(format, m.ID); err != nil {
		return err
	}

	// create new categories
	for _, catId := range m.CategoryIds {
		catMem := NewCategoryMember(catId, m.ID)
		if err := s.Insert(catMem); err != nil {
			return err
		}
	}
	return nil
}
func ProcessWarnReply(warning *models.Warning, db gorp.SqlExecutor) {
	fmt.Println("ProcessWarnReply")

	warning.WarnResp.Id_warning = warning.Id

	warning.WarnResp.Lang_key = warning.Lang_key
	warning.WarnResp.Resp_hash = GenerateSha1(warning.Contact + "-" + warning.Created_date)
	warning.WarnResp.Read_hash = GenerateSha1(warning.WarnResp.Reply_to + "-" + warning.Created_date)
	warning.WarnResp.Reply_to = warning.WarnResp.Reply_to
	warning.WarnResp.Created_date = warning.Created_date

	if strings.Contains(warning.WarnResp.Reply_to, "@") {
		warning.WarnResp.Id_contact_type = 1
	} else {
		warning.WarnResp.Id_contact_type = 3
	}

	err := db.Insert(warning.WarnResp)
	checkErr(err, "INSERT WARNING ERROR")

}
// Receives a warning tru, inserts the request and process the warning and then respond to the interface
//TODO: use (session sessions.Session, r *http.Request) to prevent flood
func AddWarning(entity models.Warning, enc Encoder, db gorp.SqlExecutor, r *http.Request) (int, string) {
	fmt.Println("AddWarning")
	if isInvalidWarning(&entity) {
		return http.StatusBadRequest, Must(enc.EncodeOne(entity))
	}

	status := &models.DefaultStruct{
		Id:       http.StatusOK,
		Name:     messages.GetLocaleMessage(entity.Lang_key, "MSG_WARNING_SENT_SUCCESS"),
		Lang_key: entity.Lang_key,
		Type:     models.MSG_TYPE_WARNING,
	}

	entity.Sent = false
	entity.Created_by = "system"
	//entity.Created_date = time.Now().String()

	err := db.Insert(&entity)
	checkErr(err, "INSERT WARNING ERROR")
	if err != nil {
		return http.StatusBadRequest, Must(enc.EncodeOne(entity))
	}

	ingnored := InIgnoreList(db, entity.Contact)

	if ingnored != nil && ingnored.Confirmed {
		status = &models.DefaultStruct{
			Id:       http.StatusBadRequest,
			Name:     messages.GetLocaleMessage(entity.Lang_key, "MSG_IGNORED_USER"),
			Lang_key: entity.Lang_key,
			Type:     models.MSG_TYPE_WARNING,
		}
	} else {
		processWarn(&entity, db, status)
	}

	return http.StatusCreated, Must(enc.EncodeOne(status))
}
Example #7
0
func (app *App) Save(txn gorp.SqlExecutor) error {
	return txn.Insert(app)
}
// Add the request to be ignored for future warnings, it requires further confimation
func AddIgnoreList(entity models.Ignore_List, w http.ResponseWriter, enc Encoder, db gorp.SqlExecutor) (int, string) {

	if isInvalidIgnoreList(&entity) {
		return http.StatusBadRequest, Must(enc.EncodeOne(entity))
	}

	status := &models.DefaultStruct{
		Id:       http.StatusOK,
		Name:     messages.GetLocaleMessage(entity.Lang_key, "MSG_CONFIRM_IGNOREME"),
		Lang_key: entity.Lang_key,
		Type:     models.MSG_TYPE_IGNORE,
	}

	if MoreThanTwoRequestByIp(db, &entity) {
		status = &models.DefaultStruct{
			Id:       http.StatusBadRequest,
			Name:     messages.GetLocaleMessage(entity.Lang_key, "MSG_TOO_MANY_IGNOREME_REQUESTS"),
			Lang_key: entity.Lang_key,
			Type:     models.MSG_TYPE_IGNORE,
		}
		return http.StatusUnauthorized, Must(enc.EncodeOne(status))
	}

	ingnored := InIgnoreList(db, entity.Contact)

	if ingnored != nil && ingnored.Confirmed {

		status = &models.DefaultStruct{
			Id:       http.StatusBadRequest,
			Name:     messages.GetLocaleMessage(entity.Lang_key, "MSG_CONTACT_ALREADY_IGNORED"),
			Lang_key: entity.Lang_key,
		}

		return http.StatusUnauthorized, Must(enc.EncodeOne(status))

	} else if ingnored != nil {
		status = &models.DefaultStruct{
			Id:       http.StatusBadRequest,
			Name:     messages.GetLocaleMessage(entity.Lang_key, "MSG_IGNORE_REQUEST_EXISTS"),
			Lang_key: entity.Lang_key,
		}

		return http.StatusUnauthorized, Must(enc.EncodeOne(status))
	}

	rand.Seed(time.Now().UTC().UnixNano())
	entity.Created_by = "user"
	entity.Confirmed = false
	entity.Confirmation_code = randomString(6)

	errIns := db.Insert(&entity)
	checkErr(errIns, "INSERT IGNORE FAIL")

	if strings.Contains(entity.Contact, "@") {
		status.Name += " via e-mail."
		go SendEmailIgnoreme(&entity, db)
	} else if strings.Contains(entity.Contact, "+55") {
		status.Name += " via SMS."
		go sendSMSIgnoreme(&entity, db)
	}

	//w.Header().Set("Location", fmt.Sprintf("/warnabroda/ignore-list/%d", entity.Id))
	return http.StatusCreated, Must(enc.EncodeOne(status))
}
func (ft FeedType) GetStories(s gorp.SqlExecutor, m *Member, f *Feed) error {
	switch ft {
	case FeedTypeRSS:
		itemHandler := func(
			fe *feeder.Feed,
			ch *feeder.Channel,
			newitems []*feeder.Item) {

			for _, item := range newitems {
				story := NewStoryRSS(m, f, item)
				s.Insert(story)
			}
		}
		feed := feeder.New(1, true, nil, itemHandler)
		if err := feed.Fetch(f.Identifier, nil); err != nil {
			fmt.Println(err)
		}
	case FeedTypeTwitter:
		v := url.Values{}
		v.Set("screen_name", f.Identifier)
		v.Set("include_rts", "false")

		anaconda.SetConsumerKey(os.Getenv("twitterApiKey"))
		anaconda.SetConsumerSecret(os.Getenv("twitterApiSecret"))
		api := anaconda.NewTwitterApi("", "")

		tweets, err := api.GetUserTimeline(v)
		if err != nil {
			fmt.Println(err)
		}

		for _, t := range tweets {
			story := NewStoryTwitter(m, f, t)
			if err := s.Insert(story); err == nil {
				fmt.Printf("Added Twitter story for %s.  Date: %s Score: %f\n", m.Name, milli.Time(story.Timestamp).String(), story.Score)
			} else {
				fmt.Printf("Failed to add Twitter story for %s.  Error: %s\n", m.Name, err)
			}
		}
	case FeedTypeFacebook:
		session := facebookSession()
		route := fmt.Sprintf("/%s/posts", f.Identifier)
		result, err := session.Api(route, facebook.GET, nil)
		if err != nil {
			fmt.Println(err)
		}

		posts := &FacebookPosts{}
		if err := result.Decode(posts); err != nil {
			fmt.Println(err)
		}

		for _, post := range posts.Data {
			story := NewFacebookStory(m, f, post)
			if story != nil {
				if err := s.Insert(story); err == nil {
					fmt.Printf("Added Facebook story for %s.  Date: %s  Score: %f\n", m.Name, milli.Time(story.Timestamp).String(), story.Score)
				} else {
					fmt.Printf("Failed to add Facebook story for %s.  Error: %s\n", m.Name, err)
				}
			}
		}
	}
	return nil
}
Example #10
0
func CreateBundle(txn gorp.SqlExecutor, bundle *Bundle) error {
	return txn.Insert(bundle)
}
Example #11
0
func (bundle *Bundle) Save(txn gorp.SqlExecutor) error {
	return txn.Insert(bundle)
}
Example #12
0
func CreateUser(txn gorp.SqlExecutor, user *User) error {
	return txn.Insert(user)
}
Example #13
0
func (user *User) Save(txn gorp.SqlExecutor) error {
	return txn.Insert(user)
}
Example #14
0
func (authority *Authority) Save(txn gorp.SqlExecutor) error {
	return txn.Insert(authority)
}
Example #15
0
func CreateAudit(txn gorp.SqlExecutor, audit *Audit) error {
	return txn.Insert(audit)
}
Example #16
0
func (audit *Audit) Save(txn gorp.SqlExecutor) error {
	return txn.Insert(audit)
}