mgebbe@0: # edge.coffee mgebbe@0: # mgebbe@0: # A network edge between users in OStatus or pump.io graph mgebbe@0: # mgebbe@0: # Copyright 2013, E14N (https://e14n.com/) mgebbe@0: # mgebbe@0: # Licensed under the Apache License, Version 2.0 (the "License"); mgebbe@0: # you may not use this file except in compliance with the License. mgebbe@0: # You may obtain a copy of the License at mgebbe@0: # mgebbe@0: # http://www.apache.org/licenses/LICENSE-2.0 mgebbe@0: # mgebbe@0: # Unless required by applicable law or agreed to in writing, software mgebbe@0: # distributed under the License is distributed on an "AS IS" BASIS, mgebbe@0: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. mgebbe@0: # See the License for the specific language governing permissions and mgebbe@0: # limitations under the License. mgebbe@0: mgebbe@0: _ = require("underscore") mgebbe@0: async = require("async") mgebbe@0: DatabankObject = require("databank").DatabankObject mgebbe@0: Edge = DatabankObject.subClass("edge") mgebbe@0: mgebbe@0: Edge.schema = mgebbe@0: pkey: "from_to" mgebbe@0: fields: [ mgebbe@0: "from" mgebbe@0: "to" mgebbe@0: "created" mgebbe@0: "received" mgebbe@0: ] mgebbe@0: indices: [ mgebbe@0: "from" mgebbe@0: "to" mgebbe@0: ] mgebbe@0: mgebbe@0: Edge.beforeCreate = (props, callback) -> mgebbe@0: if not props.from or not props.to mgebbe@0: callback new Error("Need 'from' and 'to' in an edge'"), null mgebbe@0: return mgebbe@0: props.from_to = Edge.key(props.from, props.to) mgebbe@0: props.received = Date.now() mgebbe@0: callback null, props mgebbe@0: return mgebbe@0: mgebbe@0: Edge::beforeSave = (callback) -> mgebbe@0: edge = this mgebbe@0: if not edge.from or not edge.to mgebbe@0: callback new Error("Need 'from' and 'to' in an edge'"), null mgebbe@0: return mgebbe@0: edge.from_to = Edge.key(edge.from, edge.to) mgebbe@0: edge.received = Date.now() unless edge.received mgebbe@0: callback null mgebbe@0: return mgebbe@0: mgebbe@0: Edge.key = (fromId, toId) -> mgebbe@0: fromId + "→" + toId mgebbe@0: mgebbe@0: module.exports = Edge