<commit_before>package models

import (
	"github.com/jinzhu/gorm"
	"github.com/koding/bongo"
	"time"
)

// NotificationActivity stores each user NotificationActivity related to notification content.
// When a user makes duplicate NotificationActivity for the same content
// old one is set as obsolete and new one is added to NotificationActivity table
type NotificationActivity struct {
	// unique identifier of NotificationActivity
	Id int64 `json:"id"`

	// notification content foreign key
	NotificationContentId int64 `json:"notificationContentId" sql:"NOT NULL"`

	// notifier account foreign key
	ActorId int64 `json:"actorId" sql:"NOT NULL"`

	// activity creation time
	CreatedAt time.Time `json:"createdAt" sql:"NOT NULL"`

	// activity obsolete information
	Obsolete bool `json:"obsolete" sql:"NOT NULL"`
}

func (a *NotificationActivity) BeforeCreate() {
	a.CreatedAt = time.Now()
}

func (a *NotificationActivity) BeforeUpdate() {
	a.Obsolete = true
}

func (a *NotificationActivity) GetId() int64 {
	return a.Id
}

func NewNotificationActivity() *NotificationActivity {
	return &NotificationActivity{}
}

func (a NotificationActivity) TableName() string {
	return "notification.notification_activity"
}

func (a *NotificationActivity) Create() error {
	s := map[string]interface{}{
		"notification_content_id": a.NotificationContentId,
		"actor_id":                a.ActorId,
		"obsolete":                false,
	}

	q := bongo.NewQS(s)
	found := true
	if err := a.One(q); err != nil {
		if err != gorm.RecordNotFound {
			return err
		}
		found = false
	}

	if found {
		if err := bongo.B.Update(a); err != nil {
			return err
		}
		a.Id = 0
		a.Obsolete = false
	}

	return bongo.B.Create(a)
}

func (a *NotificationActivity) FetchByContentIds(ids []int64) ([]NotificationActivity, error) {
	activities := make([]NotificationActivity, 0)
	err := bongo.B.DB.Table(a.TableName()).
		Where("notification_content_id IN (?)", ids).
		Order("id asc").
		Find(&activities).Error

	if err != nil {
		return nil, err
	}

	return activities, nil
}

func (a *NotificationActivity) FetchMapByContentIds(ids []int64) (map[int64][]NotificationActivity, error) {
	aMap := make(map[int64][]NotificationActivity, 0)
	if len(ids) == 0 {
		return aMap, nil
	}
	aList, err := a.FetchByContentIds(ids)
	if err != nil {
		return nil, err
	}

	for _, activity := range aList {
		aMap[activity.NotificationContentId] = append(aMap[activity.NotificationContentId], activity)
	}

	return aMap, nil
}

func (a *NotificationActivity) Fetch() error {

	return bongo.B.Fetch(a)
}

func (a *NotificationActivity) One(q *bongo.Query) error {

	return bongo.B.One(a, a, q)
}

func (a *NotificationActivity) Some(data interface{}, q *bongo.Query) error {

	return bongo.B.Some(a, data, q)
}

func (a *NotificationActivity) FetchContent() (*NotificationContent, error) {
	nc := NewNotificationContent()
	if err := nc.ById(a.NotificationContentId); err != nil {
		return nil, err
	}

	return nc, nil
}<commit_msg>Notification: LastActivity method used for fetching latest activity of the given content<commit_after>package models

import (
	"github.com/jinzhu/gorm"
	"github.com/koding/bongo"
	"time"
)

// NotificationActivity stores each user NotificationActivity related to notification content.
// When a user makes duplicate NotificationActivity for the same content
// old one is set as obsolete and new one is added to NotificationActivity table
type NotificationActivity struct {
	// unique identifier of NotificationActivity
	Id int64 `json:"id"`

	// notification content foreign key
	NotificationContentId int64 `json:"notificationContentId" sql:"NOT NULL"`

	// notifier account foreign key
	ActorId int64 `json:"actorId" sql:"NOT NULL"`

	// activity creation time
	CreatedAt time.Time `json:"createdAt" sql:"NOT NULL"`

	// activity obsolete information
	Obsolete bool `json:"obsolete" sql:"NOT NULL"`
}

func (a *NotificationActivity) BeforeCreate() {
	a.CreatedAt = time.Now()
}

func (a *NotificationActivity) BeforeUpdate() {
	a.Obsolete = true
}

func (a *NotificationActivity) GetId() int64 {
	return a.Id
}

func NewNotificationActivity() *NotificationActivity {
	return &NotificationActivity{}
}

func (a NotificationActivity) TableName() string {
	return "notification.notification_activity"
}

func (a *NotificationActivity) Create() error {
	s := map[string]interface{}{
		"notification_content_id": a.NotificationContentId,
		"actor_id":                a.ActorId,
		"obsolete":                false,
	}

	q := bongo.NewQS(s)
	found := true
	if err := a.One(q); err != nil {
		if err != gorm.RecordNotFound {
			return err
		}
		found = false
	}

	if found {
		if err := bongo.B.Update(a); err != nil {
			return err
		}
		a.Id = 0
		a.Obsolete = false
	}

	return bongo.B.Create(a)
}

func (a *NotificationActivity) FetchByContentIds(ids []int64) ([]NotificationActivity, error) {
	activities := make([]NotificationActivity, 0)
	err := bongo.B.DB.Table(a.TableName()).
		Where("notification_content_id IN (?)", ids).
		Order("id asc").
		Find(&activities).Error

	if err != nil {
		return nil, err
	}

	return activities, nil
}

func (a *NotificationActivity) FetchMapByContentIds(ids []int64) (map[int64][]NotificationActivity, error) {
	aMap := make(map[int64][]NotificationActivity, 0)
	if len(ids) == 0 {
		return aMap, nil
	}
	aList, err := a.FetchByContentIds(ids)
	if err != nil {
		return nil, err
	}

	for _, activity := range aList {
		aMap[activity.NotificationContentId] = append(aMap[activity.NotificationContentId], activity)
	}

	return aMap, nil
}

func (a *NotificationActivity) Fetch() error {

	return bongo.B.Fetch(a)
}

func (a *NotificationActivity) One(q *bongo.Query) error {

	return bongo.B.One(a, a, q)
}

func (a *NotificationActivity) Some(data interface{}, q *bongo.Query) error {

	return bongo.B.Some(a, data, q)
}

func (a *NotificationActivity) LastActivity() error {
	s := map[string]interface{}{
		"notification_content_id": a.NotificationContentId,
		"obsolete":                false,
	}

	q := bongo.NewQS(s)
	q.Sort = map[string]string{
		"id": "DESC",
	}

	return a.One(q)
}

func (a *NotificationActivity) FetchContent() (*NotificationContent, error) {
	nc := NewNotificationContent()
	if err := nc.ById(a.NotificationContentId); err != nil {
		return nil, err
	}

	return nc, nil
}
{'fraction_non_alphanumeric': 0.1271922894612103, 'fraction_numerical': 0.006320113762047717, 'mean_word_length': 4.319327731092437, 'pattern_counts': {'":': 9, '<': 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': '1464077', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2014, 'n_words': 566}