Gisle Aune
6 years ago
37 changed files with 1575 additions and 922 deletions
-
9.idea/dictionaries/gisle.xml
-
4database/database.go
-
2database/mongodb/characters.go
-
26database/mongodb/db.go
-
193database/mongodb/logs.go
-
345database/mongodb/posts.go
-
6graph2/complexity.go
-
4graph2/gqlgen.yml
-
143graph2/resolvers/log.go
-
165graph2/resolvers/post.go
-
4graph2/schema/types/Post.gql
-
16graph2/types/log.go
-
54internal/generate/id.go
-
20internal/generate/logid.go
-
35internal/importers/forumlog/metadata.go
-
81internal/importers/forumlog/metadata_test.go
-
48internal/importers/mirclike/log.go
-
69internal/importers/mirclike/log_test.go
-
130internal/importers/mirclike/post_test.go
-
52models/change.go
-
2models/changekeys/listed.go
-
6models/changekeys/one.go
-
62models/changes/submit.go
-
19models/log.go
-
17models/logs/db.go
-
93models/logs/import.go
-
18models/post.go
-
14repositories/log.go
-
16repositories/post.go
-
8repositories/repository.go
-
9services/characters.go
-
334services/logs.go
-
56services/parsers/forumlog.go
-
200services/parsers/forumlog_test.go
-
48services/parsers/mirclike.go
-
182services/parsers/mirclike_test.go
-
7services/services.go
@ -0,0 +1,9 @@ |
|||
<component name="ProjectDictionaryState"> |
|||
<dictionary name="gisle"> |
|||
<words> |
|||
<w>logbot</w> |
|||
<w>mirc</w> |
|||
<w>rctx</w> |
|||
</words> |
|||
</dictionary> |
|||
</component> |
@ -0,0 +1,193 @@ |
|||
package mongodb |
|||
|
|||
import ( |
|||
"context" |
|||
"errors" |
|||
"git.aiterp.net/rpdata/api/internal/generate" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"github.com/globalsign/mgo" |
|||
"github.com/globalsign/mgo/bson" |
|||
"strconv" |
|||
"sync" |
|||
) |
|||
|
|||
type logRepository struct { |
|||
openMutex sync.Mutex |
|||
|
|||
logs *mgo.Collection |
|||
posts *mgo.Collection |
|||
shortIdCounter *counter |
|||
} |
|||
|
|||
func newLogRepository(db *mgo.Database) (*logRepository, error) { |
|||
logs := db.C("logbot3.logs") |
|||
posts := db.C("logbot3.posts") |
|||
|
|||
err := logs.EnsureIndexKey("date") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = logs.EnsureIndexKey("channel") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = logs.EnsureIndexKey("characterIds") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = logs.EnsureIndexKey("event") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = logs.EnsureIndex(mgo.Index{Key: []string{"channel", "open"}}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
err = logs.EnsureIndex(mgo.Index{ |
|||
Key: []string{"shortId"}, |
|||
Unique: true, |
|||
DropDups: true, |
|||
}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &logRepository{ |
|||
logs: logs, |
|||
posts: posts, |
|||
shortIdCounter: newCounter(db, "auto_increment", "Log"), |
|||
}, nil |
|||
} |
|||
|
|||
func (r *logRepository) Find(ctx context.Context, id string) (*models.Log, error) { |
|||
log := new(models.Log) |
|||
err := r.logs.Find(bson.M{"$or": []bson.M{{"_id": id}, {"shortId": id}}}).One(log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return log, nil |
|||
} |
|||
|
|||
func (r *logRepository) List(ctx context.Context, filter models.LogFilter) ([]*models.Log, error) { |
|||
query := bson.M{} |
|||
if filter.Search != nil { |
|||
searchQuery := bson.M{ |
|||
"$text": bson.M{"$search": *filter.Search}, |
|||
"logId": bson.M{"$ne": nil}, |
|||
} |
|||
|
|||
logIds := make([]string, 0, 64) |
|||
err := r.posts.Find(searchQuery).Distinct("logId", &logIds) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
query["shortId"] = bson.M{"$in": logIds} |
|||
} |
|||
if filter.Open != nil { |
|||
r.openMutex.Lock() |
|||
defer r.openMutex.Unlock() |
|||
|
|||
query["open"] = filter.Open |
|||
} |
|||
if len(filter.Characters) > 0 { |
|||
query["characterIds"] = bson.M{"$in": filter.Characters} |
|||
} |
|||
if len(filter.Channels) > 0 { |
|||
query["channel"] = bson.M{"$in": filter.Channels} |
|||
} |
|||
if len(filter.Events) > 0 { |
|||
query["event"] = bson.M{"$in": filter.Events} |
|||
} |
|||
|
|||
logs := make([]*models.Log, 0, 32) |
|||
err := r.logs.Find(query).Sort("-date").Limit(filter.Limit).All(&logs) |
|||
if err != nil { |
|||
if err == mgo.ErrNotFound { |
|||
return logs, nil |
|||
} |
|||
|
|||
return nil, err |
|||
} |
|||
|
|||
return logs, nil |
|||
} |
|||
|
|||
func (r *logRepository) Insert(ctx context.Context, log models.Log) (*models.Log, error) { |
|||
nextShortId, err := r.shortIdCounter.Increment(1) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
log.ID = generate.LogID(log) |
|||
log.ShortID = "L" + strconv.Itoa(nextShortId) |
|||
|
|||
if log.Open { |
|||
// There can be only one open log in the same channel.
|
|||
r.openMutex.Lock() |
|||
defer r.openMutex.Unlock() |
|||
|
|||
_, err = r.logs.UpdateAll(bson.M{"channel": log.ChannelName, "open": true}, bson.M{"$set": bson.M{"open": false}}) |
|||
if err != nil { |
|||
return nil, errors.New("Cannot close other logs: " + err.Error()) |
|||
} |
|||
} |
|||
|
|||
err = r.logs.Insert(&log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &log, nil |
|||
} |
|||
|
|||
func (r *logRepository) Update(ctx context.Context, log models.Log, update models.LogUpdate) (*models.Log, error) { |
|||
updateBson := bson.M{} |
|||
if update.Open != nil { |
|||
if *update.Open == true { |
|||
// There can be only one open log in the same channel.
|
|||
r.openMutex.Lock() |
|||
defer r.openMutex.Unlock() |
|||
|
|||
_, err := r.logs.UpdateAll(bson.M{"channel": log.ChannelName, "open": true}, bson.M{"$set": bson.M{"open": false}}) |
|||
if err != nil { |
|||
return nil, errors.New("Cannot close other logs: " + err.Error()) |
|||
} |
|||
} |
|||
|
|||
updateBson["open"] = *update.Open |
|||
log.Open = *update.Open |
|||
} |
|||
if update.Title != nil { |
|||
updateBson["title"] = *update.Title |
|||
log.Title = *update.Title |
|||
} |
|||
if update.Description != nil { |
|||
updateBson["description"] = *update.Description |
|||
log.Description = *update.Description |
|||
} |
|||
if update.EventName != nil { |
|||
updateBson["event"] = *update.EventName |
|||
log.EventName = *update.EventName |
|||
} |
|||
|
|||
err := r.logs.UpdateId(log.ID, bson.M{"$set": updateBson}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &log, nil |
|||
} |
|||
|
|||
func (r *logRepository) Delete(ctx context.Context, log models.Log) error { |
|||
err := r.logs.RemoveId(log.ID) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
_, _ = r.posts.RemoveAll(bson.M{"logId": log.ShortID}) |
|||
|
|||
return nil |
|||
} |
@ -0,0 +1,345 @@ |
|||
package mongodb |
|||
|
|||
import ( |
|||
"context" |
|||
"git.aiterp.net/rpdata/api/internal/generate" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"git.aiterp.net/rpdata/api/repositories" |
|||
"github.com/globalsign/mgo" |
|||
"github.com/globalsign/mgo/bson" |
|||
"log" |
|||
"strings" |
|||
"sync" |
|||
"time" |
|||
) |
|||
|
|||
type postRepository struct { |
|||
logs *mgo.Collection |
|||
posts *mgo.Collection |
|||
|
|||
orderMutex sync.Mutex |
|||
} |
|||
|
|||
func newPostRepository(db *mgo.Database) (*postRepository, error) { |
|||
posts := db.C("logbot3.posts") |
|||
|
|||
err := posts.EnsureIndexKey("logId") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = posts.EnsureIndexKey("time") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = posts.EnsureIndexKey("kind") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
err = posts.EnsureIndexKey("position") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
err = posts.EnsureIndex(mgo.Index{ |
|||
Key: []string{"$text:text"}, |
|||
}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &postRepository{ |
|||
posts: posts, |
|||
logs: db.C("logbot3.logs"), |
|||
}, nil |
|||
} |
|||
|
|||
func (r *postRepository) Find(ctx context.Context, id string) (*models.Post, error) { |
|||
post := new(models.Post) |
|||
err := r.posts.FindId(id).One(post) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return post, nil |
|||
} |
|||
|
|||
func (r *postRepository) List(ctx context.Context, filter models.PostFilter) ([]*models.Post, error) { |
|||
query := bson.M{} |
|||
if filter.LogID != nil && *filter.LogID != "" { |
|||
logId := *filter.LogID |
|||
if !strings.HasPrefix(logId, "L") { |
|||
// Resolve long id to short id
|
|||
log := new(models.Log) |
|||
err := r.logs.FindId(logId).Select(bson.M{"logId": 1, "_id": 1}).One(log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
logId = log.ShortID |
|||
} |
|||
|
|||
query["logId"] = logId |
|||
} |
|||
if len(filter.IDs) > 0 { |
|||
query["_id"] = bson.M{"$in": filter.IDs} |
|||
} |
|||
if len(filter.Kinds) > 0 { |
|||
query["kind"] = bson.M{"$in": filter.Kinds} |
|||
} |
|||
if filter.Search != nil { |
|||
query["$text"] = bson.M{"$search": *filter.Search} |
|||
} |
|||
|
|||
posts := make([]*models.Post, 0, 32) |
|||
err := r.posts.Find(query).Sort("-logId", "position").Limit(filter.Limit).All(&posts) |
|||
if err != nil { |
|||
if err == mgo.ErrNotFound { |
|||
return []*models.Post{}, nil |
|||
} |
|||
|
|||
return nil, err |
|||
} |
|||
|
|||
return posts, nil |
|||
} |
|||
|
|||
func (r *postRepository) Insert(ctx context.Context, post models.Post) (*models.Post, error) { |
|||
r.orderMutex.Lock() |
|||
defer r.orderMutex.Unlock() |
|||
|
|||
lastPost := new(models.Post) |
|||
err := r.posts.Find(bson.M{"logId": post.LogID}).Sort("-position").One(lastPost) |
|||
if err != nil && err != mgo.ErrNotFound { |
|||
return nil, err |
|||
} |
|||
|
|||
post.ID = generate.PostID() |
|||
post.Position = lastPost.Position + 1 // Position 1 is first position, so this is safe.
|
|||
|
|||
err = r.posts.Insert(post) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &post, nil |
|||
} |
|||
|
|||
func (r *postRepository) InsertMany(ctx context.Context, posts ...*models.Post) ([]*models.Post, error) { |
|||
if len(posts) == 0 { |
|||
return []*models.Post{}, nil |
|||
} |
|||
|
|||
logId := posts[0].LogID |
|||
for _, post := range posts[1:] { |
|||
if post.LogID != logId { |
|||
return nil, repositories.ErrParentMismatch |
|||
} |
|||
|
|||
post.ID = generate.PostID() |
|||
} |
|||
|
|||
r.orderMutex.Lock() |
|||
defer r.orderMutex.Unlock() |
|||
|
|||
lastPost := new(models.Post) |
|||
err := r.posts.Find(bson.M{"logId": posts[0].LogID}).Sort("-position").One(lastPost) |
|||
if err != nil && err != mgo.ErrNotFound { |
|||
return nil, err |
|||
} |
|||
|
|||
docs := make([]interface{}, len(posts)) |
|||
for i := range posts { |
|||
posts[i].Position = lastPost.Position + 1 + i |
|||
docs[i] = posts[i] |
|||
} |
|||
|
|||
err = r.posts.Insert(docs...) |
|||
if err != nil && err != mgo.ErrNotFound { |
|||
return nil, err |
|||
} |
|||
|
|||
return posts, nil |
|||
} |
|||
|
|||
func (r *postRepository) Update(ctx context.Context, post models.Post, update models.PostUpdate) (*models.Post, error) { |
|||
updateBson := bson.M{} |
|||
if update.Time != nil { |
|||
updateBson["time"] = *update.Time |
|||
post.Time = *update.Time |
|||
} |
|||
if update.Kind != nil { |
|||
updateBson["kind"] = *update.Kind |
|||
post.Kind = *update.Kind |
|||
} |
|||
if update.Nick != nil { |
|||
updateBson["nick"] = *update.Nick |
|||
post.Nick = *update.Nick |
|||
} |
|||
if update.Text != nil { |
|||
updateBson["text"] = *update.Text |
|||
post.Text = *update.Text |
|||
} |
|||
|
|||
err := r.posts.UpdateId(post.ID, bson.M{"$set": updateBson}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &post, nil |
|||
} |
|||
|
|||
func (r *postRepository) Move(ctx context.Context, post models.Post, position int) ([]*models.Post, error) { |
|||
// If only MongoDB transactions weren't awful, this function would have been safe.
|
|||
// Since it isn't, then good luck.
|
|||
|
|||
// Validate lower position bound.
|
|||
if position < 1 { |
|||
return nil, repositories.ErrInvalidPosition |
|||
} |
|||
|
|||
// Determine the operations on adjacent posts
|
|||
var resultFilter bson.M |
|||
var pushFilter bson.M |
|||
var increment int |
|||
if post.Position > position { |
|||
pushFilter = bson.M{"position": bson.M{ |
|||
"$gte": position, |
|||
"$lt": post.Position, |
|||
}} |
|||
increment = 1 |
|||
resultFilter = bson.M{"position": bson.M{ |
|||
"$lte": post.Position, |
|||
"$gte": position, |
|||
}} |
|||
} else { |
|||
pushFilter = bson.M{"position": bson.M{ |
|||
"$lte": position, |
|||
"$gt": post.Position, |
|||
}} |
|||
increment = -1 |
|||
resultFilter = bson.M{"position": bson.M{ |
|||
"$lte": position, |
|||
"$gte": post.Position, |
|||
}} |
|||
} |
|||
pushFilter["logId"] = post.LogID |
|||
resultFilter["logId"] = post.LogID |
|||
|
|||
// From here on out, sync is required
|
|||
r.orderMutex.Lock() |
|||
defer r.orderMutex.Unlock() |
|||
|
|||
// Detect ninja shenanigans
|
|||
post2 := new(models.Post) |
|||
err := r.posts.FindId(post.ID).One(post2) |
|||
if err != nil && post2.Position != post.Position { |
|||
return nil, repositories.ErrNotFound |
|||
} |
|||
|
|||
// Validate upper position bound
|
|||
lastPost := new(models.Post) |
|||
err = r.posts.Find(bson.M{"logId": post.LogID}).Sort("-position").One(lastPost) |
|||
if err != nil && err != mgo.ErrNotFound { |
|||
return nil, err |
|||
} |
|||
if position > lastPost.Position { |
|||
return nil, repositories.ErrInvalidPosition |
|||
} |
|||
|
|||
// Move the posts
|
|||
changeInfo, err := r.posts.UpdateAll(pushFilter, bson.M{"$inc": bson.M{"position": increment}}) |
|||
if err != nil && err != mgo.ErrNotFound { |
|||
return nil, err |
|||
} |
|||
err = r.posts.UpdateId(post.ID, bson.M{"$set": bson.M{"position": position}}) |
|||
if err != nil { |
|||
// Try to undo it
|
|||
_, err := r.posts.UpdateAll(pushFilter, bson.M{"$inc": bson.M{"position": -increment}}) |
|||
if err != nil && err != mgo.ErrNotFound { |
|||
return nil, err |
|||
} |
|||
|
|||
return nil, err |
|||
} |
|||
|
|||
results := make([]*models.Post, 0, changeInfo.Matched+1) |
|||
err = r.posts.Find(resultFilter).All(&results) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return results, nil |
|||
} |
|||
|
|||
func (r *postRepository) Delete(ctx context.Context, post models.Post) error { |
|||
r.orderMutex.Lock() |
|||
defer r.orderMutex.Unlock() |
|||
|
|||
err := r.posts.RemoveId(post.ID) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
_, _ = r.posts.UpdateAll(bson.M{"logId": post.LogID, "position": bson.M{"$gt": post.Position}}, bson.M{"$inc": bson.M{"position": -1}}) |
|||
|
|||
return nil |
|||
} |
|||
|
|||
func (r *postRepository) fixPositions(logRepo *logRepository) { |
|||
disorders := make([]int, 0, 16) |
|||
diffs := make([]int, 0, 16) |
|||
startTime := time.Now() |
|||
|
|||
timeout, cancel := context.WithTimeout(context.Background(), time.Minute) |
|||
defer cancel() |
|||
|
|||
logs, err := logRepo.List(timeout, models.LogFilter{}) |
|||
if err != nil { |
|||
log.Println("Failed to get logs for position fix:", err) |
|||
} |
|||
|
|||
log.Println("Starting log position fixing, this should not take longer than 10 seconds.") |
|||
|
|||
for _, l := range logs { |
|||
r.orderMutex.Lock() |
|||
|
|||
posts, err := r.List(timeout, models.PostFilter{LogID: &l.ShortID}) |
|||
if err != nil { |
|||
r.orderMutex.Unlock() |
|||
continue |
|||
} |
|||
|
|||
disorders = disorders[:0] |
|||
diffs = diffs[:0] |
|||
for i, post := range posts { |
|||
if post.Position != (i + 1) { |
|||
disorders = append(disorders, i) |
|||
diffs = append(diffs, post.Position-(i+1)) |
|||
} |
|||
} |
|||
|
|||
if len(disorders) > 0 { |
|||
log.Println(len(disorders), "order errors detected in", l.ID) |
|||
|
|||
ops := 0 |
|||
|
|||
for i, post := range posts { |
|||
if (i + 1) != posts[i].Position { |
|||
ops++ |
|||
|
|||
err := r.posts.UpdateId(post.ID, bson.M{"$set": bson.M{"position": i + 1}}) |
|||
if err != nil { |
|||
log.Println(l.ShortID, "fix failed after", ops, "ops:", err) |
|||
break |
|||
} |
|||
} |
|||
} |
|||
|
|||
log.Println(l.ShortID, "fixed after", ops, "ops.") |
|||
} |
|||
|
|||
r.orderMutex.Unlock() |
|||
} |
|||
|
|||
log.Println("Log position fixing finished in", time.Since(startTime)) |
|||
} |
@ -0,0 +1,54 @@ |
|||
package generate |
|||
|
|||
import ( |
|||
"crypto/rand" |
|||
"encoding/binary" |
|||
mrand "math/rand" |
|||
"strconv" |
|||
"strings" |
|||
) |
|||
|
|||
// ID generates an ID using crypto-random, falling back to math random when that fails
|
|||
// to avoid disrupting operation because of a faulty RNG.
|
|||
func ID(prefix string, length int) string { |
|||
var data [32]byte |
|||
|
|||
result := strings.Builder{} |
|||
result.Grow(length + 32) |
|||
result.WriteString(prefix) |
|||
|
|||
pos := 0 |
|||
for result.Len() < length { |
|||
if pos == 0 { |
|||
randRead(data[:]) |
|||
} |
|||
|
|||
result.WriteString(strconv.FormatUint(binary.BigEndian.Uint64(data[pos:pos+8]), 36)) |
|||
|
|||
pos = (pos + 8) % 32 |
|||
} |
|||
|
|||
return result.String()[:length] |
|||
} |
|||
|
|||
func randRead(data []byte) { |
|||
n, err := rand.Read(data) |
|||
if err != nil { |
|||
mrand.Read(data[n:]) |
|||
} |
|||
} |
|||
|
|||
// PostID generates a post ID.
|
|||
func PostID() string { |
|||
return ID("P", 16) |
|||
} |
|||
|
|||
// StoryID generates a post ID.
|
|||
func StoryID() string { |
|||
return ID("S", 16) |
|||
} |
|||
|
|||
// ChapterID generates a post ID.
|
|||
func ChapterID() string { |
|||
return ID("SC", 24) |
|||
} |
@ -0,0 +1,20 @@ |
|||
package generate |
|||
|
|||
import ( |
|||
"fmt" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"time" |
|||
) |
|||
|
|||
// LogID generates a log ID in the format is 2019-05-22_191341547_RedrockAgency
|
|||
func LogID(log models.Log) string { |
|||
if len(log.ChannelName) < 1 { |
|||
panic("ChannelName is not valid (validate input before calling this function!)") |
|||
} |
|||
|
|||
datetime := log.Date.UTC().Format("2006-01-02_150405") |
|||
milliseconds := (log.Date.UnixNano() % int64(time.Second)) / 1000000 |
|||
channelName := log.ChannelName[1:] |
|||
|
|||
return fmt.Sprintf("%s%03d_%s", datetime, milliseconds, channelName) |
|||
} |
@ -1,35 +0,0 @@ |
|||
package forumlog |
|||
|
|||
import ( |
|||
"bufio" |
|||
"strings" |
|||
) |
|||
|
|||
// ParseMetadata parses metadata, discards the broken parts, and returns the
|
|||
// parsed data as a map (`m`) and the position of the first IRC post (`n`)
|
|||
func ParseMetadata(data string) map[string][]string { |
|||
result := make(map[string][]string) |
|||
key := "" |
|||
|
|||
scanner := bufio.NewScanner(strings.NewReader(data)) |
|||
for scanner.Scan() { |
|||
line := strings.Trim(scanner.Text(), "\r\t ") |
|||
|
|||
if strings.HasPrefix(line, "[") { |
|||
break |
|||
} |
|||
if len(line) < 1 { |
|||
key = "" |
|||
continue |
|||
} |
|||
|
|||
if key == "" { |
|||
split := strings.Split(line, ":") |
|||
key = split[0] |
|||
} else { |
|||
result[key] = append(result[key], line) |
|||
} |
|||
} |
|||
|
|||
return result |
|||
} |
@ -1,81 +0,0 @@ |
|||
package forumlog_test |
|||
|
|||
import ( |
|||
"strings" |
|||
"testing" |
|||
|
|||
"github.com/stretchr/testify/assert" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/importers/forumlog" |
|||
) |
|||
|
|||
func TestParseMetadata(t *testing.T) { |
|||
data := forumlog.ParseMetadata(testData1) |
|||
|
|||
assert.Equal(t, data["Date"], []string{ |
|||
"August 11, 2014", |
|||
"August 12, 2014", |
|||
"August 13, 2014", |
|||
}) |
|||
assert.Equal(t, data["Location"], []string{ |
|||
"Aroste, an island off the Derraian coastline.", |
|||
}) |
|||
assert.Equal(t, data["GM"], []string{ |
|||
"Gisle", |
|||
}) |
|||
assert.Equal(t, data["Length"], []string{ |
|||
"847 posts", |
|||
}) |
|||
assert.Equal(t, len(data["Stuff"]), 0) |
|||
|
|||
for key := range data { |
|||
if strings.HasPrefix(key, "[") { |
|||
t.Error("It should have stopped at the IRC post, yet this key exists:", key) |
|||
} |
|||
} |
|||
} |
|||
|
|||
var testData1 = ` |
|||
Date: August 2185 |
|||
August 11, 2014 |
|||
August 12, 2014 |
|||
August 13, 2014 |
|||
|
|||
Location: |
|||
Aroste, an island off the Derraian coastline. |
|||
|
|||
GM: |
|||
Gisle |
|||
|
|||
Length: |
|||
847 posts |
|||
|
|||
Characters: |
|||
Calyx Vadris - Osiris |
|||
Damien Monroe - Bowe |
|||
Jason Wolfe - Dante |
|||
Renala T'Iavay - Gisle |
|||
Victoria Steels - MCB280 |
|||
|
|||
NPCs: |
|||
Recurring: |
|||
Halisi - Tyranniac |
|||
Jattic - Dante |
|||
Jelvan Darennon - Gisle |
|||
Leah - Dante |
|||
Marissa T'Evin - Gisle |
|||
Philip Lacour - Tyranniac |
|||
|
|||
One-off: |
|||
Reidas Falten (Turian - Guard meeting them on their trip to the building) |
|||
Sarian T'Dera (Asari - Asari opening fire on Calyx, Leah and Renala) |
|||
Mariam Adams (Human - The receptionist.) |
|||
Prerix Falten (Turian2 - Victoria's chair) |
|||
Jonathan Lyng (OtherHuman - The one opening fire on Leah entering the garage) |
|||
Alerena Teris (Teris - Asari met in the maintenance corridor) |
|||
|
|||
<-- Redorck Agency - August 11, 2014 |
|||
|
|||
[13:19] * Leah looks over at Damien. "Haven't seen you around the agency." she says curiously, "New hire?" |
|||
|
|||
` |
@ -1,48 +0,0 @@ |
|||
package mirclike |
|||
|
|||
import ( |
|||
"errors" |
|||
"strings" |
|||
"time" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
// ErrEmptyLog is returned by ParseLog if there are no (valid) posts in the log.
|
|||
var ErrEmptyLog = errors.New("No valid posts found in log") |
|||
|
|||
// ParseLog parses the log and returns the things that can be gleamed from them.
|
|||
func ParseLog(data string, date time.Time, strict bool) (models.Log, []models.Post, error) { |
|||
lines := strings.Split(data, "\n") |
|||
posts := make([]models.Post, 0, len(lines)) |
|||
prev := models.Post{} |
|||
|
|||
for _, line := range lines { |
|||
line = strings.Trim(line, "\r\t ") |
|||
if len(line) < 1 { |
|||
continue |
|||
} |
|||
|
|||
post, err := ParsePost(line, date, prev) |
|||
if err != nil { |
|||
if strict { |
|||
return models.Log{}, nil, err |
|||
} |
|||
|
|||
continue |
|||
} |
|||
|
|||
posts = append(posts, post) |
|||
prev = post |
|||
} |
|||
|
|||
if len(posts) == 0 { |
|||
return models.Log{}, nil, ErrEmptyLog |
|||
} |
|||
|
|||
log := models.Log{ |
|||
Date: posts[0].Time, |
|||
} |
|||
|
|||
return log, posts, nil |
|||
} |
@ -1,69 +0,0 @@ |
|||
package mirclike_test |
|||
|
|||
import ( |
|||
"strings" |
|||
"testing" |
|||
"time" |
|||
|
|||
"github.com/stretchr/testify/assert" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/importers/mirclike" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
var testLog = strings.Join([]string{ |
|||
"[11:21] * Va`ynna_Atana returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.", |
|||
"", |
|||
"[11:21] <=Scene=> The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder. ", |
|||
"", |
|||
"[11:", |
|||
" [11:27] <Test> Stuff and things.", |
|||
}, "\r\n") |
|||
|
|||
var testLogPosts = []models.Post{ |
|||
{ |
|||
ID: "UNASSIGNED", |
|||
LogID: "UNASSIGNED", |
|||
Time: parseDate(nil, "2018-05-11 11:21:00"), |
|||
Kind: "action", |
|||
Nick: "Va`ynna_Atana", |
|||
Position: 1, |
|||
Text: "returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.", |
|||
}, |
|||
{ |
|||
ID: "UNASSIGNED", |
|||
LogID: "UNASSIGNED", |
|||
Time: parseDate(nil, "2018-05-11 11:21:00"), |
|||
Kind: "scene", |
|||
Nick: "=Scene=", |
|||
Position: 2, |
|||
Text: "The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder.", |
|||
}, |
|||
{ |
|||
ID: "UNASSIGNED", |
|||
LogID: "UNASSIGNED", |
|||
Time: parseDate(nil, "2018-05-11 11:27:00"), |
|||
Kind: "text", |
|||
Nick: "Test", |
|||
Position: 3, |
|||
Text: "Stuff and things.", |
|||
}, |
|||
} |
|||
|
|||
func TestParseLog(t *testing.T) { |
|||
log, posts, err := mirclike.ParseLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false) |
|||
if err != nil { |
|||
t.Fatal("ParseLog", err) |
|||
} |
|||
|
|||
assert.Equal(t, testLogPosts, posts) |
|||
assert.Equal(t, posts[0].Time, log.Date, "Log's date should be the first post's.") |
|||
} |
|||
|
|||
func TestParseLogErrors(t *testing.T) { |
|||
_, _, err1 := mirclike.ParseLog("\n\n\n\n\n \n\t\r\n", time.Time{}, false) |
|||
_, _, err2 := mirclike.ParseLog("\n\n\n\n\n[14:57]* Stuff \n\t\r\n", time.Time{}, true) |
|||
|
|||
assert.Equal(t, mirclike.ErrEmptyLog, err1) |
|||
assert.Equal(t, mirclike.ErrNotPost, err2) |
|||
} |
@ -1,130 +0,0 @@ |
|||
package mirclike_test |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
"time" |
|||
|
|||
"github.com/stretchr/testify/assert" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/importers/mirclike" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
func TestParsePost(t *testing.T) { |
|||
table := []struct { |
|||
Input string |
|||
TS string |
|||
Kind string |
|||
Nick string |
|||
Text string |
|||
}{ |
|||
{ |
|||
"[12:34] * Stuff does things.", |
|||
"12:34:00", "action", "Stuff", "does things.", |
|||
}, |
|||
{ |
|||
"[12:34] <Stuff> Things said.", |
|||
"12:34:00", "text", "Stuff", "Things said.", |
|||
}, |
|||
{ |
|||
"[13:36:59] <Stuff> Things said.", |
|||
"13:36:59", "text", "Stuff", "Things said.", |
|||
}, |
|||
{ |
|||
"[23:59] <=Scene=> Scenery and such.", |
|||
"23:59:00", "scene", "=Scene=", "Scenery and such.", |
|||
}, |
|||
{ |
|||
"[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.", |
|||
"01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.", |
|||
}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
post, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{}) |
|||
if err != nil { |
|||
t.Fatal("Could not parse post:", err) |
|||
} |
|||
|
|||
assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.") |
|||
assert.Equal(t, row.Kind, post.Kind, "Kinds should match.") |
|||
assert.Equal(t, row.Nick, post.Nick, "Kinds should match.") |
|||
assert.Equal(t, row.Text, post.Text, "Kinds should match.") |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func TestParsePostErrors(t *testing.T) { |
|||
table := []struct { |
|||
Input string |
|||
Err error |
|||
}{ |
|||
{"[12:34] <Stuff> Things said.", nil}, |
|||
{"[12:34] >Stuff> Things said.", mirclike.ErrNotPost}, |
|||
{"12:34] <Stuff> Things said.", mirclike.ErrNotPost}, |
|||
{"* Stuff Things said.", mirclike.ErrNotPost}, |
|||
{"", mirclike.ErrNotPost}, |
|||
{"[12:34 <Stuff> Things said.", mirclike.ErrNotPost}, |
|||
{"[TE:XT] <Stuff> Things said.", mirclike.ErrNotPost}, |
|||
{"[10] <Stuff> Things said.", mirclike.ErrNotPost}, |
|||
{"[12:34:56:789] <Stuff> Things said.", nil}, |
|||
{"[12:34:56.789] <Stuff> Things said.", mirclike.ErrNotPost}, |
|||
{"[12:34] <Stuff>", mirclike.ErrNotPost}, |
|||
{"[12:34] * Stuff", mirclike.ErrNotPost}, |
|||
{"[12:34] =Scene=", mirclike.ErrNotPost}, |
|||
{"[12:34] <=Scene=>", mirclike.ErrNotPost}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
_, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{}) |
|||
|
|||
assert.Equal(t, row.Err, err, "Error should match") |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func TestParseNextDay(t *testing.T) { |
|||
table := []struct { |
|||
Prev time.Time |
|||
TS string |
|||
Time time.Time |
|||
}{ |
|||
{Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")}, |
|||
{Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")}, |
|||
{Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")}, |
|||
{Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
input := fmt.Sprintf("[%s] * Stuff does things.", row.TS) |
|||
|
|||
post, err := mirclike.ParsePost(input, row.Prev, models.Post{Time: row.Prev}) |
|||
if err != nil { |
|||
t.Fatal("Could not parse post:", err) |
|||
} |
|||
|
|||
assert.Equal(t, row.Time, post.Time) |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func parseDate(t *testing.T, date string) time.Time { |
|||
result, err := time.Parse("2006-01-02 15:04:05", date) |
|||
if err != nil { |
|||
if t != nil { |
|||
t.Fatal("Could not parse date", date, err) |
|||
} else { |
|||
panic("Could not parse date: " + err.Error()) |
|||
} |
|||
} |
|||
|
|||
return result |
|||
} |
|||
|
|||
func formatDate(date time.Time) string { |
|||
return date.UTC().Format("2006-01-02 15:04:05") |
|||
} |
@ -1,93 +0,0 @@ |
|||
package logs |
|||
|
|||
import ( |
|||
"errors" |
|||
"time" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/importers/mirclike" |
|||
|
|||
"git.aiterp.net/rpdata/api/models/posts" |
|||
|
|||
"git.aiterp.net/rpdata/api/models/channels" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/importers/forumlog" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
// An ImportedLog contains data about an imported log.
|
|||
type ImportedLog struct { |
|||
Log models.Log |
|||
Posts []models.Post |
|||
} |
|||
|
|||
// Import makes a log and posts object from different formats.
|
|||
func Import(importer models.LogImporter, date time.Time, tz *time.Location, channelName string, data string) ([]ImportedLog, error) { |
|||
results := make([]ImportedLog, 0, 8) |
|||
|
|||
eventName := "" |
|||
if channel, err := channels.FindName(channelName); err != nil { |
|||
eventName = channel.EventName |
|||
} |
|||
|
|||
date = date.In(tz) |
|||
|
|||
switch importer { |
|||
case models.LogImporterMircLike: |
|||
{ |
|||
if date.IsZero() { |
|||
return nil, errors.New("Date is not optional for mirc-like logs") |
|||
} |
|||
|
|||
parsedLog, parsedPosts, err := mirclike.ParseLog(data, date, true) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
log, err := Add(parsedLog.Date, channelName, "", eventName, "", false) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
posts, err := posts.AddMany(log, parsedPosts) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
results = append(results, ImportedLog{ |
|||
Log: log, |
|||
Posts: posts, |
|||
}) |
|||
} |
|||
case models.LogImporterForumLog: |
|||
{ |
|||
parseResults, err := forumlog.ParseLogs(data, tz) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
for _, result := range parseResults { |
|||
log, err := Add(result.Log.Date, channelName, "", eventName, "", false) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
posts, err := posts.AddMany(log, result.Posts) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
results = append(results, ImportedLog{ |
|||
Log: log, |
|||
Posts: posts, |
|||
}) |
|||
} |
|||
} |
|||
default: |
|||
{ |
|||
return nil, errors.New("Invalid importer: " + importer.String()) |
|||
} |
|||
} |
|||
|
|||
return results, nil |
|||
} |
@ -0,0 +1,14 @@ |
|||
package repositories |
|||
|
|||
import ( |
|||
"context" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
type LogRepository interface { |
|||
Find(ctx context.Context, id string) (*models.Log, error) |
|||
List(ctx context.Context, filter models.LogFilter) ([]*models.Log, error) |
|||
Insert(ctx context.Context, log models.Log) (*models.Log, error) |
|||
Update(ctx context.Context, log models.Log, update models.LogUpdate) (*models.Log, error) |
|||
Delete(ctx context.Context, log models.Log) error |
|||
} |
@ -0,0 +1,16 @@ |
|||
package repositories |
|||
|
|||
import ( |
|||
"context" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
type PostRepository interface { |
|||
Find(ctx context.Context, id string) (*models.Post, error) |
|||
List(ctx context.Context, filter models.PostFilter) ([]*models.Post, error) |
|||
Insert(ctx context.Context, post models.Post) (*models.Post, error) |
|||
InsertMany(ctx context.Context, posts ...*models.Post) ([]*models.Post, error) |
|||
Update(ctx context.Context, post models.Post, update models.PostUpdate) (*models.Post, error) |
|||
Move(ctx context.Context, post models.Post, position int) ([]*models.Post, error) |
|||
Delete(ctx context.Context, post models.Post) error |
|||
} |
@ -0,0 +1,334 @@ |
|||
package services |
|||
|
|||
import ( |
|||
"context" |
|||
"errors" |
|||
"git.aiterp.net/rpdata/api/internal/auth" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"git.aiterp.net/rpdata/api/models/changekeys" |
|||
"git.aiterp.net/rpdata/api/models/channels" |
|||
"git.aiterp.net/rpdata/api/repositories" |
|||
"git.aiterp.net/rpdata/api/services/parsers" |
|||
"time" |
|||
) |
|||
|
|||
type LogService struct { |
|||
logs repositories.LogRepository |
|||
posts repositories.PostRepository |
|||
changeService *ChangeService |
|||
} |
|||
|
|||
func (s *LogService) Find(ctx context.Context, id string) (*models.Log, error) { |
|||
return s.logs.Find(ctx, id) |
|||
} |
|||
|
|||
func (s *LogService) FindPosts(ctx context.Context, id string) (*models.Post, error) { |
|||
return s.posts.Find(ctx, id) |
|||
} |
|||
|
|||
func (s *LogService) List(ctx context.Context, filter *models.LogFilter) ([]*models.Log, error) { |
|||
if filter == nil { |
|||
filter = &models.LogFilter{} |
|||
} |
|||
|
|||
return s.logs.List(ctx, *filter) |
|||
} |
|||
|
|||
func (s *LogService) ListPosts(ctx context.Context, filter *models.PostFilter) ([]*models.Post, error) { |
|||
// Some sanity checks to avoid querying an insame amount of posts.
|
|||
if filter == nil { |
|||
filter = &models.PostFilter{Limit: 100} |
|||
} else { |
|||
if (filter.Limit <= 0 || filter.Limit > 512) && (filter.LogID == nil && len(filter.IDs) == 0) { |
|||
return nil, errors.New("a limit of 0 (no limit) or >512 without a logId or a set of IDs is not allowed") |
|||
} |
|||
|
|||
if len(filter.IDs) > 100 { |
|||
return nil, errors.New("you may not query for more than 100 ids, split your query") |
|||
} |
|||
} |
|||
|
|||
return s.posts.List(ctx, *filter) |
|||
} |
|||
|
|||
func (s *LogService) Create(ctx context.Context, title, description, channelName, eventName string, open bool) (*models.Log, error) { |
|||
if channelName == "" { |
|||
return nil, errors.New("channel name cannot be empty") |
|||
} |
|||
|
|||
log := &models.Log{ |
|||
Title: title, |
|||
Description: description, |
|||
ChannelName: channelName, |
|||
EventName: eventName, |
|||
Date: time.Now(), |
|||
Open: open, |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "add", log); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
// TODO: s.channelService.Ensure()
|
|||
|
|||
log, err := s.logs.Insert(ctx, *log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(log), log) |
|||
|
|||
return log, nil |
|||
} |
|||
|
|||
// Import creates new logs from common formats.
|
|||
func (s *LogService) Import(ctx context.Context, importer models.LogImporter, date time.Time, tz *time.Location, channelName string, data string) ([]*models.Log, error) { |
|||
if err := auth.CheckPermission(ctx, "add", &models.Log{}); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
results := make([]*models.Log, 0, 8) |
|||
|
|||
eventName := "" |
|||
if channel, err := channels.FindName(channelName); err != nil { |
|||
eventName = channel.EventName |
|||
} |
|||
|
|||
// TODO: Ensure channel
|
|||
|
|||
date = date.In(tz) |
|||
|
|||
switch importer { |
|||
case models.LogImporterMircLike: |
|||
{ |
|||
if date.IsZero() { |
|||
return nil, errors.New("date is not optional for mirc-like logs") |
|||
} |
|||
|
|||
parsed, err := parsers.MircLog(data, date, true) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
parsed.Log.EventName = eventName |
|||
parsed.Log.ChannelName = channelName |
|||
|
|||
log, err := s.logs.Insert(ctx, parsed.Log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
for _, post := range parsed.Posts { |
|||
post.LogID = log.ShortID |
|||
} |
|||
|
|||
posts, err := s.posts.InsertMany(ctx, parsed.Posts...) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(log), log) |
|||
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Listed(log, posts), log, posts) |
|||
|
|||
results = append(results, log) |
|||
} |
|||
case models.LogImporterForumLog: |
|||
{ |
|||
parseResults, err := parsers.ForumLog(data, tz) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
for _, parsed := range parseResults { |
|||
log, err := s.logs.Insert(ctx, parsed.Log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
parsed.Log.EventName = eventName |
|||
parsed.Log.ChannelName = channelName |
|||
|
|||
for _, post := range parsed.Posts { |
|||
post.LogID = log.ShortID |
|||
} |
|||
|
|||
posts, err := s.posts.InsertMany(ctx, parsed.Posts...) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelLog, "add", true, changekeys.Listed(log), log) |
|||
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Listed(log, posts), log, posts) |
|||
} |
|||
} |
|||
default: |
|||
{ |
|||
return nil, errors.New("Invalid importer: " + importer.String()) |
|||
} |
|||
} |
|||
|
|||
return results, nil |
|||
} |
|||
|
|||
func (s *LogService) Update(ctx context.Context, id string, update models.LogUpdate) (*models.Log, error) { |
|||
log, err := s.logs.Find(ctx, id) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "edit", log); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
log, err = s.logs.Update(ctx, *log, update) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelLog, "edit", true, changekeys.Listed(log), log) |
|||
|
|||
return log, nil |
|||
} |
|||
|
|||
func (s *LogService) AddPost(ctx context.Context, logId string, time time.Time, kind, nick, text string) (*models.Post, error) { |
|||
if kind == "" || nick == "" || time.IsZero() { |
|||
return nil, errors.New("kind, nick and time must be non-empty") |
|||
} |
|||
|
|||
log, err := s.logs.Find(ctx, logId) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
post := &models.Post{ |
|||
LogID: log.ShortID, |
|||
Kind: kind, |
|||
Nick: nick, |
|||
Text: text, |
|||
Time: time, |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "add", post); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
post, err = s.posts.Insert(ctx, *post) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelPost, "add", true, changekeys.Many(log, post), log, post) |
|||
|
|||
return post, nil |
|||
} |
|||
|
|||
func (s *LogService) EditPost(ctx context.Context, id string, update models.PostUpdate) (*models.Post, error) { |
|||
if (update.Kind != nil && *update.Kind == "") || (update.Nick != nil && *update.Nick == "") || (update.Text != nil && *update.Text == "") { |
|||
return nil, errors.New("kind, nick and time must be non-empty") |
|||
} |
|||
|
|||
post, err := s.posts.Find(ctx, id) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "edit", post); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
post, err = s.posts.Update(ctx, *post, update) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
go func() { |
|||
log, err := s.logs.Find(context.Background(), post.LogID) |
|||
if err != nil { |
|||
return |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelPost, "edit", true, changekeys.Many(log, post), post) |
|||
}() |
|||
|
|||
return post, nil |
|||
} |
|||
|
|||
func (s *LogService) MovePost(ctx context.Context, id string, position int) ([]*models.Post, error) { |
|||
if position < 1 { |
|||
return nil, repositories.ErrInvalidPosition |
|||
} |
|||
|
|||
post, err := s.posts.Find(ctx, id) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "move", post); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
posts, err := s.posts.Move(ctx, *post, position) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
go func() { |
|||
if len(posts) == 0 { |
|||
return |
|||
} |
|||
|
|||
log, err := s.logs.Find(context.Background(), posts[0].LogID) |
|||
if err != nil { |
|||
return |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelPost, "move", true, changekeys.Many(log, posts), posts) |
|||
}() |
|||
|
|||
return posts, nil |
|||
} |
|||
|
|||
func (s *LogService) DeletePost(ctx context.Context, id string) (*models.Post, error) { |
|||
post, err := s.posts.Find(ctx, id) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "remove", post); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
err = s.posts.Delete(ctx, *post) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
go func() { |
|||
log, err := s.logs.Find(context.Background(), post.LogID) |
|||
if err != nil { |
|||
return |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelPost, "remove", true, changekeys.Many(log, post), post) |
|||
}() |
|||
|
|||
return post, nil |
|||
} |
|||
|
|||
func (s *LogService) Delete(ctx context.Context, id string) (*models.Log, error) { |
|||
log, err := s.logs.Find(ctx, id) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
if err := auth.CheckPermission(ctx, "remove", log); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
err = s.logs.Delete(ctx, *log) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
s.changeService.Submit(ctx, models.ChangeModelLog, "remove", true, changekeys.Listed(log), log) |
|||
|
|||
return log, nil |
|||
} |
@ -1,20 +1,58 @@ |
|||
package mirclike |
|||
package parsers |
|||
|
|||
import ( |
|||
"errors" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"strconv" |
|||
"strings" |
|||
"time" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
// ErrNotPost is returned by parsePost if the line is empty or not a post.
|
|||
var ErrNotPost = errors.New("not a post") |
|||
|
|||
// ParsePost parses a post from a mirc-like line. If the previous post is included (it can be empty), it will be used
|
|||
// ErrEmptyLog is returned by ParseLog if there are no (valid) posts in the log.
|
|||
var ErrEmptyLog = errors.New("no valid posts found in log") |
|||
|
|||
// MircLog parses the log and returns the things that can be gleamed from them.
|
|||
func MircLog(data string, date time.Time, strict bool) (*ParsedLog, error) { |
|||
lines := strings.Split(data, "\n") |
|||
posts := make([]*models.Post, 0, len(lines)) |
|||
prev := models.Post{} |
|||
|
|||
for _, line := range lines { |
|||
line = strings.Trim(line, "\r\t ") |
|||
if len(line) < 1 { |
|||
continue |
|||
} |
|||
|
|||
post, err := MircPost(line, date, prev) |
|||
if err != nil { |
|||
if strict { |
|||
return nil, err |
|||
} |
|||
|
|||
continue |
|||
} |
|||
|
|||
posts = append(posts, &post) |
|||
prev = post |
|||
} |
|||
|
|||
if len(posts) == 0 { |
|||
return nil, ErrEmptyLog |
|||
} |
|||
|
|||
log := models.Log{ |
|||
Date: posts[0].Time, |
|||
} |
|||
|
|||
return &ParsedLog{log, posts}, nil |
|||
} |
|||
|
|||
// MircPost parses a post from a mirc-like line. If the previous post is included (it can be empty), it will be used
|
|||
// to determine whether midnight has passed.
|
|||
func ParsePost(line string, date time.Time, prev models.Post) (models.Post, error) { |
|||
func MircPost(line string, date time.Time, prev models.Post) (models.Post, error) { |
|||
// Do basic validation
|
|||
line = strings.Trim(line, " \t\n\r") |
|||
if len(line) == 0 || !strings.HasPrefix(line, "[") { |
@ -0,0 +1,182 @@ |
|||
package parsers_test |
|||
|
|||
import ( |
|||
"fmt" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"git.aiterp.net/rpdata/api/services/parsers" |
|||
"github.com/stretchr/testify/assert" |
|||
"strings" |
|||
"testing" |
|||
"time" |
|||
) |
|||
|
|||
func TestMircPost(t *testing.T) { |
|||
table := []struct { |
|||
Input string |
|||
TS string |
|||
Kind string |
|||
Nick string |
|||
Text string |
|||
}{ |
|||
{ |
|||
"[12:34] * Stuff does things.", |
|||
"12:34:00", "action", "Stuff", "does things.", |
|||
}, |
|||
{ |
|||
"[12:34] <Stuff> Things said.", |
|||
"12:34:00", "text", "Stuff", "Things said.", |
|||
}, |
|||
{ |
|||
"[13:36:59] <Stuff> Things said.", |
|||
"13:36:59", "text", "Stuff", "Things said.", |
|||
}, |
|||
{ |
|||
"[23:59] <=Scene=> Scenery and such.", |
|||
"23:59:00", "scene", "=Scene=", "Scenery and such.", |
|||
}, |
|||
{ |
|||
"[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.", |
|||
"01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.", |
|||
}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
post, err := parsers.MircPost(row.Input, time.Now(), models.Post{}) |
|||
if err != nil { |
|||
t.Fatal("Could not parse post:", err) |
|||
} |
|||
|
|||
assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.") |
|||
assert.Equal(t, row.Kind, post.Kind, "Kinds should match.") |
|||
assert.Equal(t, row.Nick, post.Nick, "Kinds should match.") |
|||
assert.Equal(t, row.Text, post.Text, "Kinds should match.") |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func TestMircPostErrors(t *testing.T) { |
|||
table := []struct { |
|||
Input string |
|||
Err error |
|||
}{ |
|||
{"[12:34] <Stuff> Things said.", nil}, |
|||
{"[12:34] >Stuff> Things said.", parsers.ErrNotPost}, |
|||
{"12:34] <Stuff> Things said.", parsers.ErrNotPost}, |
|||
{"* Stuff Things said.", parsers.ErrNotPost}, |
|||
{"", parsers.ErrNotPost}, |
|||
{"[12:34 <Stuff> Things said.", parsers.ErrNotPost}, |
|||
{"[TE:XT] <Stuff> Things said.", parsers.ErrNotPost}, |
|||
{"[10] <Stuff> Things said.", parsers.ErrNotPost}, |
|||
{"[12:34:56:789] <Stuff> Things said.", nil}, |
|||
{"[12:34:56.789] <Stuff> Things said.", parsers.ErrNotPost}, |
|||
{"[12:34] <Stuff>", parsers.ErrNotPost}, |
|||
{"[12:34] * Stuff", parsers.ErrNotPost}, |
|||
{"[12:34] =Scene=", parsers.ErrNotPost}, |
|||
{"[12:34] <=Scene=>", parsers.ErrNotPost}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
_, err := parsers.MircPost(row.Input, time.Now(), models.Post{}) |
|||
|
|||
assert.Equal(t, row.Err, err, "Error should match") |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func TestMircPostNextDay(t *testing.T) { |
|||
table := []struct { |
|||
Prev time.Time |
|||
TS string |
|||
Time time.Time |
|||
}{ |
|||
{Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")}, |
|||
{Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")}, |
|||
{Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")}, |
|||
{Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
input := fmt.Sprintf("[%s] * Stuff does things.", row.TS) |
|||
|
|||
post, err := parsers.MircPost(input, row.Prev, models.Post{Time: row.Prev}) |
|||
if err != nil { |
|||
t.Fatal("Could not parse post:", err) |
|||
} |
|||
|
|||
assert.Equal(t, row.Time, post.Time) |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func TestMircLog(t *testing.T) { |
|||
parsed, err := parsers.MircLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false) |
|||
if err != nil { |
|||
t.Fatal("ParseLog", err) |
|||
} |
|||
|
|||
assert.Equal(t, testLogPosts, parsed.Posts) |
|||
assert.Equal(t, parsed.Posts[0].Time, parsed.Log.Date, "Log's date should be the first post's.") |
|||
} |
|||
|
|||
func TestMircLogErrors(t *testing.T) { |
|||
_, err1 := parsers.MircLog("\n\n\n\n\n \n\t\r\n", time.Time{}, false) |
|||
_, err2 := parsers.MircLog("\n\n\n\n\n[14:57]* Stuff \n\t\r\n", time.Time{}, true) |
|||
|
|||
assert.Equal(t, parsers.ErrEmptyLog, err1) |
|||
assert.Equal(t, parsers.ErrNotPost, err2) |
|||
} |
|||
|
|||
func parseDate(t *testing.T, date string) time.Time { |
|||
result, err := time.Parse("2006-01-02 15:04:05", date) |
|||
if err != nil { |
|||
if t != nil { |
|||
t.Fatal("Could not parse date", date, err) |
|||
} else { |
|||
panic("Could not parse date: " + err.Error()) |
|||
} |
|||
} |
|||
|
|||
return result |
|||
} |
|||
|
|||
var testLog = strings.Join([]string{ |
|||
"[11:21] * Va`ynna_Atana returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.", |
|||
"", |
|||
"[11:21] <=Scene=> The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder. ", |
|||
"", |
|||
"[11:", |
|||
" [11:27] <Test> Stuff and things.", |
|||
}, "\r\n") |
|||
|
|||
var testLogPosts = []*models.Post{ |
|||
{ |
|||
ID: "UNASSIGNED", |
|||
LogID: "UNASSIGNED", |
|||
Time: parseDate(nil, "2018-05-11 11:21:00"), |
|||
Kind: "action", |
|||
Nick: "Va`ynna_Atana", |
|||
Position: 1, |
|||
Text: "returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.", |
|||
}, |
|||
{ |
|||
ID: "UNASSIGNED", |
|||
LogID: "UNASSIGNED", |
|||
Time: parseDate(nil, "2018-05-11 11:21:00"), |
|||
Kind: "scene", |
|||
Nick: "=Scene=", |
|||
Position: 2, |
|||
Text: "The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder.", |
|||
}, |
|||
{ |
|||
ID: "UNASSIGNED", |
|||
LogID: "UNASSIGNED", |
|||
Time: parseDate(nil, "2018-05-11 11:27:00"), |
|||
Kind: "text", |
|||
Nick: "Test", |
|||
Position: 3, |
|||
Text: "Stuff and things.", |
|||
}, |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue