From ab657c7f75c8f797781ab2f242a6715dfb092cea Mon Sep 17 00:00:00 2001 From: Jeferson Lesbao de Siqueira Date: Wed, 9 Aug 2017 01:08:06 -0300 Subject: [PATCH] adapt classes to use \create method with opts arguments --- flows/followings.moon | 9 +++++++-- models/events.moon | 22 +++++++++++----------- models/timeline_events.moon | 30 ++++++++++++++++++++++-------- spec/models/events_spec.moon | 30 +++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/flows/followings.moon b/flows/followings.moon index 298e509..a1b43b4 100644 --- a/flows/followings.moon +++ b/flows/followings.moon @@ -12,7 +12,7 @@ class FollowingsFlow extends Flow super ... assert_error @current_user, "must be logged in" - follow_object: (object, type) => + follow_object: (object, type) => f = Followings\create { source_user_id: @current_user.id @@ -26,7 +26,12 @@ class FollowingsFlow extends Flow Notifications\notify_for target_user, object, type, @current_user - event = Events\create(@current_user, object, Events.event_types.subscription) + event = Events\create({ + user: @current_user + object: object + event_type: Events.event_types.subscription + }) + TimelineEvents\deliver(@current_user, event) f diff --git a/models/events.moon b/models/events.moon index f058634..072cef2 100644 --- a/models/events.moon +++ b/models/events.moon @@ -20,19 +20,19 @@ class Events extends Model }} } - @create: (user, object, event_type) => - assert user, "missing event's user" - assert object, "missing event's object" - assert event_type, "missing event_type, events must have a type" - - opts = { - :event_type - source_user_id: user.id - object_object_id: object.id - object_object_type: @@object_type_for_object object + @create: (opts={}) => + assert opts.user, "missing event's user" + assert opts.object, "missing event's object" + assert opts.event_type, "missing event_type, events must have a type" + + event_opts = { + event_type: opts.event_type + source_user_id: opts.user.id + object_object_id: opts.object.id + object_object_type: @@object_type_for_object opts.object } - event = safe_insert @, opts + event = safe_insert @, event_opts return event diff --git a/models/timeline_events.moon b/models/timeline_events.moon index 64d634e..ca4e183 100644 --- a/models/timeline_events.moon +++ b/models/timeline_events.moon @@ -1,32 +1,46 @@ db = require "lapis.db" import Model from require "lapis.db.model" -import Events, Followings, Users from require "models" + class TimelineEvents extends Model @primary_key: { "user_id", "event_id" } - @create: (user, event) => + @create: (opts={}) => + assert opts.user_id, "user id not specified" + assert opts.event_id, "event id not specified" + super { - user_id: user.id - event_id: event.id + user_id: opts.user_id + event_id: opts.event_id } @delete: (user, event) => db.delete @table_name!, { user_id: user.id, event_id: event.id } @deliver: (user, event) => + import Events, Followings, Users from require "models" + switch event.event_type when Events.event_types.update - followers = Followings\select "where object_id = ?", event.object_object_id + followers = Followings\select "where object_id = ? and object_type = ?", event.object_object_id, event.object_object_type for users in *followers follower_user = Users\find users.source_user_id - @@create(follower_user, event) + @@create({ + user_id: follower_user.id + event_id: event + }) else - @@create(user, event) + @@create({ + user_id: user.id + event_id: event.id + }) if Events\model_for_object_type(event.object_object_type) == Users - @@create(Users\find(event.object_object_id), event) + @@create({ + user_id: Users\find(event.object_object_id).id + event_id: event.id + }) @user_timeline: (user) => @@select "where user_id = ?", user.id diff --git a/spec/models/events_spec.moon b/spec/models/events_spec.moon index 874ff2f..3909a66 100644 --- a/spec/models/events_spec.moon +++ b/spec/models/events_spec.moon @@ -15,7 +15,12 @@ describe "models.events", -> user = factory.Users! followed_user = factory.Users! - event = Events\create(user, followed_user, Events.event_types.subscription) + event = Events\create({ + user: user + object: followed_user + event_type: Events.event_types.subscription + }) + user_timeline = user\timeline! assert.same user.id, event.source_user_id @@ -28,7 +33,12 @@ describe "models.events", -> user = factory.Users! module = factory.Modules! - event = Events\create(user, module, Events.event_types.subscription) + event = Events\create({ + user: user + object: module + event_type: Events.event_types.subscription + } + ) user_timeline = user\timeline! assert.same user.id, event.source_user_id @@ -41,7 +51,12 @@ describe "models.events", -> user = factory.Users! module = factory.Modules! - event = Events\create(user, module, Events.event_types.bookmark) + event = Events\create({ + user: user + object: module + event_type: Events.event_types.bookmark + }) + user_timeline = user\timeline! assert.same user.id, event.source_user_id @@ -54,10 +69,15 @@ describe "models.events", -> user = factory.Users! module = factory.Modules! - event = Events\create(user, module, Events.event_types.bookmark) + event = Events\create({ + user: user + object: module + event_type: Events.event_types.bookmark + }) + event_id = event.id event\delete! - assert.same nil, Events\find user.id, event_id + assert.same Events\find(event_id), nil assert.same 0, #user\timeline!