Example #1
0
func viewChannel(c *Context, w http.ResponseWriter, r *http.Request) {
	view := model.ChannelViewFromJson(r.Body)

	if err := SetActiveChannel(c.Session.UserId, view.ChannelId); err != nil {
		c.Err = err
		return
	}

	if len(view.ChannelId) == 0 {
		ReturnStatusOK(w)
		return
	}

	channelIds := []string{view.ChannelId}

	var pchan store.StoreChannel
	if len(view.PrevChannelId) > 0 {
		channelIds = append(channelIds, view.PrevChannelId)

		if *utils.Cfg.EmailSettings.SendPushNotifications && !c.Session.IsMobileApp() {
			pchan = app.Srv.Store.User().GetUnreadCountForChannel(c.Session.UserId, view.ChannelId)
		}
	}

	uchan := app.Srv.Store.Channel().UpdateLastViewedAt(channelIds, c.Session.UserId)

	if pchan != nil {
		if result := <-pchan; result.Err != nil {
			c.Err = result.Err
			return
		} else {
			if result.Data.(int64) > 0 {
				app.ClearPushNotification(c.Session.UserId, view.ChannelId)
			}
		}
	}

	if result := <-uchan; result.Err != nil {
		c.Err = result.Err
		return
	}

	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, c.TeamId, "", c.Session.UserId, nil)
	message.Add("channel_id", view.ChannelId)
	go app.Publish(message)

	ReturnStatusOK(w)
}
Example #2
0
func updateLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	id := params["channel_id"]

	data := model.StringInterfaceFromJson(r.Body)

	var active bool
	var ok bool
	if active, ok = data["active"].(bool); !ok {
		active = true
	}

	doClearPush := false
	if *utils.Cfg.EmailSettings.SendPushNotifications && !c.Session.IsMobileApp() && active {
		if result := <-app.Srv.Store.User().GetUnreadCountForChannel(c.Session.UserId, id); result.Err != nil {
			l4g.Error(utils.T("api.channel.update_last_viewed_at.get_unread_count_for_channel.error"), c.Session.UserId, id, result.Err.Error())
		} else {
			if result.Data.(int64) > 0 {
				doClearPush = true
			}
		}
	}

	go func() {
		if err := SetActiveChannel(c.Session.UserId, id); err != nil {
			l4g.Error(err.Error())
		}
	}()

	app.Srv.Store.Channel().UpdateLastViewedAt([]string{id}, c.Session.UserId)

	// Must be after update so that unread count is correct
	if doClearPush {
		go app.ClearPushNotification(c.Session.UserId, id)
	}

	chanPref := model.Preference{
		UserId:   c.Session.UserId,
		Category: c.TeamId,
		Name:     model.PREFERENCE_NAME_LAST_CHANNEL,
		Value:    id,
	}

	teamPref := model.Preference{
		UserId:   c.Session.UserId,
		Category: model.PREFERENCE_CATEGORY_LAST,
		Name:     model.PREFERENCE_NAME_LAST_TEAM,
		Value:    c.TeamId,
	}

	app.Srv.Store.Preference().Save(&model.Preferences{teamPref, chanPref})

	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, c.TeamId, "", c.Session.UserId, nil)
	message.Add("channel_id", id)

	go app.Publish(message)

	result := make(map[string]string)
	result["id"] = id
	w.Write([]byte(model.MapToJson(result)))
}