<commit_before>package models

import (
	"encoding/json"
	"net/http"
	"time"

	"github.com/swagchat/chat-api/utils"
)

type Users struct {
	Users []*User `json:"users"`
}

type User struct {
	Id             uint64         `json:"-" db:"id"`
	UserId         string         `json:"userId" db:"user_id,notnull"`
	Name           string         `json:"name" db:"name,notnull"`
	PictureUrl     string         `json:"pictureUrl,omitempty" db:"picture_url"`
	InformationUrl string         `json:"informationUrl,omitempty" db:"information_url"`
	UnreadCount    *uint64        `json:"unreadCount,omitempty" db:"unread_count,notnull"`
	MetaData       utils.JSONText `json:"metaData,omitempty" db:"meta_data"`
	IsBot          *bool          `json:"isBot,omitempty" db:"is_bot,notnull"`
	IsPublic       *bool          `json:"isPublic,omitempty" db:"is_public,notnull"`
	IsCanBlock     *bool          `json:"isCanBlock,omitempty" db:"is_can_block,notnull"`
	IsShowUsers    *bool          `json:"isShowUsers,omitempty" db:"is_show_users,notnull"`
	Lang           string         `json:"lang,	omitempty" db:"lang,notnull"`
	AccessToken    string         `json:"accessToken,omitempty" db:"access_token"`
	Created        int64          `json:"created,omitempty" db:"created,notnull"`
	Modified       int64          `json:"modified,omitempty" db:"modified,notnull"`
	Deleted        int64          `json:"-" db:"deleted,notnull"`

	Rooms   []*RoomForUser `json:"rooms,omitempty" db:"-"`
	Devices []*Device      `json:"devices,omitempty" db:"-"`
	Blocks  []string       `json:"blocks,omitempty" db:"-"`
}

type UserMini struct {
	RoomId      string `json:"roomId" db:"room_id"`
	UserId      string `json:"userId" db:"user_id"`
	Name        string `json:"name" db:"name"`
	PictureUrl  string `json:"pictureUrl,omitempty" db:"picture_url"`
	IsShowUsers *bool  `json:"isShowUsers,omitempty" db:"is_show_users"`
}

type RoomForUser struct {
	// from room
	RoomId             string         `json:"roomId" db:"room_id"`
	UserId             string         `json:"userId" db:"user_id"`
	Name               string         `json:"name" db:"name"`
	PictureUrl         string         `json:"pictureUrl,omitempty" db:"picture_url"`
	InformationUrl     string         `json:"informationUrl,omitempty" db:"information_url"`
	MetaData           utils.JSONText `json:"metaData" db:"meta_data"`
	Type               *RoomType      `json:"type,omitempty" db:"type"`
	LastMessage        string         `json:"lastMessage" db:"last_message"`
	LastMessageUpdated int64          `json:"lastMessageUpdated" db:"last_message_updated"`
	IsCanLeft          *bool          `json:"isCanLeft,omitempty" db:"is_can_left,notnull"`
	Created            int64          `json:"created" db:"created"`
	Modified           int64          `json:"modified" db:"modified"`

	Users []*UserMini `json:"users" db:"-"`

	// from RoomUser
	RuUnreadCount int64          `json:"ruUnreadCount" db:"ru_unread_count"`
	RuMetaData    utils.JSONText `json:"ruMetaData" db:"ru_meta_data"`
	RuCreated     int64          `json:"ruCreated" db:"ru_created"`
	RuModified    int64          `json:"ruModified" db:"ru_modified"`
}

type UserUnreadCount struct {
	UnreadCount *uint64 `json:"unreadCount" db:"unread_count"`
}

func (u *User) MarshalJSON() ([]byte, error) {
	l, _ := time.LoadLocation("Etc/GMT")
	return json.Marshal(&struct {
		UserId         string         `json:"userId"`
		Name           string         `json:"name"`
		PictureUrl     string         `json:"pictureUrl"`
		InformationUrl string         `json:"informationUrl"`
		UnreadCount    *uint64        `json:"unreadCount"`
		MetaData       utils.JSONText `json:"metaData"`
		IsBot          *bool          `json:"isBot"`
		IsPublic       *bool          `json:"isPublic"`
		IsCanBlock     *bool          `json:"isCanBlock"`
		IsShowUsers    *bool          `json:"isShowUsers"`
		Lang           string         `json:"lang"`
		AccessToken    string         `json:"accessToken,omitempty"`
		Created        string         `json:"created"`
		Modified       string         `json:"modified"`
		Rooms          []*RoomForUser `json:"rooms,omitempty"`
		Devices        []*Device      `json:"devices,omitempty"`
		Blocks         []string       `json:"blocks,omitempty"`
	}{
		UserId:         u.UserId,
		Name:           u.Name,
		PictureUrl:     u.PictureUrl,
		InformationUrl: u.InformationUrl,
		UnreadCount:    u.UnreadCount,
		MetaData:       u.MetaData,
		IsBot:          u.IsBot,
		IsPublic:       u.IsPublic,
		IsCanBlock:     u.IsCanBlock,
		IsShowUsers:    u.IsShowUsers,
		Lang:           u.Lang,
		AccessToken:    u.AccessToken,
		Created:        time.Unix(u.Created, 0).In(l).Format(time.RFC3339),
		Modified:       time.Unix(u.Modified, 0).In(l).Format(time.RFC3339),
		Rooms:          u.Rooms,
		Devices:        u.Devices,
		Blocks:         u.Blocks,
	})
}

