Example #1
0
func NewUserMap(db *sql.DB, httpClient http.Client, rooms *RoomMap, matrixEchoSuppresser *common.EchoSuppresser) (*UserMap, error) {
	m := &UserMap{
		matrixToSlack:        make(map[string]*slack.User),
		slackToMatrix:        make(map[string]*matrix.User),
		db:                   db,
		matrixEchoSuppresser: matrixEchoSuppresser,
	}

	rows, err := db.Query("SELECT id, slack_user_id, slack_access_token, matrix_user_id, matrix_access_token, matrix_homeserver FROM users ORDER BY id ASC")
	if err != nil {
		return nil, err
	}
	for rows.Next() {
		var id int32
		var slackID, matrixID string
		var slackToken, matrixToken, matrixHomeserver sql.NullString
		if err := rows.Scan(&id, &slackID, &slackToken, &matrixID, &matrixToken, &matrixHomeserver); err != nil {
			return nil, err
		}
		if !matrixToken.Valid {
			log.Printf("Skipping user matrix:%q = slack:%q because no matrix token", matrixID, slackID)
			continue
		}
		if !matrixHomeserver.Valid {
			log.Printf("Skipping user matrix:%q = slack:%q because no matrix homeserver", matrixID, slackID)
			continue
		}
		if !slackToken.Valid {
			log.Printf("Skipping user matrix:%q = slack:%q because no slack token", matrixID, slackID)
			continue
		}
		matrixClient := matrix.NewClient(matrixToken.String, httpClient, matrixHomeserver.String, m.matrixEchoSuppresser)
		matrixUser := matrix.NewUser(matrixID, matrixClient)

		slackClient := slack.NewClient(slackToken.String, httpClient, rooms.ShouldNotify)
		slackUser := &slack.User{slackID, slackClient}

		if err := m.Link(matrixUser, slackUser); err != nil {
			return nil, err
		}
	}
	if rows.Err() != nil {
		return nil, rows.Err()
	}
	return m, nil
}
Example #2
0
func (b *Bridge) matrixBotClient() matrix.Client {
	return matrix.NewClient(b.Config.MatrixASAccessToken, b.Client, b.Config.HomeserverBaseURL, b.MatrixEchoSuppresser)
}