Beispiel #1
0
func (h *hub) notifyUser(userID, otherUserID int64, typ string, v interface{}) {
	m := message{typ, v}

	c, ok := h.connections[userID]
	if !ok {
		return
	}

	b, err := json.Marshal(m)
	if err != nil {
		debug.Warn(err)
		return
	}

	c.send <- b

	// chatting with yourself so just send the message once
	if userID == otherUserID {
		return
	}

	d, ok := h.connections[otherUserID]
	if !ok {
		return
	}

	d.send <- b
}
Beispiel #2
0
func PostChat(w http.ResponseWriter, r *http.Request) {
	var parser store.Parser

	userID := parser.Int(r.FormValue("userID"))
	channelID := parser.Int(r.FormValue("channelID"))
	if parser.Err != nil {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	chatParams := store.PostChatParams{
		UserID:      userID,
		ChannelID:   channelID,
		ChannelType: r.FormValue("channelType"),
		Text:        r.FormValue("text"),
	}

	id, err := store.PostChat(chatParams)
	if err != nil {
		response.ServerError(w, err)
		return
	}

	if err = store.NotifyChat(store.NotifyChatParams{id, chatParams}); err != nil {
		debug.Warn(err)
		return
	}

	response.OK(w, id)
}
Beispiel #3
0
func Init() {
	var err error

	db, err = sql.Open("postgres", *dataSource)
	if err != nil {
		debug.Fatal(err)
	}

	create := func(name, content string) {
		if err != nil {
			debug.Fatal(err)
		}
		err = createTable(name, content)
	}

	// primary tables
	create("user_", createUserSQL)
	create("project", createProjectSQL)
	create("task", createTaskSQL)
	create("milestone", createMilestoneSQL)
	create("member", createMemberSQL)
	create("worker", createWorkerSQL)
	create("friend", createFriendSQL)
	create("chat", createChatSQL)

	// secondary tables
	create("featured_project", createFeaturedProjectSQL)
	create("user_tag", createUserTagSQL)
	create("task_tag", createTaskTagSQL)
	create("tag", createTagSQL)

	// setup listener
	if *listenerEnabled {
		listener = pq.NewListener(*dataSource, 1*time.Second, time.Minute, func(ev pq.ListenerEventType, err error) {
			if err != nil {
				log.Fatal(err)
			}
		})

		if err := listener.Listen("chat"); err != nil {
			log.Fatal(err)
		}

		go func() {
			for {
				select {
				case notification := <-listener.Notify:
					if Notify != nil {
						Notify(notification.Channel, notification.Extra)
					}
				}
			}
		}()
	} else {
		debug.Warn("PostgreSQL listener is disabled")
	}
}
Beispiel #4
0
func IsUserVerified(email string) bool {
	const q = `SELECT COUNT(*) FROM user_ WHERE email = $1 AND verification_code = 'verified'`

	var count int64
	if err := db.QueryRow(q, email).Scan(&count); err != nil {
		if err != sql.ErrNoRows {
			debug.Warn(err)
		}
		return false
	}
	return count > 0
}
func HasUserWithEmail(email string) bool {
	const rawSQL = `SELECT COUNT(*) FROM user_ WHERE email = $1`

	var count int64
	if err := db.QueryRow(rawSQL, email).Scan(&count); err != nil {
		if err != sql.ErrNoRows {
			debug.Warn(err)
		}
		return false
	}
	return count > 0
}
Beispiel #6
0
func CurrentUser(r *http.Request) User {
	user, err := GetUserByEmail(session.GetEmail(r))
	if err != nil {
		debug.Warn(err)
		return nil
	}

	if user.ID() == 0 {
		return nil
	}

	return user
}
Beispiel #7
0
func createTable(name, content string) error {
	if exists, err := tableExists(name); err != nil {
		return err
	} else if exists {
		return nil
	}

	if _, err := db.Exec("CREATE TABLE " + name + "(" + content + ")"); err != nil {
		debug.Warn(err)
		return err
	}

	fmt.Println("created table:", name)
	return nil
}
Beispiel #8
0
func (h *hub) notifyRelatedUsers(userID int64, typ string, v interface{}) {
	m := message{typ, v}

	ids, err := store.GetRelatedUserIDs(userID)
	if err != nil {
		debug.Warn(err)
		return
	}

	b, err := json.Marshal(m)
	if err != nil {
		debug.Warn(err)
		return
	}

	for _, id := range ids {
		c, ok := h.connections[id]
		if !ok {
			continue
		}

		c.send <- b
	}
}
Beispiel #9
0
func CreateProject(w http.ResponseWriter, r *http.Request) {
	title := r.FormValue("title")
	if title == "" {
		response.ClientError(w, http.StatusBadRequest)
		return
	}

	user := context.Get(r, "user").(store.User)
	tagline := r.FormValue("tagline")
	description := r.FormValue("description")

	// basic project info
	projectID, err := store.CreateProject(map[string]string{
		"authorID":    user.IDStr(),
		"title":       title,
		"tagline":     tagline,
		"description": description,
	})
	if err != nil {
		response.ServerError(w, err)
		return
	}

	var ok bool

	// add author to project user list
	if err = store.AddMember(projectID, user.ID()); err != nil {
		goto error
	}

	// image
	if ok, err = store.SaveProjectImage(w, r, projectID); err != nil || !ok {
		goto error
	}

	response.OK(w, projectID)
	return

error:
	if err := store.DeleteProject(projectID); err != nil {
		debug.Warn(err)
	}
	response.ServerError(w, err)
}
Beispiel #10
0
//
// /ws
//
func ws(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		response.ClientError(w, http.StatusMethodNotAllowed)
		return
	}

	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		debug.Warn(err)
		return
	}

	user := context.Get(r, "user").(store.User)
	c := &connection{send: make(chan []byte, 256), ws: ws, userID: user.ID()}
	h.register <- c

	go func() {
		c.writePump()
	}()
	c.readPump()
}