func (rfu *RoomForUser) MarshalJSON() ([]byte, error) {
	l, _ := time.LoadLocation("Etc/GMT")
	lmu := ""
	if rfu.LastMessageUpdated != 0 {
		lmu = time.Unix(rfu.LastMessageUpdated, 0).In(l).Format(time.RFC3339)
	}
	return json.Marshal(&struct {
		RoomId             string         `json:"roomId"`
		UserId             string         `json:"userId"`
		Name               string         `json:"name"`
		PictureUrl         string         `json:"pictureUrl,omitempty"`
		InformationUrl     string         `json:"informationUrl,omitempty"`
		MetaData           utils.JSONText `json:"metaData"`
		Type               *RoomType      `json:"type,omitempty"`
		LastMessage        string         `json:"lastMessage"`
		LastMessageUpdated string         `json:"lastMessageUpdated"`
		IsCanLeft          *bool          `json:"isCanLeft,omitempty"`
		Created            string         `json:"created"`<commit_msg>Update user model<commit_after>package models

import (
	"encoding/json"
	"net/http"
	"time"

	"github.com/swagchat/chat-api/utils"
)

type Users struct {
	Users []*User `json:"users"`
}

type User struct {
	Id             uint64         `json:"-" db:"id"`
	UserId         string         `json:"userId" db:"user_id,notnull"`
	Name           string         `json:"name" db:"name,notnull"`
	PictureUrl     string         `json:"pictureUrl,omitempty" db:"picture_url"`
	InformationUrl string         `json:"informationUrl,omitempty" db:"information_url"`
	UnreadCount    *uint64        `json:"unreadCount,omitempty" db:"unread_count,notnull"`
	MetaData       utils.JSONText `json:"metaData,omitempty" db:"meta_data"`
	IsBot          *bool          `json:"isBot,omitempty" db:"is_bot,notnull"`
	IsPublic       *bool          `json:"isPublic,omitempty" db:"is_public,notnull"`
	IsCanBlock     *bool          `json:"isCanBlock,omitempty" db:"is_can_block,notnull"`
	IsShowUsers    *bool          `json:"isShowUsers,omitempty" db:"is_show_users,notnull"`
	Lang           string         `json:"lang,	omitempty" db:"lang,notnull"`
	AccessToken    string         `json:"accessToken,omitempty" db:"access_token"`
	Created        int64          `json:"created,omitempty" db:"created,notnull"`
	Modified       int64          `json:"modified,omitempty" db:"modified,notnull"`
	Deleted        int64          `json:"-" db:"deleted,notnull"`

	Rooms   []*RoomForUser `json:"rooms,omitempty" db:"-"`
	Devices []*Device      `json:"devices,omitempty" db:"-"`
	Blocks  []string       `json:"blocks,omitempty" db:"-"`
}

type UserMini struct {
	RoomId      string `json:"roomId" db:"room_id"`
	UserId      string `json:"userId" db:"user_id"`
	Name        string `json:"name" db:"name"`
	PictureUrl  string `json:"pictureUrl,omitempty" db:"picture_url"`
	IsShowUsers *bool  `json:"isShowUsers,omitempty" db:"is_show_users"`
}

type RoomForUser struct {
	// from room
	RoomId             string         `json:"roomId" db:"room_id"`
	UserId             string         `json:"userId" db:"user_id"`
	Name               string         `json:"name" db:"name"`
	PictureUrl         string         `json:"pictureUrl,omitempty" db:"picture_url"`
	InformationUrl     string         `json:"informationUrl,omitempty" db:"information_url"`
	MetaData           utils.JSONText `json:"metaData" db:"meta_data"`
	Type               *RoomType      `json:"type,omitempty" db:"type"`
	LastMessage        string         `json:"lastMessage" db:"last_message"`
	LastMessageUpdated int64          `json:"lastMessageUpdated" db:"last_message_updated"`
	IsCanLeft          *bool          `json:"isCanLeft,omitempty" db:"is_can_left,notnull"`
	Created            int64          `json:"created" db:"created"`
	Modified           int64          `json:"modified" db:"modified"`

	Users []*UserMini `json:"users" db:"-"`

	// from RoomUser
	RuUnreadCount int64          `json:"ruUnreadCount" db:"ru_unread_count"`
	RuMetaData    utils.JSONText `json:"ruMetaData" db:"ru_meta_data"`
	RuCreated     int64          `json:"ruCreated" db:"ru_created"`
	RuModified    int64          `json:"ruModified" db:"ru_modified"`
}

type UserUnreadCount struct {
	UnreadCount *uint64 `json:"unreadCount" db:"unread_count"`
}

func (u *User) MarshalJSON() ([]byte, error) {
	l, _ := time.LoadLocation("Etc/GMT")

	isBot := false
	if u.IsBot != nil {
		isBot = true
	}

	isPublic := false
	if u.IsPublic != nil {
		isPublic = true
	}

	isCanBlock := false
	if u.IsCanBlock != nil {
		isCanBlock = true
	}

	isShowUsers := false
	if u.IsShowUsers != nil {
		isShowUsers = true
	}

	return json.Marshal(&struct {
		UserId         string         `json:"userId"`
		Name           string         `json:"name"`
		PictureUrl     string         `json:"pictureUrl"`
		InformationUrl string         `json:"informationUrl"`
		UnreadCount    *uint64        `json:"unreadCount"`
		MetaData       utils.JSONText `json:"metaData"`
		IsBot          bool           `json:"isBot"`
		IsPublic       bool           `json:"isPublic"`
		IsCanBlock     bool           `json:"isCanBlock"`
		IsShowUsers    bool           `json:"isShowUsers"`
		Lang           string         `json:"lang"`
		AccessToken    string         `json:"accessToken,omitempty"`
		Created        string         `json:"created"`
		Modified       string         `json:"modified"`
		Rooms          []*RoomForUser `json:"rooms,omitempty"`
		Devices        []*Device      `json:"devices,omitempty"`
		Blocks         []string       `json:"blocks,omitempty"`
	}{
		UserId:         u.UserId,
		Name:           u.Name,
		PictureUrl:     u.PictureUrl,
		InformationUrl: u.InformationUrl,
		UnreadCount:    u.UnreadCount,
		MetaData:       u.MetaData,
		IsBot:          isBot,
		IsPublic:       isPublic,
		IsCanBlock:     isCanBlock,
		IsShowUsers:    isShowUsers,
		Lang:           u.Lang,
		AccessToken:    u.AccessToken,
		Created:        time.Unix(u.Created, 0).In(l).Format(time.RFC3339),
		Modified:       time.Unix(u.Modified, 0).In(l).Format(time.RFC3339),
		Rooms:          u.Rooms,
		Devices:        u.Devices,
		Blocks:         u.Blocks,
	})
}

func (rfu *RoomForUser) MarshalJSON() ([]byte, error) {
	l, _ := time.LoadLocation("Etc/GMT")
	lmu := ""
	if rfu.LastMessageUpdated != 0 {
		lmu = time.Unix(rfu.LastMessageUpdated, 0).In(l).Format(time.RFC3339)
	}
	return json.Marshal(&struct {
		RoomId             string         `json:"roomId"`
		UserId             string         `json:"userId"`
		Name               string         `json:"name"`
		PictureUrl         string         `json:"pictureUrl,omitempty"`
		InformationUrl     string         `json:"informationUrl,omitempty"`
		MetaData           utils.JSONText `json:"metaData"`
		Type               *RoomType      `json:"type,omitempty"`
		LastMessage        string         `json:"lastMessage"`
		LastMessageUpdated string         `json:"lastMessageUpdated"`
		IsCanLeft          *bool          `json:"isCanLeft,omitempty"`
		Created            string         `json:"created"`
{'fraction_non_alphanumeric': 0.13015737983836664, 'fraction_numerical': 0.007145895363675032, 'mean_word_length': 2.5995101041028783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6292881', 'n_tokens_mistral': 3764, 'n_tokens_neox': 3626, 'n_words': 719}