diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e847aec --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +config/database.yml +config/settings.yml +log/*.log +tmp/**/* +db/schema.rb +*\.swp + diff --git a/Capfile b/Capfile new file mode 100644 index 0000000..e04728e --- /dev/null +++ b/Capfile @@ -0,0 +1,4 @@ +load 'deploy' if respond_to?(:namespace) # cap2 differentiator +Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) } + +load 'config/deploy' # remove this line to skip loading any of the default tasks \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README b/README new file mode 100644 index 0000000..f78aff1 --- /dev/null +++ b/README @@ -0,0 +1,7 @@ +Jyte.com +======== + +"Make claims and share cred." + +Depends on: + * rmagick / graphicsmagick for user profile image diff --git a/README.debian b/README.debian new file mode 100644 index 0000000..280d220 --- /dev/null +++ b/README.debian @@ -0,0 +1,18 @@ +Here are the packages you need to run jyte on debian. + +apt-get install: + +build-essential +ruby1.8 +ruby1.8-dev +irb +rubygems +libmysql-ruby +libgraphicsmagick1-dev +librmagick-ruby +darcs + +gem install rails +gem install ruby-openid --include-dependencies +gem install mongrel +gem install json \ No newline at end of file diff --git a/README.jyte.overview b/README.jyte.overview new file mode 100644 index 0000000..0d5484e --- /dev/null +++ b/README.jyte.overview @@ -0,0 +1,73 @@ +Here's a high level overview of Jyte. + +Overview +--------- +Jyte runs on two machines: japp1, jdb1 + +japp1: +We use apache to reverse-proxy to the mongrel based rails app servers. There are roughly 8 mongrels serving the dynamic parts of the Jyte website. Static files including CSS, images, and javascript are served directly by apache. We use a forking mongrel server which is bound to a single port. To control the rails server, use the following command: + +/etc/init.d/mongrel2 start|stop|restart + +This command will start running the rails code that is in /var/jyte/jyte/current + +jdb1: +Here lives the MySql database. We also run an Apache Solr instance here which powers the full-text indexing and search capability of the site. + +Release Cutting +--------------- +At launch we used capistrano to manage release cutting, but it burned us so many times that we ditched it for a simpler approach. There is a darcs repo at /var/jyte/jyte/current. On our code server (janrain internal) we managed several code branches (jyte-development, jyte-production, etc). When code is ready to deploy on the live site, we would simply push the patches from jyte-production to japp1:/var/jyte/jyte/current and then restart the service. If migrations were involved, we would run them manually before restarting the app server. This approach does result in a few seconds of service downtime, so there is much room for improvement there. + + +Important Models +--------------- + +User: +This model represents a user of the site. Just about every other model in the system references the User model. A user has many Identifier(s). + + +Identifier: +OpenID identifier. This may or may not be tied to a user. It is used as the unique id for signing a user in to Jyte, as well as identifying the user in a claim about a person. + + +Claim: +The model that contains all the information about a claim. A claim has votes, tags, and comments. + + +FeaturedClaim: +This model has a hard coded algorithm which examines claims to see if they should be featured. Newly featured claims show up on the homepage. The algoithm is something simple like: set Featured if Claim.votes > X && Claim.comments > Y + +Vote: +Represents a vote on a claim by a user. + + +Comment: +Belongs to a Claim. + + +Cred: +An object that stores the actual gift from one user to another. Underneath it uses the taggable interface, just like a claim, to manage the cred strings. + + +CredScore: +Where the acutal cred scores are stored during the cred calculation. See the Rakefile for the code that processes cred and generates scores. It was triggered by a cron every 15 minutes before it got too big for memory. + + +Tag, Tagging, Taggable: +We the acts_as_taggable rails plugin + +Caching: +Jyte does not use memcahe or any other caching tool. Some models like Claims have vote counter caches, but that's about it. + + +Things I would do to Jyte: +---------------------- +* Get rid of apache and use nginix instead. It's faster, simpler, and doesn't crash randomly. +* Split out cred processing to another server. In it's current form it uses too much memory to run on the appserver. +* Remove OpenID as a first class thing on website, and remove the display of OpenID identifiers. OpenID didn't catch on in a visible way on the web, and the service should be re-focused on the User instead of their OpenID. Additionally, many OpenIDs are opaque, non human readable strings, which doesn't work with the original Jyte approach. +* Re-evaluate SOLR. I was never thrilled with our search capability and it is a total black box. It still works, but I have no idea what is going on inside. +* Check the database indexes. I bet there are many optimizations that could be made by tweaking these. +* Make sure rails is up to date. We only updated rails once during Jyte's lifetime and I'm sure it's running a very old and insecure version +* Update the app server OS. +* Test infrastructure. Add tests. Jyte was written *very* quickly, and with our limited resources we focused on creating a fun site instead of writing correct and bug-free code. Some tests would probably be good for Jyte. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..ca6e47a --- /dev/null +++ b/Rakefile @@ -0,0 +1,289 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require(File.join(File.dirname(__FILE__), 'config', 'boot')) + +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +require 'tasks/rails' + +task :pull => [:darcspull, :migrate] + +task :darcspull do |t| + sh "darcs pull" +end + +# XXX: these should go in jyte/lib/tasks/jyte.rake (which doesn't exist) +namespace :jyte do + + desc 'Calc user ranks' + task :calc_cred => :environment do |t| + + while true + start = Time.now + begin + Cred.calc + rescue ActiveRecord::Transactions::TransactionError + STDERR.puts("#{Time.now} - Calc aborted, TransactionError") + Kernel.sleep(3600) + else + STDERR.puts("#{Time.now} - Calc took #{Time.now - start} seconds.") + end + Kernel.sleep(3600) + end + end + + desc 'Cred dump' + task :cred_dump => :environment do |t| + + temp_filename = '/tmp/cret.txt.tmp' + final_filename = Pathname.new(RAILS_ROOT).join('/var/jyte/static/misc/cred.txt') + + f = File.open(temp_filename, 'w') + f.write("# generated #{DateTime.now.to_s}\n#\n") + f.write("# line format: from_openid\tto_openid\tcsv cred tags\n#\n") + f.write("# Contents of this file are distrubuted under the \n# Creative Commons Attribution-Noncommercial-Share Alike 2.5 License\n") + f.write("# http://creativecommons.org/licenses/by-nc-sa/2.5/\n#\n") + + Cred.find(:all).each {|c| + line = "#{c.source.openid}\t#{c.sink.openid}\t#{c.tag_list}\n" + f.write(line) + } + f.close + FileUtils.mv(temp_filename, final_filename) + `gzip -f #{final_filename}` + end + + desc 'User dump' + task :user_openid_dump => :environment do |t| + f= File.open('users.txt','w') + User.find(:all).each {|u| + f.write("#{u.openid}\n") + } + f.close + end + + desc 'Minify Jyte Javascript' + task :minifyjs => :environment do |t| + libs = ['public/javascripts/prototype.js', + 'public/javascripts/effects.js', + 'public/javascripts/dragdrop.js', + 'public/javascripts/dragdrop.js', + 'public/javascripts/controls.js', + 'public/javascripts/application.js', + 'public/javascripts/global.js', + 'public/javascripts/claimpage.js', + 'public/javascripts/prototype_extensions.js' + ] + jsmin = 'script/javascript/jsmin.rb' + final = 'public/javascripts/j.js' + + # create single js file + tmp = Tempfile.open('all') + libs.each {|lib| open(lib) {|f| tmp.write(f.read)}} + tmp.rewind + + # minify js + %x[ruby #{jsmin} < #{tmp.path} > #{final}] + puts "\n#{final}" + end + + desc 'GC bot sessions' + task :gc_bot_sessions => :environment do |t| + chunks = 10000 + last_id = 0 + while last_id != nil + sessions = Session.find(:all, + :limit => chunks, + :conditions => ['id > ?',last_id], + :order => 'id ASC') + + p "Got #{sessions.length} sessions #{last_id}" + + if sessions.length > 0 + last_id = sessions[-1].id.to_i + to_delete = [] + sessions.each {|s| + to_delete << s.id if Marshal.load(Base64.decode64(s.data))[:user_id].nil? + } + + if to_delete.length > 0 + p "Deleting #{to_delete.length} sessions..." + Session.delete_all(["id IN (#{to_delete.join(',')})"]) + else + p 'No sessions to delete in this batch' + end + + else + last_id = nil + end + Kernel.sleep(0.5) + end + end + + desc 'Run all jyte garbage collection tasks' + task :gc => [:gc_sessions, :gc_openid_store] + + desc 'Remove all sessions that have not been updated in over a week' + task :gc_sessions => :environment do |t| + CGI::Session::ActiveRecordStore::Session.delete_all(['updated_at < ?', 1.week.ago]) + end + + desc 'GC Happenings' + task :gc_happenings => :environment do |t| + h = Happening.find(:all, :order => 'id DESC', :limit => 1)[0] + Happening.destroy_all(['id < ?', h.id - 15]) + end + + desc 'Remove stale nonces' + task :gc_openid_store => :environment do + ActiveRecordOpenIDStore.new.gc + end + + desc 'recalc yeas/nays' + task :recalc_yeas_nays => :environment do |t| + + Claim.find(:all, :order => 'id DESC').each {|c| + ActiveRecord::Base.transaction { + c.yeas = c.yea_votes.length + c.nays = c.nay_votes.length + c.save + } + + } + end + + desc 'delete bad votes' + task :delete_bad_votes => :environment do + ActiveRecord::Base.transaction { + badvotes=[] + Claim.find(:all).each{|c| + c.voters.uniq.each{|u| + badvotes += ClaimVote.find_by_sql(['select * from claim_votes where user_id = ? AND claim_id = ? order by created_at desc limit 1, 1000', u.id, c.id]) + } + } + puts "#{badvotes.size} bad votes. Deleting..." + badvotes.each {|v| v.destroy} + } + end + + # a temp task for cleaning up bad data from the mentioned identifiers + desc 'clean up mentioned identifeirs' + task :clean_mi => :environment do + for i in 0..Claim.count + seen = [] + mis = MentionedIdentifier.find_all_by_claim_id(i) + next if mis.nil? + mis.each {|mi| + x = [mi.identifier_id, mi.order] + if seen.member?(x) + mi.destroy + else + seen << x + end + } + end + end + + task :tag_counts => [:init_tag_counts_table, :load_tag_counts] + + desc 'init tag_counts table' + task :init_tag_counts_table => :environment do + conn = ActiveRecord::Base.connection + ActiveRecord::Base.transaction { + begin + conn.drop_table :tag_counts + rescue + nil + end + conn.create_table(:tag_counts, :id => false, :options => 'ENGINE=MyISAM') do |t| + t.column :tag_id, :integer + t.column :mentioned_tag_id, :integer + t.column :taggable_type, :string + t.column :count, :integer + end + conn.add_index :tag_counts, [:tag_id,:taggable_type] + } + end + + desc 'load tag counts table from taggings' + task :load_tag_counts => :environment do + + ignore_tags = ['team tomato', 'bubble tea'] + ignore_ids = Tag.find_all_by_name(ignore_ids).map {|t| t.id} + + ['User','Claim','Cred'].each {|klass| + + all_taggings = ActiveRecord::Base.connection.select_all("SELECT tag_id,taggable_id,taggable_type FROM taggings WHERE taggable_type = '#{klass}'") + + # tags_for_taggable = {[taggable_id,taggable_type] => [tag_id,]} + tags_for_taggable = {} + all_taggings.each {|h| + key = [h['taggable_id'].to_i, h['taggable_type']] + tag_id = h['tag_id'].to_i + next if ignore_ids.member?(tag_id) + + if tags_for_taggable.has_key?(key) + tags_for_taggable[key] |= [tag_id] + else + tags_for_taggable[key] = [tag_id] + end + } + # gc hack + all_taggings = nil + GC.start + + # tag_counts = {[tag_id_1,tag_id_2,taggable_type] => count} + tag_counts = {} + tags_for_taggable.each {|k,tag_ids| + + taggable_id, taggable_type = k + + # permutate and update counts + tag_ids.each {|tag_id| + tag_ids_copy = tag_ids.dup + tag_ids_copy.delete(tag_id) + tag_ids_copy.each {|tag_id_2| + key = [tag_id,tag_id_2] + key.sort! + key << taggable_type + if tag_counts.has_key?(key) + tag_counts[key] += 1 + else + tag_counts[key] = 1 + end + } + } + } + # gc hack + tags_for_taggable = nil + GC.start + + # generate all the table entries for efficient searching + le_table = [] + tag_counts.each {|k,count| + tag_id_1, tag_id_2, taggable_type = k + count = count / 2 + le_table << [tag_id_1, tag_id_2, taggable_type, count] + le_table << [tag_id_2, tag_id_1, taggable_type, count] + } + tag_counts = nil + GC.start + + # update database + chunk = 5000 + (0..le_table.size).step(chunk) { |offset| + + rows = le_table[offset..offset+chunk] + break if rows.size == 0 + + sql = "INSERT INTO tag_counts (tag_id,mentioned_tag_id,taggable_type,count) VALUES " + sql += rows.collect {|a,b,c,d| "(#{a},#{b},'#{c}',#{d})"}.join(', ') + ActiveRecord::Base.connection.execute(sql) + } + } + end + +end diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb new file mode 100644 index 0000000..7f4aeca --- /dev/null +++ b/app/controllers/admin_controller.rb @@ -0,0 +1,89 @@ +class AdminController < ApplicationController + + before_filter :check_admin_status + secure_actions :except => [:index] + + def index + @unique_users = User.count + @verified_identifers = Identifier.count(:conditions => ['user_id IS NOT NULL']) + @todays_users = User.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 0 DAY) <= created_at']) + @yesterdays_users = User.count(:conditions => ['created_at >= CURRENT_DATE() - INTERVAL 1 DAY AND created_at <= CURRENT_DATE()']) + @seven_day_users = User.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 7 DAY) <= created_at']) + @thirty_day_users = User.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 29 DAY) <= created_at']) + @total_groups = Group.count + + @num_users_ingroups = GroupMembership.count_by_sql(["select count(distinct user_id) from group_memberships"]) + @num_groups_joined = GroupMembership.count_by_sql(["select count(gm.id) from groups g, group_memberships gm where g.user_id = gm.user_id"]) + + @total_claims = Claim.count + @total_group_claims = Claim.count :conditions => ['state = 4'] + @total_votes = ClaimVote.count + @total_comments = Comment.count + @todays_claims = Claim.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 0 DAY) <= created_at']) + @todays_votes = ClaimVote.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 0 DAY) <= created_at']) + @todays_comments = Comment.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 0 DAY) <= created_at']) + + @total_creds = Cred.count + @todays_creds = Cred.count(:conditions => ['DATE_SUB(CURRENT_DATE(),INTERVAL 0 DAY) <= created_at']) + + end + + def user + @user = user = User.find_by_openid(params[:openid]) + unless @user + @user = user = User.find_by_id(params[:user_id]) + end + + unless @user + flash[:notice] = 'Unknown user' + redirect_to :action => 'index' + return + end + + @total_claims = Claim.count(:conditions => ['user_id = ?',user.id]) + @total_comments = Comment.count(:conditions => ['user_id = ?',user.id]) + end + + def suspend_submit + user = User.find(params[:user_id]) + user.set_state(:suspended) + user.save! + + # red flag all their claims + if params[:red_flag_claims].to_s == '1' + user.claims.each {|c| + c.state = 3 # red flag + c.save! + } + + # delete all their comments + Comment.destroy_all(['user_id = ?', user.id]) + + # delete all their votes + ClaimVote.destroy_all(['user_id = ?', user.id]) + + # delete all their cred to and from + Cred.destroy_all(['source_id = ? OR sink_id = ?', user.id, user.id]) + end + + flash[:notice] = 'User suspended.' + redirect_to :action => 'index' + end + + def log_in_as + session[:user_id] = params[:user_id].to_i + redirect_to :controller => 'home' + end + + private + + def check_admin_status + if is_admin? + return true + end + + redirect_to front_url + return false + end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000..7a8a2e2 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,192 @@ +# Filters added to this controller will be run for all controllers in the application. +# Likewise, all the methods added will be available for all controllers. +class ApplicationController < ActionController::Base + include ExceptionNotifiable + local_addresses.clear + + include ApplicationHelper + layout 'jyte' + + before_filter :check_user_state + + #session :session_key => '_session_id' + #session :off, :if => proc {|req| + # (req.cookies['_session_id'].empty?) + #} + + private + + + #===| FILTERS |===# + def check_user_state + if logged_in? and (liu.suspended? or liu.deleted?) + session[:user_id] = nil + cookies.delete(:openid) + redirect_to front_url + return false + end + return true + end + + + def check_logged_in + if logged_in? + + # updating :last is a round about way of getting the + # session to re-save itself every week. Ugh. + last = session[:last] + if last.nil? or last < 1.week.ago + session[:last] = Time.now + end + + return true + end + + if request.query_string.nil? or request.query_string.empty? + dest = request.path + else + dest = request.path + '?' + request.query_string + end + + if cookies[:openid] + redirect_to :controller => 'auth', :action => 'login', :dest => dest + else + redirect_to :controller => 'auth', :action => 'signup', :dest => dest + end + + return false + end + + def auto_login # try openid immediate mode + if not logged_in? and cookies[:openid] and not session[:tried_immediate] + dest = request.path + '?' + request.query_string + redirect_to :controller => 'auth', :action => 'openid_start', :openid_identifier => cookies[:openid], :immediate => true, :dest => dest + return false + else + return true + end + end + + def update_user_last_seen + unless logged_in_user_id.nil? + u = liu + u.last_seen_at = DateTime.now + u.save + end + end + + # Does the argument look like an email address? + def is_email_address?(email_address) + return false unless email_address + if email_address.match /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i + return true + end + return false + end + + ##### Invite stuff ###### + # Send an invite to an email address. you must pass in an Invitation object + # to this method, it will be saved within or destroyed if something bad + # happens. + # + # Use dispatches to invite someone to a group. + def send_invite(inv, email) + unless is_email_address?(email) + return false, 'Could not send invite. Try using their email address.' + end + + # find or create a new email response code + if erc = EmailResponseCode.find_by_email(email) + inv.response = erc + inv.save + if inv.valid? + return true, 'User invited' + else + raise ArguemntError, 'Bad Invitation' + end + else + # haven't sent an email yet. + erc = EmailResponseCode.create(:email => email) + inv.response = erc + inv.save + + if inv.valid? + resp_url = url_for(:controller => 'auth', + :action => 'invite_response', + :code => erc.code, + :only_path => false) + DearStrongbad.deliver_invite(inv, resp_url) + return true, 'Invite sent to ' + ERB::Util.h(email) + else + erc.destroy + return false, 'Invite failed' + end + end + + raise ArgumentError, 'should not get here' + end + + # limit is 3 megs + def read_blob(file, limit=3145728) + return nil if file.nil? + return nil unless file.respond_to?('read') + + blob = file.read(limit) + + if file.read(1) + return nil + end + + return blob + end + + def session_id_salt + 'dsnvndfsver26237nfew3h21b4b2b11b' + end + + def bad_sig_handler + end + + # Batch load all the stuff necessary to render a set of claims by + # the claim_id. the ApplicationHelper::render_claim_title uses the instance + # variables created here. + def batch_load_claim_data(claim_ids) + return if claim_ids.empty? + claim_ids = claim_ids.uniq + @all_mentioned_identifiers = MentionedIdentifier.find_by_sql("SELECT * FROM mentioned_identifiers WHERE claim_id IN (#{claim_ids.join(',')})") + @all_identifier_ids = @all_mentioned_identifiers.collect {|mi|mi.identifier_id}.uniq + + if @all_identifier_ids.length > 0 + @all_identifiers = Identifier.find_by_sql("SELECT * FROM identifiers WHERE id IN (#{@all_identifier_ids.join(',')})") + else + @all_identifiers = [] + end + + @all_user_ids = @all_identifiers.collect {|i| i.user_id}.uniq + @all_user_ids.reject! {|uid| uid == nil} + + if @all_user_ids.length > 0 + @all_users = User.find_by_sql("SELECT * FROM users WHERE id IN (#{@all_user_ids.uniq.join(',')})") + else + @all_users = [] + end + + @identifiers_by_identifier_id = @all_identifiers.inject({}) { + |p,e| p[e.id] = e;p} + + @users_by_user_id = @all_users.inject({}) {|p,u| p[u.id] = u;p} + + @identifiers_by_claim_id = {} + claim_ids.each {|claim_id| + mids = @all_mentioned_identifiers.find_all {|mi| mi.claim_id == claim_id} + mids.sort! {|mi1,mi2| mi1.order <=> mi2.order} + @identifiers_by_claim_id[claim_id] = mids.collect {|mi| @identifiers_by_identifier_id[mi.identifier_id]} + } + + # get image stuff + @claim_has_image = [] + imagings = Imaging.find_by_sql("SELECT imagable_id FROM imagings WHERE imagable_type = 'Claim' AND imagable_id IN (#{claim_ids.join(',')})") + imagings.each {|i| @claim_has_image << i.imagable_id } + end + +end diff --git a/app/controllers/auth_controller.rb b/app/controllers/auth_controller.rb new file mode 100644 index 0000000..b099846 --- /dev/null +++ b/app/controllers/auth_controller.rb @@ -0,0 +1,586 @@ +#XXX: move this into environement.rb +require "openid" +require "openid_ar_store" +require 'openid/extensions/sreg' + +class AuthController < ApplicationController + + #session :disabled => false, :only => [:openid_start, :invite_response, :rpx_response] + secure_actions :only => [:logout] + + def login + @openid = cookies[:openid] + if params[:dest] + @flash_notice = 'You must sign in to do that.' + end + end + + def login2 + @openid = cookies[:openid] + if params[:dest] + @flash_notice = 'You must sign in to do that.' + end + end + + def logout + # they've logged out, don't try to log them back in w/ immediate mode. + session[:tried_immediate] = true + + session[:user_id] = nil + redirect_to :controller => 'site' + end + + def openid_start + openid = params[:openid_identifier] + immediate = params[:immediate] + + if openid.nil? or openid.strip.empty? + flash[:notice] = 'Please enter your OpenID.' + redirect_to :action => 'login' + return + end + + o_req = nil + begin + o_req = consumer.begin(openid) + rescue OpenID::OpenIDError => e + RAILS_DEFAULT_LOGGER.info("OpenID '#{openid}' Error: #{e}") + flash[:notice] = "OpenID Error: #{e}" + cookies.delete :openid + redirect_to :action => 'login' + return + end + + server_url = o_req.endpoint.server_url + if server_blacklisted?(server_url) + flash[:notice] = 'Use a different OpenID server.' + redirect_to :action => 'login' + return + end + + dest = params[:dest] + trust_root = url_for(:controller => 'site', :only_path => false) + if immediate + return_to = url_for :action => 'immediate_response', :dest => dest, :only_path => false + else + return_to = url_for :action => 'openid_response', :dest => dest, :only_path => false + end + + # New User Case + identifier = o_req.endpoint.claimed_id + unless User.find_by_openid(identifier) + if immediate # no immediate mode for new users + session[:tried_immediate] = true + if params[:dest] + redirect_to params[:dest] + else + redirect_to front_url + end + return + end + + unless server_whitelist_okay(server_url) or botbouncer_okay(identifier) + redirect_to botbouncer_captcha_url(:return_to => url_for(:only_path => false, :openid_identifier => identifier), :openid => identifier) + return + end + # XXX add policy URL + sregreq = OpenID::SReg::Request.new + sregreq.request_fields(['email', 'nickname', 'dob'], true) + o_req.add_extension(sregreq) + end + + redirect_to o_req.redirect_url(trust_root, return_to, immediate) + return + end + + def immediate_response + return_to = url_for(:action => 'immediate_response', :only_path => false) + parameters = params.reject{|k,v|request.path_parameters[k]} + o_resp = consumer.complete(parameters, return_to) + + case o_resp.status + when OpenID::Consumer::SUCCESS + openid = o_resp.display_identifier + + if liuid + log_info("Logged-in immediate mode: User #{liuid}, OpenID #{openid}") + redirect_to :controller => 'home' + return + end + + user = User.find_by_openid(openid) + unless user + log_info("Non-registered immediate mode: OpenID #{openid}") + render :text => "Error: #{openid} is not registered", :status => 500 + return + end + + user.last_login_at = DateTime.now + user.last_login_ip = request.remote_ip + user.save + + session[:user_id] = user.id + if params[:dest] + redirect_to params[:dest] + else + redirect_to :controller => 'home' + end + return + + when OpenID::Consumer::SETUP_NEEDED, OpenID::Consumer::FAILURE, OpenID::Consumer::CANCEL + session[:tried_immediate] = true + if params[:dest] + redirect_to params[:dest] + else + redirect_to :action => 'login' + end + return + + else + + render :text => "OpenID server returned something strange", :status => 500 + return + end + + raise StandardError, 'should never get here' + end + + def openid_response + return_to = url_for(:action => 'openid_response', :only_path => false) + parameters = params.reject{|k,v|request.path_parameters[k]} + o_resp = consumer.complete(parameters, return_to) + + case o_resp.status + when OpenID::Consumer::SUCCESS + openid = o_resp.display_identifier + + # Try immediate mode next time + session[:tried_immediate] = nil + cookies[:openid] = {:value => openid, :expires => Time.now + 500000000} + + # find this identifier, or create it if it doesn't exist + identifier = Identifier.find_or_create_by_value(openid) + + # if we're already logged in, then we're just claiming + # another openid. do it and split. + if logged_in_user + if identifier.user_id and User.find_by_id(identifier.user_id) + if identifier.user_id != liuid + flash[:notice] = 'Sorry, that OpenID is already attached to an account.' + end + else + identifier.user_id = liuid + identifier.save + end + redirect_to :controller => 'user', :action => 'account' + return + end + + # do the inumber dance + if is_iname?(openid) and \ + o_resp.endpoint.canonical_id and \ + (user = User.find_by_i_number(o_resp.endpoint.canonical_id)) + identifier.user = user + identifier.save + session[:user_id] = user.id + redirect_to :controller => 'claims', :action => 'find' + return + end + + user = identifier.user + + # is the new identifier delegated to an existing identifier? + # if so attach it to the current account and then log in. + unless user + s_id = Identifier.find_or_create_by_value(o_resp.endpoint.local_id) + if s_id and s_id != identifier + user = s_id.user + if user + identifier.user = user + identifier.save! + flash[:notice] = "You have successfully added a new OpenID to your account." + end + end + end + + # create a new user + unless user + # -= New User =- + sreg = OpenID::SReg::Response.from_success_response(o_resp) + user = User.new + # XXX FIXME: i_number should only be set if it's an iname + user.i_number = o_resp.endpoint.canonical_id + user.set_state(:early_adopter) + + identifier.primary = true + user.last_login_at = DateTime.now + user.created_ip = request.remote_ip + user.last_login_ip = request.remote_ip + user.nickname = sreg ? sreg['fullname'] : nil + if !user.valid? + user.nickname = nil + end + user.settings = {} + user.save! + identifier.user_id = user.id + identifier.save! + + if session[:invite_code] # we got here via email invite + e = EmailResponseCode.find_by_code(session[:invite_code]) + user.email = e.email + user.save + + inv = Invitation.find_by_response_id(e.id, :order => 'created_at') + if inv.claim_id + session[:dest] = claim_url :urlslug => inv.claim.urlslug + else + session[:dest] = url_for :controller => 'home' + end + + Invitation.find_all_by_response_id(e.id).each {|inv| + if inv.group + inv.recipient = user + inv.response = nil + inv.save + else + user.dispatch(inv.claim, :reason => 'invite', :from => inv.sender) + inv.destroy + end + } + + session[:invite_code] = nil + e.destroy + + else + # Confirm the email address + if sreg and email = sreg['email'] + user.email = email + user.save + #response_code = EmailResponseCode.create(:email => email).code + #response_url = url_for :controller => 'user', :action => 'confirm_email', :code => response_code + #begin + # DearStrongbad.deliver_confirm(user, email, response_url) + #rescue + # # XXX: handle case where confirm email fails!!! + # raise + #end + end + + end + user.claims_about(:limit => 10).each {|c| user.dispatch(c)} + if params[:dest] + redirect_to params[:dest] + else + flash[:notice] = 'Welcome to Jyte!' + redirect_to :controller => 'claim', :action => 'find', :order => 'featured' + end + else + # -= Returning User =- + user.last_login_at = DateTime.now + user.last_login_ip = request.remote_ip + user.save + if params[:dest] + redirect_to params[:dest] + else + redirect_to :controller => 'claims', :action => 'find' + end + end + + session[:user_id] = user.id + return + + when OpenID::Consumer::CANCEL + cookies.delete :openid + flash[:notice] = 'Login cancelled.' + + when OpenID::Consumer::FAILURE + if o_resp.identity_url + flash[:notice] = "Verification of #{o_resp.identity_url} failed: #{o_resp.message}" + else + flash[:notice] = "Verification failed: #{o_resp.message}" + end + else + flash[:notice] = 'Unknown response from OpenID server.' + + end + + redirect_to :action => 'login' + end + + def rpx_response + u = URI.parse('https://rpxnow.com/api/v2/auth_info') + req = Net::HTTP::Post.new(u.path) + req.set_form_data({'token' => params[:token], + 'apiKey' => SETTINGS["rpxnow"]["apikey"], + 'format' => 'json'}) + + + json = http_request(u, req) + + logger.error(json) + + openid = json['profile']['identifier'] + nickname = json['profile']['preferredUsername'] + email = json['profile']['email'] + handle_rpx_response(openid,nickname,email) + end + + def http_request(uri, req) # for mocking + http = Net::HTTP.new(uri.host,uri.port) + http.use_ssl = true + res = http.request(req) + JSON.parse(res.body) + end + + def handle_rpx_response(openid, nickname, email) + session[:tried_immediate] = nil + cookies[:openid] = {:value => openid, :expires => Time.now + 500000000} + + + if is_iname?(openid) + + identifier = Identifier.find_by_value(nickname) + if identifier and identifier.user + user = identifier.user + else + user = User.find_by_i_name(nickname) + end + + unless user + identifier = Identifier.find_or_create_by_value(openid) + if identifier and identifier.user + user = identifier.user + else + user = User.find_by_i_name(openid) + end + end + + else + + # find this identifier, or create it if it doesn't exist + identifier = Identifier.find_or_create_by_value(openid) + user = identifier.user + + end + + # is the new identifier delegated to an existing identifier? + # if so attach it to the current account and then log in. + unless user + s_id = Identifier.find_or_create_by_value(openid) + if s_id and s_id != identifier + user = s_id.user + if user + identifier.user = user + identifier.save! + flash[:notice] = "You have successfully added a new OpenID to your account." + end + end + end + + # create a new user + unless user + # -= New User =- + user = User.new + if is_iname?(openid) + user.i_number = openid + end + + user.set_state(:early_adopter) + + identifier.primary = true + user.last_login_at = DateTime.now + user.created_ip = request.remote_ip + user.last_login_ip = request.remote_ip + user.nickname = nickname + if !user.valid? + user.nickname = nil + end + user.settings = {} + user.save! + identifier.user_id = user.id + identifier.save! + + if session[:invite_code] # we got here via email invite + e = EmailResponseCode.find_by_code(session[:invite_code]) + user.email = e.email + user.save + + inv = Invitation.find_by_response_id(e.id, :order => 'created_at') + if inv.claim_id + session[:dest] = claim_url :urlslug => inv.claim.urlslug + else + session[:dest] = url_for :controller => 'home' + end + + Invitation.find_all_by_response_id(e.id).each {|inv| + if inv.group + inv.recipient = user + inv.response = nil + inv.save + else + user.dispatch(inv.claim, :reason => 'invite', :from => inv.sender) + inv.destroy + end + } + + session[:invite_code] = nil + e.destroy + + else + # Confirm the email address + if email + user.email = email + user.save + #response_code = EmailResponseCode.create(:email => email).code + #response_url = url_for :controller => 'user', :action => 'confirm_email', :code => response_code + #begin + # DearStrongbad.deliver_confirm(user, email, response_url) + #rescue + # # XXX: handle case where confirm email fails!!! + # raise + #end + end + + end + user.claims_about(:limit => 10).each {|c| user.dispatch(c)} + if params[:dest] + redirect_to params[:dest] + else + flash[:notice] = 'Welcome to Jyte!' + redirect_to :controller => 'claim', :action => 'find', :order => 'featured' + end + else + # -= Returning User =- + user.last_login_at = DateTime.now + user.last_login_ip = request.remote_ip + user.save + if params[:dest] + redirect_to params[:dest] + else + redirect_to :controller => 'claims', :action => 'find' + end + end + + session[:user_id] = user.id + return + + end + + def xrds + headers['content-type'] = 'application/xrds+xml' + xrds = " + + + + + http://specs.openid.net/auth/2.0/return_to + #{url_for :only_path => false, :controller => 'auth', :action => 'openid_response'} + #{url_for :only_path => false, :controller => 'auth', :action => 'immediate_response'} + + + +" + render :text => xrds + end + + def beta + if params[:beta_code] == 'jytebyjanrain' + session[:beta] = true + if params[:dest] + redirect_to params[:dest] + else + redirect_to :controller => '' + end + return + else + render :action => 'beta', :layout => false + end + end + + def invite_response + if logged_in? + u = liu + rc = EmailResponseCode.find_by_code(params[:code]) + unless rc.nil? + u.email = rc.email + u.save + end + redirect_to :controller => 'home' + else + # grab the response code and throw it into the session + session[:invite_code] = params[:code] + render :template => 'auth/signup' + end + end + + + def crash + raise 'TheCrash' + end + + private + + def log_info(something) + RAILS_DEFAULT_LOGGER.info(something) + end + + def consumer + store = ActiveRecordStore.new + # fetcher = OpenID::StandardFetcher.new + # fetcher.ca_path = Pathname.new(RAILS_ROOT).join('cacert.pem').to_s + return OpenID::Consumer.new(session, store) + end + + def server_whitelist_okay(server_url) + whitelist = ["http://www.myopenid.com/server", + "https://www.myopenid.com/server", + "http://1id.com/sso/", + ] + yahoo_re = /^https?:\/\/[\w\.]*(yahoo|yahooapis).[a-z\.]{2,6}\/openid\/op\/auth$/ + return whitelist.member?(server_url) || server_url.match(yahoo_re) + end + + def server_blacklisted?(server_url) + ['http://www.jkg.in/openid'].each {|blacklisted| + return true if server_url.starts_with?(blacklisted) + } + return false + end + + def botbouncer_okay(openid) + fetcher = OpenID::StandardFetcher.new + botbouncer = "http://botbouncer.com/api/info" + url = OpenID::Util.append_args(botbouncer, 'api_key' => SETTINGS["botbouncer"]["apikey"], 'openid' => openid) + r = nil + begin + r = fetcher.fetch(url) + rescue Timeout::Error + RAILS_DEFAULT_LOGGER.info("Botbouncer timed out. Letting #{openid} through. URL: #{url}") + end + if r + furl = r.final_url + body = r.body + if body.match "verified:true" + return true + elsif + body.match "verified:false" + return false + else + raise "unexpected response from botbouncer ( #{url} )" + end + else + RAILS_DEFAULT_LOGGER.info("Fetch to botbouncer failed. Letting #{openid} through. ( #{url} )") + return true + # XXX d'oh + end + end + + def botbouncer_captcha_url(options) + botbouncer = "http://botbouncer.com/captcha/queryuser" + return OpenID::Util.append_args(botbouncer, 'return_to' => options[:return_to], 'openid' => options[:openid]) + end + +end diff --git a/app/controllers/claim_controller.rb b/app/controllers/claim_controller.rb new file mode 100644 index 0000000..7af1c67 --- /dev/null +++ b/app/controllers/claim_controller.rb @@ -0,0 +1,1321 @@ +class Array + + def hash_by(attr) + h = {} + self.each {|i| h[i.send(attr)] = i} + return h + end + +end + + +Kernel.srand + +class ClaimController < ApplicationController + extend ActionView::Helpers::SanitizeHelper::ClassMethods + include ActionView::Helpers::SanitizeHelper + include ClaimHelper + + before_filter :auto_login + before_filter :check_logged_in, :except => [:show, :find, :votes, :random] + + secure_actions :only => [:new_submit,:preview_submit,:vote,:discard_claim, + :delete_image,:comment,:publish,:flag,:mark + ] + # XXX:These didn't work on the live site as secure actions for some unknown reason: + # :save_search,:remove_saved_search] + + def new + @title = "Make a Claim" + + # make sure the person trying to make the claim is a group member + if params[:group_id] + group = Group.find_by_id(params[:group_id]) + if group and group.member?(liu) + @group = group + end + end + + @claimable_type = params[:claimable_type] + @claimable_id = params[:claimable_id] + if @claimable_type == "Comment" + @claimable = Comment.find_by_id(@claimable_id) + elsif @claimable_type == "Claim" + @claimable = Claim.find_by_id(@claimable_id) + end + end + + def new_submit + claim_text = params[:new_claim_text] + ct = params[:claimable_type] + cid = params[:claimable_id] + + if claim_text.nil? or claim_text.empty? + flash[:notice] = 'You cannot make an empty claim' + redirect_to :action => 'new' + return + end + + # XXX comma separation? + claim_tags = (params[:new_claim_tags] or '') + + c = Claim.create(:user_id => liuid, + :original => claim_text) + unless c.valid? + if same = c.errors.on(:same) + flash[:notice] = "Someone already made that claim." + redirect_to :controller => 'claim', :action => 'show', :urlslug => same + return + end + if errors = c.errors[:text] + if errors.class == Array + errors = errors.join(" ") + end + flash[:notice] = errors + + if ct + redirect_to :action => 'new', :text => claim_text, :tags => claim_tags, :claimable_type => ct, :claimable_id => cid + else + redirect_to :action => 'new', :text => claim_text, :tags => claim_tags, :group_id => params[:group_id] + end + return + end + end + + # make sure the person trying to make the claim is a group member + if params[:group_id] + group = Group.find_by_id(params[:group_id]) + if group and group.member?(liu) + c.group_id = group.id + c.save + end + end + + c.tag_with claim_tags + + if ct == 'Comment' + comment = Comment.find(cid) + c.inspired_by(comment) + elsif ct == 'Claim' + c2 = Claim.find(cid) + c.inspired_by(c2) + end + + c = Claim.find_by_id(c.id) + + redirect_to :action => 'preview', :urlslug => c.urlslug + end + + def preview + @claim = Claim.find_by_urlslug(params[:urlslug]) + if @claim.nil? + redirect_to :controller => 'home', :action => 'drafts' + flash[:notice] = "Couldn't find that draft claim." + return + elsif @claim.state > 0 + redirect_to :action => 'show', :urlslug => @claim.urlslug + return + end + @title = @claim.title + + scrubbed = @claim.original.downcase.gsub(/[(<\[\]>)=!^~?:*+.,\\]/,'').strip + scrubbed = scrubbed.gsub(/-/, ' ').gsub(' +', ' ') + similar_query = scrubbed.split.uniq.reject{|w|w.size < 3}.join(' OR ') + + begin + @similar = Claim.find_by_solr(similar_query, :start => 0, :rows => 10) + @similar.reject! {|c| c.digest == @claim.digest or c.state != 1} + rescue + @similar = [] + end + end + + def preview_submit + @claim = Claim.find_by_id(params[:id]) + slug = @claim.urlslug + + if liuid == @claim.user_id and @claim.state == 0 + @claim.original = params[:claim_text] + @claim.body = params[:claim_body] + + # apply group + if params[:claim] + @claim.group_id = params[:claim][:group_id] + end + + if @claim.save + slug = @claim.urlslug + end + @claim.tag_with params[:claim_tags] + + # apply image + if image_blob = read_blob(params[:claim_image]) + if old_image = @claim.image + @claim.imagings.destroy_all + old_image.destroy_image(@claim) + end + + if im = Image.from_blob(image_blob, @claim) + im.on(@claim) + end + end + + errors = [] + if text_errors = @claim.errors[:text] + if text_errors.class == Array + errors = text_errors + else + errors = [text_errors] + end + end + if @claim.errors[:same] + errors << "Someone already made that claim." + end + flash[:notice] = errors.join(' ') unless errors.empty? + + if errors.empty? and params[:commit] == "Publish" or params[:commit] == "Publish Changes" + redirect_to :action => 'publish', :id => @claim.id + else + redirect_to :action => 'preview', :urlslug => slug, :group_id => params[:group_id] + end + return + else + if not logged_in? + flash[:notice] = "You must sign in." + elsif liuid != @claim.user_id + flash[:notice] = "That is not your claim." + elsif @claim.state > 0 + redirect_to claim_url(:urlslug => @claim.urlslug) + return + end + redirect_to front_url + end + end + + def change_tags + claim = Claim.find(params[:claim_id]) + if claim.user_id == liuid + claim.tag_with(params[:tags]) + render :partial => 'tags', :locals => {:claim => claim} + else + render :text => "You cannot do that." + end + end + + def discard_claim + claim = Claim.find_by_id(params[:id]) + if claim and liuid == claim.user_id and claim.state == 0 + claim.image.destroy_image(claim) if claim.image + claim.destroy + flash[:notice] = 'Draft claim deleted' + end + redirect_to front_url + end + + def delete_image + @claim = Claim.find(params[:id]) + if liuid == @claim.user_id and @claim.state == 0 + if old_image = @claim.image + @claim.imagings.destroy_all + old_image.destroy_image(@claim) + end + end + redirect_to :action => 'preview', :urlslug => @claim.urlslug + end + + def publish + @claim = Claim.find_by_id(params[:id]) + if liuid == @claim.user_id + if @claim.state == 0 + @claim.publish + + elsif params[:retract] and (@claim.state == 1 or @claim.state == 4) + if @claim.yeas + @claim.nays + @claim.comments_count == 1 + @claim.state = 0 + @claim.save + Dispatch.find_all_by_dispatchable_type_and_dispatchable_id("Claim", @claim.id).each{|d|d.destroy} + @claim.solr_destroy + flash[:notice] = "Claim retracted." + redirect_to :action => 'preview', :urlslug => @claim.urlslug + return + else + flash[:notice] = "Sorry, too late." + redirect_to :action => 'show', :urlslug => @claim.urlslug + return + end + end + else + if not logged_in? + flash[:notice] = "You must sign in." + elsif liuid != @claim.user_id + flash[:notice] = "That is not your claim." + end + end + if @claim.state > 0 + redirect_to claim_url(:urlslug => @claim.urlslug) + else + redirect_to front_url + end + end + + def show + if params[:id] + @claim = Claim.find_by_id(params[:id], :conditions => 'state > 0') + elsif params[:urlslug] + @claim = Claim.find_by_urlslug(params[:urlslug], :conditions => 'state > 0') + end + unless @claim + flash[:notice] = "Didn't find a claim" + # XXX or 404? + redirect_to front_url + return + end + + ims = headers['if-modified-since'] + modtime = @claim.commented_at + modtime = @claim.created_at if modtime.nil? + if ims and DateTime.parse(ims) > modtime + render :text => '', :status => 304 + return + end + + if @claim.state == 2 + unless logged_in? + # XXX maybe issue a 403 if the user-agent doesn't look like a browser + flash[:notice] = "That claim is yellow flagged. You must sign in to see it." + redirect_to front_url + return + else + flash[:notice] = "This claim is yellow flagged. Only signed in users may see it." + end + end + + if @claim.state == 3 + if @claim.user_id == liuid + flash[:notice] = "This claim is red flagged. Only you can see it." + else + # XXX maybe issue a 403 if the user-agent doesn't look like a browser + flash[:notice] = "That claim is red flagged and may not be viewed." + redirect_to front_url + return + end + end + unless params[:urlslug] + redirect_to :urlslug => @claim.urlslug + return + end + + if @claim.state == 4 + unless @claim.group.member?(liu) + flash[:notice] = "You must be a member of #{@claim.group.name} to see that claim" + redirect_to front_url + return + end + + end + + @title = @claim.title + " - Cast your vote" + + @claimant_vote = @claim.user.vote_on(@claim) + if @claimant_vote + @claimant_disagrees = @claimant_vote.vote == false + end + + if @claimant_disagrees + dis = " but disagreed" + else + dis = "" + end + @meta_tags = {"description" => "#{@claim.user.dn} claimed#{dis}, #{@claim.title} #{@claim.yeas} agree and #{@claim.nays} disagree. #{@claim.comments_count} comments.", + "keywords" => @claim.tag_list + } + + @comments = Comment.find_by_sql("SELECT * FROM comments WHERE claim_id = #{@claim.id}") + @user_icons = Image.for_users(@comments.map{|c|c.user_id}.uniq) unless @comments.empty? + + if logged_in? + # Clear user's dispatches to this claim + Dispatch.find_all_by_dispatchable_type_and_dispatchable_id_and_user_id('Claim', @claim.id, logged_in_user_id).each {|d| d.destroy } + + # clear the dispatches to the comments as well + Dispatch.find_by_sql(["SELECT dispatches.* FROM dispatches JOIN comments ON comments.claim_id = ? AND dispatches.dispatchable_type = 'Comment' AND dispatches.dispatchable_id = comments.id AND dispatches.user_id = ?", @claim.id, logged_in_user_id]).each { |d| + d.destroy + } + + # the validation sometimes fails. argh. + looks = Look.find_all_by_user_id_and_object_type_and_object_id(liuid, 'Claim', @claim.id, :order => 'created_at') + if looks.empty? + Look.create(:user_id => liuid, :object => @claim) + else + looks[0].touch + if looks.size > 1 + looks[1..-1].each{|l| + RAILS_DEFAULT_LOGGER.info("deleting duplicate look on claim #{@claim.title} for user #{liu.display_name}") + l.destroy + } + end + end + + @blocked_user_ids = liu.blocked_user_ids + end + + tag_ids = Claim.connection.select_values("SELECT tag_id FROM taggings WHERE taggable_type = 'Claim' AND taggable_id = #{@claim.id}") + @liuvote, @yea_vote_users, @nay_vote_users = votes_for_claim(@claim, tag_ids) + + if tag_ids.empty? + @similar = [] + else + tag_ids_frag = '(' + tag_ids.join(',') + ')' + similar_sql = "SELECT claims.* + FROM claims + JOIN taggings ON taggings.taggable_id = claims.id + AND taggings.taggable_type = 'Claim' + AND taggings.tag_id IN #{tag_ids_frag} + WHERE claims.id <> #{@claim.id} + AND claims.state = 1 + GROUP BY claims.id + ORDER BY COUNT(taggings.id) DESC, id + LIMIT 5" + @similar = Claim.find_by_sql(similar_sql) + end + + @sameclaims = Claim.find_all_by_digest(@claim.digest, :conditions => ['state = 1 AND id != ?', @claim.id]) + + @can_edit_tags = !! (liuid.to_i == @claim.user_id or Contact.find_by_user_id_and_contact_id(@claim.user_id,liuid)) + + cred_user_ids = ([liuid.to_i]+(@yea_vote_users+@nay_vote_users).map{|u|u.id}+@comments.map{|c|c.user_id}).uniq + @norm_cred_scores = {} + @norm_cred_scores[nil] = Cred.scores_for_users(cred_user_ids, :normalized => true) + end + + + + def invite + invitee = params[:openid_or_email] + if invitee.nil? or invitee.strip.empty? + render :text => "Please enter an OpenID or email address." + return + end + + cid = params[:claim_id].to_i + raise if cid == 0 + user = User.find_by_openid_or_email(invitee) + inv = Invitation.new(:sender_id => logged_in_user_id, + :claim_id => cid) + if user + claim = Claim.find(cid) + if (user.voted? claim or user.commented? claim) + render :text => "#{user.dn} has already been to this claim" + return + elsif Dispatch.find_by_user_id_and_dispatchable_type_and_dispatchable_id(user.id, 'Claim', claim.id) + render :text => "This claim is already on #{user.dn}'s list." + return + else + user.dispatch(claim, :from => liu, :reason => 'invite') + render :text => "Invited #{user.dn}" + end + elsif invitee.match(/.+@.+/) + if erc = EmailResponseCode.find_by_email(invitee) + if Invitation.find_by_response_id_and_claim_id(erc.id, cid) + render :text => "That person has already been invited to view this claim." + else + inv.response = erc + inv.save! + render :text => "Jyte will only send one invitation email per address, but we've added this to their list for when they arrive" + end + return + else # Haven't sent a mail yet + erc = EmailResponseCode.create(:email => invitee) + inv.response = erc + inv.save! + response_url = url_for :controller => 'auth', :action => 'invite_response', :code => erc.code + DearStrongbad.deliver_invite(inv, response_url) + @invitee = invitee + render :text => "Sent an invitation to that email address." + end + else + render :text => "No user with that OpenID has yet signed into Jyte. We can send an email invitation if you provide an email address." + return + end + end + + def find + if params[:bc_order] + params[:order] = params[:bc_order] + params[:bc_order] = nil + redirect_to params + return + end + @page = params[:page].to_i + @page = 1 if @page == 0 + + @claims, extras = find_claims({:count => true, + :linked_title => true, + :limit => 10, + :offset => (@page - 1) * 10, + :user_id => liuid, + :tagnames => true, + }.merge(clean_search_params(params))) + @search_title = extras[:title] + @title = strip_tags(@search_title) + @claim_count = extras[:count] + @tagnames = extras[:tagnames] + + # this view can do rss? + @rss_links = [] + if rss_allowed? + rss_params = params.dup + rss_params[:only_path] = false + rss_params[:page] = nil + @rss_link = rss_claims_url(rss_params) + @rss_links << @rss_link + + if params[:format] == 'rss' + rss_params[:format] = nil + @rss_channel_link = find_claims_url(rss_params) + + headers['Content-Type'] = 'text/xml' + render :template => 'rss/claims', :layout => false + return + end + else + @rss_link = nil + end + + batch_load_claim_data(@claims.collect {|c| c.id}) + + @start_n = 10 * @page - 9 + @end_n = 10 * @page + @end_n = @claim_count if @end_n > @claim_count + + @norm_cred_scores = {} + @norm_cred_scores[nil] = Cred.scores_for_users(@claims.map{|c|c.user_id}, :normalized => true) + + if logged_in? + @liu_votes = ClaimVote.find_all_votes_hash(liuid,@claims.map{|c|c.id}) + end + + # Stuff for the left column + # If we have some tags, show contextual tag info (similar tags, + # interested users, user's w/ that cred) + if @tagnames and @tagnames.size > 0 and (tag = Tag.find_by_name(@tagnames[0])) + @tag = tag + scores = Cred.score_class.table_name + @users = User.find_by_sql("SELECT users.id, users.nickname FROM users JOIN #{scores} scores ON scores.user_id = users.id AND scores.tag_id = #{tag.id} ORDER BY scores.value DESC LIMIT 10") + cred_users_ids = @users.map{|u|u.id} + if @users.empty? + @users_title = "No users with #{tag.name} cred" + else + @users_title = "Users with most #{tag.name} cred" + end + @more_users = User.find_by_sql("SELECT users.id, users.nickname FROM users JOIN taggings ON taggings.taggable_type = 'User' AND taggings.taggable_id = users.id AND taggings.tag_id = #{tag.id} ORDER BY users.id DESC LIMIT 10").reject{|u|cred_users_ids.member? u.id}[0..14] + if @more_users.empty? + # may not strictly be true... oh well. + @more_users_title = "No users interested in #{tag.name}" + else + @more_users_title = "Users interested in #{tag.name}" + end + @similar_tags = Tag.find_neighbors(tag.id, 'Claim', :min_count=>2) + elsif params[:interests] and logged_in? + @interests = liu.tags + unless @interests.empty? + @users_title = "Users with similar interests" + tids = @interests.map{|t|t.id} + @users = User.find_by_sql("SELECT users.id, users.nickname FROM users JOIN taggings ON taggings.taggable_type = 'User' AND taggings.taggable_id = users.id AND taggings.tag_id IN (#{tids.join(',')}) WHERE users.id != #{liuid} GROUP BY users.id ORDER BY count(*) DESC LIMIT 10") + end + else + @static_tags_title = "Tags" + @static_tags = %w(jyte politics food music internet philosophy psychology life religion language programming culture science technology silly humor).sort + + @users_title = "Today's Top Claimants" + yesterday = Claim.connection.quoted_date(DateTime.now - 1) + @users = User.find_by_sql("SELECT users.id, users.nickname, SUM(claims.yeas + claims.nays - 1) AS claim_count FROM users JOIN claims ON claims.user_id = users.id AND claims.state = 1 AND claims.created_at > '#{yesterday}' GROUP BY claims.user_id ORDER BY claim_count DESC LIMIT 10") + @users_link_to_claims = true + end + end + + # the full interface to find + def advanced_search + @saved_searches = liu.settings[:saved_searches] or {} + end + + def save_search + u = liu + unless u.settings[:saved_searches] + u.settings[:saved_searches] = {} + end + search = clean_search_params(params) + errors = [] + if url_for(params.update(:action => 'find')).size > 2000 + errors << "That search is too long." + end + search_name = params[:search_name] + if search_name.size > 50 + errors << "That name is too long." + end + if search_name.empty? + errors << "Your search needs a name." + end + if u.settings[:saved_searches].size > 19 + errors << "You can only have 20 saved searches. Remove some or use bookmarks to save additional searches." + end + if errors.empty? + u.settings[:saved_searches].update(search_name => search) + u.save + end + if !request.xhr? + unless errors.empty? + flash[:notice] = errors.join(' ') + end + redirect_to params.update(:action => 'advanced_search') + else + if errors.empty? + render :partial => 'saved_searches' + else + render :text => errors.join(' '), :status => 500 + end + end + end + + def remove_saved_search + u = liu + if u.settings[:saved_searches] + u.settings[:saved_searches].delete(params[:name]) + u.save + end + if !request.xhr? + redirect_to request.referer + else + render :partial => 'saved_searches' + end + end + + def vote + claim_id = params[:claim_id].to_i + raise if claim_id == 0 + + approval = (params[:vote] == 'yes') + + ch = Claim.connection.select_one("SELECT group_id, state FROM claims WHERE claims.id = #{claim_id}") + state = ch['state'].to_i + group_id = ch['group_id'].to_i + if state == 4 + unless GroupMembership.find_by_user_id_and_group_id(liuid,group_id) + render :text => 'cannot vote on this group claim', :status => 403 + return + end + elsif state != 1 + render :text => 'cannot vote this claim', :status => 403 + return + end + + v = ClaimVote.find(:first, :conditions => ["claim_id = ? AND user_id = ?", claim_id, liuid]) + if v + v.vote = approval + v.save + else + ClaimVote.create!(:claim_id => claim_id, :user_id => liuid, :vote => approval) + end + + Dispatch.find(:all, :conditions => ["dispatchable_type = 'Claim' AND dispatchable_id = ? AND user_id = ?", claim_id, liuid]).each{|d|d.destroy} + + if !request.xhr? + redirect_to :controller => 'claim', :action => 'show', :id => params[:claim_id] #XXX added by brian to stop exceptions for non-js users or clicked while logged out users + else + # XXX: what do we really want to do here? + render :text => 'voted' + end + end + + def votes + @claim = Claim.find_by_id(params[:id]) + unless @claim + flash[:notice] = "Sorry, that claim could not be found." + if ref = request.referer + redirect_to ref + else + redirect_to front_url + end + return + end + + options = { + :per_page => 30, + :order => 'claim_votes.created_at DESC', + :joins => 'JOIN users ON users.id = claim_votes.user_id', + :include => :user + } + + @voted = voted = params[:votes] + if voted == 'yes' + options[:conditions] = ["claim_id = ? AND vote = true AND #{User.exclude_sql}", @claim.id] + elsif voted == 'no' + options[:conditions] = ["claim_id = ? AND vote = false AND #{User.exclude_sql}", @claim.id] + else + options[:conditions] = ["claim_id = ? AND #{User.exclude_sql}", @claim.id] + end + + @votes = ClaimVote.paginate(:all, options.merge({:page => params[:page], :per_page => 30})) + + end + + def comment + @claim = Claim.find(params[:claim_id]) + + group_id = @claim.group_id + if group_id + unless GroupMembership.find_by_user_id_and_group_id(liuid,group_id) + render :text => 'cannot comment on this group claim', :status => 403 + return + end + end + + c = Comment.new(:claim_id => params[:claim_id], + :user_id => session[:user_id], + :body => params[:body]) + + if !request.xhr? + c.save + redirect_to claim_url(:urlslug=>@claim.urlslug, :anchor=>'new_comment') + elsif params[:preview]=='t' + c.created_at = DateTime.now + render :partial => 'comment_preview', :locals => {:c => c} + elsif params[:publish]=='t' + c.save + l = Look.find_by_user_id_and_object_type_and_object_id(liuid, 'Claim', @claim.id) + if l.nil? # odd + l = Look.new(:user_id => liuid, :object => @claim) + end + l.touch + @blocked_user_ids = liu.blocked_user_ids + com_html = render_to_string :partial => 'comment', :collection => @claim.comments + html = com_html + " " + render :text => html + else + raise "Bad comment submission" + end + + end + + def flag + c = Claim.find_by_id(params[:claim_id]) + if c or liu.can_flag(c) + # Flagging.create(:user_id => liuid, :claim_id => c.id) + c.flag(:red) + c.save + redirect_to claim_url(:urlslug => c.urlslug) + else + flash[:notice] = "You can't flag that claim." + redirect_to front_url #claim_url(:urlslug => c.urlslug) + end + end + + def mark + cid = params[:claim_id].to_i + cid == 0 and raise + raise unless params[:watch] or params[:trash] + Dispatch.find_all_by_dispatchable_type_and_dispatchable_id_and_user_id('Claim', cid, liuid).each {|d| + d.destroy + } + f = Flagging.find_or_create_by_user_id_and_claim_id(liuid, cid) + if params[:watch] == 'y' + f.watch = true + else + f.watch = false + end + if params[:trash] == 'y' + f.trash = true + else + f.trash = false + end + f.save + if !request.xhr? + redirect_to request.referer + else + render :text => '' + end + end + + def tagroll_setup + @tags = params[:tags] || '' + @tags = Tag.parse(@tags).join(', ') + end + + def random + c = Claim.count + + claim = Claim.find_by_id(rand(c)) + while claim.nil? or + claim.state != 1 or + (logged_in? and ill=liu.settings[:ignore_list] and ill.member?(claim.user_id)) or + (logged_in? and Flagging.find_by_user_id_and_claim_id_and_trash(liuid,claim.id,true)) + + claim = Claim.find_by_id(rand(c)) + end + + redirect_to claim_url(claim.urlslug) + end + + + def inspiration_tree + + end + + def inspiration_tree_xml + @base_claim = Claim.find(params[:id]) + @root_claim = @base_claim.root_inspiring_claim + if @root_claim.nil? + #error + raise + end + + render :text => inspired_claims_xml(@root_claim, :root => true) + + end + + + private + + ALLOWED_RSS_ORDERS = [nil,'featured'] #nil is recent + def rss_allowed? # this is weird to have in a separate function since it's used exactly once + return false unless ALLOWED_RSS_ORDERS.member?(params[:order]) + return false if params[:voted] or params[:comments] + return false if params[:group_id] + return true + end + + def attr_escape(s) # XXX use a real escaper + s.gsub("'","'") + end + + def find_claims(options) + extras = {} + errors = [] + + if options[:states] == :all + cond_list = [] + elsif options[:states].nil? + if options[:group_id] + cond_list = [] + else + cond_list = ["claims.state = 1"] + end + elsif states = options[:states].map{|s|s.to_i} + cond_list = ["claims.state IN (#{states.join(',')})"] + else + raise "bad states option" + end + + user_id = options[:user_id] + if user_id + user_id = user_id.to_i + user = User.find(user_id) + ignore_list = user.blocked_user_ids + end + + join_list = [] + + if options[:order] == 'voted' + order = '(claims.yeas + claims.nays) DESC' + title = "Most voted on claims" + elsif options[:order] == 'discussed' + order = 'claims.comments_count DESC' + cond_list << 'claims.comments_count > 0' + title = "Most discussed claims" + elsif options[:order] == 'contested' + order = "CASE WHEN claims.yeas > claims.nays THEN claims.nays ELSE claims.yeas END DESC" + cond_list << "claims.nays > 0 AND claims.yeas > 0" + title = "Contested claims" + elsif options[:order] == 'solid' + order = "CASE WHEN claims.yeas > claims.nays THEN claims.yeas - 4 * claims.nays ELSE claims.nays - 4 * claims.yeas END DESC" + cond_list << "(claims.nays > 1 OR claims.yeas > 1)" + title = "Solid claims" + elsif options[:order] == 'recently_commented' + title = "Recently commented on claims" + order = "claims.commented_at DESC" + cond_list << 'claims.comments_count > 0' + group_frag = "GROUP BY claims.id" + elsif options[:order] == 'recently_voted' + title = "Recently voted on claims" + cond_list << '(claims.yeas + claims.nays) > 1' + order = "claims.voted_at DESC" + group_frag = "GROUP BY claims.id" + elsif options[:order] == 'featured' + order = 'claims.created_at DESC' + title = "Featured claims" + join_list << "JOIN featured_claims ON claims.id = featured_claims.claim_id" + elsif options[:order] == 'relevance' + title = "Most relevant claims" + order = "COUNT(*) DESC" + group_frag = "GROUP BY claims.id" + elsif options[:order] == 'oldest' + order = 'claims.created_at' + title = "Oldest claims" + elsif options[:order] == 'watched' + # XXX we could maintain a count + join_list << "JOIN flaggings AS sf ON claims.id = sf.claim_id AND sf.watch = true" + order = 'COUNT(*) DESC' + group_frag = "GROUP BY claims.id" + title = "Most watched claims" + elsif options[:order] == 'trashed' + # XXX we could maintain a count + join_list << "JOIN flaggings AS sf ON claims.id = sf.claim_id AND sf.trash = true" + order = 'COUNT(*) DESC' + group_frag = "GROUP BY claims.id" + title = "Most trashed claims" + else + order = 'claims.created_at DESC' + title = "Newest claims" + end + + if user_id + + if (group_id = options[:group_id].to_i) != 0 + if GroupMembership.find_by_group_id_and_user_id(group_id, user_id) + cond_list << "claims.group_id = #{group_id}" + group = Group.find(group_id) + cond_list << "claims.state = 4" + if options[:linked_title] + title << " for #{ERB::Util.h(group.name)}" + else + title << " for #{ERB::Util.h(group.name)}" + end + else + errors << "You are not a member of that group." + cond_list << "claims.state = 1" + end + end + + if options[:voted] == 'no' + title << " you have not yet voted on" + join = "LEFT JOIN claim_votes ON claim_votes.claim_id = claims.id AND claim_votes.user_id = #{user_id}" + join_list << join + cond_list << "claim_votes.id IS NULL" + elsif options[:voted] == 'yes' + title << " you have voted on" + join = "JOIN claim_votes ON claim_votes.claim_id = claims.id AND claim_votes.user_id = #{user_id}" + join_list << join + elsif options[:voted] == 'up' + title << " you have agreed with" + join = "JOIN claim_votes ON claim_votes.claim_id = claims.id AND claim_votes.user_id = #{user_id} AND claim_votes.vote = true" + join_list << join + elsif options[:voted] == 'down' + title << " you have disagreed with" + join = "JOIN claim_votes ON claim_votes.claim_id = claims.id AND claim_votes.user_id = #{user_id} AND claim_votes.vote = false" + join_list << join + elsif options[:voted] == 'minority' + title << " you voted with the minority" + join = "JOIN claim_votes ON claim_votes.claim_id = claims.id AND claim_votes.user_id = #{user_id} AND claim_votes.vote = (claims.yeas < claims.nays)" + join_list << join + elsif options[:voted] == 'majority' + title << " you voted with the majority" + join = "JOIN claim_votes ON claim_votes.claim_id = claims.id AND claim_votes.user_id = #{user_id} AND claim_votes.vote = (claims.yeas > claims.nays)" + join_list << join + end + if options[:visited] + title << " you have visited" + join = "JOIN looks visits ON visits.object_type = 'Claim' AND visits.object_id = claims.id AND visits.user_id = #{user_id}" + join_list << join + end + if options[:watched] + title << " you are watching" + join = "JOIN flaggings wings ON wings.claim_id = claims.id AND wings.watch = true AND wings.user_id = #{user_id}" + join_list << join + end + if options[:trashed] + title << " you have filtered" + join = "JOIN flaggings tings ON tings.claim_id = claims.id AND tings.trash = true AND tings.user_id = #{user_id}" + join_list << join + elsif options[:order] != 'trashed' + cond_list << "claims.id NOT IN (SELECT claim_id FROM flaggings WHERE user_id = #{user_id} AND trash = true)" + end + if options[:new_comments] + title << " with new comments" + join = "LEFT JOIN looks ON looks.object_type = 'Claim' AND looks.object_id = claims.id AND looks.user_id = #{user_id}" + cond_list << "(looks.id IS NULL OR claims.commented_at > looks.updated_at)" + # A casualty of removing the comments join + #unless ignore_list.empty? + # join += " AND comments.user_id NOT IN (#{ignore_list.join(',')})" + #end + join_list << join + end + end + + if about = options[:about] + if about.downcase == 'contacts' and logged_in? + join = "JOIN mentioned_identifiers ON mentioned_identifiers.claim_id = claims.id AND mentioned_identifiers.identifier_id IN (SELECT identifiers.id FROM identifiers JOIN contacts ON contacts.contact_id = identifiers.user_id AND contacts.user_id = #{liuid})" + join_list << join + title << " about your contacts" + else + vals = options[:about].split(' ').map{|oid| + begin + Identifier.normalize(oid) + rescue URI::InvalidURIError + errors << "#{oid} is not a valid OpenID." + next + end + } + abis = Identifier.find_all_by_value(vals) + unless abis.empty? + abi_ids = abis.map{|i|i.id} + join = "JOIN mentioned_identifiers ON mentioned_identifiers.claim_id = claims.id AND mentioned_identifiers.identifier_id IN (#{abi_ids.join(',')}) " + join_list << join + + title << " about " + if options[:linked_title] + title << oxford_comma_list(abis.map{|abi| + if abi.user_id + "#{ERB::Util.h(abi.shorten)}" + else + "#{ERB::Util.h(abi.shorten)}" + end + }, 'or') + + else + title << oxford_comma_list(abis.map{|abi| ERB::Util.h(abi.shorten) }, 'or') + end + end + end + end + + if options[:comments_by] + if options[:comments_by].downcase == 'contacts' and logged_in? + join = "JOIN comments bycomments ON bycomments.claim_id = claims.id AND bycomments.user_id IN (SELECT contact_id FROM contacts WHERE user_id = #{liuid})" + join_list << join + title << " with comments from your contacts" + else + openids = options[:comments_by].split(' ') + cbyus = User.find_all_lite_by_openid(openids) + if openids.size < cbyus.size + errors << "Some of those users could not be found." + end + unless cbyus.empty? + cbyuids = cbyus.map{|u|u.id} + join = "JOIN comments bycomments ON bycomments.claim_id = claims.id AND bycomments.user_id IN (#{cbyuids.join(',')})" + join_list << join + names = oxford_comma_list(cbyus.map{|u|u.dn}, "or") + title << " with comments from #{names}" + end + end + end + + if options[:voted_by] + if options[:voted_by].downcase == 'contacts' and logged_in? + join = "JOIN claim_votes byvotes ON byvotes.claim_id = claims.id AND byvotes.user_id IN (SELECT contact_id FROM contacts WHERE user_id = #{liuid})" + join_list << join + title << " voted on by your contacts" + else + openids = options[:voted_by].split(' ') + vbyus = User.find_all_lite_by_openid(openids) + if openids.size < vbyus.size + errors << "Some of those users could not be found." + end + unless vbyus.empty? + vbyuids = vbyus.map{|u|u.id} + join = "JOIN claim_votes byvotes ON byvotes.claim_id = claims.id AND byvotes.user_id IN (#{vbyuids.join(',')})" + join_list << join + names = oxford_comma_list(vbyus.map{|u|u.dn}, "or") + title << " voted on by #{names}" + end + end + end + + if options[:by] + if options[:by].downcase == 'contacts' and logged_in? + cond_list << "claims.user_id IN (SELECT contact_id FROM contacts WHERE user_id = #{liuid})" + title << " by your contacts" + else + openids = options[:by].split(' ') + byus = User.find_all_lite_by_openid(openids) + if openids.size < byus.size + errors << "Some of those users could not be found." + end + unless byus.empty? + byuids = byus.map{|u|u.id} + cond_list << "claims.user_id IN (#{byuids.join(',')})" + names = oxford_comma_list(byus.map{|u|u.dn}, "or") + title << " by #{names}" + end + end + end + + if options[:not_by] + if options[:not_by].downcase == 'contacts' and logged_in? + cond_list << "claims.user_id NOT IN (SELECT contact_id FROM contacts WHERE user_id = #{liuid})" + title << " not by your contacts" + else + openids = options[:not_by].split(' ') + nbyus = User.find_all_lite_by_openid(openids) + if openids.size < nbyus.size + errors << "Some of those users could not be found." + end + if defined? byus and byus + nbyus.reject!{|u|byus.member? u} + end + unless nbyus.empty? + nbyuids = nbyus.map{|u|u.id} + cond_list << "claims.user_id NOT IN (#{nbyuids.join(',')})" + names = oxford_comma_list(nbyus.map{|u|u.dn}, "or") + title << " not by #{names}" + end + end + end + + if user_id + il = ignore_list + if il and defined? byuids and byuids + il.reject!{|i|byuids.member? i} + end + if il and not il.empty? + cond_list << "claims.user_id NOT IN (#{il.join(',')})" + end + end + + if logged_in? and options[:interests] + tag_ids = user.tags.map{|t|t.id} + join = "JOIN taggings ON taggings.taggable_type = 'Claim' AND taggings.taggable_id = claims.id AND taggings.tag_id IN (#{tag_ids.join(',')})" + join_list << join + title << " tagged with your interests" + elsif options[:tags] or options[:tag] + t = options[:tags] + t = options[:tag] + "," unless t + tagnames = Tag.parse(t).reject{|n|n.empty?} + tags = Tag.find_all_by_name(tagnames) + if tagnames.size > tags.size + errors << "Some of those tags could not be found." + end + unless tags.empty? + tag_ids = tags.map{|t|t.id} + join = "JOIN taggings ON taggings.taggable_type = 'Claim' AND taggings.taggable_id = claims.id AND taggings.tag_id IN (#{tag_ids.join(',')})" + join_list << join + names = oxford_comma_list(tags.map{|t|t.name}, "or") + title << " tagged with #{names}" + end + + extras[:tagnames] = tagnames if options[:tagnames] + end + + if options[:filter_tags] + tagnames = Tag.parse(options[:filter_tags]) + ftags = Tag.find_all_by_name(tagnames) + if tagnames.size > ftags.size + errors << "Some of those tags could not be found." + end + if defined? tags and tags + ftags.reject{|t| tags.member? t} + end + unless ftags.empty? + tag_ids = ftags.map{|t|t.id} + join = "LEFT JOIN taggings ftaggings ON ftaggings.taggable_type = 'Claim' AND ftaggings.taggable_id = claims.id AND ftaggings.tag_id IN (#{tag_ids.join(',')})" + join_list << join + cond_list << "ftaggings.id IS NULL" + names = oxford_comma_list(ftags.map{|t|t.name}, "or") + title << " not tagged with #{names}" + end + end + + min_votes = options[:min_votes].to_i + if min_votes > 0 + cond_list << "(claims.yeas + claims.nays) > #{min_votes}" + title << " with more than #{min_votes} votes" + end + + min_score = options[:min_score].to_f + if min_score > 0 + min_score /= 8 # the display factor + st = Cred.score_table_name + join = "JOIN #{st} scores ON scores.tag_id IS NULL AND scores.user_id = claims.user_id AND scores.value > #{min_score}" + join_list << join + end + + if options[:limit] + lim = options[:limit].to_i + offset = options[:offset].to_i + limit_frag = "LIMIT #{offset}, #{lim}" + else + limit_frag = "" + end + + join_frag = join_list.join(" ") + if cond_list.empty? + cond_frag = "" + else + cond_frag = "WHERE " << cond_list.join(" AND ") + end + unless defined? group_frag + group_frag = "" + end + sql = "SELECT DISTINCT claims.* FROM claims #{join_frag} #{cond_frag} #{group_frag} ORDER BY #{order} #{limit_frag}" + + claims = Claim.find_by_sql(sql) + + if options[:count] + extras[:count] = Claim.count_by_sql("SELECT COUNT(*) FROM (SELECT DISTINCT claims.id FROM claims #{join_frag} #{cond_frag}) foo") + end + + if options[:title] or options[:linked_title] + extras[:title] = title + end + + unless errors.empty? + flash[:notice] = errors.uniq.join(' ') + end + + if extras.empty? + return claims + else + return claims, extras + end + end + + def clean_search_params(orig_params) + return {:order => orig_params[:order], + :voted => orig_params[:voted], + :new_comments => orig_params[:new_comments], + :tags => orig_params[:tags], + :tag => orig_params[:tag], + :interests => orig_params[:interests], + :filter_tags => orig_params[:filter_tags], + :by => orig_params[:by], + :not_by => orig_params[:not_by], + :about => orig_params[:about], + :comments_by => orig_params[:comments_by], + :voted_by => orig_params[:voted_by], + :min_votes => orig_params[:min_votes], + :min_score => orig_params[:min_score], + :visited => orig_params[:visited], + :watched => orig_params[:watched], + :trashed => orig_params[:trashed], + :group_id => orig_params[:group_id], + }.reject {|k,v| v.nil? or v.empty?} + end + + + def inspired_claims_xml(claim, options = {}) + if @infinite_recursion_protection.nil? + @infinite_recursion_protection = [claim.id] + else + raise 'loop detected' if @infinite_recursion_protection.member? claim.id + @infinite_recursion_protection << claim.id + end + if options[:root] + xml = " true) } + + if options[:root] + xml += "" + else + xml += "" + end + + return xml + end + + def votes_for_claim(claim, tag_ids) + max = 10 + yea_vote_users = [] + nay_vote_users = [] + + @voter_labels = {} + + # add logged in user vote first + liuvote = nil + if logged_in? + liuvote = ClaimVote.find_by_claim_id_and_user_id(@claim.id, liuid.to_i) + (liuvote.vote ? yea_vote_users : nay_vote_users) << liu if liuvote + end + + # add claimant vote + if !logged_in? or claim.user_id != liuid.to_i + claimant_vote = ClaimVote.find_by_user_id_and_claim_id(claim.user_id,claim.id) + (claimant_vote.vote ? yea_vote_users : nay_vote_users) << claim.user if claimant_vote + end + + + mentioned_user_ids = claim.identifiers.select {|i| i.user_id}.map {|i| i.user_id.to_i} + if mentioned_user_ids.size > 0 + # need votes of mentioned users if they are not the claimant or liu + needs_vote = mentioned_user_ids.dup + needs_vote.delete(liuvote.user_id) if liuvote + needs_vote.delete(claim.user_id) + + if needs_vote.size > 0 + ClaimVote.find_by_sql( + "SELECT claim_votes.* FROM claim_votes + WHERE claim_votes.claim_id = #{claim.id} + AND claim_votes.user_id IN (#{needs_vote.join(',')})").each {|v| + (v.vote ? yea_vote_users : nay_vote_users) << v.user + } + end + + # add yes votes of contacts on mentioned users + yea_contact_voters = claim.voters_by_contacts_of_mentioned( + :limit => max-yea_vote_users.size, + :vote => true, + :exclude_ids => yea_vote_users.map {|u|u.id}, + :mentioned_user_ids => mentioned_user_ids) + yea_vote_users += yea_contact_voters + + # add no votes of contacts of mentioned users + nay_contact_voters = claim.voters_by_contacts_of_mentioned( + :limit => max-nay_vote_users.size, + :vote => false, + :exclude_ids => nay_vote_users.map {|u|u.id}, + :mentioned_user_ids => mentioned_user_ids) + nay_vote_users += nay_contact_voters + + contact_voter_ids = (yea_contact_voters + nay_contact_voters).map{|u|u.id} + + # claimant may not be a contact, but let's add him to this list to get + # contact info if he is + contact_voter_ids << claim.user_id + + # Build title messagses for contacts + unless contact_voter_ids.empty? + mentioned_users = User.find_all_lite(mentioned_user_ids).hash_by(:id) + + Contact.find(:all, :conditions => "contact_id IN (#{contact_voter_ids.join(',')}) AND user_id IN (#{mentioned_user_ids.join(',')})").each {|con| + unless @voter_labels.has_key? con.contact_id + @voter_labels[con.contact_id] = "A contact of " + else + @voter_labels[con.contact_id] += "; of " + end + tl = con.tag_list + tl = " (#{tl})" unless tl.empty? + @voter_labels[con.contact_id] += "#{mentioned_users[con.user_id].dn} #{tl}" + } + end + end + + # add voter label to subjects who voted + mentioned_user_ids.each {|user_id| @voter_labels[user_id] = 'Subject of claim.'} + + # add other yea votes + if yea_vote_users.size < max + yea_vote_users += claim.yea_voters(:limit => max-yea_vote_users.size, + :exclude_ids => yea_vote_users.map {|u|u.id}, + :tag_ids => tag_ids) + end + + # add other nay votes + if nay_vote_users.size < max + nay_vote_users += claim.nay_voters(:limit => max-nay_vote_users.size, + :exclude_ids => nay_vote_users.map {|u|u.id}, + :tag_ids => tag_ids) + end + + return [liuvote, yea_vote_users, nay_vote_users] + end + +end diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb new file mode 100644 index 0000000..b9e761a --- /dev/null +++ b/app/controllers/contacts_controller.rb @@ -0,0 +1,167 @@ +class ContactsController < ApplicationController + + before_filter :check_logged_in, :except => [:index,:api_roster,:api_is_member] + secure_actions :only => [:add_submit,:remove_submit,:edit_submit] + + def index + @user = User.find_by_id(params[:id]) + unless @user + flash[:notice] = "No contacts for that user." + redirect_to front_url + return + end + + @contacts = Contact.find(:all, + :conditions => ['user_id = ?', @user.id], + :include => [:contact]) + + # don't include blocked users as contact of + blocked = @user.blocked_user_ids + if blocked.size > 0 + blocked_sql = " AND user_id NOT IN (#{blocked.join(',')})" + else + blocked_sql = "" + end + + @contact_of = Contact.find(:all, + :conditions => ["contact_id = ? #{blocked_sql}", @user.id], + :include => [:contacter]) + end + + def add + @user = User.find_by_id(params[:user_id]) + unless @user + flash[:notice] = 'Cannot find user' + redirect_to :controller => 'contacts' + return + end + + # get all existing tags. this could be more efficient, but we'll + # save that for later :) + @all_tags = [] + Contact.find_all_by_user_id(liuid).each {|c| @all_tags |= c.tags.collect {|t| t.name}} + @all_tags.sort! + end + + def add_submit + user = User.find(params[:user_id]) + c = Contact.find_or_create_by_user_id_and_contact_id(liuid, user.id) + c.tag_with(params[:contact_tags]) + + Dispatch.create(:user_id => user.id, :dispatchable => liu, :reason => " added you as a contact.") + + flash[:notice] = 'Contacts updated.' + redirect_to xprofile_url(user.s) + end + + def edit + @contact = Contact.find_by_user_id_and_contact_id(liuid, params[:user_id]) + unless @contact + flash[:notice] = "No contact relationship found." + redirect_to :controller=>'contacts',:action => 'index', :id => liuid + return + end + @user = @contact.contact + @all_tags = [] + Contact.find_all_by_user_id(liuid).each {|c| @all_tags |= c.tags.collect {|t| t.name}} + @all_tags.sort! + end + + def edit_submit + @contact = Contact.find_by_user_id_and_contact_id(liuid, params[:user_id]) + unless @contact + flash[:notice] = "No contact relationship found." + else + @contact.tag_with(params[:contact_tags]) + end + + redirect_to :controller=>'contacts',:action => 'index', :id => liuid + end + + def remove_submit + c = Contact.find_by_user_id_and_contact_id(liuid, params[:user_id]) + Dispatch.create(:user_id => params[:user_id], :dispatchable => liu, :reason => " removed you as a contact.") + if c + c.destroy + flash[:notice] = 'Contact removed.' + end + redirect_to :action => 'index',:id => liuid + end + + def api_is_member + ids = identifiers_for + return if ids.nil? + + c_openid = params[:contact_openid] + + if c_openid and !c_openid.empty? + contact_openid = Identifier.detect(c_openid) + if ids.member?(contact_openid) + render :text => 'true', :status => 200 + else + render :text => 'false', :status => 200 + end + else + render :text => 'error: must specify contact_openid', :status => 400 + end + end + + def api_roster + ids = identifiers_for + return if ids.nil? + render :text => ids.join("\n"), :status => 200 + end + + def compare + end + + def compare_gmail + gmail = GMail.new(liu) + @contacts = gmail.contacts + if @contacts.is_a?(Net::HTTPUnauthorized) + flash[:notice] = "Authorization for your gmail contact list was denied. Please reauthorize." + redirect_to :action => :compare + end + @jyters = User.all(:conditions => {:email => @contacts}) + @missing_jyters = @jyters - liu.contacts + end + + private + + def identifiers_for + user = User.find_by_openid(params[:openid]) + unless user + render :text => 'error: unknown user', :status => 400 + return nil + end + + contacts = Contact.find(:all, + :conditions => ['user_id = ?', user.id], + :include => [:contact]) + + tag_name = params[:tag] + if tag_name and !tag_name.empty? + + if tag = Tag.find_by_name(tag_name) + # not sure how to do a join w/ this... so this is ugly + tag_conds = '(' + contacts.collect {|c| c.id}.join(',') + ')' + taggings = Tagging.find(:all, + :conditions => ["taggable_id in #{tag_conds} AND tag_id = ? AND taggable_type = ?",tag.id,'Contact']) + taggings_ids = taggings.collect {|t| t.taggable_id} + contacts = contacts.find_all {|c| taggings_ids.member?(c.id)} + else + contacts = [] + end + + end + + ids = [] + if params[:primary] == 'true' + contacts.each {|c| ids << c.contact.openid} + else + contacts.each {|c| ids |= c.contact.identifiers.collect {|i| i.value}} + end + return ids + end + +end diff --git a/app/controllers/cred_controller.rb b/app/controllers/cred_controller.rb new file mode 100644 index 0000000..fe6f2e6 --- /dev/null +++ b/app/controllers/cred_controller.rb @@ -0,0 +1,82 @@ +class CredController < ApplicationController + + def api_cred_by_tags + openids = JSON.parse(params[:openids]) + + # normalize incoming identifiers + openids = openids.collect {|oid| Identifier.detect(oid)} + + # get all the tag ids + incoming_tags = JSON.parse(params[:tags]) + tags = Tag.find_all_by_name(incoming_tags) + tags_by_id = {} + tags.each {|t| tags_by_id[t.id.to_i] = t} + tag_ids = tags_by_id.keys + tag_names = tags.collect {|t| t.name} + unknown_tag_names = incoming_tags.find_all {|t| !tag_names.member?(t)} + + # result Hash + r = {} + + # find all the jyte users by their identifier + if openids.size > 0 and tag_ids.size > 0 + identifier_objects = Identifier.find_all_by_value(openids) + + # handle requested identifiers that are not in jyte at all + identifier_values = identifier_objects.collect {|i| i.value} + openids.find_all {|oid| !identifier_values.member?(oid)}.each {|oid| + r[oid] = nil + } + + identifiers_with_users = identifier_objects.find_all {|i| !i.user_id.nil?} + identifiers_without_users = identifier_objects - identifiers_with_users + + # handle requested identifers that aren't jyte users, + # but are mentioned in jyte + identifiers_without_users.each {|i| r[i.value] = nil} + + if identifiers_with_users.size > 0 + # init response object hashes + identifiers_with_users.each {|i| + r[i.value] = {:tag_scores => {}} + } + identifiers_by_user_id = {} + identifiers_with_users.each {|i| + identifiers_by_user_id[i.user_id.to_i] = i} + + # get cred scored + user_ids = identifiers_with_users.collect {|i| i.user_id} + cred = Cred.scores_by_tag_and_user(tag_ids, user_ids, :normalized=>true) + + # load scores into response object + cred.each {|tag_id, scores| + next if tag_id.nil? # skip overall + tag_name = tags_by_id[tag_id].name + scores.each {|user_id, score| + openid = identifiers_by_user_id[user_id].value + r[openid][:tag_scores][tag_name] = score + } + } + + # generate combined score + r.each {|openid,rsection| + if rsection + # add zero score for tags which aren't found on jyte + unknown_tag_names.each {|tn| rsection[:tag_scores][tn] = 0.0} + + vals = rsection[:tag_scores].values + if vals.size == 0 + r[openid][:combined_score] = 0.0 + else + r[openid][:combined_score] = vals.sum / vals.size + end + end + } + end + + end + + render :text => r.to_json, :status => 200 + end + +end diff --git a/app/controllers/group_controller.rb b/app/controllers/group_controller.rb new file mode 100644 index 0000000..a76f0ef --- /dev/null +++ b/app/controllers/group_controller.rb @@ -0,0 +1,508 @@ +class GroupController < ApplicationController + + before_filter :auto_login + before_filter :check_logged_in, :except => [:index,:show,:api_roster,:api_is_member,:claims,:find] + secure_actions :only => [:new_submit,:edit_submit,:delete_icon,:add_yourself, + :remove_yourself,:del_member,:del_group, + :invite_decision] + + def index + conds = nil + group_frag = '' + @hot_groups = [] + @contacts_groups = [] + @interests_groups = [] + @your_groups = [] + + if liu + @your_groups = liu.groups + unless @your_groups.empty? + group_ids = @your_groups.collect {|g|g.id.to_i} + group_frag = " AND groups.id not IN (#{group_ids.join(',')})" + end + + if liu.tags.size > 0 + # try and find some groups that match + tag_ids = liu.tags.collect {|t|t.id} + if tag_ids.size > 0 + @interests_groups, + @interests_count = groups_by_interest(:limit => 6, + :invite_only=>false) + + end + end + + if liu.contacts.size > 0 + # get groups ordered by the number of you contacts in the group + @contacts_groups, + @contacts_count = groups_by_contacts(:limit => 6, + :invite_only => false) + end + + + end + + if @interests_groups.empty? or @contacts_groups.empty? + + limit = logged_in? ? 12 : 18 + @hot_groups,_c = groups_by_popularity(:limit => limit) + + end + + @latest_groups, + @latest_count = groups_by_recency(:limit => 6) + + end + + def find + by = params[:by] + + page = params.fetch(:page,1).to_i + limit = 24 + offset = (page * limit) - limit + + case by + when 'contacts' + if logged_in? + @groups, @g_count = groups_by_contacts(:limit => limit, + :offset => offset) + @g_title = 'Your contacts groups' + @g_subtitle = "Groups that you aren't a member of, ordered by how many of your contacts are members." + else + redirect_to :controller => 'group' + return + end + + when 'interests' + if logged_in? + @groups, @g_count = groups_by_interest(:limit => limit, + :offset => offset) + + @g_title = 'Groups that match your interests' + @g_subtitle = "Groups that you aren't a member of, ordered by the number of shared interests." + else + redirect_to :controller => 'group' + return + end + + when 'hot' + @groups, @g_count = groups_by_popularity(:limit => limit, + :offset => offset) + @g_title = 'Popular groups' + @g_subtitle = 'All groups ordered by number of members' + + when 'latest' + + @groups, @g_count = groups_by_recency(:limit => limit, + :offset => offset) + @g_title = 'All groups' + @g_subtitle = 'All groups with the latest ones first.' + + when 'search' + q = params[:q] + + begin + @groups = Group.find_by_solr(q, :rows => limit, :start => offset) + @g_count = Group.count_by_solr(q) + rescue + @groups = [] + @g_count = 0 + end + @g_title = "Search results" + else + redirect_to :controller => 'group' + return + end + + @group_pages = WillPaginate::Collection.create(page, limit) do |pager| + pager.replace(@groups) + end + end + + def new + end + + def name_status + @g = Group.find_by_name(params[:name]) + unless @g + @valid = Group.new(:name => params[:name], + :user_id => liuid).valid? + else + @valid = true + end + render :partial => 'name_status' + end + + def new_submit + g = Group.new(:user_id => liuid) + g.update_attributes(params[:group]) + + if g.errors[:name] + flash[:notice] = 'Your group must have a unique name' + redirect_to :action => 'new' + return + else + if params[:group_interests] + g.tag_with(params[:group_interests]) + end + end + + g.save + GroupMembership.create(:user_id => liuid, + :group_id => g.id, + :moderator => true) + + redirect_to :action => 'edit', :id => g.id, :new => 1 + end + + def edit + @group = Group.find(params[:id]) + unless @group.can_edit?(liu) + flash[:notice] = 'You cannot edit that group.' + redirect_to gurl(show) + return + end + end + + def edit_submit + @group = Group.find(params[:id]) + + unless @group.can_edit?(liu) + redirect_to gurl(@group) + return + end + + @group.update_attributes(params[:group]) + @group.tag_with(params[:tags]) + @group.save + + # process image + image_blob = read_blob(params[:image]) + + if image_blob + # destroy old image is necessary + if old_image = @group.image + @group.imagings.destroy_all + old_image.destroy_image(@group) + end + + begin + im = Image.from_blob(image_blob, @group) + im.on(@group) + rescue + flash[:notice] = "Sorry, we couldn't read that image. Try another." + redirect_to :action => 'edit', :id => @group.id + return + end + end + + flash[:notice] = 'Group updated' + redirect_to gurl(@group) + end + + def delete_icon + @group = Group.find(params[:id]) + + unless @group.can_edit?(liu) + flash[:notice] = 'nope' + redirect_to gurl(@group) + return + end + + i = @group.image + if i + @group.imagings.destroy_all + i.destroy_image(@group) + end + + redirect_to :action => 'edit', :id => @group.id + end + + def claims + @group = Group.find_by_id(params[:id]) + unless @group + flash[:notice] = 'Unknown group' + redirect_to :controller => '' + return + end + + uids = @group.user_ids + + @claims = Claim.paginate_by_user_id(uids, :conditions => 'state = 1', :order => 'created_at DESC', :limit => 10, :page => params[:page]) + end + + def show + if params[:urlslug] + @group = Group.find_by_urlslug(params[:urlslug]) + else + @group = Group.find_by_id(params[:id]) + end + + unless @group + flash[:notice] = 'Unknown group' + redirect_to :controller => '' + return + end + + if liu + @invite = Invitation.find_by_group_id_and_recipient_id(@group.id, liuid) + else + @invite = nil + end + + if logged_in? + # Clear user's dispatches to this group + Dispatch.find_all_by_dispatchable_type_and_dispatchable_id_and_user_id('Group', params[:id], logged_in_user_id).each {|d| d.destroy } + end + + if @group.member?(liu) + @claims = Claim.find(:all, :conditions => ["state = 4 AND group_id = ?", @group.id], :limit => 10, :order => 'created_at DESC') + @group_claim_count = Claim.count(:conditions => ['state = 4 AND group_id = ?', @group.id]) + else + @claims = [] + end + + claim_ids = @claims.collect {|c| c.id} + batch_load_claim_data(claim_ids) #faster! ;) + @liu_votes = ClaimVote.find_all_votes_hash(liuid, claim_ids) + end + + def invite + u = liu + @group = Group.find_by_id(params[:group_id]) + mod = params[:mod] + if !@group or !@group.can_invite?(u) or (mod and !@group.can_invite_as_moderator?(u)) + @invite_msg = 'You cannot do that' + render :partial => 'invite_form' + return + end + + openid_or_email = params[:openid_or_email] + + if openid_or_email.nil? or openid_or_email.empty? + @invite_msg = 'Please enter an OpenID or Email' + else + inv = Invitation.create(:sender_id => u.id, + :group_id => @group.id, + :group_moderator => mod) + + user = User.find_by_openid_or_email(openid_or_email) + if user + if @group.member?(user) + inv.destroy + @invite_msg = ERB::Util.h(openid_or_email) + ' is already a member' + else + inv.recipient = user + inv.save + @invite_msg = 'Invite sent to ' + ERB::Util.h(openid_or_email) + end + else + invite_sent, @invite_msg = send_invite(inv, openid_or_email) + end + end + + render :partial => 'invite_form' + end + + def invite_decision + inv = Invitation.find_by_id(params[:invite_id]) + group = Group.find_by_id(inv.group_id) + if inv.nil? or group.nil? or inv.recipient_id != logged_in_user_id + flash[:notice] = 'You cannot join that group.' + redirect_to :controller => 'home' + return + end + + if params[:decision] == 'accept' + GroupMembership.create(:group_id => group.id, + :user_id => liuid, + :moderator => inv.group_moderator) + inv.destroy + flash[:notice] = 'Invitation accepted' + + elsif params[:decision] == 'decline' + inv.destroy + flash[:notice] = 'Invitation declined' + + else + raise ArgumentError, "'#{params[:decision]}' is not a valid decision" + end + + redirect_to gurl(group) + end + + def add_yourself + g = Group.find(params[:group_id]) + if g.user_id == liuid or !g.invite_only? + Group.transaction { + GroupMembership.create(:group_id => g.id, :user_id => liuid) + Invitation.delete_all(['group_id = ? AND recipient_id = ?', g.id, liuid]) + } + end + redirect_to gurl(g) + end + + def remove_yourself + g = Group.find(params[:group_id]) + GroupMembership.find_by_group_id_and_user_id(g.id, liuid).destroy + if Group.find_by_id(params[:group_id]) + redirect_to gurl(g) + else + flash[:notice] = "Group deleted due to lack of membership." + redirect_to :action => 'index' + end + end + + def del_member + group = Group.find(params[:group_id]) + user = User.find_by_openid(params[:openid]) + + if user and (group.can_edit?(liu) or user == liu) + if gm = GroupMembership.find_by_group_id_and_user_id(group.id, user.id) + gm.destroy + flash[:notice] = 'User removed.' + else + flash[:notice] = 'That user is not a member of this group.' + end + else + flash[:notice] = 'You may not remove that member from this group.' + end + + redirect_to :action => 'edit', :id => group.id + end + + def del_group + g = Group.find_by_id(params[:group_id]) + + # only owner may delete + if g and g.user == liu + + # XXX: should probably dispatch members letting alerting them + # that the group has been deleted. + g.destroy + flash[:notice] = 'Group deleted' + else + flash[:notice] = 'You cannot do that' + end + + redirect_to :controller => 'home' + end + + def roster + render_text Group.find(params[:id]).all_identifiers.collect {|i| i.value}.join("\n") + end + + # maybe use 200, 400 responses here? + def api_is_member + group = Group.find_by_urlslug(params[:slug]) + user = User.find_by_openid(params[:openid]) + + if group + if group.member?(user) + render :text => 'true', :status => 200 + else + render :text => 'false', :status => 400 + end + else + render :text => 'error: unknown group', :status => 400 + end + end + + def api_roster + group = Group.find_by_urlslug(params[:slug]) + if group + ids = [] + group.users.each {|u| + u.identifiers.each {|i| + ids << i.value + } + } + render :text => ids.join("\n"), :status => 200 + else + render :text => 'error: unknown group', :status => 400 + end + end + + private + + + def group_frag + return '' unless logged_in? + return @group_frag if @group_frag + group_ids = liu.groups.collect {|g|g.id.to_i} + if group_ids.size == 0 + @group_frag = '' + else + @group_frag = " AND groups.id not IN (#{group_ids.join(',')})" + end + return @group_frag + end + + def groups_by_popularity(ops={}) + limit = ops.fetch(:limit,6) + offset = ops.fetch(:offset,0) + invite_only = ops.fetch(:invite_only, false) ? 'true' : 'false' + + the_groups = Group.find_by_sql("SELECT groups.* FROM groups JOIN group_memberships ON groups.id = group_memberships.group_id AND groups.invite_only = #{invite_only} #{group_frag} GROUP BY groups.id ORDER BY COUNT(group_memberships.group_id) DESC LIMIT #{limit} OFFSET #{offset}") + the_count = Group.count_by_sql("SELECT COUNT(DISTINCT groups.id) FROM groups JOIN group_memberships ON groups.id = group_memberships.group_id AND groups.invite_only = #{invite_only} #{group_frag}") + + return the_groups, the_count + end + + def groups_by_contacts(ops={}) + limit = ops.fetch(:limit,6) + offset = ops.fetch(:offset,0) + invite_only = ops.fetch(:invite_only, false) ? 'true' : 'false' + + contact_ids = liu.contacts.collect {|c|c.id} + if contact_ids.size > 0 + + the_groups = Group.find_by_sql("SELECT groups.*, COUNT(group_memberships.user_id) AS contacts_count FROM groups JOIN group_memberships ON groups.id = group_memberships.group_id AND group_memberships.user_id IN (#{contact_ids.join(',')}) AND groups.invite_only = #{invite_only} #{group_frag} GROUP BY group_memberships.group_id ORDER BY COUNT(group_memberships.user_id) DESC LIMIT #{limit} OFFSET #{offset}") + + the_count = Group.count_by_sql("SELECT COUNT(DISTINCT groups.id) FROM groups JOIN group_memberships ON groups.id = group_memberships.group_id AND groups.invite_only = #{invite_only} AND group_memberships.user_id IN (#{contact_ids.join(',')}) #{group_frag}") + + return [the_groups, the_count] + + else + + return [[], 0] + end + + + end + + def groups_by_interest(ops={}) + limit = ops.fetch(:limit,6) + offset = ops.fetch(:offset,0) + invite_only = ops.fetch(:invite_only, false) ? 'true' : 'false' + + tag_ids = liu.tags.collect {|t|t.id} + if tag_ids.size > 0 + + the_groups = Group.find_by_sql("SELECT groups.*, COUNT(taggings.taggable_id) AS interests_count FROM groups JOIN taggings ON taggings.taggable_id = groups.id AND taggings.tag_id IN (#{tag_ids.join(',')}) AND taggings.taggable_type = 'Group' AND groups.invite_only = #{invite_only} #{group_frag} GROUP BY groups.id ORDER BY COUNT(taggings.taggable_id) DESC LIMIT #{limit} OFFSET #{offset}") + + the_count = Group.count_by_sql("SELECT COUNT(DISTINCT groups.id) FROM groups JOIN taggings ON taggings.taggable_id = groups.id AND taggings.tag_id IN (#{tag_ids.join(',')}) AND taggings.taggable_type = 'Group' AND groups.invite_only = #{invite_only} #{group_frag}") + + return [the_groups, the_count] + + else + + + return [[], 0] + end + + end + + def groups_by_recency(ops={}) + limit = ops.fetch(:limit,6) + offset = ops.fetch(:offset,0) + invite_only = ops.fetch(:invite_only, false) ? 'true' : 'false' + + the_groups = Group.find_by_sql("SELECT * FROM groups WHERE groups.invite_only = #{invite_only} #{group_frag} ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}") + + the_count = Group.count_by_sql("SELECT COUNT(*) FROM groups WHERE groups.invite_only = #{invite_only} #{group_frag}") + + return [the_groups, the_count] + end + +end diff --git a/app/controllers/help_controller.rb b/app/controllers/help_controller.rb new file mode 100644 index 0000000..aca478e --- /dev/null +++ b/app/controllers/help_controller.rb @@ -0,0 +1,18 @@ +class HelpController < ApplicationController + + def index + @title = "Help!" + end + + def cred + end + + def cred_step_by_step + @title = "Help: Giving cred, step by step" + end + + def claims_step_by_step + @title = "Help: Making a claim, step by step" + end + +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 0000000..7e84341 --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,198 @@ +class HomeController < ApplicationController + + before_filter :auto_login + before_filter :check_logged_in + before_filter :home_new_counts + + secure_actions :only => [:clear,:clear_activity,:pibbme] + + + def pibbme + liu.toggle_pibbme + flash[:notice] = 'Pibb settings saved.' + redirect_to :action => 'settings' + end + + def index + @group_invites = Invitation.find(:all, :conditions => ['recipient_id = ? AND group_id IS NOT NULL', liuid]) + + @user_events = Dispatch.find_by_sql(["SELECT * FROM dispatches WHERE user_id = ? AND dispatchable_type = 'User' ORDER BY created_at",liuid]) + + @claim_invites = Dispatch.find_by_sql(["SELECT * FROM dispatches WHERE user_id = ? AND reason = 'invite' AND dispatchable_type = 'Claim'", liuid]) + + + num_recent = 10 + num_recent -= 2 unless @claim_invites.empty? + num_recent -= 2 unless @user_events.empty? + num_recent -= 2 unless @group_invites.empty? + + + @your_recent_claims = Claim.find_by_sql(["SELECT * FROM claims WHERE user_id = ? AND state IN (1,4) ORDER BY created_at DESC LIMIT #{num_recent}",liuid]) + activity # get the values from the now-merged controller action + end + + + # remove dispatches from "Home" page for a logged in user + def clear + if params[:t] == 'all' + + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.dispatchable_type = 'Claim')") + + elsif params[:t] == 'page' + + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.dispatchable_type = 'Claim') ORDER BY dispatches.created_at LIMIT 15") + + elsif params[:t] == 'users' + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.dispatchable_type = 'User')") + + elsif (t = params[:t].to_i) != 0 + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.id = #{t})") + else + flash[:notice] = 'Ouch.' + end + + redirect_to :action => 'index' + end + + def settings + @section = 'settings' + end + + def interests + @section = 'interests' + + tag_ids = liu.tags.collect {|t| t.id.to_i} + blocked_user_ids = liu.blocked_user_ids + if blocked_user_ids.size > 0 + block_sql = " AND claims.user_id NOT IN (#{blocked_user_ids.join(',')})" + else + block_sql = "" + end + + @claims = Claim.paginate_by_sql("SELECT DISTINCT claims.* FROM claims JOIN taggings ON claims.id = taggings.taggable_id AND taggings.taggable_type = 'Claim' AND taggings.tag_id IN (#{tag_ids.join(',')}) AND claims.state = 1 AND claims.user_id != #{liuid} AND claims.id NOT IN (SELECT claim_id FROM flaggings WHERE user_id = #{liuid} AND trash = true) #{block_sql} ORDER BY claims.created_at DESC ", :page => params[:page], :per_page => 10) + @claim_count = Claim.count_by_sql("SELECT DISTINCT COUNT(*) FROM claims JOIN taggings ON claims.id = taggings.taggable_id AND taggings.taggable_type = 'Claim' AND taggings.tag_id IN (#{tag_ids.join(',')}) AND claims.state = 1 AND claims.user_id != #{liuid} AND claims.id NOT IN (SELECT claim_id FROM flaggings WHERE user_id = #{liuid} AND trash = true) #{block_sql}") + + end + + def group_claims + @section = 'group_claims' + limit = 10 + + @groups = liu.groups + @group_ids = @groups.collect {|g| g.id.to_i} + + if @groups.size > 0 + @claims = Claim.paginate_by_sql(["SELECT * FROM claims WHERE id NOT IN (SELECT claim_id FROM flaggings WHERE user_id = #{liuid} AND trash = true) AND group_id IN (#{@group_ids.join(',')}) AND state = 4 ORDER BY created_at DESC"], :page => params[:page], :per_page => limit) + @claim_count = Claim.count_by_sql(["SELECT COUNT(*) FROM claims WHERE id NOT IN (SELECT claim_id FROM flaggings WHERE user_id = #{liuid} AND trash = true) AND group_id IN (#{@group_ids.join(',')}) AND state = 4"]) + else + @claims = [] + @claim_count = 0 + end + + claim_group_ids = @claims.collect {|c| c.group_id.to_i}.uniq + @showing_groups = @groups.find_all {|g| claim_group_ids.member?(g.id)} + + # try and suggest some groups if they have none + if @claims.size == 0 + @suggested_groups = [] + if liu.tags.size > 0 + # try and find some groups that match + tag_ids = liu.tags.collect {|t|t.id} + if tag_ids.size > 0 + @suggested_groups = Group.find_by_sql("SELECT DISTINCT groups.* FROM groups JOIN taggings ON taggings.taggable_id = groups.id AND taggings.tag_id IN (#{tag_ids.join(',')}) AND taggings.taggable_type = 'Group' AND groups.invite_only = false ORDER BY groups.created_at ASC LIMIT 15") + end + + else + # no interests, so let's suggest the most popular public groups + @suggested_groups = Group.find_by_sql("SELECT DISTINCT groups.* FROM groups JOIN group_memberships ON groups.id = group_memberships.group_id AND groups.invite_only = false GROUP BY groups.id ORDER BY COUNT(group_memberships.group_id) DESC LIMIT 25") + end + + end + end + + def contact_claims + @section = 'contact_claims' + limit = 10 + + @contacts = Contact.find(:all, + :conditions => ['user_id = ?', liuid], + :include => [:contact]) + + @contact_ids = @contacts.collect {|c| c.contact_id.to_i} + + if @contact_ids.size > 0 # prevent SQL error + @claims = Claim.paginate_by_sql("SELECT claims.* FROM claims WHERE claims.id NOT IN (SELECT claim_id FROM flaggings WHERE user_id = #{liuid} AND trash = true) AND claims.user_id IN (#{@contact_ids.join(',')}) AND claims.state = 1 ORDER BY claims.created_at DESC", :page => params[:page], :per_page => limit) + else + @claims = [] + end + + claim_user_ids = @claims.collect {|c|c.user_id.to_i}.uniq + @showing_contacts = @contacts.find_all {|c| claim_user_ids.member?(c.contact_id)} + end + + # this action has changed to "activity" + def comments + redirect_to :action => 'activity' + end + + def activity + @section = 'activity' + limit = 10 + + @dispatches = Dispatch.paginate_by_sql("SELECT * FROM dispatches WHERE user_id = #{liuid} AND reason IN ('mentioned','inspired','inspired by watched','comment') ORDER BY created_at DESC", :page => params[:page], :per_page => limit) + + # Destroy dispatch if dispatchable is gone, or if related claim + # has been red flagged + @dispatches.reject! {|d| + if !d + @dispatch_count -= 1 + true + + # lazy cleanup for dispatches which cannot be viewed. this is here + # because some old dispatches exist that cause the activity tab to + # break + elsif d.dispatchable.nil? or (d.dispatchable.class == Claim and ![1,4].member?(d.dispatchable.state)) or (d.dispatchable.class == Comment and ![1,4].member?(d.dispatchable.claim.state)) + @dispatch_count -= 1 + d.destroy + true + + else + false + end + } + end + + def clear_activity + limit = 10 + page = params.fetch(:page, 1).to_i + offset = (page * limit) - limit + + @dispatches = Dispatch.find_by_sql("SELECT * FROM dispatches WHERE user_id = #{liuid} AND reason IN ('mentioned','inspired','inspired by watched','comment') ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}").each {|d| d.destroy} + redirect_to :action => 'index' + end + + def drafts + @section = 'drafts' + @draft_claims = Claim.find_all_by_user_id_and_state(liuid, 0) + end + + def clear_drafts + @draft_claims = Claim.find_all_by_user_id_and_state(liuid, 0) + @draft_claims.each{|c|c.destroy} + redirect_to :action => 'drafts' + end + + private + + # this is a before filter on this controller for + def home_new_counts + # check for new + if @action_name != 'activity' + @activity_count = Dispatch.count_by_sql("SELECT COUNT(*) FROM dispatches WHERE user_id = #{liuid} AND reason IN ('mentioned','inspired','inspired by watched','comment')") + else + @activity_count = 0 + end + true + end + +end diff --git a/app/controllers/oauth_controller.rb b/app/controllers/oauth_controller.rb new file mode 100644 index 0000000..6376a80 --- /dev/null +++ b/app/controllers/oauth_controller.rb @@ -0,0 +1,58 @@ +class OauthController < ApplicationController + before_filter :check_logged_in + + def gmail_start + gmail = GMail.new(liu) + callback = url_for(:controller => :oauth, :action => :gmail_authorization) + request_token = gmail.request_token(callback) + session[:gmail_request_token] = request_token.token + session[:gmail_request_secret] = request_token.secret + redirect_to request_token.authorize_url + end + + def gmail_authorization + gmail = GMail.new(liu) + gmail.authorize( + session[:gmail_request_token], + session[:gmail_request_secret], + {:oauth_verifier => params[:oauth_verifier]}) + gmail.store_access_token + flash[:notice] = "GMail authorization stored." + redirect_to :controller => :contacts, :action => :compare_gmail + end + + def gmail_deauthorize + gmail = GMail.new(liu) + gmail.remove_access_token + flash[:notice] = "GMail authorization forgotten." + redirect_to :controller => :contacts, :action => :compare + end + + def twitter_start + twitter = Twitter.new(liu) + callback = url_for(:controller => :oauth, :action => :twitter_authorization) + request_token = twitter.request_token(callback) + session[:twitter_request_token] = request_token.token + session[:twitter_request_secret] = request_token.secret + redirect_to request_token.authorize_url.sub('authorize', 'authenticate') + end + + def twitter_authorization + twitter = Twitter.new(liu) + twitter.authorize( + session[:twitter_request_token], + session[:twitter_request_secret], + :oauth_verifier => params[:oauth_verifier]) + twitter.store_access_token + flash[:notice] = "Twitter authorization stored." + redirect_to :controller => :contacts, :action => :compare + end + + def twitter_deauthorize + twitter = Twitter.new(liu) + twitter.remove_access_token + flash[:notice] = "Twitter authorization forgotten." + redirect_to :controller => :contacts, :action => :compare + end + +end diff --git a/app/controllers/rss_controller.rb b/app/controllers/rss_controller.rb new file mode 100644 index 0000000..92655f1 --- /dev/null +++ b/app/controllers/rss_controller.rb @@ -0,0 +1,133 @@ +class RssController < ApplicationController + layout nil + + # XXX Limit to recent stuff? currently we give EVERYTHING + + def claims_about + if @openid = params[:openid] + @openid = Identifier.normalize(@openid) + + ident = Identifier.find_by_value @openid + if ident + @title = "Claims about #{@openid}" + @claims = ident.claims + else + @claims = [] + @title = "No claims about #{@openid}" + end + elsif @search = params[:search] + begin + @title = "Claims for search '#{@search}'" + @claims = Claim.find_by_contents(@search) + rescue + @title = "Bad search string" + render :template => 'rss/error' + end + else + @title = "This action needs an 'openid' or a 'search'" + render :template => 'rss/error' + end + @claims = [] if @claims.nil? + @link = url_for params + render :template => 'rss/claims' + end + + def claims_by + @user = (User.find_by_id(params[:user_id]) or User.find_by_openid(Identifier.normalize(params[:openid]))) + if @user.nil? + @title = "This action needs a valid 'user_id' or an 'openid' for a jyte user" + render :template => 'rss/error' + return + end + @claims = @user.claims + @title = "Claims by #{@user.nickname}(#{@user.openid})" + @link = url_for params + render :template => 'rss/claims' + end + + def comments_on + @claim = Claim.find_by_id(params[:claim_id]) + if @claim.nil? + @title = "This action needs a valid 'claim_id'" + render :template => 'rss/error' + return + end + @comments = @claim.comments + @title = "Comments on Claim '#{@claim.title}'" + @link = url_for params + render :template => 'rss/comments' + end + + def comments_by + @user = (User.find_by_id(params[:user_id]) or User.find_by_openid(Identifier.normalize(params[:openid]))) + if @user.nil? + @title = "This action needs a valid 'user_id' or an 'openid' for a jyte user" + render :template => 'rss/error' + return + end + @comments = @user.comments + @title = "Comments by #{@user.nickname}(#{@user.openid})" + @link = url_for params + render :template => 'rss/comments' + end + + def votes_on + @claim = Claim.find_by_id(params[:claim_id]) + if @claim.nil? + @title = "This action needs a valid 'claim_id'" + render :template => 'rss/error' + return + end + if params[:include_old] + @votes = @claim.all_votes + @title = "Votes on #{claim.title} including expired ones" + else + @votes = @claim.votes + @title = "Votes on #{claim.title}" + end + @link = url_for params + render :template => 'rss/votes' + end + + def votes_by + @user = (User.find_by_id(params[:user_id]) or User.find_by_openid(Identifier.normalize(params[:openid]))) + if @user.nil? + @title = "This action needs a valid 'user_id' or an 'openid' for a jyte user" + render :template => 'rss/error' + return + end + if params[:include_old] + @votes = @user.votes + @title = "Votes by #{@user.nickname}(#{@user.openid}) including expired ones" + else + @votes = @user.current_votes + @title = "Votes by #{@user.nickname}(#{@user.openid})" + end + @link = url_for params + render :template => 'rss/votes' + end + + def votes_about + @user = (User.find_by_id(params[:user_id]) or User.find_by_openid(Identifier.normalize(params[:openid]))) + if @user.nil? + @title = "This action needs a valid 'user_id' or an 'openid' for a jyte user" + render :template => 'rss/error' + return + end + @link = url_for params + render :template => 'rss/votes' + end + + def dispatches + # XXX provide users with keys for this feed + @user = (User.find_by_id(params[:user_id]) or User.find_by_openid(Identifier.normalize(params[:openid]))) + if @user.nil? + @title = "This action needs a valid 'user_id' or an 'openid' for a jyte user" + render :template => 'rss/error' + return + end + @link = url_for params + render :template => 'rss/dispatches' + end + +end diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb new file mode 100644 index 0000000..6da7b32 --- /dev/null +++ b/app/controllers/site_controller.rb @@ -0,0 +1,105 @@ +class SiteController < ApplicationController + #before_filter :auto_login + before_filter :check_logged_in, :except => [:index, :find_users, :all_claims, :search,:contact,:tos,:api] + + def index + # a featured claim has more than 25 votes. + #@recent_claims = Claim.find_by_sql('SELECT claims.* FROM claims JOIN featured_claims ON claims.id = featured_claims.claim_id AND claims.state = 1 ORDER BY featured_claims.id DESC LIMIT 5') + # top 5 most recent claims with more than 5 votes + @recent_claims = Claim.find(:all, :conditions => ["claim_votes_count > 5"], :limit => 5, + :order => "id desc") + + batch_load_claim_data(@recent_claims.collect {|c| c.id}) + + @norm_cred_scores = {} + @norm_cred_scores[nil] = Cred.scores_for_users @recent_claims.map{|c|c.user_id}, :normalized => true + + @toptags = Tag.find_by_sql("SELECT tags.*, COUNT(taggings.id) AS tagging_count FROM tags JOIN taggings ON taggings.tag_id = tags.id AND taggings.taggable_type = 'Claim' WHERE tags.name != 'jyte' GROUP BY taggings.tag_id ORDER BY tagging_count DESC LIMIT 10") + @three_tag_names = @toptags.sort{|a,b|rand(3)-1}[0..2].map{|t|t.name} + + if logged_in? + @liu_votes = ClaimVote.find_all_votes_hash(liuid, + @recent_claims.collect{|c|c.id}) + end + @rss_links = [rss_claims_url(:order=>'featured',:format=>'rss', + :only_path=>false)] + + headers['x-xrds-location'] = url_for :only_path => false, :controller => 'auth', :action => 'xrds' + end + + def search + @title = "Search" + @search_string = params[:q] + @page = params[:page].to_i + if @page.nil? or @page < 1 + @page = 1 + end + return if @search_string.nil? + offset = ((@page - 1) * 10) + limit_frag = "LIMIT #{offset}, 10" + begin + @claims = Claim.find_by_solr(@search_string, :start => offset, :rows => 10) + @claims.reject!{|c| c.state != 1} + @comments = Comment.find_by_solr(@search_string, :start => offset, :rows => 10) + @users = User.find_by_solr(@search_string, :start => offset, :rows => 10) + rescue + # try scrubbing search string + flash[:notice] = "There was an error with your search string. These results are for a similar query." + @search_string.gsub!(/[\(<\[\]>\)=!^~\?:;\*\+\-\\]/,'') + @search_string.gsub!(/OR|AND|NOT/,'') + @claims = Claim.find_by_solr(@search_string, :start => offset, :rows => 10) + @claims.reject!{|c| c.state != 1} + @comments = Comment.find_by_solr(@search_string, :start => offset, :rows => 10) + @users = User.find_by_solr(@search_string, :start => offset, :rows => 10) + end + claim_ids = @claims.collect {|cl|cl.id} + @comment_claims = @comments.reject {|com|claim_ids.member? com.claim_id}.collect {|com| com.claim}.uniq + + # get the first tag in the query + # XXX multiple tags? + words = @search_string.split(' ').reject{|w|w.size < 3}[0..5] + unless words.empty? + w_frag = '(' << (['?']*words.size).join(',') << ')' + tags = Tag.find_by_sql(["SELECT * FROM tags WHERE name IN #{w_frag}",words].flatten) + end + + unless tags.nil? or tags.empty? + tag_ids = tags.map{|t|t.id} + tag_frag = "(#{tag_ids.join(',')})" + if tags.size == 1 + @tag_names = tags[0].name + else + @tag_names = tags[0..-2].map{|t|t.name}.join(', ') << " or #{tags[-1].name}" + end + scores = Cred.score_class.table_name + @cred_users = User.find_by_sql("SELECT users.*, SUM(scores.value) tag_cred FROM users JOIN #{scores} scores ON scores.user_id = users.id AND scores.tag_id IN #{tag_frag} GROUP BY users.id ORDER BY tag_cred DESC, users.id DESC #{limit_frag}") + @tag_users = User.find_by_sql("SELECT users.*, COUNT(taggings.id) tag_count FROM users JOIN taggings ON taggings.taggable_type = 'User' AND taggings.taggable_id = users.id AND taggings.tag_id IN #{tag_frag} GROUP BY users.id ORDER BY tag_count DESC, users.id DESC #{limit_frag}") + @groups = Group.find_by_sql("SELECT groups.*, COUNT(taggings.id) tag_count FROM groups JOIN taggings ON taggings.taggable_type = 'Group' AND taggings.taggable_id = groups.id AND taggings.tag_id IN #{tag_frag} GROUP BY groups.id ORDER BY tag_count DESC, groups.id DESC #{limit_frag}") + @tag_claims = Claim.find_by_sql("SELECT claims.*, COUNT(taggings.id) tag_count FROM claims JOIN taggings ON taggings.taggable_type = 'Claim' AND taggings.taggable_id = claims.id AND taggings.tag_id IN #{tag_frag} AND claims.state = 1 GROUP BY claims.id ORDER BY tag_count DESC, claims.id DESC #{limit_frag}") + else + @cred_users = @tag_users = @groups = @tag_claims = [] + end + + if logged_in? + claim_ids |= @comments.collect {|c| c.claim_id} + claim_ids |= @tag_claims.collect {|c| c.id} + @liu_votes = ClaimVote.find_all_votes_hash(liuid, claim_ids) + end + end + + + def openid_search + ids = Identifier.find_like(params[:q]) + render_text '
    '+ ids.collect {|i| '
  • '+i.value+'
  • '}.join('') +'
' + end + + def contact + end + + def tos + end + + def api + end + +end diff --git a/app/controllers/spy_controller.rb b/app/controllers/spy_controller.rb new file mode 100644 index 0000000..eb5d37f --- /dev/null +++ b/app/controllers/spy_controller.rb @@ -0,0 +1,90 @@ +class SpyController < ApplicationController + before_filter :auto_login + + def index + @haps = Happening.find_all_since + end + + def update + t = params[:t] + t = 0 if t.nil? + t = t.to_i + + show = [] + if params[:show_claims] + show << 'Claim' + end + if params[:show_comments] + show << 'Comment' + end + if params[:show_votes] + show << 'ClaimVoteHistory' + end + if params[:show_cred] + show << 'Cred' + end + + if show.empty? + @haps = Happening.find_all_since(t) + else + @haps = Happening.find_all_since(t, :show => show) + end + + # determine latest high water mark + t = @haps.empty? ? t : @haps[0].id + + @haps.reverse! + + r = {'t' => t} + r_count = 0 + r_html = '' + + if logged_in? + blocked_user_ids = liu.blocked_user_ids + else + blocked_user_ids = [] + end + + @haps.each {|h| + happenable = h.happenable + + + if happenable.nil? + h.destroy + next + + elsif happenable.class == Claim + if blocked_user_ids.member? happenable.user_id + next + end + + r["u_#{r_count}"] = render_to_string(:partial => 'spy/claim', :locals => {:claim=>happenable}) + + elsif happenable.class == Comment + if blocked_user_ids.member? happenable.user_id + next + end + r["u_#{r_count}"] = render_to_string(:partial => 'spy/comment', :locals=>{:comment=>happenable}) + + elsif happenable.class == ClaimVoteHistory + if blocked_user_ids.member? happenable.user_id + next + end + r["u_#{r_count}"] = render_to_string(:partial => 'spy/vote', :locals => {:vote=>happenable}) + + elsif happenable.class == Cred + r["u_#{r_count}"] = render_to_string(:partial => 'spy/cred', :locals => {:cred=>happenable}) + + else + raise ArgumentError, "Cannot handle happenable of type #{happenable.class}" + end + + r_count += 1 + } + + r['u_count'] = r_count; + + render :text => r.to_json + end + +end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb new file mode 100644 index 0000000..75a47fd --- /dev/null +++ b/app/controllers/user_controller.rb @@ -0,0 +1,803 @@ +class UserController < ApplicationController + + before_filter :auto_login + before_filter :check_logged_in, + :except => [:profile,:api_user_info, :tag, :profile_about_claims, + :profile_about_claims, :profile_by_claims, :profile_votes, + :profile_comments, :profile_out_cred, :profile_in_cred, :profile_contacts, + :quick_find] + secure_actions :only => [:account_submit,:give_cred_submit,:delete_icon, + :del_identifier,:set_primary,:settings, + :delete_account_submit,:delete_account_final, + :delete_account_confirm, :ignore] + + def home + redirect_to :controller => 'home' + end + + # remove dispatches from "Home" page for a logged in user + def clear + if params[:t] == 'all' + + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.dispatchable_type = 'Claim')") + + elsif params[:t] == 'page' + + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.dispatchable_type = 'Claim') ORDER BY dispatches.created_at LIMIT 15") + + elsif (t = params[:t].to_i) != 0 + Dispatch.connection.execute("DELETE FROM dispatches WHERE (dispatches.user_id = #{liuid} AND dispatches.id = #{t})") + else + flash[:notice] = 'Ouch.' + end + + redirect_to :action => 'home' + end + + def profile_about_claims + render :text => p_about_claims(params[:user_id]) + end + + def profile_by_claims + render :text => p_by_claims(params[:user_id]) + end + + def profile_comments + render :text => p_comments(params[:user_id]) + end + + def profile_votes + render :text => p_votes(params[:user_id]) + end + + def profile_in_cred + render :text => p_in_cred(params[:user_id]) + end + + def profile_out_cred + render :text => p_out_cred(params[:user_id]) + end + + def profile_contacts + render :text => p_contacts(params[:user_id]) + end + + def profile + if params[:uid] + @user = User.find(params[:uid]) + @openid = 'o' + else + @openid = Identifier.detect(params[:openid]) + + ident = Identifier.find_by_value(@openid) + if ident.nil? or ident.user.nil? + flash[:notice] = "That user doesn't have a profile yet." + redirect_to find_claims_url(:about => params[:openid]) + return + end + + @user = ident.user + end + + if @user.get_state == :suspended + flash[:notice] = 'That user account has been suspended.' + redirect_to front_url + return + end + + if @user.get_state == :deleted + redirect_to front_url + return + end + + ims = headers['if-modified-since'] + if ims and DateTime.parse(ims) > @user.last_login_at + render :text => '', :status => 304 + return + end + + @title = "Profile for #{@user.dn}" + + @profile_section = params[:show] + sections = ["about_claims", "in_cred", "by_claims", "comments", "votes", "out_cred", "contacts"] + @profile_section = "by_claims" unless sections.member? @profile_section + + # for "best qualities" + unless @profile_section == "in_cred" + @overall_score, @tagged_scores = Cred.scores(:user => @user, :limit => 6) + @norm_cred_scores = Cred.scores_by_tag_and_user(@tagged_scores.keys, [@user.id], :normalized => true) + @in_tags = Tag.find_all_by_id(@tagged_scores.keys) + @in_tags.sort! {|a, b|(@tagged_scores[b.id] or 0) <=> (@tagged_scores[a.id] or 0)} + end + + # for the give cred widget + if liu + if @cred_from_liu = Cred.find_by_source_id_and_sink_id(liuid, @user.id) + @cred_from_liu_tags = @cred_from_liu.tag_list + else + @cred_from_liu_tags = '' + end + end + + @non_primary_identifiers = @user.identifiers.reject {|i| i.primary == true} + @interests = @user.tags + @groups = @user.groups + fmt = @user.created_at.year == DateTime.now.year ? '%B %d' : '%B %d, %Y' + @user_since = @user.created_at.strftime(fmt) + + @claim_tags = Tag.find_by_sql(["select tags.*, count(*) tag_count from tags join taggings on taggings.tag_id = tags.id join claims on taggings.taggable_id = claims.id and taggings.taggable_type = 'Claim' and claims.user_id = ? group by tags.id order by tag_count desc limit 23", @user.id]) + + @claim_count = Claim.count(:conditions => ['user_id = ? AND state = 1', @user.id]) + @comment_count = Comment.count(:conditions => ['user_id = ?', @user.id]) + @agree_count = ClaimVote.count(:conditions => ['user_id = ? AND vote = true', @user.id]) + @disagree_count = ClaimVote.count(:conditions => ['user_id = ? AND vote = false', @user.id]) + @flipflop_count = ClaimVoteHistory.count(:conditions => ['user_id = ?', @user.id]) - (@agree_count + @disagree_count) + + @ignoring = !UserBlocking.find_by_user_id_and_blocked_user_id(liuid, @user.id).nil? #rails2.3 + + @ignores_count = UserBlocking.count(:conditions => ['user_id = ?', @user.id]) + @ignored_count = UserBlocking.count(:conditions => ['blocked_user_id = ?', @user.id]) + + # microid + microids = @non_primary_identifiers.collect {|i| i.value} + microids << @openid + @microids = microids.collect {|i| + proto = is_iname?(i) ? 'xri' : i.split(':')[0] + "#{proto}+http:sha1:"+Digest::SHA1.hexdigest(Digest::SHA1.hexdigest(i)+Digest::SHA1.hexdigest(xprofile_url(@user.s)))} + + # rss links + @rss_about_link = rss_claims_url(:about => @user.s, :only_path=>false) + @rss_by_link = rss_claims_url(:by => @user.s, :only_path=>false) + @rss_links = [@rss_by_link,@rss_about_link] + + case @profile_section + when "by_claims" + @history_box_content = p_by_claims(@user.id) + when "about_claims" + @history_box_content = p_about_claims(@user.id) + when "votes" + @history_box_content = p_votes(@user.id) + when "comments" + @history_box_content = p_comments(@user.id) + when "in_cred" + @history_box_content = p_in_cred(@user.id) + when "out_cred" + @history_box_content = p_out_cred(@user.id) + when "contacts" + @history_box_content = p_contacts(@user.id) + end + end + + def all_votes + claim = Claim.find_by_urlslug(params[:urlslug]) + @votes, @voters, @scores_by_user_id = claim.votes_with_users_and_scores + end + + def rank + @page = params[:page].to_i + params[:by] ||= "claims" + if @page.nil? or @page < 1 + @page = 1 + end + offset = ((@page - 1) * 23) + limit_frag = "LIMIT #{offset}, 23" + @tag = Tag.find_by_name(params[:tag]) + @tag_name = @tag.name if @tag + + if @tag + cred_tag_join = " JOIN taggings ON taggable_type = 'Cred' + AND taggable_id = creds.id + AND tag_id = #{@tag.id}" + claim_tag_join = " JOIN taggings ON taggable_type = 'Claim' + AND taggable_id = claims.id + AND tag_id = #{@tag.id}" + elsif !params[:tag].blank? + @title = "Tag \"#{params[:tag]}\" has not been used yet." + @users = [] + return + end + + @sort = params[:by] + if @sort == "creds_given" + sql = "SELECT users.id, users.nickname, COUNT(creds.id) cnt FROM users + JOIN creds ON users.id = creds.source_id" + if @tag + sql << cred_tag_join + @title = "People who've given #{@tag} cred to the most people" + else + @title = "People who've given cred to the most people" + end + @cnt_title = "people" + elsif @sort == "creds_gotten" + sql = "SELECT users.id, users.nickname, COUNT(creds.id) cnt FROM users + JOIN creds ON users.id = creds.sink_id" + if @tag + sql << cred_tag_join + @title = "People who've gotten #{@tag} cred from the most people" + else + @title = "People who've gotten cred from the most people" + end + @cnt_title = "people" + elsif @sort == "contacted" + sql = "SELECT users.id, users.nickname, COUNT(*) cnt FROM users + JOIN contacts ON users.id = contacts.contact_id" + if @tag + sql << " JOIN taggings ON taggable_type = 'Contact' + AND taggable_id = contacts.id + AND tag_id = #{@tag.id}" + @title = "People called a contact (#{@tag}) by the most people" + else + @title = "People called a contact by the most people" + end + @cnt_title = "people" + elsif @sort == "agreeability" + sql = "SELECT users.id, users.nickname, SUM(CASE WHEN claim_votes.vote = (claims.yeas < claims.nays) THEN -1 ELSE 1 END) cnt FROM claim_votes + JOIN users ON claim_votes.user_id = users.id + JOIN claims ON claim_votes.claim_id = claims.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People who agree with the majority most often about #{@tag}" + else + @title = "People who agree with the majority most often" + end + @cnt_title = "votes in majority less votes in minority" + elsif @sort == "personal_agreeability" + sql = "SELECT users.id, users.nickname, SUM(CASE WHEN sv.vote = ov.vote THEN 1 ELSE -1 END) cnt FROM claim_votes ov + JOIN users ON ov.user_id = users.id AND users.id <> #{liuid} + JOIN claim_votes sv ON sv.user_id = #{liuid} + AND sv.claim_id = ov.claim_id" + if @tag + sql << " JOIN taggings ON taggable_type = 'Claim' + AND taggable_id = sv.claim_id + AND taggable_id = ov.claim_id + AND tag_id = #{@tag.id}" + @title = "People who agree with you most about #{@tag}" + else + @title = "People who agree with you most" + end + @cnt_title = "assent less dissent" + elsif @sort == "personal_disagreeability" + sql = "SELECT users.id, users.nickname, SUM(CASE WHEN sv.vote = ov.vote THEN -1 ELSE 1 END) cnt FROM claim_votes ov + JOIN users ON ov.user_id = users.id AND users.id <> #{liuid} + JOIN claim_votes sv ON sv.user_id = #{liuid} + AND sv.claim_id = ov.claim_id" + if @tag + sql << " JOIN taggings ON taggable_type = 'Claim' + AND taggable_id = sv.claim_id + AND taggable_id = ov.claim_id + AND tag_id = #{@tag.id}" + @title = "People who disagree with you most about #{@tag}" + else + @title = "People who disagree with you most" + end + @cnt_title = "dissent less assent" + elsif @sort == "comments" + sql = "SELECT users.id, users.nickname, COUNT(comments.id) cnt FROM users + JOIN comments ON comments.user_id = users.id" + if @tag + sql << " JOIN taggings ON taggable_type = 'Claim' + AND taggable_id = comments.claim_id + AND tag_id = #{@tag.id}" + @title = "People who have commented most on claims about #{@tag}" + else + @title = "People who have commented most" + end + @cnt_title = "comments" + elsif @sort == "claims" + sql = "SELECT users.id, users.nickname, COUNT(claims.id) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People who've made the most claims about #{@tag}" + else + @title = "People who've made the most claims" + end + @cnt_title = "claims" + elsif @sort == "votes" + sql = "SELECT users.id, users.nickname, COUNT(claim_votes.id) cnt FROM users + JOIN claim_votes ON claim_votes.user_id = users.id" + if @tag + sql << " JOIN taggings ON taggable_type = 'Claim' + AND taggable_id = claim_votes.claim_id + AND tag_id = #{@tag.id}" + @title = "People who have voted most on claims about #{@tag}" + else + @title = "People who have voted on the most claims" + end + @cnt_title = "votes" + elsif @sort == "votes_on_claims" + sql = "SELECT users.id, users.nickname, SUM(claims.yeas + claims.nays) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People who've made the most voted claims about #{@tag}" + else + @title = "People who've made the most voted claims" + end + @cnt_title = "votes" + elsif @sort == "votes_per_claim" + sql = "SELECT users.id, users.nickname, SUM(claims.yeas + claims.nays - 1)/COUNT(*) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People with the most votes per claim about #{@tag}" + else + @title = "People with the most votes per claim" + end + @cnt_title = "votes (not including claimant's)" + elsif @sort == "watched_claims" + sql = "SELECT users.id, users.nickname, COUNT(*) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1 + JOIN flaggings ON flaggings.claim_id = claims.id + AND flaggings.watch = true" + if @tag + sql << claim_tag_join + @title = "People whose claims about #{@tag} are most watched" + else + @title = "People whose claims are most watched" + end + @cnt_title = "eyeballs" + elsif @sort == "trashed_claims" + sql = "SELECT users.id, users.nickname, COUNT(*) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1 + JOIN flaggings ON flaggings.claim_id = claims.id + AND flaggings.trash = true" + if @tag + sql << claim_tag_join + @title = "People whose claims about #{@tag} are most trashed" + else + @title = "People whose claims are most trashed" + end + @cnt_title = "trashcans" + elsif @sort == "nays_on_claims" + sql = "SELECT users.id, users.nickname, SUM(claims.nays) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People who've made the most disagreed with claims about #{@tag}" + else + @title = "People who've made the most disagreed with claims" + end + @cnt_title = "disagree votes" + elsif @sort == "yeas_on_claims" + sql = "SELECT users.id, users.nickname, SUM(claims.yeas) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People who've made the most agreed with claims about #{@tag}" + else + @title = "People who've made the most agreed with claims" + end + @cnt_title = "agree votes" + elsif @sort == "contested_claims" + sql = "SELECT users.id, users.nickname, + SUM(CASE WHEN claims.yeas > claims.nays THEN claims.nays ELSE claims.yeas END) cnt + FROM users + JOIN claims ON claims.user_id = users.id" + if @tag + sql << claim_tag_join + @title = "People who've made the most contested claims about #{@tag}" + else + @title = "People who've made the most contested claims" + end + @cnt_title = "total votes in minority" + elsif @sort == "discussed_claims" + sql = "SELECT users.id, users.nickname, SUM(claims.comments_count) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1" + if @tag + sql << claim_tag_join + @title = "People who've made the most discussed claims about #{@tag}" + else + @title = "People who've made the most discussed claims" + end + @cnt_title = "comments" + elsif @sort == "your_votes_on_claims" + sql = "SELECT users.id, users.nickname, COUNT(claim_votes.id) cnt FROM users + JOIN claims ON claims.user_id = users.id + AND claims.state = 1 + JOIN claim_votes ON claim_votes.user_id = #{liuid} + AND claim_votes.claim_id = claims.id" + if @tag + sql << claim_tag_join + @title = "People who've made the most claims about #{@tag} that you've voted on" + else + @title = "People who've made the most claims that you've voted on" + end + @cnt_title = "claims" + elsif @sort == "cred_score" + st = Cred.score_table_name + sql = "SELECT users.id, users.nickname, scores.value cnt FROM users + JOIN #{st} scores ON scores.user_id = users.id" + if @tag + sql << " AND scores.tag_id = #{@tag.id}" + @title = "People with most #{@tag} cred" + else + @title = "People with most overall cred" + sql << " AND scores.tag_id IS NULL" + end + @cnt_title = "score" + end + if sql + sql << " GROUP BY users.id ORDER BY cnt DESC " << limit_frag + @users = User.find_by_sql(sql) + else + @users = [] + end + + end + + def quick_find + offset = (params[:page].to_i - 1) * 20 + offset = 0 if offset < 0 + @search_string = params[:uq] + begin + @users = User.find_by_solr(@search_string, :start => offset, :rows => 20) + rescue + # try scrubbing search string + @search_string.gsub!(/[\(<\[\]>\)=!^~\?:;\*\+\-\\]/,'') + @search_string.gsub!(/OR|AND|NOT/,'') + @users = User.find_by_solr(@search_string, :start => offset, :rows => 20) + end + render :partial => '/user_find_results', :locals => {:users => @users} + end + + def account + @user = logged_in_user + end + + def account_submit + u = logged_in_user + u.update_attributes(params[:user]) + u.tag_with(params[:tags]) + + old_username = nil + dn = params[:display_name].strip + if dn and !dn.empty? + if u.nickname.nil? + u.nickname = dn + elsif u.nickname != dn + old_username = OldUsername.new(:user_id => u.id, :name => u.nickname) + u.nickname = dn + end + else + if u.nickname + OldUsername.create(:user_id => u.id, :name => u.nickname) + end + u.nickname = nil + end + + u.save + u.solr_save + + # process image + image_blob = read_blob(params[:image]) + + if image_blob + # destroy old image is necessary + old_image = u.image + if old_image + u.imagings.destroy_all + old_image.destroy_image(u) + end + + begin + im = Image.from_blob(image_blob, u) + im.on(u) + rescue + flash[:notice] = "Sorry, we couldn't read that image. Try another." + redirect_to :action => 'account' + return + end + end + + if u.valid? + if old_username + old_username.save + end + flash[:notice] = 'Profile saved.' + redirect_to xprofile_url(u.s) + else + @user = u + render :template => 'user/account' + end + end + + def delete_icon + u = logged_in_user + i = u.image + if i + u.imagings.destroy_all + i.destroy_image(u) + end + + redirect_to :action => 'account' + end + + def set_primary + Identifier.transaction { + i = Identifier.find(params[:primary_id]) + raise 'doh' unless liu.identifiers.member?(i) + cur = liu.identifier + cur.primary = false + i.primary = true + i.save + cur.save + } + redirect_to :action => 'account' + end + + def del_identifier + i = Identifier.find(params[:id]) + raise 'doh' if i.user != liu + raise 'cannot delete primary identifer' if i.primary + i.user = nil + i.save + redirect_to :action => 'account' + end + + def confirm_email + rc = EmailResponseCode.find_by_code(params[:code]) + if rc.nil? # or rc.expired? + flash[:notice] = "Sorry, couldn't find your confirmation code. Please check the URL and try again." + redirect_to :controller => 'home' + return + end + u = liu + u.email = rc.email + u.save + flash[:notice] = "You have successfully confirmed #{rc.email} as your email address." + rc.destroy + redirect_to :controller => 'home' + end + + + def give_cred + @user = User.find_by_openid(params[:openid]) + unless @user + flash[:notice] = 'Unknown user.' + redirect_to :controller => '' + return + end + + @overall_cred, @tagged_cred = Cred.scores(:user_id => @user.id) + if liu + if @cred_from_liu = Cred.find_by_source_id_and_sink_id(liuid, @user.id) + @cred_from_liu_tags = @cred_from_liu.tags.collect {|t| t.name}.join(' ') + else + @cred_from_liu_tags = '' + end + end + end + + + def give_cred_submit + user = User.find(params[:user_id]) + + if user == liu + flash[:notice] = "Cannot give yourself cred." + redirect_to xprofile_url(user.s) + return + end + + if params[:cred] and not params[:cred].strip.empty? + cred = Cred.find_or_create_by_source_id_and_sink_id(liuid, user.id) + old_tags = cred.tags.map{|t|t.name} + if params[:cred].split(',').size > 200 + flash[:notice] = 'You are limited to giving 200 kinds of cred per person.' + else + cred.tag_with(params[:cred]) + new_tags = Tag.parse(params[:cred]) + flash[:notice] = 'Cred given. Scores will be updated shortly!' + Happening.create(:happenable => cred) + + added_tags = new_tags - old_tags + removed_tags = old_tags - new_tags + if added_tags.empty? + note = "" + else + note = "gave you #{oxford_comma_list(added_tags)} cred" + end + unless removed_tags.empty? + note << " and " unless note.empty? + note << "took back #{oxford_comma_list(removed_tags)} cred" + end + Dispatch.create(:user => user, :dispatchable => liu,:reason => note) unless note.empty? + end + elsif params[:add] + t = Tag.find_or_create_by_name(params[:add]) + cred = Cred.find_or_create_by_source_id_and_sink_id(liuid, user.id) + Tagging.create(:taggable => cred, :tag_id => t.id) + elsif params[:remove] + t = Tag.find_by_name(params[:add]) + cred = Cred.find_by_source_id_and_sink_id(liuid, user.id) + if t and cred + ting = Tagging.find_by_tag_id_and_taggable_type_and_taggable_id(t.id, 'Cred', cred.id) + ting.destroy if ting + cred.destroy if cred.tags.empty? + end + elsif params[:remove_all] + cred = Cred.find_by_source_id_and_sink_id(liuid, user.id) + cred.destroy if cred + elsif cred + cred.destroy + flash[:notice] = "Cred taken back." + end + + if params[:render] == 'out_cred' + @out_users, @out_tags, @out_tags_by_user_id, @out_users_by_tag_id = liu.out_cred_with_extras + render :partial => 'out_cred', :locals => {:user => liu, :out_users => @out_users, :out_tags_by_user_id => @out_tags_by_user_id} + elsif params[:render] == 'in_cred' + + else + redirect_to xprofile_url(user.s) + end + end + + def ignore + if add = params[:add] + u = User.find_by_id(add) + if u + UserBlocking.find_or_create_by_user_id_and_blocked_user_id(liuid, u.id) + render :partial => 'ignoring', :locals => {:user => u, :ignoring => true} + return + else + # return an error code XXX + render :status => 500, :text => "Couldn't find that user." + end + elsif remove = params[:remove] + u = User.find_by_id(remove) + if ub = UserBlocking.find_or_create_by_user_id_and_blocked_user_id(liuid, u.id) + ub.destroy + render :partial => 'ignoring', :locals => {:user => u, :ignoring => false} + return + else + # return an error code XXX + render :status => 500, :text => "You were not ignoring that person." + end + end + end + + def settings + @ignored_users = liu.blocked_users + end + + def delete_account_submit + session[:delete_account] = true + redirect_to :action => 'delete_account_confirm' + end + + def delete_account_confirm + end + + def delete_account_final + if session[:delete_account] == true + session[:delete_account] = nil + u = liu + u.set_state(:deleted) + u.save! + flash[:notice] = 'Account deleted' + redirect_to front_url + end + end + +# def XXXdelete_account_final +# if session[:delete_account] == true +# session[:delete_account] = nil +# ActiveRecord::Base.transaction { +# u = liu +# +# # unset identifier as primary +# pi = u.identifier +# pi.primary = false +# pi.save! + + # delete all imges +# u.images.each {|i| +# i.destroy_image(u) +# } + +# u.destroy +# session[:user_id] = nil +# } +# flash[:notice] = 'Account deleted.' +# redirect_to front_url +# else +# redirect_to :action => 'settings' +# end +# end + + include UserHelper + def api_user_info + user = User.find_by_openid(params[:openid]) + unless user + render :text => "error: no user for that openid\n", :status => 400 + return + end + + stuff = user_api_info(user) + + render :text => OpenID::Util.kvform(stuff), :status => 200 + end + + def tag + @by = by = params.fetch(:by, :interest).to_sym + + @tag = params[:tag] + if @tag.nil? + flash[:notice] = "Need a tag to show that page." + redirect_to front_url + return + end + + # set page title + if by == :interest + @title = 'Users interested in ' + @tag + elsif by == :cred + @title = 'Users with ' + @tag + ' cred' + elsif by == :gave_cred + @title = 'Users who have given ' + @tag + ' cred' + else + flash[:notice] = "Don't know how to do that." + redirect_to front_url + return + end + + per_page = 30 + @page = params.fetch(:page, 1).to_i + offset = (@page - 1) * per_page + t = Tag.find_by_name(@tag) + + if t.nil? + @user_count = 0 + @users = [] + + elsif by == :interest + @user_count = User.count_by_sql(["SELECT COUNT(*) FROM users JOIN taggings ON users.id = taggings.taggable_id AND taggings.taggable_type = 'User' AND taggings.tag_id = ?", t.id]) + + @users = User.find_by_sql(["SELECT DISTINCT users.* FROM users JOIN taggings ON users.id = taggings.taggable_id AND taggings.taggable_type = 'User' AND taggings.tag_id = ? ORDER BY users.created_at DESC LIMIT #{per_page} OFFSET #{offset}", t.id]) + + elsif by == :cred + score_table = Cred.score_class.table_name + @user_count = User.count_by_sql("SELECT COUNT(*) FROM users JOIN #{score_table} scores ON scores.user_id = users.id AND scores.tag_id = #{t.id}") + + @users = User.find_by_sql("SELECT users.* FROM users JOIN #{score_table} scores ON scores.user_id = users.id AND scores.tag_id = #{t.id} ORDER BY scores.value DESC LIMIT #{per_page} OFFSET #{offset}") + + elsif by == :gave_cred + @user_count = User.count_by_sql("SELECT COUNT(*) FROM users WHERE users.id IN (SELECT creds.source_id FROM creds JOIN taggings ON taggings.taggable_type = 'Cred' AND taggings.tag_id = #{t.id} AND taggings.taggable_id = creds.id)") + + @users = User.find_by_sql("SELECT * FROM users WHERE users.id IN (SELECT creds.source_id FROM creds JOIN taggings ON taggings.taggable_type = 'Cred' AND taggings.tag_id = #{t.id} AND taggings.taggable_id = creds.id) ORDER BY users.created_at DESC LIMIT #{per_page} OFFSET #{offset}") + + end + @last_page_number = (@user_count / per_page)+1 + + if @user_count > 0 + user_ids = @users.collect {|u| u.id} + @cred_scores = Cred.scores_for_users(user_ids, :tag_id => t.id, :normalized => true) + else + @cred_scores = {} + end + + # find similar tags + if t + @similar_tags = Tag.find_neighbors(t.id,'User',:limit=>7,:min_count=>5) if by == :interest + @similar_tags = Tag.find_neighbors(t.id,'Cred',:limit=>7,:min_count=>5) if by == :cred + end + + end + + def widgets + end + + def claimroll_setup + end + +end diff --git a/app/controllers/widget_controller.rb b/app/controllers/widget_controller.rb new file mode 100644 index 0000000..712c425 --- /dev/null +++ b/app/controllers/widget_controller.rb @@ -0,0 +1,142 @@ +class WidgetController < ApplicationController + + layout nil + + def claim + @claim = Claim.find_by_urlslug(params[:urlslug]) + if @claim.nil? + render :text => 'Unknown claim' + return + end + end + + include UserHelper + include ActionView::Helpers::JavaScriptHelper + def user_badge + @user = User.find_by_openid(params[:openid]) + if @user + @stuff = user_api_info(@user) + else + @stuff = {} + end + + @badge_html = render_to_string(:template => 'widget/user_badge_html') + @badge_html = escape_javascript(@badge_html) + + headers['Content-Type'] = 'text/javascript' + render :text => "document.writeln('#{@badge_html}');" + return + end + + def tagclaimroll + @rolltype = 'tag' + t = params[:tags] + if t.nil? or t.length == 0 + tags = [] + tagnames = [] + else + tagnames = Tag.parse(t) + tags = Tag.find_all_by_name(tagnames) + end + + @tagnames = tagnames.join(', ') + + if tags.length > 0 + count = [[1,params.fetch(:count, 5).to_i].max,25].min + tag_ids = tags.map {|t|t.id} + if params[:set] == 'all' + @claims = Claim.find_by_sql(["SELECT claims.* FROM claims JOIN taggings ON taggings.taggable_type = 'Claim' AND taggings.taggable_id = claims.id AND taggings.tag_id in (#{tag_ids.join(',')}) GROUP BY claims.id HAVING COUNT(taggings.id) = #{tag_ids.size} ORDER BY claims.created_at DESC LIMIT ?",count]) + else + @claims = Claim.find_by_sql(["SELECT claims.* FROM claims JOIN taggings ON taggings.taggable_type = 'Claim' AND taggings.taggable_id = claims.id AND taggings.tag_id IN (#{tag_ids.join(', ')}) WHERE claims.state = 1 ORDER BY claims.created_at DESC LIMIT ?", count]) + + end + @title = "Recent claims tagged #{params[:tags]}" + batch_load_claim_data(@claims.collect {|c| c.id}) + else + @claims = [] + @title = "No claims found" + end + + widget_html = render_to_string(:template => 'widget/claim_roll') + + if params[:preview] == 'y' + render :text => widget_html + else + escaped_widget_html = escape_javascript(widget_html) + headers['Content-Type'] = 'text/javascript' + render :text => "document.writeln('#{escaped_widget_html}');" + end + end + + def claimroll + @rolltype = 'user' + @user = User.find_by_openid(params[:openid]) + + # ensures a min of 1, max of 25, with a no-value of 5 + count = [[1,params.fetch(:count, 5).to_i].max,25].min + + case params[:type] + when 'by' + @claims = Claim.find(:all, + :limit => count, + :conditions => ['state = 1 AND user_id = ?',@user.id], + :order => 'created_at DESC') + + @title = "Claims by #{@user.dn}" + + when 'about' + @claims = Claim.find_by_sql(["SELECT claims.* FROM claims JOIN mentioned_identifiers ON (claims.id = mentioned_identifiers.claim_id AND mentioned_identifiers.identifier_id = ?) WHERE claims.state = 1 ORDER BY claims.created_at DESC LIMIT ?",@user.identifier.id, count]) + batch_load_claim_data(@claims.collect {|c| c.id}) + @title = "Claims about #{@user.dn}" + + when 'voted' + @claims = Claim.find(:all, + :limit => count, + :conditions => ['claims.state = 1'], + :joins => ["JOIN claim_votes byvotes ON byvotes.claim_id = claims.id AND byvotes.user_id = #{@user.id}"], + :order => 'byvotes.created_at DESC', + :include => :identifiers) + @title = "Claims recently voted on by #{@user.dn}" + + when 'watched' + @claims = Claim.find(:all, + :limit => count, + :conditions => ['claims.state = 1'], + :joins => ["JOIN flaggings on flaggings.claim_id = claims.id AND flaggings.watch = true AND flaggings.user_id = #{@user.id}"], + :order => 'flaggings.created_at DESC', + :include => :identifiers) + @title = "Claims #{@user.dn} is watching" + + when 'commented' + @claims = Claim.find(:all, + :limit => count, + :conditions => ['claims.state = 1'], + :joins => ["JOIN comments bycomments ON bycomments.claim_id = claims.id AND bycomments.user_id = #{@user.id}"], + :order => 'bycomments.created_at DESC', + :include => :identifiers) + + @title = "Claims recently commented on by #{@user.dn}" + else + @claims = [] + @title = "Jyte error" + end + + unless params[:no_show_vote] + @votes = ClaimVote.find_all_votes_hash(@user.id,@claims.map{|c|c.id}) + else + @votes = {} + end + + widget_html = render_to_string(:template => 'widget/claim_roll') + + if params[:preview] == 'y' + render :text => widget_html + else + escaped_widget_html = escape_javascript(widget_html) + headers['Content-Type'] = 'text/javascript' + render :text => "document.writeln('#{escaped_widget_html}');" + end + + end + +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 0000000..1c5d122 --- /dev/null +++ b/app/helpers/application_helper.rb @@ -0,0 +1,673 @@ +# Methods added to this helper will be available to all templates in the application. +module ApplicationHelper + + def logged_in? + session[:user_id] + end + alias logged_in_user_id logged_in? + alias liuid logged_in? + + def logged_in_user + return nil unless logged_in? + return @logged_in_user if @logged_in_user + return @logged_in_user = User.find_by_id(session[:user_id]) + end + alias liu logged_in_user + + def ensure_tag_cache_init + unless @tags_by_id + @tags_by_id = {} + @tags_by_name = {} + end + end + + def find_tag(options) + ensure_tag_cache_init + if tag_id = options[:tag_id] + if @tags_by_id[tag_id] + return @tags_by_id[tag_id] + else + tag = Tag.find_by_id(tag_id) + end + elsif tag_name = options[:tag_name] + if @tags_by_name[tag_name] + return @tags_by_name[tag_name] + else + tag = Tag.find_by_name(tag_name) + end + else + tag = options[:tag] + end + @tags_by_name[tag.name] = tag + @tags_by_id[tag.id] = tag + return tag + end + + def ensure_user_cache_init + unless @users_by_id + @users_by_id = {} + @users_by_openid = {} + end + end + + def claim_link(c) + link_to(render_claim_title(c,false), + :controller => 'claim', + :action => 'show', + :urlslug => c.urlslug) + end + + # cache user objects on a per-request basis + def find_user(options) + ensure_user_cache_init + if user = options[:user] + @users_by_id[user.id] = user + return user + elsif user_id = (options[:id] or options[:user_id]) + if @users_by_id[user_id] + return @users_by_id[user_id] + else + user = User.find_by_id(user_id) + @users_by_id[user_id] = user + return user + end + elsif openid = options[:openid] + if @users_by_openid[openid] + return @users_by_openid[openid] + else + user = User.find_by_openid(openid) + if user + @users_by_openid[openid] = user + @users_by_id[user.id] = user + end + return user + end + end + end + + # openid links to page about that user + def user_link(options, html_options = {}) + user = find_user options + if user.nil? + raise unless openid = options[:openid] + else + openid = user.openid + @users_by_openid[openid] = user + end + + html_options = {:title => openid}.merge(html_options) + + if options[:nolink] == openid or options[:nolink] == true + return span_tag(user_display(options), html_options) + else + # the line below was commented out because we need to escape + # slashes in the user profile url. this is now done in xprofile_url + #return link_to(user_display(options), {:controller => 'user', :action => 'profile', :openid => denormalize_url(openid)}, html_options) + + dopenid = denormalize_url(openid) + if dopenid.index('/').nil? + return link_to(user_display(options), + xprofile_url(openid), + html_options) + else + return link_to(user_display(options), + url_for(:controller=>'user',:action=>'profile',:uid=>user.id.to_s,:only_path=>false), + html_options) + end + end + end + + def xprofile_url(openid, options={}) + o = { + :controller => 'user', + :action => 'profile', + :openid => denormalize_url(openid), + :only_path => false + } + + options.update(o) + + return url_for(options).gsub(/%2f/i, '/') + end + + def profile_url(args) + raise ArgumentError, 'use xprofile_url instead' + end + + # user nickname or denormalized openid + def user_display(options) + user = find_user options + if user.nil? + raise unless options[:openid] + result = h(denormalize_url(options[:openid])) + else + result = user.display_name + end + + if options[:truncate] + result = truncate(result, options[:truncate]) + end + + return result + end + + def denormalize_url(url) + no_proto = url.sub(/^http:\/\//, '') + de_slash = no_proto.sub(/^([^\/]+)\/$/, '\1') + return de_slash + end + + def format_datetime(dt) + return dt.strftime("%c") + end + + def random_greek_letter + # Poke fun at web 2.0, the perpetual beta. + # Also at flickr, now in 'gamma' whatever that means + greek_letters = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', + 'eta', 'theta', 'iota', 'kappa', 'lambda', 'mu', 'nu', + 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', + 'upsilon', 'phi', 'chi', 'psi', 'omega'] + return greek_letters[Time.now.yday%24] + end + + def ie6? + if @ie6.nil? + ua = request.env['HTTP_USER_AGENT'] + if ua and ua.downcase.index('msie 6') + @ie6 = true + else + @ie6 = false + end + end + return @ie6 + end + + def ie_overlay_style + if ie6? + return "filter: alpha(opacity=50);" + else + return "" + end + end + + def ie_image_tag(source, options = {}) + ie_sucks_monkey_balls = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{image_path(source)}');" + style = ie_sucks_monkey_balls + (options[:style] or "") + options[:style] = style + image_tag('blank.gif', options) + end + + # work around ie6 for png transparency + def t_image_tag(source, options = {}) + if ie6? + ie_image_tag(source, options) + else + image_tag(source, options) + end + end + + def image_url(image, size) + if RAILS_ENV == 'production' + return '/static/'+image.url_fragment(size) + else + return '/jimages/'+image.url_fragment(size) + end + end + + def oxford_comma_list(words, conjunction = "and") + if words.size == 1 + s = words[0] + elsif words.size == 2 + s = words[0] + " #{conjunction} " + words[1] + else + s = words[0..-2].join(', ') + ", #{conjunction} " + words[-1] + end + s + end + + # XXX probably we should be using a separate html_options hash + def icon_image(options={}) + user = find_user options + + options.delete(:user_id) + options.delete(:user) + + if user + openid = user.openid + elsif options[:openid] + openid = options[:openid] + end + options.delete(:openid) + + size = options[:size] == 'favicon' ? 'favicon' : 'icon' + size = options.fetch(:size, 'icon') + + if @user_icons + im = @user_icons[user.id] + else + im = user.image + end + + if user and im + url = image_url(im, size) + #url = url_for_file_column(user, :image, size) + return url if options[:url] + else + if options[:size] == 'favicon' + url = image_path("buddyiconfav.jpg") + elsif size == 'big' + url = image_path("no_icon_big.png") + else + url = image_path('no_icon_thumb.png') + end + return url if options[:url] + end + + options[:alt] = openid + options[:title] = openid + + linked = options.delete(:linked) + + img_html = image_tag(url, options) + + if linked == false + return img_html + elsif linked.kind_of? String + return "#{img_html}" + else + return "#{img_html}" + end + end + + # XXX: ugh, the user and group icon rendering stuff should be merged at some + # point, i think. + def group_icon(options) + group = options.delete(:group) + raise ArgumentError unless group + + size = options.fetch(:size, 'thumb') + + if group.image + url = image_url(group.image, size) + else + if size == 'favicon' + url = image_path("buddyiconfav.jpg") + elsif size == 'big' + url = image_path('no_icon_big.png') + else + url = image_path('no_icon_thumb.png') + end + end + + return url if options[:url] + + options[:alt] = h(group.name) + options[:title] = h(group.name) + + img_html = image_tag(url, options) + if options[:linked] == false + return img_html + else + return link_to(img_html, gurl(group)) + end + + end + + def render_claim_title(claim, fancy = true) + unless @simple_claim_titles + @simple_claim_titles = {} + @fancy_claim_titles = {} + end + if fancy and @fancy_claim_titles[claim.id] + return @fancy_claim_titles[claim.id] + end + if not fancy and @simple_claim_titles[claim.id] + return @simple_claim_titles[claim.id] + end + + if @identifiers_by_claim_id + identifiers = @identifiers_by_claim_id[claim.id] + else + identifiers = claim.identifiers + end + + names = identifiers.collect {|i| + if @users_by_user_id and i.user_id + u = @users_by_user_id[i.user_id.to_i] + else + u = find_user(:id => i.user_id) + end + + if u + if fancy + user_link({:user => u}, + {:style => 'text-decoration:none', + :title => i.value, + :onmouseover => "this.style.textDecoration='underline'", + :onmouseout => "this.style.textDecoration='none'"}) + else + u.dn + end + else + s = strip_tags(i.shorten) + if fancy + link_to s, find_claims_url(:about => i.shorten), :title => "Find more claims about #{s}" + else + s + end + end + } + claim.parsed % names + end + + def gmapjsurl + # XXX: this could be smarter + url = url_for :controller => '', :only_path => false + uri = URI.parse(url) + # why are the &s escaped in this url? + gurl = 'http://maps.google.com/maps?file=api&v=2&key=' + + key = GMAP_KEYS[uri.host] + unless key + raise ArgumentError, 'what the gmap key for this url? '+url + end + + return gurl + key + end + + def params_for_url(url) + path = url.split('/')[(3..-1)] + ActionController::Routing::Routes.recognize_path(path) + end + + def display_cred(cred) + # Re adjusted so that the scores don't get too messed up by the cred algorithm + (cred * 80.0).round/10.0 + end + + # XXX: this needs to escape javascript + def contact_rel_link(t) + et = escape_javascript(t) + '%s' % [et,et,h(t)] + end + + def gurl(group) + group_url :urlslug => group.urlslug + end + + def cw_classes(yeas, nays) + if yeas == nays + return ['even_value', 'even_value'] + end + + class_names = ['lowest','low','high','higher','highest'] + + if yeas > nays + percent = nays.to_f / yeas + else + percent = yeas.to_f / nays + end + + green = class_names[((1 - percent) * class_names.length-1).to_i] + pink = class_names[(percent * class_names.length-1).to_i] + + return [green+'_green_value', pink+'_pink_value'] + end + + def cw_class(c) + return "even_value" if c.yeas == c.nays + class_names = ["highest_pink_value", "higher_pink_value", "high_pink_value", "low_pink_value", "lowest_pink_value", "lowest_green_value", "low_green_value", "high_green_value", "higher_green_value", "highest_green_value"] + percent = c.yeas.to_f / (c.yeas + c.nays) + return class_names[(percent * (class_names.length - 1)).round] + end + + def user_allowed_links?(user_id) + cred_n(user_id).to_i > 2 + end + + def cred_n(user_id, options = {}) + return nil unless user_id + tag = options[:tag] + if tag + tag_id = options[:tag].id + else + tag_id = options[:tag_id] + end + scores = @norm_cred_scores + if scores and scores[tag_id] + score = scores[tag_id][user_id] + else + p "falling back to query for single score", @title + score = Cred.score(:user_id => user_id, :normalized => true, :tag_id => tag_id) + end + if score.nil? + return nil + end + + return cred_n_for_score(score) + end + + def cred_n_for_score(score) + # To hell with precision, let's get more people with big dots. + if score > 0.4 + if score > 0.6 + if score > 0.9 + return 10 + else + if score > 0.75 + return 9 + else + return 8 + end + end + else + if score > 0.5 + return 7 + else + return 6 + end + end + else + if score > 0.2 + if score > 0.3 + return 5 + else + return 4 + end + else + if score > 0.13 + return 3 + else + if score > 0.07 + return 2 + else + if score > 0.0 + return 1 + else + return 0 + end + end + end + end + end + end + + def cred_img_for_score(score) + if score.nil? + return t_image_tag("dots/no_score.png", :class => 'inline', :size => '11x11') + end + n = cred_n_for_score(score) + return t_image_tag("dots/#{n}.png", :class => 'inline', :size => '11x11') + end + + def cred_img(user_id, options = {}) + n = cred_n(user_id, options) + if n.nil? + return t_image_tag("dots/no_score.png", :class => 'inline', :size => '11x11') + else + return t_image_tag("dots/#{n}.png", :class => 'inline', :size => '11x11') + end + end + + def cred_class_for_score(score) + return "no_cred_color" if score.nil? + n = cred_n_for_score(score) + return "cred_color#{n}" + end + + def cred_class(user_id, options = {}) + n = cred_n(user_id, options) + if n.nil? + return "no_cred_color" + end + return "cred_color#{n}" + end + + def cred_dot_class(user_id, options = {}) + n = cred_n(user_id, tag) + if n.nil? + return "no_cred_dot" + end + return "cred_dot#{n}" + end + + def sign_up_url + if RAILS_ENV == 'production' + return 'https://www.myopenid.com/affiliate_signup?affiliate_id=119' + else + return 'https://www.myopenid.com/affiliate_signup?affiliate_id=1' + end + end + + def safe_formatted(s, allow_links=false) + #s = jsanitize(auto_link(s,:all,:target=>'_blank')) + if allow_links + s = auto_link(jsanitize(s, true), :all, {:target=>'_blank'}) + else + s = auto_link(jsanitize(s), :all, {:target=>'_blank', :rel => 'nofollow'}) + end + return s.gsub(/\r\n?/, "\n"). + gsub(/\n\n+/, "

\n\n

"). + gsub(/([^\n]\n)(?=[^\n])/, '\1
') + end + + VERBOTEN_ATTRS = /^(o|on|style|class|a)/ + VERBOTEN_VIDEO_ATTRS = /^(o|on|class)/ + TAG_WHITELIST = %(a ul ol li b strong i br blockquote img em p embed object param pre tt) + EMBED_SRC_WHITELIST = /^http:\/\/(www\.youtube\.com|video\.google\.com)/ + + # this is a modified version of TextHelper.sanitize + def jsanitize(html, allow_links = false) + return '' if html.nil? + # only do this if absolutely necessary + if html.index("<") + tokenizer = HTML::Tokenizer.new(html) + new_text = "" + + while token = tokenizer.next + node = HTML::Node.parse(nil, 0, 0, token, false) + x = case node + when HTML::Tag + unless TAG_WHITELIST.include?(node.name) + node.to_s.gsub(/ 'claim',:action=>'find'} + end + + if sep.nil? + sep = ', ' + end + + tag_names = tags.collect {|t| t.name} + tag_names.sort! if sort + tag_names.collect{|n| + tag_name = h(n).gsub(' ', ' ') + p = params.dup + p[:tag] = n + link_to(tag_name, p) + }.join(sep) + end + + def is_admin? + if logged_in? and [:janrain,:jyte_team].member?(liu.get_state) + return true + end + return false + end + + def claim_has_image?(claim) + return @claim_has_image.member?(claim.id) if @claim_has_image + return claim.image + end + + def html_ops_for_voter(claim, user) + if @voter_labels.has_key?(user.id) + html_ops = {:title=> @voter_labels[user.id], + :style=>'font-weight: bold;'} + else + html_ops = {:title => user.openid} + end + return html_ops + + end + + def display_tab(text) + return "

#{text}
" + end + +end + diff --git a/app/helpers/auth_helper.rb b/app/helpers/auth_helper.rb new file mode 100644 index 0000000..31afc3a --- /dev/null +++ b/app/helpers/auth_helper.rb @@ -0,0 +1,2 @@ +module AuthHelper +end diff --git a/app/helpers/claim_helper.rb b/app/helpers/claim_helper.rb new file mode 100644 index 0000000..91a0b4c --- /dev/null +++ b/app/helpers/claim_helper.rb @@ -0,0 +1,2 @@ +module ClaimHelper +end diff --git a/app/helpers/group_helper.rb b/app/helpers/group_helper.rb new file mode 100644 index 0000000..b82886e --- /dev/null +++ b/app/helpers/group_helper.rb @@ -0,0 +1,2 @@ +module GroupHelper +end diff --git a/app/helpers/oauth_helper.rb b/app/helpers/oauth_helper.rb new file mode 100644 index 0000000..010cf9f --- /dev/null +++ b/app/helpers/oauth_helper.rb @@ -0,0 +1,2 @@ +module OauthHelper +end diff --git a/app/helpers/rss_helper.rb b/app/helpers/rss_helper.rb new file mode 100644 index 0000000..9031e55 --- /dev/null +++ b/app/helpers/rss_helper.rb @@ -0,0 +1,2 @@ +module RssHelper +end diff --git a/app/helpers/site_helper.rb b/app/helpers/site_helper.rb new file mode 100644 index 0000000..c879486 --- /dev/null +++ b/app/helpers/site_helper.rb @@ -0,0 +1,2 @@ +module SiteHelper +end diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb new file mode 100644 index 0000000..987e88b --- /dev/null +++ b/app/helpers/user_helper.rb @@ -0,0 +1,132 @@ +module UserHelper + + def user_api_info(user) + stuff = {} + stuff['nickname'] = user.dn + stuff['primary_openid'] = user.s + stuff['incoming_cred_count'] = Cred.count(:conditions => ['sink_id = ?',user.id]).to_s + stuff['votes_count'] = ClaimVote.count(:conditions => ['user_id = ?',user.id]) + stuff['claims_count'] = Claim.count(:conditions => ['user_id = ? AND state = 1', user.id]) + stuff['comments_count'] = Comment.count(:conditions => ['user_id = ? ', user.id]) + + if user.image + stuff['thumb_icon_url'] = front_url.chomp('/') + image_url(user.image,'thumb') + stuff['big_icon_url'] = front_url.chomp('/') + image_url(user.image,'big') + end + + # cred + overall_score, tag_scores = Cred.scores(:user => user) + if overall_score + stuff['cred_total'] = display_cred(overall_score) + else + stuff['cred_total'] = '0.0' + end + + if tag_scores.length > 0 + top = tag_scores.collect {|tid,s| [s,tid]} + top.sort! + top = top[-5..-1] + top_tag_ids = top.collect {|s,tid| tid} + + tags = Tag.find(:all, :conditions => "id IN (#{top_tag_ids.join(',')})") + tags_by_id = tags.hash_by(:id) + stuff['top_cred_tags'] = top.collect {|s,tid| [tags_by_id[tid].name, s]}.reverse + else + stuff['top_cred_tags'] = [] + end + + return stuff + end + + def p_about_claims(user_id) + @user = User.find_by_id(user_id) + @claims = @user.claims_about :limit => 10 + claim_ids = @claims.map{|c|c.id} + @liu_votes = ClaimVote.find_all_votes_hash(liuid, claim_ids) + batch_load_claim_data(claim_ids) + @headline = "Claims about #{@user.dn}" + if @claims.size == 10 + @more_url = url_for :controller => 'claim', :action => 'find', :about => @user.s, :page => 2 + end + render_to_string :partial => 'claims' + end + + def p_by_claims(user_id) + @user = User.find(user_id) + @claims = @user.recent_claims :limit => 10 + claim_ids = @claims.map{|c|c.id} + @liu_votes = ClaimVote.find_all_votes_hash(liuid, claim_ids) + batch_load_claim_data(claim_ids) + @headline = "Claims by #{@user.dn}" + if @claims.size == 10 + @more_url = url_for :controller => 'claim', :action => 'find', :by => @user.s, :page => 2 + end + render_to_string :partial => 'claims' + end + + def p_comments(user_id) + @user = User.find(user_id) + @claims = @user.claims_commented :limit => 10 + claim_ids = @claims.map{|c|c.id} + @liu_votes = ClaimVote.find_all_votes_hash(liuid, claim_ids) + batch_load_claim_data(claim_ids) + @headline = "Claims with comments from #{@user.dn}" + if @claims.size == 10 + @more_url = url_for :controller => 'claim', :action => 'find', :comments_by => @user.s, :page => 2 + end + render_to_string :partial => 'claims' + end + + def p_votes(user_id) + @user = User.find(user_id) + @voted_claims = @user.claims_voted :limit => 10 + claim_ids = @voted_claims.map{|c|c.id} + @liu_votes = ClaimVote.find_all_votes_hash(liuid, claim_ids) + batch_load_claim_data(claim_ids) + render_to_string :partial => 'claim_votes' + end + + def p_in_cred(user_id) + @user = User.find(user_id) + + @in_users, @in_tags, @in_tags_by_user_id, @in_users_by_tag_id = @user.in_cred_with_extras + @overall_score, @tagged_scores = Cred.scores(:user => @user) + @in_tags.sort! {|a, b|(@tagged_scores[b.id] or 0) <=> (@tagged_scores[a.id] or 0)} + user_ids = @in_users.map{|u|u.id} << @user.id + tag_ids = @in_tags.map{|t|t.id} + @norm_cred_scores = Cred.scores_by_tag_and_user(tag_ids, user_ids, :normalized => true) + render_to_string :partial => 'in_cred' + end + + def p_out_cred(user_id) + @user = User.find(user_id) + @out_users, @out_tags, @out_tags_by_user_id, @out_users_by_tag_id = @user.out_cred_with_extras + + uids = @out_users.map{|u|u.id} << @user.id + @user_icons = Image.for_users(uids) + render_to_string :partial => 'out_cred', :locals => {:out_users => @out_users, :out_tags => @out_tags, :out_tags_by_user_id => @out_tags_by_user_id, :out_users_by_tag_id => @out_users_by_tag_id, :user => @user} + end + + def p_contacts(user_id) + @user = User.find(user_id) + + @contacts = Contact.find(:all, + :conditions => ['user_id = ?', @user.id], + :include => [:contact]) + + # don't include blocked users as contact of + blocked = @user.blocked_user_ids + if blocked.size > 0 + blocked_sql = " AND user_id NOT IN (#{blocked.join(',')})" + else + blocked_sql = "" + end + + @contact_of_count = Contact.count_by_sql(["SELECT COUNT(*) FROM contacts WHERE contact_id = ? #{blocked_sql}", @user.id]) + + render_to_string :partial => 'contacts', :locals => {:contacts => @contacts, + :contact_of_count => @contact_of_count} + + end + +end diff --git a/app/models/authorization_token.rb b/app/models/authorization_token.rb new file mode 100644 index 0000000..861e2b5 --- /dev/null +++ b/app/models/authorization_token.rb @@ -0,0 +1,3 @@ +class AuthorizationToken < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/claim.rb b/app/models/claim.rb new file mode 100644 index 0000000..7347ee5 --- /dev/null +++ b/app/models/claim.rb @@ -0,0 +1,650 @@ +class Claim < ActiveRecord::Base + belongs_to :user + + has_many :votes, :class_name => 'ClaimVote', :dependent => :destroy + has_many :all_votes, :class_name => 'ClaimVoteHistory', :dependent => :destroy + + has_many :yea_votes, :class_name => 'ClaimVote', :conditions => 'vote = true' + has_many :nay_votes, :class_name => 'ClaimVote', :conditions => 'vote = false' + + has_many :comments, :dependent => :destroy, :order => 'id' + has_many :root_comments, :class_name => 'Comment', :conditions => 'parent_id IS NULL' + + belongs_to :group + + has_many :mentioned_identifiers, :dependent => :destroy, :order => 'mentioned_identifiers.order' + has_many :identifiers, :through => :mentioned_identifiers + + # resources + has_many :resourceables, :as => :resourceable, :dependent => :destroy + has_many :resources, :through => :resourceables + + has_one :featured_claim, :dependent => :destroy + + has_many :imagings, :as => :imagable, :dependent => :destroy + has_many :images, :through => :imagings + + has_many :claimings, :dependent => :destroy + + has_many :watchings, :class_name => 'Flagging', :conditions => 'watch = true' + has_many :watchers, :through => :watchings, :source => :user + + has_many(:viewings, + :class_name => 'Look', + :conditions => ["object_type = 'User'"], + :foreign_key => 'object_id', + :dependent => :destroy) + has_many :viewers, :through => :viewings, :source => :user + + validates_associated :user + validates_presence_of :original + validates_uniqueness_of :urlslug + + acts_as_taggable + acts_as_solr :fields => [:original] + + attr_accessor :contact_msgs + + # claim.votes_by_group(5) or claim.votes_by_group :group => group + def votes_by_group(options = {}) + group = Group.find_by_id(options[:group_id]) unless group = options[:group] + # XXX trap nil group? + user_conditions = "user_id IN (" << group.all_users.map {|u| "#{u.id}"}.join(",") << ")" + conditions = "claim_id = #{self.id}" + yes_count = ClaimVote.count(:conditions => "(#{user_conditions}) AND #{conditions} AND vote = true") + no_count = ClaimVote.count(:conditions => "(#{user_conditions}) AND #{conditions} AND vote = false") + return [yes_count, no_count] + end + + def voter_groups + # get a flat list of groups for each user + gl = voters.inject([]) {|memo,u|memo + u.groups} + gh = {} + # count the appearances of each group into a hash + gl.each{|g| gh[g.id] ? gh[g.id] += 1 : gh[g.id] = 1} + # sort by number of appearances, collect the group ids, and return the groups + gh.to_a.sort{|a,b| a[1] <=> b[1]}.collect {|a| a[0]}.collect{|gid|Group.find(gid)} + end + + def voters_by_contacts_of_mentioned(options={}) + # find mentioned users + unless mentioned_user_ids = options[:mentioned_user_ids] + mentioned_user_ids = identifiers.select {|i| i.user_id}.map {|i| i.user_id} + end + + contact_voters = [] + + if (ex_user_ids = options[:exclude_ids]) and ex_user_ids.size > 0 + exclude_frag = "AND claim_votes.user_id NOT IN (#{ex_user_ids.join(',')})" + else + exclude_frag = '' + end + + if mentioned_user_ids.size > 0 + # if claim is about someone, first try to find votes by people + # she has added as a contact + contact_voters += User.find_by_sql([ + "SELECT users.id, users.nickname + FROM users + JOIN contacts + ON contacts.user_id IN (#{mentioned_user_ids.join(',')}) + AND users.id = contacts.contact_id + JOIN claim_votes + ON claim_votes.user_id = contacts.contact_id + AND claim_votes.user_id = users.id + AND claim_votes.claim_id = #{self.id} + AND claim_votes.vote = ? + #{exclude_frag} + GROUP BY users.id LIMIT ?", + options[:vote], options[:limit]]) + + + #contact_votes += ClaimVote.find_by_sql([ + # "SELECT claim_votes.* FROM claim_votes, contacts + # WHERE contacts.user_id IN (#{mentioned_user_ids.join(',')}) + # AND claim_votes.user_id = contacts.contact_id + # AND claim_votes.claim_id = #{self.id} + # AND claim_votes.vote = ? + # #{exclude_frag} + # LIMIT ?", + # options[:vote], options[:limit]]) + end + + return contact_voters + end + + def root_inspiring_claim + claim_ids = [self.id] + claim = self + + # find root claim + begin + claimings = claim.claimings + claim_ids += claimings.map{|c| + cid = nil + if c.claimable_type == 'Comment' + cid = c.claimable.claim_id + elsif c.claimable_type == 'Claim' + cid = c.claimable_id + end + if claim_ids.member? cid + return nil + end + cid + } + claim = Claim.find(claim_ids[-1]) + end while( claimings.size > 0 ) + return claim + end + + def inspired_by(x) + claimings.create :claimable => x + end + + def inspired_by_comments + x = [] + claimings.each {|l| x << l.claimable if l.claimable_type == 'Comment'} + return x if x.length + end + + def inspired_by_claims + x = [] + claimings.each {|l| x << l.claimable if l.claimable_type == 'Claim' and l.claimable and l.claimable.state = 1} + return x + end + + def inspired_claims + Claim.find_by_sql(['SELECT claims.* FROM claims JOIN claimings ON claims.id = claimings.claim_id AND claimings.claimable_id = ? AND claimings.claimable_type = ? AND claims.state = 1',self.id, 'Claim']) + end + + def inspired_claims_from_comments + Claim.find_by_sql(['SELECT claims.* FROM claims JOIN comments ON comments.claim_id = ? JOIN claimings ON claims.id = claimings.claim_id AND claimings.claimable_id = comments.id AND claimings.claimable_type = "Comment" AND claims.state = 1 ',self.id]) + end + + def votes_with_users_and_scores(options = {}) + ActiveRecord::Base.transaction { + st = Cred.score_table_name + tag_ids_frag = '(' + tags.map{|t|t.id}.join(',') + ')' + sql = "SELECT claim_votes.*, + users.id user_id, users.nickname user_nickname, + SUM(CASE scores.value IS NULL 0 ELSE scores.value) score + FROM claim_votes + JOIN users ON votes.user_id = users.id + AND votes.claim_id = #{self.id} + LEFT JOIN #{st} scores ON scores.user_id = users.id + AND scores.tag_id IN #{tag_ids_frag} + GROUP BY users.id + ORDER BY score DESC, id" + if options[:limit] + lim = options[:limit].to_i + if options[:offset] + off = options[:offset].to_i + sql << " LIMIT #{off}, #{lim}" + else + sql << " LIMIT #{lim}" + end + end + votes = [] + users_by_id = {} + user_ids = [] + Claim.connection.select_all(sql).each {|r| + votes << ClaimVote.new(:vote => r['vote'], :user_id => r['user_id'], :claim_id => self.id) + user_ids << r['user_id'] + user = User.new(:nickname => r['user_nickname']) + user.id = r['user_id'] + users_by_id[r['user_id']] = user + scores_by_user_id[r['user_id']] = r['score'] + } + } + return yea_voters, nay_voters, scores_by_user_id + end + + def voters(options = {}) + if (lim = options[:limit].to_i) > 0 + limit_frag = "LIMIT #{lim}" + else + limit_frag = "" + end + unless options[:vote].nil? + if options[:vote] + vote_frag = "AND claim_votes.vote = true" + else + vote_frag = "AND claim_votes.vote = false" + end + end + if exclude_user_id = options[:exclude] + exclude_frag = "WHERE users.id <> #{exclude_user_id.to_i}" + elsif (exclude_user_ids = options[:exclude_ids]) and exclude_user_ids.size > 0 + exclude_frag = "WHERE users.id NOT IN (#{exclude_user_ids.join(',')})" + else + exclude_frag = "" + end + + # Active record can be a pain sometimes + tag_ids = options[:tag_ids] + if tag_ids.nil? + tag_ids = Claim.connection.select_values("SELECT tag_id FROM taggings WHERE taggable_type = 'Claim' AND taggable_id = #{self.id}") + end + + if tag_ids.empty? + sql = "SELECT users.id, users.nickname + FROM users + JOIN claim_votes ON claim_votes.user_id = users.id + AND claim_votes.claim_id = #{self.id} + #{vote_frag} + #{exclude_frag} + GROUP BY users.id + ORDER BY claim_votes.id DESC #{limit_frag}" + voters = User.find_by_sql(sql) + else + tag_ids_frag = '(' + tag_ids.join(',') + ')' + st = Cred.score_table_name + sql = "SELECT users.id, users.nickname, + SUM(scores.value) score + FROM users + JOIN claim_votes ON claim_votes.user_id = users.id + AND claim_votes.claim_id = #{self.id} + #{vote_frag} + JOIN #{st} scores ON scores.user_id = users.id + AND scores.tag_id IN #{tag_ids_frag} + #{exclude_frag} + GROUP BY users.id + ORDER BY score DESC #{limit_frag}" + voters = User.find_by_sql(sql) + if lim > 0 and voters.size < lim + lim -= voters.size + limit_frag = "LIMIT #{lim}" + if voters.size > 0 + voters_frag = "AND users.id NOT IN (#{voters.map{|u|u.id}.join(',')})" + else + voters_frag = "" + end + + sql = "SELECT users.id, users.nickname + FROM users + JOIN claim_votes ON claim_votes.user_id = users.id + AND claim_votes.claim_id = #{self.id} + #{voters_frag} + #{vote_frag} + #{exclude_frag} + GROUP BY users.id + ORDER BY claim_votes.id DESC #{limit_frag}" + voters += User.find_by_sql(sql) + end + end + return voters + end + + def yea_voters(options = {}) + voters(options.update({:vote => true})) + end + + def nay_voters(options = {}) + voters(options.update({:vote => false})) + end + + def validate_on_create + parse + end + + def validate_on_update + old = Claim.find(self.id) + unless original == old.original + if state > 0 + errors.add(:text, "You may not change the text after publishing a claim.") + else + parse + end + end + end + + def after_save + if @idents + MentionedIdentifier.find(:all, :conditions => ['claim_id = ?',self.id]).each {|mi| mi.destroy} + @idents.each_with_index { |identifier,index| + # add mention of this identifier for this claim + MentionedIdentifier.create(:claim_id => self.id, + :identifier_id => identifier.id, + :order => index) + } + end + FeaturedClaim.check_claim_hook(self) + end + + def parse + idents = [] + parsed = '' + slug = '' + self.original = self.original.sub(/^ +/,"").sub(/ +$/,"") + tokens = self.original.split(/[ ]+/) + tokens.each {|t| + t = t.sub(/^(['"])/, '') + prepunc = $~[1] if $~ + t = t.sub(/([,;])$/, '') + postpunc = $~[1] if $~ + t = t.gsub('%','%').gsub('<','<') + if id_s = Identifier.detect(t) # is t an identifier string? + idents << id_s + parsed << "#{prepunc}%s#{postpunc} " + slug << Identifier.shorten(id_s) << "-" + else + if t.size > 40 + errors.add(:text, "No word in a claim may be longer than 40 characters - it screws up the formatting.") + end + parsed << "#{prepunc}#{t}#{postpunc} " + slug << t << "-" + end + } + self.parsed = parsed.strip + + STOP_WORDS.each {|sw| + if parsed.downcase.index(sw) + errors.add(:text, "Please rephrase without the strong language.") + end + } + + oslug = slug = slug.downcase.gsub(/[^-.\w]/,'').chomp("-").chomp(".") + i = 2; + while Claim.find_by_urlslug(slug) + slug = "#{oslug}-#{i}" + i += 1 + end + self.urlslug = slug + self.digest = self.calculate_digest + + if idents.size > 5 + # XXX allow making claims about groups + errors.add(:text, "Please limit yourself to 5 OpenIDs per claim.") + end + + @idents = idents.collect {|i| + identifier = Identifier.find_by_value(i) + unless identifier + # create identifiers that don't yet exist + identifier = Identifier.create(:value => i) + end + identifier + } + # the same code from normalized_identifiers + # but we can't use it because this claim hasn't got an id yet + # and the mentioned_identifers entries haven't yet been created + norm_idents = @idents.collect {|i| + if i.user + i.user.identifier + else + i + end + } + + Claim.find_all_by_digest(self.digest, :conditions => 'state > 0').each {|c| + if c.normalized_identifiers == norm_idents + errors.add(:same, "#{c.urlslug}") + end + } + + end + + def publish + if self.group_id + self.state = 4 # group published + else + self.state = 1 # public published + end + self.created_at = DateTime.now + + # Make sure there aren't any rogue votes due to retraction + ClaimVote.connection.execute("DELETE FROM claim_votes WHERE claim_votes.claim_id = #{self.id}") + + # hacking around race condition: claim shown before vote created. + self.yeas = 1 + self.nays = 0 + self.save! + + ClaimVote.create(:claim_id => self.id, :user_id => self.user_id, :vote => true) + + # notify mentioned users + mentioned_users.each {|u| + u.dispatch(self, :reason => 'mentioned') unless u == self.user + } + # user who made the comment that inspired this claim gets notified, + # and watchers of that claim + inspired_by_comments.each {|c| u = c.user + u.dispatch(self, :reason => 'inspired') unless u == self.user + c.claim.watchers.each {|u| + u.dispatch(self, :reason => 'inspired by watched') unless u == self.user + } + c.user.dispatch(self, :reason => 'inspired') unless c.user_id == self.user_id + } + # user who made the claim that inspired this claim gets notified, + # and watchers of that claim as well + inspired_by_claims.each {|c| u = c.user + u.dispatch(self, :reason => 'inspired') unless u == self.user + c.watchers.each {|u| + u.dispatch(self, :reason => 'inspired by watched') unless u == self.user + } + } + + # This is one happening claim. + unless self.group_id + Happening.create(:happenable => self) + self.solr_save + end + + end + + def flag(color) + if color == :green + self.state = 1 + elsif color == :yellow + self.state = 2 + elsif color == :red + self.state = 3 + else + raise "unknown flag type" + end + save! + end + + extend ActionView::Helpers::SanitizeHelper::ClassMethods + include ActionView::Helpers::SanitizeHelper + def title + # XXX at most 6 queries. Ideally we'd get this to 2 or 1 + parsed % identifiers.map{|i| u = i.user if i.user_id + if u + u.dn + else + strip_tags(i.shorten) + end + } + end + + def text + parsed % (identifiers.map{|i| strip_tags(i.shorten)}) + end + + def normalized_identifiers + self.identifiers.collect {|i| + if i.user + i.user.identifier + else + i + end + } + end + + def mentioned_peoples_identifiers + self.identifiers.inject([]) {|ids,i| + if i.user + ids += i.user.identifiers + else + ids += [i] + end + } + end + + def mentioned_users + User.find_by_sql("SELECT users.* FROM users JOIN identifiers ON user_id = users.id JOIN mentioned_identifiers ON identifier_id = identifiers.id AND claim_id = #{self.id}") + end + + def mentions?(openid) + self.mentioned_peoples_identifiers.collect {|i| i.value}.member?(openid) + end + + def mentions_user?(user) + self.mentioned_users.member?(user) + end + alias is_about mentions_user? + + def calculate_digest + require 'digest/sha1' + text = self.parsed.downcase.gsub(' +',' ').gsub(/[^\w\n]/,' ').strip + return Digest::SHA1.hexdigest(text) + end + + def location_resources + resources.inject([]) {|p,r| r.class == LocationResource ? p << r : p} + end + + def url_resources + resources.inject([]) {|p,r| r.class == UrlResource ? p << r : p} + end + + def image_resources + resources.inject([]) {|p,r| r.class == ImageResource ? p << r : p} + end + def widget_resources + resources.inject([]) {|p,r| r.class == WidgetResource ? p << r : p} + end + def time_resources + resources.inject([]) {|p,r| r.class == TimeResource ? p << r : p} + end + + def prepare_comments + comment_hash = {} + root_comment_ids = [] + comment_children = {} + self.comments.each {|c| + comment_hash[c.id] = c + if c.parent_id + if comment_children[c.parent_id] + comment_children[c.parent_id] << c.id + else + comment_children[c.parent_id] = [c.id] + end + else + root_comment_ids << c.id + end + c.child_ids = [] + } + comment_children.each {|i,l| + comment_hash[i].child_ids = l + } + return comment_hash, root_comment_ids + end + + def flag_by(user) + if user.class == User + user_id = user.id + else + user_id = user + end + Flagging.find_by_user_id_and_claim_id(user_id, self.id) + end + + def watched_by(user) + if user.class == User + user_id = user.id + else + user_id = user + end + f = Flagging.find_by_user_id_and_claim_id(user_id, self.id) + return false unless f + return f.watch + end + + def trashed_by(user) + if user.class == User + user_id = user.id + else + user_id = user + end + f = Flagging.find_by_user_id_and_claim_id(user_id, self.id) + return false unless f + return f.trash + end + + # use :since => DateTime + # or :comment_conditions => where_fragment_string + # to restrict comments considered + # use :conditions or :claim_conditions with a where fragment to restrict + # claims returned + def self.find_discussed(options = {}) + + if options[:limit] + limit_frag = "LIMIT #{options[:limit]}" + else + limit_frag = "" + end + + since = options[:since] + if since + qds = ActiveRecord::Base.connection.quoted_date(since) + comment_conditions = "created_at > '#{qds}'" + else + comment_conditions = options[:comment_conditions] + end + + if comment_conditions and not comment_conditions.empty? + comment_conditions_frag = "WHERE #{comment_conditions}" + else + comment_conditions_frag = "" + end + + claim_conditions = options[:conditions] or options[:claim_conditions] + if claim_conditions.nil? or claim_conditions.empty? + claim_conditions_frag = "" + else + claim_conditions_frag = "AND #{claim_conditions}" + end + + # XXX: Post-postgres migration, limit should go inside subquery + Claim.find_by_sql("SELECT claims.*, counts.comment_count FROM claims JOIN (SELECT claim_id, COUNT(*) as comment_count FROM comments #{comment_conditions_frag} GROUP BY claim_id) counts ON claims.id = counts.claim_id WHERE claims.state = 1 #{claim_conditions_frag} #{limit_frag}") + end + + def self.find_contested(options = {}) + if options[:limit] + limit_frag = "LIMIT #{options[:limit]}" + else + limit_frag = "" + end + + # XXX: Post-postgres migration, limit should go inside subquery + Claim.find_by_sql("SELECT *, CASE WHEN yeas > nays THEN nays ELSE yeas END AS contests FROM claims WHERE yeas > 0 AND nays > 0 AND state = 1 ORDER BY contests DESC #{limit_frag}") + end + + def self.find_solid(options = {}) + if options[:limit] + limit_frag = "LIMIT #{options[:limit]}" + else + limit_frag = "" + end + + Claim.find_by_sql("SELECT claims.*, CASE WHEN claims.yeas > claims.nays THEN claims.yeas - 4 * claims.nays ELSE claims.nays - 4 * claims.yeas END AS solidity FROM claims WHERE claims.state = 1 ORDER BY solidity DESC #{limit_frag}") + end + + def image + images[0] + end + + def image_sizes + ['claim'] + end + + def has_supporting_material? + (self.body and self.body.length > 0) or self.tags.length > 0 or image + end + + + +end + diff --git a/app/models/claim_vote.rb b/app/models/claim_vote.rb new file mode 100644 index 0000000..cfb360f --- /dev/null +++ b/app/models/claim_vote.rb @@ -0,0 +1,59 @@ +class ClaimVote < ActiveRecord::Base + belongs_to :user + belongs_to :claim, :counter_cache => true + validates_associated :user, :claim + validates_presence_of :user, :claim + validates_uniqueness_of :user_id, :scope => [:claim_id] + + attr_accessor :contact_msg + + def self.find_all_by_user_id_and_claim_ids(user_id, claim_ids) + return [] if user_id.nil? or claim_ids.empty? + find(:all, :conditions => "user_id = #{user_id} AND claim_id IN (#{claim_ids.join(',')})") + end + + def self.find_all_votes_hash(user_id, claim_ids) + results = find_all_by_user_id_and_claim_ids(user_id, claim_ids) + h = {} + results.each {|v| h[v.claim_id] = v} + return h + end + + def validate_on_create + # old_vote = ClaimVote.find_by_claim_id_and_user_id(self.claim_id, self.user_id, :conditions => 'current = true') + # if old_vote + # old_vote.expire + # self.claim.reload + # end + end + + def after_save + # update the claim vote counts + vote_time = Claim.connection.quoted_date(DateTime.now) + (1..3).each{|i| + begin + ActiveRecord::Base.transaction { + Claim.connection.update("UPDATE claims SET yeas = (SELECT COUNT(*) FROM claim_votes WHERE claim_id = #{self.claim_id} AND vote = true), nays = (SELECT COUNT(*) FROM claim_votes WHERE claim_id = #{self.claim_id} AND vote = false), voted_at = '#{vote_time}' WHERE id = #{self.claim_id}") + + ClaimVoteHistory.create(:user_id => self.user_id, :claim_id => self.claim_id, :vote => self.vote) + } + break; + rescue ActiveRecord::StatementInvalid + if i == 3 + raise + end + end + } + end + + def after_destroy + if vote + Claim.connection.update("UPDATE claims SET yeas = yeas - 1 WHERE id = #{self.claim_id}") + else + Claim.connection.update("UPDATE claims SET nays = nays - 1 WHERE id = #{self.claim_id}") + end + end + + +end + diff --git a/app/models/claim_vote_history.rb b/app/models/claim_vote_history.rb new file mode 100644 index 0000000..e744a0a --- /dev/null +++ b/app/models/claim_vote_history.rb @@ -0,0 +1,10 @@ +class ClaimVoteHistory < ActiveRecord::Base + belongs_to :claim + belongs_to :user + + def after_create + if self.user_id != self.claim.user_id + Happening.create(:happenable => self) unless self.claim.group_id + end + end +end diff --git a/app/models/claim_weights.rb b/app/models/claim_weights.rb new file mode 100644 index 0000000..1c69a2f --- /dev/null +++ b/app/models/claim_weights.rb @@ -0,0 +1,8 @@ +class ClaimWeights < ActiveRecord::Base + + belongs_to :user, :counter_cache => true + belongs_to :claim, :counter_cache => true + + acts_as_taggable + +end diff --git a/app/models/claiming.rb b/app/models/claiming.rb new file mode 100644 index 0000000..5687423 --- /dev/null +++ b/app/models/claiming.rb @@ -0,0 +1,4 @@ +class Claiming < ActiveRecord::Base + belongs_to :claim + belongs_to :claimable, :polymorphic => true +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..89b2425 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,69 @@ +class Comment < ActiveRecord::Base + belongs_to :user + belongs_to :claim + + acts_as_tree # threaded comments + acts_as_solr :fields => [:body] + + has_many :claimings, :as => :claimable, :dependent => :destroy + + validates_presence_of :body, :user_id, :claim_id + + # used when preparing comments for a claim + attr_accessor :child_ids + + def inspired_claims + x = [] + claimings.each {|c| x << c.claim if c.claim.state == 1} + return x + end + + def validate_on_create + Comment.find_all_by_claim_id_and_user_id(claim_id, user_id).each {|c| + errors.add(:body, "Duplicate!") if c.body == body + } + end + + def validate + unless self.parent_id.nil? or self.claim_id == self.parent.claim_id + errors.add(:parent_id, "Comment and parent must be on same claim.") + end + end + + def after_create + cl = self.claim + + Happening.create(:happenable => self) if cl.state == 1 + + # notify the owner of the parent of this comment + # XXX: user preferences + if self.parent_id and self.parent.user_id != self.user_id + self.parent.user.dispatch(self, :reason => 'comment') + elsif self.claim.user_id != self.user_id + cl.user.dispatch(self, :reason => 'comment') + end + cl.watchers.each {|u| + u.dispatch(self, :reason => 'comment') unless self.user_id == u.id + } + cl.mentioned_users.each {|u| + u.dispatch(self, :reason => 'comment') unless self.user_id == u.id + } + + cl.comments_count += 1 + cl.commented_at = DateTime.now + cl.save + + # don't index comments on group claims + if cl.state == 1 + self.solr_save + end + + end + + # current vote at time of comment + def user_vote + ClaimVote.find(:first, :conditions => "user_id = #{self.user_id} AND claim_id = #{self.claim_id}", :order => "created_at DESC") + #AND created_at < '#{self.created_at_before_type_cast}'" + end + +end diff --git a/app/models/contact.rb b/app/models/contact.rb new file mode 100644 index 0000000..e43728b --- /dev/null +++ b/app/models/contact.rb @@ -0,0 +1,24 @@ +class Contact < ActiveRecord::Base + belongs_to :contacter, :class_name => 'User', :foreign_key => 'user_id' + belongs_to :contact, :class_name => 'User', :foreign_key => 'contact_id' + + acts_as_taggable + + validates_uniqueness_of :contact_id, :scope => [:user_id] + validates_presence_of :contact_id, :user_id + + def validate_on_create + cuser = User.find_by_id(self.contact_id) + + # contact must be a valid user + if cuser.nil? + errors.add(:invalid_user, 'That user does not exist.') + + # cannot be your own contact + elsif self.user_id == self.contact_id + errors.add(:same_user, 'Cannot be your own contact.') + end + end + +end + diff --git a/app/models/cred.rb b/app/models/cred.rb new file mode 100644 index 0000000..74e70ad --- /dev/null +++ b/app/models/cred.rb @@ -0,0 +1,398 @@ +class Cred < ActiveRecord::Base + acts_as_taggable + + belongs_to :source, :class_name => 'User', :foreign_key => 'source_id' + belongs_to :sink, :class_name => 'User', :foreign_key => 'sink_id' + + validates_presence_of :source_id, :sink_id + validates_uniqueness_of :sink_id, :scope => :source_id + + def validate + if source_id == sink_id + errors.add(:text, "Cannot give yourself cred.") + end + end + + def self.score_table_setting + setting = OpenIdSetting.find_by_setting('cur_score_table') + if setting.nil? # XXX Is this the right place to be initializing this? + setting = OpenIdSetting.create(:setting => 'cur_score_table', :value => 1) + end + return setting.value + end + + def self.swap_score_tables + t = OpenIdSetting.find_by_setting('cur_score_table') + if t.value == '1' + t.value = '2' + else + t.value = '1' + end + t.save + end + + # return the class of the current score table for reading + def self.score_class + return score_table_setting == '1' ? Cred1Score : Cred2Score + end + + # class of writable score table + def self.write_score_class + return score_table_setting == '1' ? Cred2Score : Cred1Score + end + + def self.score_table_name + return score_table_setting == '1' ? 'cred1_scores' : 'cred2_scores' + end + + # options: + # :tag_id, :tag, or :tag_name for tagged score. defaults to overall score + # :normalized => true - normalize score + def self.score(options = {}) + user_id = find_user_id(options) + tag_id = find_tag_id(options) + + score = score_class.find_by_user_id_and_tag_id(user_id, tag_id) + if score + if options[:normalized] + ms = MaxScore.find_by_tag_id(tag_id) + score.value / ms.value + else + score.value + end + else + if score_class.find_by_user_id_and_tag_id(user_id, nil) + return 0.0 + else + return nil + end + end + end + + def self.scores_for_users(user_ids, options = {}) + tag_id = find_tag_id(options) + scores = score_class.find_all_by_user_id_and_tag_id(user_ids, tag_id) + scorehash = {} + if options[:normalized] + ms = MaxScore.find_by_tag_id(tag_id) + scores.each{|s| scorehash[s.user_id] = s.value/ms.value } + else + scores.each{|s| scorehash[s.user_id] = s.value } + end + return scorehash + end + + def self.scores_by_tag_and_user(tag_ids, user_ids, options = {}) + user_id_frag = '('+user_ids.join(',')+')' + scores = {} + if tag_ids.empty? + tag_id_frag = 'tag_id IS NULL' + else + tag_id_frag = 'tag_id IN ('+tag_ids.join(',')+') OR tag_id IS NULL' + end + sql = "SELECT * FROM #{score_table_name} scores WHERE user_id IN #{user_id_frag} AND (#{tag_id_frag})" + tag_ids.each {|i| scores[i] = {}} + scores[nil] = {} + if options[:normalized] + maxes = {} + MaxScore.find_by_sql("SELECT * FROM max_scores WHERE #{tag_id_frag}").each {|m| maxes[m.tag_id] = m.value} + end + connection.select_all(sql).each{|r| + val = r['value'].to_f + tag_id = r['tag_id'] + tag_id = tag_id.to_i if tag_id + user_id = r['user_id'].to_i + if options[:normalized] + val /= maxes[tag_id] + end + scores[tag_id][user_id] = val + } + # score 0 instead of nil for users with scores + scores[nil].keys.each{|uid| + tag_ids.each{|tid| + unless scores[tid][uid] + scores[tid][uid] = 0.0 + end + } + } + return scores + end + + def self.scores(options) + user_id = find_user_id(options) + if user_id.nil? + raise ArgumentError, 'need to pass in a user_id' + end + + if (lim = options[:limit].to_i) > 0 + lim_frag = "LIMIT #{lim}" + else + lim_frag = "" + end + + st = score_table_name + + # overall cred + if options[:normalized] + sql = "SELECT (scores.value/max_scores.value) FROM #{st} scores + JOIN max_scores ON max_scores.tag_id IS NULL + AND scores.tag_id IS NULL + AND scores.user_id = #{user_id}" + else + sql = "SELECT scores.value FROM #{st} scores + WHERE scores.tag_id IS NULL + AND scores.user_id = #{user_id}" + end + overall_score = connection.select_value(sql).to_f + + # tagged cred + if options[:normalized] + sql = "SELECT scores.tag_id tag_id, (scores.value/max_scores.value) value FROM #{st} scores + JOIN max_scores ON max_scores.tag_id = scores.tag_id + AND scores.tag_id IS NOT NULL + AND scores.user_id = #{user_id} + ORDER BY scores.value DESC #{lim_frag}" + else + sql = "SELECT scores.tag_id tag_id, scores.value value FROM #{st} scores + WHERE scores.user_id = #{user_id} + AND scores.tag_id IS NOT NULL + ORDER BY scores.value DESC #{lim_frag}" + end + + scores_by_tag_id = {} + connection.select_all(sql).each {|r| + scores_by_tag_id[r['tag_id'].to_i] = r['value'].to_f + } + + return overall_score, scores_by_tag_id + end + + def self.initialize_write_score_table + ActiveRecord::Base.transaction { + # We have one table live for reading, and one table for recalculation + # determine which table we are reading and writing from/to + conn = ActiveRecord::Base.connection + if score_table_setting == '1' + conn.drop_table :cred2_scores + conn.create_table :cred2_scores do |t| + t.column :user_id, :integer + t.column :tag_id, :integer, :default => nil + t.column :value, :float + end + conn.add_index :cred2_scores, [:user_id,:tag_id] + else + conn.drop_table :cred1_scores + conn.create_table :cred1_scores do |t| + t.column :user_id, :integer + t.column :tag_id, :integer, :default => nil + t.column :value, :float + end + conn.add_index :cred1_scores, [:user_id,:tag_id] + end + } + end + + def self.find_sink_ids(tag_id = nil) + if tag_id.nil? + connection.select_values("SELECT DISTINCT sink_id FROM creds") + else + connection.select_values("SELECT DISTINCT creds.sink_id FROM creds JOIN taggings ON taggings.taggable_type = 'Cred' AND taggings.taggable_id = creds.id AND taggings.tag_id = #{tag_id}") + end + end + + def self.out_counts(tag_id = nil) + if tag_id.nil? + records = connection.select_all("SELECT source_id, COUNT(id) cnt FROM creds GROUP BY source_id") + else + records = connection.select_all("SELECT source_id, COUNT(creds.id) cnt FROM creds JOIN taggings ON taggable_type = 'Cred' AND taggable_id = creds.id AND tag_id = #{tag_id} GROUP BY source_id") + end + oc = {} + records.each{|r| + oc[r['source_id']] = r['cnt'].to_i + } + return oc + end + + def self.all_scores(tag_id = nil) + if tag_id.nil? + records = connection.select_all("SELECT user_id, value FROM #{score_table_name} WHERE tag_id IS NULL") + else + records = connection.select_all("SELECT user_id, value FROM #{score_table_name} WHERE tag_id = #{tag_id}") + end + allscores = {} + records.each{|r| + allscores[r['user_id']] = r['value'].to_f + } + return allscores + end + + def self.sources_for_sink(sink_id, tag_id = nil) + if tag_id.nil? + connection.select_values("SELECT creds.source_id FROM creds WHERE creds.sink_id = #{sink_id}") + else + connection.select_values("SELECT creds.source_id FROM creds JOIN taggings ON taggable_type = 'Cred' AND taggable_id = creds.id AND tag_id = #{tag_id} AND creds.sink_id = #{sink_id}") + end + end + + # we should keep an eye on the memory usage of this feller + # complexity analysis: u number of users, c number of creds + # memory usage O(u) + # db queries O(u) + # arithmetic O(c) + # and again for each tag, with u and c reduced + def self.calc + ActiveRecord::Base.transaction { + Cred.initialize_write_score_table + + # pagerank parameters + d = 0.85 + initial_score = (1.0 - d) + + overall_scores = all_scores + out_count = out_counts() + out_count.default = 1000000 # protect against cred changing race condition + + find_sink_ids.each{|sink_id| + # print "sink #{sink_id}\n" + score = nil + sources_for_sink(sink_id).each{|source_id| + # print " source #{source_id}" + # only those who already have a score can affect others' scores + if overall_scores[source_id] and overall_scores[source_id] > 0 + # print ": #{overall_scores[source_id]}\n" + score = initial_score if score.nil? + score += d * overall_scores[source_id] / (out_count[source_id] + 1) + #else + # print " has no score\n" + end + } + unless score.nil? + write_score_class.create(:user_id => sink_id, + :value => score) + # print " final score for #{sink_id}: #{score}\n" + #else + # print " no score for #{sink_id}.\n" + end + } + + # by tag + cred_tag_ids = connection.select_values("SELECT DISTINCT tag_id FROM taggings WHERE taggable_type = 'Cred'") + cred_tag_ids.each{|tag_id| + scores = all_scores(tag_id) + out_count = out_counts(tag_id) + out_count.default = 1000000 # protect against cred changing race condition + + find_sink_ids(tag_id).each{|sink_id| + score = nil + sources_for_sink(sink_id, tag_id).each{|source_id| + # only those who already have a score can affect others' scores + if overall_scores[source_id] + score = initial_score if score.nil? + if scores[source_id] + score += d * scores[source_id] / (out_count[source_id] + 1) + else + # starting value for 1 cred from 1 person in a topic with no cred in or out is 1 + # 5 * (0.05 + 0.15) + score += 0.1 / (out_count[source_id] + 1) + end + end + } + unless score.nil? + write_score_class.create(:user_id => sink_id, + :tag_id => tag_id, + :value => score) + end + } + } + + # Make people be careful with the cred they give + # kill scores if they've given cred to bad people + User.find_bad_ids.each {|i| + write_score_class.find_all_by_user_id(i).each{|s| + s.destroy + } + Cred.find_all_by_sink_id(i).each {|c| uid = c.source_id + write_score_class.find_all_by_user_id(uid).each {|s| + if s.tag_id.nil? + s.value = -0.001 + else + s.destroy + end + } + } + } + + top_scores = write_score_class.find_top_scores + top_scores.each {|ts| + ms = MaxScore.find_or_create_by_tag_id(ts.tag_id) + ms.value = ts.value + ms.save! + } + Cred.swap_score_tables + } + end + + # seeds must have a loop of cred, or scores will die off + def self.initialize(seed_list = [1,4]) + # init score tables + ActiveRecord::Base.transaction { + Cred.initialize_write_score_table + seed_list.each{|user_id| + write_score_class.create(:user_id => user_id, :value => 1.0) + } + Cred.swap_score_tables + } + 20.times{Cred.calc} + end + + + + # tests with production data + def self.test + test_scores_by_tag_and_user + end + + + private + + def self.find_tag_id(options) + if options[:tag_id] + tag_id = options[:tag_id] + elsif options[:tag] + tag_id = options[:tag].id + elsif options[:tag_name] + tag_id = Tag.find_by_name(options[:tag_name]).id + end + return tag_id + end + + def self.find_user_id(options) + if options[:user_id] + return options[:user_id] + elsif options[:user] + return options[:user].id + end + end + + def self.test_scores_by_tag_and_user + u = User.find(4) # Dag + users, tags, tbu, ubt = u.in_cred_with_extras + s = scores_by_tag_and_user(tags.map{|t|t.id}, users.map{|u|u.id}) + users.each{|u| + print "User #{u.dn}\n" + tags.each{|t| + hs = s[t.id][u.id] + ds = score(:user => u, :tag => t) + if ds > 0 + print " #{t.name} " + if hs == ds + print "OK: #{hs} \n" + else + print "!!!!!!!!!!!!!!!!!!!! hash: #{hs} db: #{ds}\n" + end + end + } + } + p users, tags + end +end diff --git a/app/models/cred1_score.rb b/app/models/cred1_score.rb new file mode 100644 index 0000000..9878a40 --- /dev/null +++ b/app/models/cred1_score.rb @@ -0,0 +1,11 @@ +class Cred1Score < ActiveRecord::Base + belongs_to :tag + belongs_to :user + + validates_presence_of :user_id + validates_uniqueness_of :user_id, :scope => [:tag_id] + + def self.find_top_scores + find_by_sql("SELECT user_id, tag_id, MAX(value) AS value FROM #{table_name} GROUP BY tag_id") + end +end diff --git a/app/models/cred2_score.rb b/app/models/cred2_score.rb new file mode 100644 index 0000000..878de71 --- /dev/null +++ b/app/models/cred2_score.rb @@ -0,0 +1,11 @@ +class Cred2Score < ActiveRecord::Base + belongs_to :tag + belongs_to :user + + validates_presence_of :user_id + validates_uniqueness_of :user_id, :scope => [:tag_id] + + def self.find_top_scores + find_by_sql("SELECT user_id, tag_id, MAX(value) AS value FROM #{table_name} GROUP BY tag_id") + end +end diff --git a/app/models/dear_strongbad.rb b/app/models/dear_strongbad.rb new file mode 100644 index 0000000..78e6439 --- /dev/null +++ b/app/models/dear_strongbad.rb @@ -0,0 +1,62 @@ +class DearStrongbad < ActionMailer::Base + extend ActionView::Helpers::SanitizeHelper::ClassMethods + include ActionView::Helpers::SanitizeHelper + + def invite(invitation, response_url) + recipient = invitation.response.email + inviter = invitation.sender + + inviter_names = [inviter.openid] + inviter_names.insert 0, inviter.nickname unless (inviter.nickname.nil? or inviter.nickname.empty?) + inviter_names.insert -1, inviter.email unless inviter.email.nil? + + recipients recipient + from 'Jyte ' + subject "#{inviter_names[0]} invites you to Jyte!" + + body :invitation => invitation, :response_url => response_url, :inviter_names => inviter_names + end + + # we got this sreg claim that owns this email address. is this true? + def confirm(user, email, response_url) + recipients email + from 'Jyte ' + subject "Confirm your email address on Jyte, #{user.nickname}" + body :user => user, :response_url => response_url + end + + # X new claims have been made about you or people in your network since you last logged in! + def notify(user) + recipients user.email + from 'Jyte ' + + conditions = user.network.collect {|i| "identifier_id = #{i.id}"}.join(' OR ') + + @network_claims = Claim.find_by_sql("SELECT * from claims WHERE created_at > #{user.last_seen_at_before_typecast} AND id IN (SELECT claim_id FROM mentioned_identifiers WHERE (#{conditions}))") + + conditions = user.identifiers.collect {|i| "identifier_id = #{i.id}"}.join(' OR ') + + @user_claims = Claim.find_by_sql("SELECT * from claims WHERE created_at > #{user.last_seen_at_before_typecast} AND id IN (SELECT claim_id FROM mentioned_identifiers WHERE (#{conditions}))") + + if @user_claims.length > 0 + if @user_claims.length > 1 + foo = 'claims have' + else + foo = 'claim has' + end + subject "#{@user_claims.length} new #{foo} been made about you on Jyte" + elsif @network_claims.length > 0 + if @network_claims.length > 1 + foo = 'claims have' + else + foo = 'claim has' + end + subject "#{@network_claims.length} new #{foo} been made about people you know on Jyte" + else + return false + end + body :user_claims => @user_claims, :network_claims => @network_claims, :user => user + return true + end + +end diff --git a/app/models/dispatch.rb b/app/models/dispatch.rb new file mode 100644 index 0000000..32a56ed --- /dev/null +++ b/app/models/dispatch.rb @@ -0,0 +1,10 @@ +class Dispatch < ActiveRecord::Base + belongs_to :user + belongs_to :sender, :class_name => 'User', :foreign_key => 'sender_id' + belongs_to :dispatchable, :polymorphic => true + + validates_presence_of :user_id + validates_associated :user, :sender, :dispatchable + validates_uniqueness_of :dispatchable_id, :scope => [:user_id, :dispatchable_type, :reason] +end + diff --git a/app/models/email_response_code.rb b/app/models/email_response_code.rb new file mode 100644 index 0000000..e4a2014 --- /dev/null +++ b/app/models/email_response_code.rb @@ -0,0 +1,8 @@ +class EmailResponseCode < ActiveRecord::Base + has_many :invitations, :foreign_key => 'response_id', :dependent => :destroy + def before_create + while self.code.nil? or EmailResponseCode.find_by_code(self.code) + self.code = rand(2000000000) + end + end +end diff --git a/app/models/featured_claim.rb b/app/models/featured_claim.rb new file mode 100644 index 0000000..be7d2be --- /dev/null +++ b/app/models/featured_claim.rb @@ -0,0 +1,16 @@ +class FeaturedClaim < ActiveRecord::Base + + belongs_to :claim + validates_uniqueness_of :claim_id + + def self.check_claim_hook(claim) + return if claim.state != 1 + return if FeaturedClaim.find_by_claim_id(claim.id) + + # heurestic for popular claim + if (claim.yeas+claim.nays) > 15 + FeaturedClaim.create(:claim_id => claim.id) + end + end + +end diff --git a/app/models/flagging.rb b/app/models/flagging.rb new file mode 100644 index 0000000..ef151b9 --- /dev/null +++ b/app/models/flagging.rb @@ -0,0 +1,4 @@ +class Flagging < ActiveRecord::Base + belongs_to :user + belongs_to :claim +end diff --git a/app/models/group.rb b/app/models/group.rb new file mode 100644 index 0000000..ca0ec36 --- /dev/null +++ b/app/models/group.rb @@ -0,0 +1,97 @@ +class Group < ActiveRecord::Base + belongs_to :user + + has_many :group_memberships, :dependent => :destroy + has_many :users, :through => :group_memberships + alias :members :users + + has_many :invitations, :dependent => :destroy + + # moderators + has_many :group_membership_moderators, :class_name => 'GroupMembership', :conditions => 'moderator = TRUE', :foreign_key => 'group_id' + has_many :moderators, :through => :group_membership_moderators, :source => :user + + # image + has_many :imagings, :as => :imagable, :dependent => :destroy + has_many :images, :through => :imagings + + acts_as_taggable + acts_as_solr :fields => [:name, :description] + + validates_presence_of :user_id + validates_associated :user + + validates_presence_of :name + validates_uniqueness_of :name + validates_format_of :name, :with => /^[\w]{1}[\w \-]*$/ + validates_length_of :name, :within => 1..50, :too_short => "Name must be at least %d character.", :too_long => "Name cannot be longer than %d characters." + + validates_uniqueness_of :urlslug + + def Group.urlslug(name) + name.downcase.gsub(/[^-\w]/, '_') + end + + def Group.most_members(options={}) + limit = options.fetch(:limit, 10) + end + + def before_create + self.name = self.name.strip + self.urlslug = Group.urlslug(self.name) + end + + def invite_only? + self.invite_only + end + + def public? + !invite_only? + end + + def can_invite?(u) + return true if can_edit?(u) + if !invite_only? + return true if self.member?(u) + end + return false + end + + def can_invite_as_moderator?(u) + can_invite?(u) and can_edit?(u) + end + + def can_edit?(u) + if u.class == User + return true if u == user # creator + return moderators.member?(u) # moderator + end + + raise ArgumentError, 'can_edit? needs a User' + end + + def moderator?(u) + return can_edit?(u) + end + + def member?(u) + return users.member?(u) + end + + def image + images[0] + end + + def image_sizes + ['big', 'thumb'] + end + + def user_ids + Group.connection.select_values("SELECT user_id FROM group_memberships WHERE group_id = #{self.id}").map{|uid|uid.to_i} + end + + def after_save + self.solr_save + end + +end diff --git a/app/models/group_membership.rb b/app/models/group_membership.rb new file mode 100644 index 0000000..f84ed23 --- /dev/null +++ b/app/models/group_membership.rb @@ -0,0 +1,20 @@ +class GroupMembership < ActiveRecord::Base + belongs_to :group + belongs_to :user + + validates_uniqueness_of :user_id, :scope => [:group_id] + + def before_destroy + ClaimVote.find_by_sql(["SELECT claim_votes.* FROM claim_votes + JOIN claims ON claims.group_id = ? + AND claim_votes.claim_id = claims.id + AND claim_votes.user_id = ?", + self.group_id, self.user_id]).each{ |v| v.destroy } + end + + def after_destroy + if group.group_memberships.empty? + group.destroy + end + end +end diff --git a/app/models/happening.rb b/app/models/happening.rb new file mode 100644 index 0000000..bdfba1c --- /dev/null +++ b/app/models/happening.rb @@ -0,0 +1,22 @@ +class Happening < ActiveRecord::Base + + belongs_to :happenable, :polymorphic => true + + def self.find_all_since(t=0, options = {}) + if options[:show] + show = options[:show] + ph_list = '('+show.map{|i|'?'}.join(',')+')' + return Happening.find(:all, + :conditions => ["id > ? AND happenable_type IN #{ph_list}", t]+show, + :order => 'id DESC', + :limit => 20) + else + return Happening.find(:all, + :conditions => ['id > ?',t], + :order => 'id DESC', + :limit => 20) + end + + end + +end diff --git a/app/models/identifier.rb b/app/models/identifier.rb new file mode 100644 index 0000000..f16d163 --- /dev/null +++ b/app/models/identifier.rb @@ -0,0 +1,86 @@ +class Identifier < ActiveRecord::Base + include ApplicationHelper + require 'uri' + + belongs_to :user + + has_many :mentioned_identifiers + has_many :claims, :through => :mentioned_identifiers, :order => 'created_at DESC', :conditions => 'claims.state = 1' + + has_many :group_memberships + has_many :groups, :through => :group_memberships + has_many :communities, :through => :group_memberships + has_many :personal_groups, :through => :group_memberships + + validates_presence_of :value + validates_associated :user + + # shortcut instead of having to do identifier.value everywhere. + # XXX: this may have unexpected side effects, i'm not sure. + #def to_s + # value + #end + + def Identifier.find_like(q) + like_pattern ='%'+q.gsub(' ','%')+'%' + ids = Identifier.find_by_sql(["SELECT DISTINCT identifiers.* FROM identifiers LEFT JOIN users ON identifiers.user_id = users.id WHERE (users.nickname LIKE ?) OR (identifiers.value LIKE ?)", like_pattern, like_pattern]) + return ids + end + + def Identifier.find_sloppy(q) + begin + norm = normalize(q) + rescue URI::InvalidURIError + return nil + end + find_by_value(norm) + end + + # Does this string look like an identifier? + # XXX rename this guy? He detects and normalizes. + def Identifier.detect(s) + return nil unless s + # i-name detection: starts with @ or = + if s.match(/^[@=]./) + return s + + elsif s.match( /^https?:\/\// ) or + (s.match(/.+\..+/) and s.match( /\.(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|glgm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|o|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw|aero|biz|cat|com|coop|info|jobs|mobi|museum|name|net|org|pro|travel|gov|edu|mil|int)(\/.*)?$/ )) or s.match(/[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d(\/.*)?/) + begin + return normalize(s) + rescue URI::InvalidURIError + return nil + end + else + return nil + end + end + + def Identifier.shorten(s) + if s.match(/^[@=]/) + return s + else + return s.sub(/^http:\/\//, '').sub(/^([^\/]+)\/$/, '\1').sub(/#.*/,'') + end + end + + def shorten + Identifier.shorten(value) + end + + #XXX these should escape the strings + def display + return user.identifier.shorten if user_id + return shorten + end + alias display_url display + + def self.normalize(s) + return s if s.index(/[=@]/) == 0 + if s.index("://").nil? + s = 'http://' + s + end + OpenID::URINorm.urinorm(s) + end + +end diff --git a/app/models/image.rb b/app/models/image.rb new file mode 100644 index 0000000..0deff45 --- /dev/null +++ b/app/models/image.rb @@ -0,0 +1,158 @@ +require 'rubygems' +require 'fileutils' +require 'socket' + +class Image < ActiveRecord::Base + + has_many :imagings + validates_presence_of :host_num, :day, :suffix + + def Image.for_users(user_ids) + uid_frag = '('+user_ids.join(',')+')' + image_hash = {} + find_by_sql("SELECT images.*, imagings.imagable_id user_id + FROM images JOIN imagings ON imagings.image_id = images.id + AND imagings.imagable_type = 'User' + AND imagings.imagable_id IN #{uid_frag}").each {|i| + image_hash[i.user_id.to_i] = i + } + return image_hash + end + + def Image.from_blob(image_blob, imagable) + logger.info "IMAGE.from_blob #{image_blob.size}" + begin + i = Magick::Image.from_blob(image_blob)[0] # pass on the animation 4 now + rescue + return nil + end + + + + day = nil + suffix = nil + + imagable.image_sizes.each {|size| + sized_image = self.send('size_'+size, i) + sized_image.format = 'PNG' + day, suffix = write_png(sized_image.to_blob, size, day, suffix) + } + + image = Image.create(:host_num => HOST_NUM, + :day => day, + :suffix => suffix) + end + + def Image.size_orig(i) + return i + end + + def Image.size_big(i) + return i.change_geometry('%dx>' % BIG_ICON_WIDTH) do |c,r,_i| + _i.resize(c, r) + end + end + + def Image.size_thumb(i) + # resize such that the larger side becomes THUMB_ICON_WIDTH + if i.columns > i.rows + g_string = '%dx' % [THUMB_ICON_WIDTH] + elsif i.rows > i.columns + g_string = 'x%d' % [THUMB_ICON_WIDTH] + else + # cols = rows, so we do a straight resize + g_string = '%dx%d!' % [THUMB_ICON_WIDTH,THUMB_ICON_WIDTH] + end + + # perform the resize + i = i.change_geometry(g_string) do |c,r,_i| + _i.resize(c, r) + end + + # make it square again by compositing i onto a square transparent + # image + b = Magick::Image.new(THUMB_ICON_WIDTH,THUMB_ICON_WIDTH) { + self.background_color = 'transparent' + } + return b.composite(i, Magick::CenterGravity, Magick::OverCompositeOp) + end + + def Image.size_claim(i) + return i.change_geometry('%dx>' % '500') do |c,r,_i| + _i.resize(c, r) + end + end + + def Image.write_png(png_data, size, day=nil, suffix=nil) + unless day + day = Time.now.to_i / 60 / 60 / 24 + end + + day_s = "%08d" % day + bucket_s = bucket(day) + + full_path = STATIC_IMAGE_DIR.join(HOST_NUM.to_s).join(bucket_s).join(day_s) + FileUtils.mkdir_p(full_path) + + unless suffix + mktemp_file_name = full_path.join("#{size}-XXXXXX") + file_name = `mktemp #{mktemp_file_name}`.strip + FileUtils.chmod(0644, file_name) + suffix = file_name.to_s.split('-')[-1] + else + file_name = full_path.join("#{size}-#{suffix}") + end + + file_name = file_name.to_s.chomp('/') + + f = File.new(file_name, 'w+') + f.write(png_data) + f.close + + FileUtils.mv(file_name, file_name+'.png') + + return [day, suffix] + end + + def Image.bucket(day) + "%03d" % (day % 1000) + end + + # XXX: this should be in a helper i think + def url(size) + raise NotImplementedError + end + + def filename(size) + STATIC_IMAGE_DIR.join(HOST_NUM.to_s).join(Image.bucket(self.day)).join("%08d" % self.day).join("#{size}-#{self.suffix}.png") + end + + # delete files on disk and then destory record + def destroy_image(imagable) + filenames = [] + imagable.image_sizes.each {|s| + filenames << self.filename(s) + } + + FileUtils.rm(filenames) + self.destroy + end + + # check to make sure the backing image files are there + def check(imagable) + imagable.image_sizes.each {|s| + return false unless File.exists?(self.filename(s)) + } + return true + end + + def on(imagable) + imagings.create :imagable => imagable + end + + def url_fragment(size) + [HOST_NUM.to_s, Image.bucket(self.day), "%08d" % self.day, "#{size}-#{self.suffix}.png"].join('/') + + end + +end diff --git a/app/models/image_resource.rb b/app/models/image_resource.rb new file mode 100644 index 0000000..27aec8a --- /dev/null +++ b/app/models/image_resource.rb @@ -0,0 +1,11 @@ +class ImageResource < Resource + file_column :image, :magick => { + :versions => { + :favicon => {:size => '20x20!', :crop => '1:1'}, + :icon => {:size => '48x48!', :crop => '1:1'}, + :medium => {:size => '250x>'} + } + } + + validates_presence_of :image +end diff --git a/app/models/imaging.rb b/app/models/imaging.rb new file mode 100644 index 0000000..9b512a8 --- /dev/null +++ b/app/models/imaging.rb @@ -0,0 +1,7 @@ +class Imaging < ActiveRecord::Base + belongs_to :image + belongs_to :imagable, :polymorphic => true + + validates_associated :imagable, :image + validates_presence_of :imagable_id, :imagable_type, :image_id +end diff --git a/app/models/invitation.rb b/app/models/invitation.rb new file mode 100644 index 0000000..c44bf18 --- /dev/null +++ b/app/models/invitation.rb @@ -0,0 +1,21 @@ +class Invitation < ActiveRecord::Base + belongs_to :response, :class_name => 'EmailResponseCode', :foreign_key => :response_id + belongs_to :sender, :class_name => 'User', :foreign_key => :sender_id + belongs_to :recipient, :class_name => 'User', :foreign_key => :recipient_id + belongs_to :group + belongs_to :claim + + validates_presence_of :sender + validates_associated :sender, :group, :claim, :recipient, :response + + def validate + # we have some either/or presence_of constraints + unless self.response or self.recipient + errors.add(:recipient, "Needs a recipient - user or email response code") + end + + unless self.group or self.claim + errors.add(:target, "Needs a group or a claim") + end + end +end diff --git a/app/models/location_resource.rb b/app/models/location_resource.rb new file mode 100644 index 0000000..b3395cd --- /dev/null +++ b/app/models/location_resource.rb @@ -0,0 +1,14 @@ +require 'rubygems' +require 'json' + +class LocationResource < Resource + + def after_create + + end + + def LocationResource.geocode(q) + + end + +end diff --git a/app/models/look.rb b/app/models/look.rb new file mode 100644 index 0000000..3a690c7 --- /dev/null +++ b/app/models/look.rb @@ -0,0 +1,8 @@ +class Look < ActiveRecord::Base + belongs_to :object, :polymorphic => true + belongs_to :user + validates_associated :object, :user + validates_presence_of :object_id, :object_type, :user_id + validates_uniqueness_of :user_id, :scope => [:object_id, :object_type] + +end diff --git a/app/models/max_score.rb b/app/models/max_score.rb new file mode 100644 index 0000000..6ff493b --- /dev/null +++ b/app/models/max_score.rb @@ -0,0 +1,4 @@ +class MaxScore < ActiveRecord::Base + belongs_to :tag + validates_uniqueness_of :tag_id +end diff --git a/app/models/mentioned_identifier.rb b/app/models/mentioned_identifier.rb new file mode 100644 index 0000000..6a2b193 --- /dev/null +++ b/app/models/mentioned_identifier.rb @@ -0,0 +1,6 @@ +class MentionedIdentifier < ActiveRecord::Base + belongs_to :claim + belongs_to :identifier + validates_presence_of :claim_id, :identifier_id + validates_associated :claim, :identifier +end diff --git a/app/models/old_username.rb b/app/models/old_username.rb new file mode 100644 index 0000000..9e75e64 --- /dev/null +++ b/app/models/old_username.rb @@ -0,0 +1,3 @@ +class OldUsername < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/resource.rb b/app/models/resource.rb new file mode 100644 index 0000000..96f77a6 --- /dev/null +++ b/app/models/resource.rb @@ -0,0 +1,9 @@ +class Resource < ActiveRecord::Base + has_many :resourceables + + def add_to(resourceable) + resourceables.create :resourceable => resourceable + end + +end + diff --git a/app/models/resourceable.rb b/app/models/resourceable.rb new file mode 100644 index 0000000..4ef702c --- /dev/null +++ b/app/models/resourceable.rb @@ -0,0 +1,6 @@ +class Resourceable < ActiveRecord::Base + belongs_to :resource + belongs_to :resourceable, :polymorphic => true + + validates_uniqueness_of :resourceable_id, :scope => [:resource_id] +end diff --git a/app/models/session.rb b/app/models/session.rb new file mode 100644 index 0000000..54fee45 --- /dev/null +++ b/app/models/session.rb @@ -0,0 +1,2 @@ +class Session < ActiveRecord::Base +end diff --git a/app/models/social_claim.rb b/app/models/social_claim.rb new file mode 100644 index 0000000..6e69631 --- /dev/null +++ b/app/models/social_claim.rb @@ -0,0 +1,3 @@ +class SocialClaim < Claim + +end diff --git a/app/models/time_resource.rb b/app/models/time_resource.rb new file mode 100644 index 0000000..f57c4d9 --- /dev/null +++ b/app/models/time_resource.rb @@ -0,0 +1,5 @@ +class TimeResource < Resource + + validates_presence_of :time + +end diff --git a/app/models/url_resource.rb b/app/models/url_resource.rb new file mode 100644 index 0000000..3030b56 --- /dev/null +++ b/app/models/url_resource.rb @@ -0,0 +1,6 @@ +class UrlResource < Resource + + validates_presence_of :url + validates_uniqueness_of :url + +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..6dfe3f1 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,460 @@ +class User < ActiveRecord::Base + serialize :settings, Hash + has_many :claims, :conditions => 'state = 1' + has_many :all_claims, :class_name => 'Claim', :dependent => :destroy + has_many :votes, :class_name => 'ClaimVote', :dependent => :destroy + has_many :vote_histories, :class_name => 'ClaimVoteHistory', :dependent => :destroy + has_many :comments, :dependent => :destroy + + has_many :group_memberships, :dependent => :destroy + has_many :groups, :through => :group_memberships + has_many(:created_groups, + :class_name => 'Group', + :foreign_key => 'user_id', + :dependent => :destroy) + + # contact associations + has_many :contacts_as_contact, :foreign_key => 'contact_id', :class_name => 'Contact', :dependent => :destroy + has_many :contacts_as_contacter, :foreign_key => 'user_id', :class_name => 'Contact', :dependent => :destroy + has_many :contacts, :through => :contacts_as_contacter + has_many :contacters, :through => :contacts_as_contact + + # identifier associations + has_many :identifiers, :dependent => :nullify + has_one :identifier, :conditions => 'identifiers.primary = true' + + has_many :dispatches, :dependent => :destroy + has_one :first_dispatch, :class_name => "Dispatch", :order => 'created_at' + + has_many(:dispatched, :class_name => 'Dispatch', + :conditions => ["dispatchable_type = 'User'"], + :foreign_key => 'dispatchable_id', + :dependent => :destroy) + + # looks + has_many :looks, :dependent => :destroy + has_many(:viewings, + :class_name => 'Look', + :conditions => ["object_type = 'User'"], + :foreign_key => 'object_id', + :dependent => :destroy) + has_many :viewers, :through => :viewings, :source => :user + + # whuffie! See Cory Doctorow, "Down and Out in the Magic Kingdom" + has_many :creds, :foreign_key => 'sink_id', :dependent => :destroy + has_many :given_creds, :class_name => 'Cred', :foreign_key => 'source_id', :dependent => :destroy + + # invitations to groups + has_many(:sent_invites, + :class_name => 'Invitation', + :foreign_key => 'sender_id', + :dependent => :destroy) + + has_many(:invites, + :class_name => 'Invitation', + :foreign_key => 'recipient_id', + :dependent => :destroy) + + has_many(:group_invites, + :class_name => 'Invitation', + :conditions => 'group_id IS NOT NULL', + :foreign_key => 'recipient_id') + + has_many :old_usernames + + # interests take the form of tags + acts_as_taggable + acts_as_solr :fields => [:email, :description, :nickname] + + has_many :imagings, :as => :imagable, :dependent => :destroy + has_many :images, :through => :imagings + + has_many :blockings, :class_name => 'UserBlocking', :foreign_key => 'user_id', :dependent => :destroy + has_many :blocked_users, :through => :blockings + has_many :blockings_as_blocked, :class_name => 'UserBlocking', :foreign_key => 'blocked_user_id', :dependent => :destroy + has_many :blockers, :through => :blockings_as_blocked, :source => :user + + validates_uniqueness_of :nickname, :allow_nil => true + validates_length_of :nickname, :within => 1..30, :too_short => 'Display is too short', :too_long => 'Display name is too long. ', :allow_nil => true + validates_numericality_of :state + + def validate + if self.nickname and self.nickname.match(/^\w{1}[\w \-!@=']*$/).nil? + if self.nickname.index('.') + errors.add(:nickname, 'Sorry, periods are not allowed in your display name. Leave field blank if you want to use your OpenID URL. ') + else + errors.add(:nickname, 'Contains invalid characters. ') + end + end + end + + + def User.find_by_openid(openid) + return nil unless openid + openid = Identifier.detect(openid) + return nil unless openid + i = Identifier.find_by_value(openid) + return nil unless i + return i.user + end + + # XXX could be using a JOIN or a subquery. + # find_all_lite: just return the id and nickname. TODO: do this more! + def User.find_all_lite_by_openid(openids) + idents = Identifier.find_all_by_value(openids.map{|i|Identifier.detect(i)}.reject{|i|i.nil?}) + user_ids = idents.reject{|i| i.user_id.nil? }.map{|i|i.user_id} + return [] if user_ids.empty? + find_all_lite(user_ids) + end + + def User.find_all_lite(user_ids) + find_by_sql("SELECT id, nickname FROM users WHERE id IN (#{user_ids.join(',')})") + end + + def User.find_by_openid_or_email(openid_or_email) + return nil unless openid_or_email + u = User.find_by_email(openid_or_email) + unless u + u = User.find_by_openid(openid_or_email) + end + return u + end + + def User.find_nickname_like(q) + like_pattern ='%'+q.gsub(' ','%')+'%' + return User.find_by_sql(["SELECT * FROM users WHERE (nickname LIKE ?)", like_pattern]) + end + + def image + images[0] + end + + def image_sizes + ['orig', 'big', 'thumb'] + end + + def blocked_user_ids + User.connection.select_values("SELECT blocked_user_id FROM user_blockings WHERE user_id = #{self.id}").map{|i|i.to_i} + end + + def cred_score(options = {}) + Cred.score(options.update(:user => self)) + end + + def cred_scores(options = {}) + Cred.scores(options.update(:user => self)) + end + + def cred_tags + Tag.find_by_sql(["SELECT tags.* FROM tags JOIN taggings ON taggings.tag_id = tags.id JOIN creds ON taggings.taggable_type = 'Cred' AND taggings.taggable_id = creds.id AND creds.sink_id = ? GROUP BY tags.id", self.id]) + end + + def out_cred_tags + Tag.find_by_sql(["SELECT tags.* FROM tags JOIN taggings ON taggings.tag_id = tags.id JOIN creds ON taggings.taggable_type = 'Cred' AND taggings.taggable_id = creds.id AND creds.source_id = ? GROUP BY tags.id", self.id]) + end + + def in_cred_with_extras + sql = "SELECT creds.*, tags.id, tag_id, tags.name tag_name, users.nickname user_name + FROM creds JOIN taggings + ON taggings.taggable_type = 'Cred' + AND taggings.taggable_id = creds.id + AND creds.sink_id = #{self.id} + JOIN tags + ON taggings.tag_id = tags.id + JOIN users + ON users.id = creds.source_id + AND users.state <> #{USER_STATES.index(:suspended)}" + get_users_and_tags(sql, 'source_id') + end + + def out_cred_with_extras + sql = "SELECT creds.*, tags.id, tag_id, tags.name tag_name, users.nickname user_name + FROM creds JOIN taggings + ON taggings.taggable_type = 'Cred' + AND taggings.taggable_id = creds.id + AND creds.source_id = #{self.id} + JOIN tags + ON taggings.tag_id = tags.id + JOIN users + ON users.id = creds.sink_id" + get_users_and_tags(sql, 'sink_id') + end + + # this is the "display" method. It will show the nickname of the user + # if set, or the shortened primary identifier. + extend ActionView::Helpers::SanitizeHelper::ClassMethods + include ActionView::Helpers::SanitizeHelper + def display_name + return @nick_or_short if @nick_or_short + if self.nickname + return @nick_or_short = strip_tags(self.nickname) + else + return @nick_or_short = strip_tags(self.s) + end + end + alias :dn :display_name + + + # cache the primary identifier value + def openid + return @openid if @openid + return @openid = identifier.value + end + + def self.find_without_identifier + find_by_sql("SELECT users.* FROM users LEFT JOIN identifiers ON identifiers.user_id = users.id WHERE identifiers.id IS NULL") + end + + # shortcut for identifier.shorten + def s + return @short if @short + # to let us do display of users eagerly loaded (in/out_cred_with_extras) + ident = (identifier or Identifier.find_by_user_id(self.id, :conditions => 'identifiers.primary = true')) + return @short = ident.shorten + end + + # safe s + def ss + strip_tags(self.s) + end + + # XXX fix the hack + def vote_on(claim) + if claim.class == Claim + ClaimVote.find(:first, :conditions => ['user_id = ? AND claim_id = ?', self.id, claim.id]) + else + ClaimVote.find(:first, :conditions => ['user_id = ? AND claim_id = ?', self.id, claim]) + end + end + alias voted? vote_on + + # XXX do a :claim/:claim_id lookup + def votes_on(claim) + ClaimVote.find_all_by_user_id_and_claim_id(self.id, claim.id, + :order => 'created_at DESC') + end + + def commented?(claim) + Comment.find_by_claim_id_and_user_id(claim.id, self.id) + end + + def votes_on_claims(claim_ids) + ClaimVote.find_all_by_user_id_and_claim_ids(self.id, claim_ids) + end + + def votes_on_my_claims(options = {}) + if options[:include_own] + ClaimVote.find_by_sql(["SELECT claim_votes.* FROM claim_votes JOIN claims on claim_votes.claim_id = claims.id AND claims.user_id = ?", self.id]) + else + ClaimVote.find_by_sql(["SELECT claim_votes.* FROM claim_votes JOIN claims on claim_votes.claim_id = claims.id AND claims.user_id = ? AND claim_votes.user_id != ?", self.id, self.id]) + end + end + + def votes_on_others_claims(options = {}) + if limit = options[:limit] + limit = 'LIMIT '+limit.to_s + else + limit = '' + end + + ClaimVote.find_by_sql(["SELECT claim_votes.* FROM claim_votes JOIN claims on claim_votes.claim_id = claims.id AND claims.state = 1 AND claims.user_id != ? AND claim_votes.user_id = ? ORDER BY claim_votes.id DESC #{limit}", self.id, self.id]) + end + + def claims_voted(options = {}) + if limit = options[:limit] + lim = options[:limit].to_i + if options[:offset] + off = options[:offset].to_i + limit = "LIMIT #{off}, #{lim}" + else + limit = "LIMIT #{lim}" + end + else + limit = '' + end + if group_id = options[:group_id] + group_frag = "claims.state = 4 AND group_id = #{group_id.to_i}" + else + group_frag = "claims.state = 1" + end + Claim.find_by_sql("SELECT claims.*, claim_votes.vote AS user_vote + FROM claims + JOIN claim_votes ON claim_votes.claim_id = claims.id + AND claim_votes.user_id = #{self.id} + WHERE claims.user_id != #{self.id} + AND #{group_frag} + ORDER BY claim_votes.id DESC + #{limit}") + end + + def claims_about(options = {}) + ids = identifiers + if options[:limit] + if options[:offset] + limit_fragment = "LIMIT #{options[:offset]}, #{options[:limit]}" + else + limit_fragment = "LIMIT #{options[:limit]}" + end + else + limit_fragment = "" + end + + order = options.fetch(:order, 'claims.created_at DESC') + + conditions = "identifier_id IN (#{ids.map{|i| i.id}.join(',')})" + sql = "SELECT claims.* FROM claims + JOIN mentioned_identifiers ON mentioned_identifiers.claim_id = claims.id + AND mentioned_identifiers.identifier_id IN (#{ids.map{|i| i.id}.join(',')}) + AND claims.state = 1 + ORDER BY #{order} #{limit_fragment}" + #sql = "SELECT * from claims WHERE state = 1 AND id IN (SELECT claim_id from mentioned_identifiers WHERE (#{conditions})) ORDER BY #{order} #{limit_fragment}" + return Claim.find_by_sql(sql) + end + + def claims_commented(options = {}) + if lim = options[:limit] + lim_frag = "LIMIT #{lim}" + else + lim_frag = "" + end + Claim.find_by_sql(["SELECT DISTINCT claims.* FROM claims JOIN comments ON comments.user_id = ? AND comments.claim_id = claims.id AND claims.state = 1 ORDER BY claims.created_at DESC #{lim_frag}", self.id]) + end + + def recent_claims(options = {}) + if lim = options[:limit] + lim_frag = "LIMIT #{lim}" + else + lim_frag = "" + end + Claim.find_by_sql(["SELECT * FROM claims WHERE user_id = ? AND state = 1 ORDER BY created_at DESC #{lim_frag}",self.id]) + end + + def contact?(other) + return false + self.contacts.member?(other) + end + + def contact?(other) + Contact.find_by_user_id_and_contact_id(self.id, other.id) ? true : false + end + + def contact_of?(other) + Contact.find_by_user_id_and_contact_id(other.id, self.if) ? true : false + end + + def dispatch(dispatchable, options = {}) + + # don't dispatch if dispatchable is by an ignored user + if [Claim, Comment].member?(dispatchable.class) + return if blocked_user_ids.member?(dispatchable.user_id) + end + + unless Dispatch.find_by_dispatchable_type_and_dispatchable_id_and_user_id(dispatchable.class.to_s, dispatchable.id, self.id) + dispatches.create :dispatchable => dispatchable, :reason => options[:reason], :sender => options[:from] + end + end + + # Remember to change the help text on claim/show if you alter this. + def can_flag(claim) + return true if get_state == :janrain or get_state == :jyte_team + #return true if claim.is_about(self) + return false + #return (claim.is_about(self) or Cred.score_class.find_by_user_id(self.id)) + end + + # Come up with something for user to do next. + # Dispatches first + # Unseen claim + # TODO: XXX + # Commented-on claims with new comments + # Voted-on claims with highly rated new comments + # Claims by people in your groups + # users with similar interests + def next_dispatch + if d = first_dispatch + return d.dispatchable + end + unseen = Claim.find_by_sql(["SELECT claims.* FROM claims LEFT JOIN looks ON claims.state = 1 AND looks.object_type = 'Claim' AND looks.object_id = claims.id AND looks.user_id = ? WHERE looks.user_id IS NULL LIMIT 20", self.id]) + return unseen[rand(unseen.size)] + end + + USER_STATES = [:jyte_team,:janrain,:early_adopter,:suspended,:deleted] + BAD_USER_STATES = [:suspended] + def set_state(s) + raise ArgumentError, "unknown user state #{s}" unless USER_STATES.member?(s) + self.state = USER_STATES.index(s) + end + + def get_state + return USER_STATES[self.state] + end + + def deleted? + self.get_state == :deleted + end + + def suspended? + self.get_state == :suspended + end + alias bad? suspended? # we may want to change this later + + def User.bad_states + BAD_USER_STATES.collect {|s| USER_STATES.index(s)} + end + + def User.exclude_sql + "users.state NOT IN (#{User.bad_states.join(',')})" + end + + def User.find_bad_ids + connection.select_values("SELECT users.id FROM users WHERE users.state = #{USER_STATES.index(:suspended)}") + end + + def pibbme? + !! self.settings['pibbme'] + end + + def toggle_pibbme + self.settings['pibbme'] = !self.settings['pibbme'] + save! + end + + private + + # user_list, tag_list, tags_by_user_id, users_by_tag_id + def get_users_and_tags(sql, user_id_column_name) + records = User.connection.select_all(sql) + tags_by_user_id = {} + users_by_tag_id = {} + users = {} + tags = {} + records.each{|r| + user = users[r['source_id']] + if user.nil? + user = User.new(:nickname => r['user_name']) + user.id = r[user_id_column_name] + users[r[user_id_column_name]] = user + end + + tag = tags[r['tag_id']] + if tag.nil? + tag = Tag.new(:name => r['tag_name']) + tag.id = r['tag_id'] + tags[r['tag_id']] = tag + end + + if tags_by_user_id[user.id].nil? + tags_by_user_id[user.id] = [] + end + tags_by_user_id[user.id] << tag + + if users_by_tag_id[tag.id].nil? + users_by_tag_id[tag.id] = [] + end + users_by_tag_id[tag.id] << user + } + return users.values, tags.values, tags_by_user_id, users_by_tag_id + end + + +end diff --git a/app/models/user_blocking.rb b/app/models/user_blocking.rb new file mode 100644 index 0000000..77c5ea5 --- /dev/null +++ b/app/models/user_blocking.rb @@ -0,0 +1,5 @@ +class UserBlocking < ActiveRecord::Base + belongs_to :user + belongs_to :blocked_user, :class_name => 'User', :foreign_key => 'blocked_user_id' + validates_presence_of :user_id, :blocked_user_id +end diff --git a/app/models/votable.rb b/app/models/votable.rb new file mode 100644 index 0000000..3d8a713 --- /dev/null +++ b/app/models/votable.rb @@ -0,0 +1,62 @@ +class Votable < ActiveRecord::Base + belongs_to :votable, :polymorphic => true + has_many :votes, :conditions => 'current = true' + has_many :up_votes, :class_name => 'Vote', :conditions => 'current = true AND vote = true' + has_many :down_votes, :class_name => 'Vote', :conditions => 'current = true AND vote = false' + has_many :voters, :through => :votes, :source => :user + has_many :up_voters, :through => :up_votes, :source => :user + has_many :down_voters, :through => :down_votes, :source => :user + has_many :all_votes, :class_name => 'Vote' + + # snippet to fix missing votables + # Claim.find_all.find_all{|c|c.votable.nil?}.each{|c|c.votable = Votable.create(:votable => c)} + +# validates_associated :votable +# validates_presence_of :votable + def validate + # XXX hack: the counts get screwy sometimes + recalculate if up_count.nil? or down_count.nil? + if up_count + down_count != votes.length + recalculate false + p "Vote counts for votable #{id} required recalculation. (FIXME!)" + end + end + + def votes_by_group(options = {}) + group = Community.find_by_id(options[:group_id]) unless group = options[:group] + user_conditions = group.all_users.collect {|u| "user_id = #{u.id}"}.join " OR " + conditions = "votable_id = #{self.id} AND current = true" + yes_count = Vote.count(:conditions => "(#{user_conditions}) AND #{conditions} AND vote = true") + no_count = Vote.count(:conditions => "(#{user_conditions}) AND #{conditions} AND vote = false") + return [yes_count, no_count] + end + + def recalculate(saveme = true) + self.up_count = Vote.count(:conditions => ['votable_id = ? AND vote = true AND current = true', self.id]) + self.down_count = Vote.count(:conditions => ['votable_id = ? AND vote = false AND current = true', self.id]) + self.save if saveme + end + + # this will likely be pretty expensive + # it's probably possible to do this inside postgres much faster + # XXX bring this up with Jonathan + # change the sorting so that the groups that appear are those where the + # opinions are most different from the average (and limit the number) + def voter_groups + # get a flat list of groups for each user + gl = voters.inject([]) {|memo,u|memo + u.groups} + gh = {} + # count the appearances of each group into a hash + gl.each{|g| gh[g.id] ? gh[g.id] += 1 : gh[g.id] = 1} + # sort by number of appearances, collect the group ids, and return the groups + gh.to_a.sort{|a,b| a[1] <=> b[1]}.collect {|a| a[0]}.collect{|gid|Group.find(gid)} + end + + def up_voters + votes.reject {|v| not v.vote}.collect{|v|v.user} + end + + def down_voters + votes.reject {|v| v.vote}.collect{|v|v.user} + end +end diff --git a/app/models/vote.rb b/app/models/vote.rb new file mode 100644 index 0000000..a2b6284 --- /dev/null +++ b/app/models/vote.rb @@ -0,0 +1,63 @@ +class Vote < ActiveRecord::Base + belongs_to :votable, :counter_cache => true + belongs_to :user + validates_associated :votable, :user + validates_presence_of :votable, :user + + def Vote.find_all_by_user_id_and_claim_ids(user_id, claim_ids) + return [] if user_id.nil? or claim_ids.empty? + conditions = claim_ids.collect {|cid| "(votables.votable_id = #{cid} AND votables.votable_type = 'Claim')"}.join(' OR ') + conditions = "votes.user_id = #{user_id} AND votes.current = 1 AND (#{conditions})" + votes = Vote.find(:all, + :joins => "LEFT OUTER JOIN votables ON votables.id = votes.votable_id", + :conditions => conditions) + votes_by_claim_id = {} + votes.each {|v| votes_by_claim_id[v.votable_id] = v} + return votes_by_claim_id + end + + def validate_on_create + old_vote = Vote.find_by_votable_id_and_user_id(self.votable_id, self.user_id, :conditions => 'current = true') + old_vote.expire if old_vote + end + + def after_create + v = votable + if self.vote + v.up_count += 1 + else + v.down_count += 1 + end + v.save + end + + # since votes are destroyed along with the users they belong to + def before_destroy + v = votable + if self.vote + v.up_count -= 1 + else + v.down_count -= 1 + end + v.save + end + + def expire + self.current = false + v = votable + if self.vote + v.up_count -= 1 + else + v.down_count -= 1 + end + v.save + save + end + + def target + return votable.votable + end + alias claim target + alias comment target + +end diff --git a/app/models/widget_resource.rb b/app/models/widget_resource.rb new file mode 100644 index 0000000..ce40da5 --- /dev/null +++ b/app/models/widget_resource.rb @@ -0,0 +1,51 @@ +class WidgetResource < Resource + + validates_presence_of :widget + + def resize + if widget.index('youtube.com') + width_match = widget.match(/width=\"(\d+)\"/) + if width_match + width = width_match[1].to_f + else + raise ArgumentError, "bad yt width" + end + + height_match = widget.match(/height=\"(\d+)\"/) + if height_match + height = height_match[1].to_f + else + raise ArgumentError, "bad yt height" + end + + ratio = height / width + new_width = 250 + new_height = (new_width * ratio) + + resized = widget.dup + resized.gsub!(width_match[0], "width=\"#{new_width}\"") + resized.gsub!(height_match[0], "height=\"#{new_height}\"") + return resized + elsif widget.index('video.google.com') + + style_match = widget.match(/style=\"width:(\d+)px;\s*height:(\d+)px;"/) + if style_match + width = style_match[1].to_f + height = style_match[2].to_f + ratio = height / width + new_width = 250 + new_height = new_width * ratio + return widget.gsub(style_match[0], "style=\"width:#{new_width}px;height:#{new_width}px;\"") + else + raise ArgumentError, "weird google video size" + end + end + + raise ArgumentError, "does not look like a youtube or google video" + end + + def clean + + end + +end diff --git a/app/views/_claim.rhtml b/app/views/_claim.rhtml new file mode 100644 index 0000000..d5e277a --- /dev/null +++ b/app/views/_claim.rhtml @@ -0,0 +1,111 @@ +class="<%= tr_class%>"<% end %> + <%- if logged_in? -%> + onmouseover="Element.show('claim_marking_<%= claim.id %>');" + onmouseout="var cm = $('claim_marking_<%= claim.id %>'); + if(! cm.hasClassName('flagged')) Element.hide(cm);" + <%- end -%> + > + + <%= render :partial => '/claim_votes', :locals => {:claim=>claim} %> + + + + style="text-decoration: line-through;"<% end %> + href="<%= claim_url :urlslug=>claim.urlslug%>"> + <%= render_claim_title(claim,false) %> + + +
+ + <% if (claim.body and !claim.body.strip.empty?) or claim_has_image?(claim) -%> + + + + <% end -%> + + + By <%= cred_img(claim.user_id) %> <%= user_link :user_id => claim.user_id %> + <%- if claim.group -%> + for <%=link_to(h(claim.group.name), group_url(:urlslug=>claim.group.urlslug))%> + <%- end -%> + <%= time_ago_in_words(claim.created_at) %> ago + <% if claim.comments_count > 0 %> +  → + <%= pluralize(claim.comments_count,'comment','comments')%> + <% end -%> + + +
+ + + <% if logged_in? %> + +
+ <% flag = claim.flag_by(liu) %> + + + <% end %> + diff --git a/app/views/_claim_votes.rhtml b/app/views/_claim_votes.rhtml new file mode 100644 index 0000000..5e906ca --- /dev/null +++ b/app/views/_claim_votes.rhtml @@ -0,0 +1,73 @@ +<% @n = 0 unless @n; @n += 1 -%> + +<% +if @liu_votes + liuvote = @liu_votes[claim.id] +else + liuvote = logged_in? ? liu.vote_on(claim) : nil +end +%> + + + +<% if logged_in? -%> + <% vote_up_url = + url_for(:controller=>'claim',:action=>'vote',:claim_id=>claim.id,:vote=>'yes')-%> + <% vote_down_url = + url_for(:controller=>'claim',:action=>'vote',:claim_id=>claim.id,:vote=>'no')-%> +<% else -%> +<%vote_up_url=vote_down_url=url_for(:controller=>'auth',:action=>'login',:dest=>claim_url(:urlslug=>claim.urlslug))%> +<% end -%> + + diff --git a/app/views/_drawer.rhtml b/app/views/_drawer.rhtml new file mode 100644 index 0000000..857083e --- /dev/null +++ b/app/views/_drawer.rhtml @@ -0,0 +1,35 @@ +<% + label = name unless defined? label or label.nil? + alt_url = '#' unless defined? alt_url + open_up = false unless defined? open_up + opened = false unless defined? opened + partial_name = nil unless defined? partial_name + partial_locals = nil unless defined? partial_locals + text = nil unless defined? text +%> +
+
+ <% unless open_up -%> + <%=label%> + <% end -%> +
+
+ <% if text %><%= text %><% end %> + <% unless partial_locals.nil? %> + <%= render :partial => partial_name, :locals => partial_locals %> + <% else %> + <%= render :partial => partial_name %> + <% end %> +
+
+ <% if open_up -%> + <%=label%> + <% end -%> +
+
diff --git a/app/views/_group.rhtml b/app/views/_group.rhtml new file mode 100644 index 0000000..292c6b0 --- /dev/null +++ b/app/views/_group.rhtml @@ -0,0 +1,20 @@ + + + + + +
+ + + + + <%= h(group.name) %> +
+ <%=group.users.length%> members +
+
+ <% if group.description -%> + <%= truncate(strip_tags(group.description),40) %> + <% end -%> +
+
diff --git a/app/views/_user_find.rhtml b/app/views/_user_find.rhtml new file mode 100644 index 0000000..1d389b8 --- /dev/null +++ b/app/views/_user_find.rhtml @@ -0,0 +1,6 @@ + + + +
diff --git a/app/views/_user_find_results.rhtml b/app/views/_user_find_results.rhtml new file mode 100644 index 0000000..a284c5a --- /dev/null +++ b/app/views/_user_find_results.rhtml @@ -0,0 +1,28 @@ +<% if @users.size == 20 %> +<%= link_to_remote 'Next 20', :controller => 'user', :action => 'quick_find', :update => 'user_find_results' %> +<% end %> + + + + +
+<% unless users.empty? %> +
    +<% users[0...10].each{|u| %> +
  • +<%= user_link :user => u %>  <%= u.ss %> +
  • +<% } %> +
+<% end %> +
+<% if users.size > 10 %> +
    +<% users[10...20].each{|u| %> +
  • +<%= user_link :user => u %>  <%= u.ss %> +
  • +<% } %> +
+<% end %> +
diff --git a/app/views/admin/index.rhtml b/app/views/admin/index.rhtml new file mode 100644 index 0000000..7342ee6 --- /dev/null +++ b/app/views/admin/index.rhtml @@ -0,0 +1,112 @@ +

Jyte User Stats

+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Users<%= @unique_users %>
Verified identifiers<%= @verified_identifers %>
New users today<%= @todays_users %>
New users yesterday<%= @yesterdays_users %>
Users last 7 days<%= @seven_day_users%> +
Users last 30 days<%= @thirty_day_users%> +
+ +

+ +

Jyte Group Stats

+

+ + + + + + + + + +
Total Groups<%= @total_groups%>
Groups Claims<%= @total_group_claims%>
+ + + + +
There are <%= @num_users_ingroups%> users who have joined groups <%= @num_groups_joined%> times
+

+ +

+

Jyte Claim Stats

+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Total Claims<%= @total_claims %>
Total Votes<%= @total_votes %>
Total Comments<%= @total_comments %>
Today's claims<%= @todays_claims%>
Today's votes<%= @todays_votes%>
Today's comments<%= @todays_comments%>
+

+ +

+

Jyte Cred Stats

+

+ + + + + + + + + +
Cred entries<%= @total_creds %>
Creds entries today<%= @todays_creds %>
+

+ +

+

User Admin

+<% form_tag :action => 'user' do %> +OpenID: <%= text_field_tag :openid %> +<%= submit_tag 'Show user' %> +<% end %> diff --git a/app/views/admin/user.rhtml b/app/views/admin/user.rhtml new file mode 100644 index 0000000..b7ff71c --- /dev/null +++ b/app/views/admin/user.rhtml @@ -0,0 +1,44 @@ +

User Admin

+ +<%= link_to 'admin home', :action => 'index' %> +

+

<%=@user.dn%>

+ +
    +
  • view profile
  • +
  • State: <%= @user.get_state.to_s %>
  • +
  • Created IP: <%= @user.created_ip %>
  • +
  • Last login IP: <%= @user.last_login_ip %>
  • +
  • Email: <%= @user.email %>
  • +
+ + + +
+ + +
+

Log in as

+<% form_tag :action => 'log_in_as', :user_id => @user.id do %> + <%= submit_tag 'Log in as '+@user.dn %> +<% end %> +
+ +
+ +<% if @user.get_state != :suspended -%> +
+

Suspend account

+ + <% form_tag :action => 'suspend_submit',:user_id=>@user.id do %> + + <%= check_box_tag 'red_flag_claims', '1', checked=true %> + Red flag all claims +
+ + <%= submit_tag 'Suspend account' %> + + <% end %> + +
+<% end -%> diff --git a/app/views/auth/beta.rhtml b/app/views/auth/beta.rhtml new file mode 100644 index 0000000..a390729 --- /dev/null +++ b/app/views/auth/beta.rhtml @@ -0,0 +1,3 @@ +<% form_tag :action => 'beta' do %> + +<% end %> diff --git a/app/views/auth/invite_response.rhtml b/app/views/auth/invite_response.rhtml new file mode 100644 index 0000000..ab7bcd7 --- /dev/null +++ b/app/views/auth/invite_response.rhtml @@ -0,0 +1,9 @@ +

Welcome to Jyte!

+Sign in with your OpenID +<% form_tag :controller => 'auth', :action => 'openid_start' do %> + <%= text_field_tag :openid_identifier, {}, {:id=>'openid_url'}%> + <%= submit_tag 'Login' %> +<% end %> + +Or, Sign up for you.jyte.com + diff --git a/app/views/auth/login.rhtml b/app/views/auth/login.rhtml new file mode 100644 index 0000000..7a78d26 --- /dev/null +++ b/app/views/auth/login.rhtml @@ -0,0 +1,24 @@ + + + + + <%= submit_tag 'Sign in', :style=>'font-size:20px;' %> + <% end %> +
+ +
+

Already have an account and want to change or add an OpenID?

+

Sign in to your existing account first. You can add or change your OpenIDs from the "edit profile" page.

+
+ +--> diff --git a/app/views/auth/login2.rhtml b/app/views/auth/login2.rhtml new file mode 100644 index 0000000..cc71b67 --- /dev/null +++ b/app/views/auth/login2.rhtml @@ -0,0 +1,22 @@ +
+ <% form_tag :controller=>'auth', :action=>'openid_start', :dest=>params[:dest] do %> + + + + + <%= submit_tag 'Sign in', :style=>'font-size:20px;' %> + <% end %> +
+ +
+

Already have an account and want to change or add an OpenID?

+

Sign in to your existing account first. You can add or change your OpenIDs from the "edit profile" page.

+
+ diff --git a/app/views/auth/signup.rhtml b/app/views/auth/signup.rhtml new file mode 100644 index 0000000..d4d9718 --- /dev/null +++ b/app/views/auth/signup.rhtml @@ -0,0 +1,45 @@ +
+ +

Jyte uses OpenID

+ +

OpenID is an internet-wide identity system that lets you sign in + to different websites with a + single account. In OpenID, your identifier is + a URL, and all OpenID + does is provide a way to prove that you are owner of your URL.

+
+ +
+

Sign Up for Jyte

+

You'll get your own OpenID to use all over the web.

+
+ +
+

Already have an OpenID? Just sign in.

+ <% form_tag :controller=>'auth', :action=>'openid_start' do %> + + <%= submit_tag 'Sign in', :style=>'font-size:20px;' %> +

+

Examples:

+ http://openid.aol.com/AIMscreenname
+ http://you.myopenid.com/
+ http://first.last.name/
+ <% end %> + +
+ +

Sign in using your Yahoo! or Flickr account:

+ Your Yahoo! account is OpenID enabled. Click the button below to get started.
+ + +
+ + + diff --git a/app/views/claim/_comment.rhtml b/app/views/claim/_comment.rhtml new file mode 100644 index 0000000..44e8e26 --- /dev/null +++ b/app/views/claim/_comment.rhtml @@ -0,0 +1,70 @@ +<% c = comment if defined? comment and not comment.nil? %> +<% raise "comment partial rendered with no comment" unless c %> +<% uv = c.user_vote %> +<% if @blocked_user_ids and @blocked_user_ids.member? c.user_id + comment_blocked = true + else + comment_blocked = false + end +%> + + + <% if comment_blocked %> + + + + + <% end %> + style="display: none;"<% end %>> + + + +
+ Comment from <%= user_link :user_id => c.user_id %>, who you are ignoring. + Show +
+ + <%= icon_image :user_id => c.user_id, :size => 'thumb', :linked=>false %> + + +

+ + <%= cred_img c.user_id %> + <%= user_link :user_id => c.user_id %> + <% if uv.nil? -%> + who hasn't voted, says + <% elsif uv.vote -%> + who agreed, says + <% else -%> + who disagreed, says + <% end -%> + +

+
+

+ <%= safe_formatted(c.body, user_allowed_links?(c.user_id)) %> +

+
+
+ <% if c.id # don't show this stuff in the preview -%> + + <%- unless @claim.group_id -%> + + <%= link_to('Make a related claim',:controller=>'claim',:action=>'new',:claimable_id=>c.id,:claimable_type=>'Comment')%> + + <%- end -%> + + <%= time_ago_in_words(c.created_at) %> ago + (<%= link_to 'link', claim_url(:urlslug => @claim.urlslug, :anchor => "comment_#{c.id}") %>) +
+ <% if c.inspired_claims.length > 0 -%> +
+

Claims inspired by this comment

+ <% c.inspired_claims.each {|ic| -%> + <%= link_to(render_claim_title(ic,false),:controller=>'claim',:action=>'show',:urlslug=>ic.urlslug) %>
+ <% } -%> +
+ <% end -%> + <% end -%> +
+
diff --git a/app/views/claim/_comment_preview.rhtml b/app/views/claim/_comment_preview.rhtml new file mode 100644 index 0000000..60681e6 --- /dev/null +++ b/app/views/claim/_comment_preview.rhtml @@ -0,0 +1,3 @@ +<% c = comment if defined? comment and not comment.nil? %> +

Preview

+<%= render :partial => 'comment', :locals => {:c => c} %> diff --git a/app/views/claim/_hints.rhtml b/app/views/claim/_hints.rhtml new file mode 100644 index 0000000..4f849a3 --- /dev/null +++ b/app/views/claim/_hints.rhtml @@ -0,0 +1,38 @@ +<% unless hints.empty? -%> +

Are you trying to make a claim about?

+ + +<% end -%> + +<% unless about.empty? -%> +

Jyte thinks this claim is about

+
    + <% about.each {|i| -%> +
  • + <% if i.class == String -%> + <%= i %> + <% else -%> + <%= icon_image :user_id => i.user_id, :size => 'favicon' %> + <%= i.user_id ? user_display(:user_id => i.user_id) : i.value %> + <% end -%> +
  • + <% } -%> +
+<% end -%> + diff --git a/app/views/claim/_invite_failed.rhtml b/app/views/claim/_invite_failed.rhtml new file mode 100644 index 0000000..92b60af --- /dev/null +++ b/app/views/claim/_invite_failed.rhtml @@ -0,0 +1,2 @@ +Invite Failed! +<%= h(@message) %> diff --git a/app/views/claim/_invite_success.rhtml b/app/views/claim/_invite_success.rhtml new file mode 100644 index 0000000..15bbfcf --- /dev/null +++ b/app/views/claim/_invite_success.rhtml @@ -0,0 +1,2 @@ +Invited <%= h(@invitee) %> +<%= h(@message) %> diff --git a/app/views/claim/_quick_claim_link.rhtml b/app/views/claim/_quick_claim_link.rhtml new file mode 100644 index 0000000..7285a39 --- /dev/null +++ b/app/views/claim/_quick_claim_link.rhtml @@ -0,0 +1,25 @@ +<% +if response_to + r = response_to + r_js = "$('qc_claimable_id').value = '#{response_to.id.to_s}';" + r_js += "$('qc_claimable_type').value = '#{response_to.class.to_s}';" + + if response_to.class == Comment + reason = "inspired by #{user_display :id => r.user_id}’s comment on "#{render_claim_title(response_to.claim, false)}"" + elsif response_to.class == Claim + reason = "inspired by - "+render_claim_title(response_to, false) + end + reason = reason.gsub('"','"').gsub("'",'’') + r_js += "$('qc_reason').innerHTML = '"+escape_javascript(reason)+"';" +else + r_js = "$('qc_claimable_id').value = '';" + r_js += "$('qc_claimable_type').value = '';" +end +r_js += "$('claim_box').value='';" +r_js += "window.scroll(0,100);" +%> + +<%= text %> diff --git a/app/views/claim/_saved_searches.rhtml b/app/views/claim/_saved_searches.rhtml new file mode 100644 index 0000000..92b82eb --- /dev/null +++ b/app/views/claim/_saved_searches.rhtml @@ -0,0 +1,14 @@ +
    + <% if saved_searches = liu.settings[:saved_searches] %> + <% saved_searches.sort{|(a,z),(b,y)|a<=>b}.each{|name,params| %> +
  • + <%= link_to h(name), params.update(:controller => 'claim', :action => 'find') %> + <%= link_to_remote "remove", :url => {:action => 'remove_saved_search', :name => name}, + :update => 'saved_searches', + :confirm => "Really remove saved search #{h(name)}?" %> +
  • + <% } %> + <% else %> +
  • None yet.
  • + <% end %> +
diff --git a/app/views/claim/_tags.rhtml b/app/views/claim/_tags.rhtml new file mode 100644 index 0000000..8fbfe02 --- /dev/null +++ b/app/views/claim/_tags.rhtml @@ -0,0 +1,28 @@ +
+<% if claim.tags.length > 0 %> + Tags: <%= linked_tags(claim.tags, false, :action => 'find', :group_id => claim.group_id) %> +<% else -%> + No Tags +<% end -%> +<% if liuid.to_i == claim.user_id.to_i %> +
+ Edit Tags +
+ diff --git a/app/views/claim/_vote.rhtml b/app/views/claim/_vote.rhtml new file mode 100644 index 0000000..cf70a83 --- /dev/null +++ b/app/views/claim/_vote.rhtml @@ -0,0 +1,14 @@ +
+ +<%= icon_image :user_id => vote.user_id, :size => 'favicon', :class => 'iconborder' %> +<% if vote.user_id != liuid -%> +<%= user_link :user_id => vote.user_id %> +<% else %> +You +<% end -%> + + + <%= vote.vote == true ? 'Agreed' : 'Disagreed' %> +<%= time_ago_in_words(vote.created_at) %> ago + +
diff --git a/app/views/claim/advanced_search.rhtml b/app/views/claim/advanced_search.rhtml new file mode 100644 index 0000000..f1fab6b --- /dev/null +++ b/app/views/claim/advanced_search.rhtml @@ -0,0 +1,301 @@ +

Advanced Search

+
+

Find Claims

+ +

+ + + + +

+ +

Saved Searches

+
+ <%= render :partial => 'saved_searches' %> +
+ diff --git a/app/views/claim/find.rhtml b/app/views/claim/find.rhtml new file mode 100644 index 0000000..a2846f9 --- /dev/null +++ b/app/views/claim/find.rhtml @@ -0,0 +1,172 @@ +
+<% if @search_name %> +

<%= @search_name %>

+<% end %> + +
+ +
+ <% if @rss_link -%> + + + + <% end -%> +

<%= @search_title %>

+ + +

+<% if @claim_count > 10 %> +Showing <%= @start_n %> to <%= @end_n %> of <%= @claim_count %> total. +<% unless @page == 1 %> +<% prev_params = params.dup; prev_params[:page] = @page-1 %> +<%= link_to "Previous 10", prev_params %> +<% end %> +<% unless @end_n == @claim_count %> +<% next_params = params.dup; next_params[:page] = @page+1 %> +<%= link_to "Next 10", next_params %> +<% end %> +<% elsif @claim_count > 1 %> +Showing all <%= @claim_count %> +<% elsif @claim_count == 1 %> +Just one. +<% elsif @claim_count == 0 %> +No such claims. +<% end %> +

+ +
+ + + <%= render :partial => '/claim', :collection => @claims %> +
+ +
+<% if @claim_count > 10 %> +
+<% unless @page == 1 %> +<%= link_to "Previous 10", prev_params %> +<% end %> +<% unless @end_n == @claim_count %> +<%= link_to "Next 10", next_params %> +<% end %> +
+<%= i=1; link_to("#{i}", params.merge(:page => i))%> +... + +<% ipstart = @page-5 > 1 ? @page-5 : 2 %> +<% ipend = @page+5 > (@claim_count/10) ? (@claim_count/10)-1 : @page+5 %> +<% ipstart.upto(ipend) do |i| %> + <% if i == @page %> + <%= i %> + <% else %> + <%= link_to("#{i}", params.merge(:page => i))%> + <% end %> +<% end %> +... +<%= i=(@claim_count/10.0).ceil; link_to("#{i}", params.merge(:page => i))%> +<% end %> +
+ + diff --git a/app/views/claim/new.rhtml b/app/views/claim/new.rhtml new file mode 100644 index 0000000..aa70f42 --- /dev/null +++ b/app/views/claim/new.rhtml @@ -0,0 +1,84 @@ +

+
+ + + +
+<% form_tag :action => 'new_submit' do %> + + <%- if @group -%> + <%= hidden_field_tag :group_id, h(@group.id) %> + <%- end -%> + + <% if @claimable -%> + <% c = @claimable %> + <%= hidden_field_tag :claimable_type, h(@claimable_type) %> + <%= hidden_field_tag :claimable_id, h(@claimable_id) %> + <% end -%> + + <% text = (params[:text] or "") -%> + +
+
+
+ + +

What's a claim?

+

A claim is a statement about someone or something. Really, it + can be anything you want. Be creative. +

+ +

+ When making a claim about yourself or someone else, use their OpenID. +

+ +

Example claims

+
+ → kveton.com lives in Oregon
+ → brianellin.com speaks Klingon
+ → Portland, Oregon has the best coffee in the world
+ → Borat is funny
+ → For more examples, browse + <%= link_to "everyone's claims", :controller=>'claim',:action=>'find'%> +
+
+
+ +
+ +<% end %> + +
+
+ <%= render :partial => '/user_find' %> +
+ + + +
+ + diff --git a/app/views/claim/preview.rhtml b/app/views/claim/preview.rhtml new file mode 100644 index 0000000..42e41e9 --- /dev/null +++ b/app/views/claim/preview.rhtml @@ -0,0 +1,213 @@ +

Is your claim a duplicate?

+ + <% @similar.each {|c| -%> + + + + <% } -%> +
+ + + <%= c.yeas.to_s+'-'+c.nays.to_s %> + + + + + <%= render_claim_title(c,false) %> + +
+
+

+ <%= link_to('scrap claim', + {:action=>'discard_claim',:id=>@claim.id}, + :confirm=>'Really discard this claim?' + ) %> +

+
+

Make sure your claim is the way you want it

+

Once your claim is published, you will be unable to change it.

+<% if @claim.parsed.match(/\?/) %> +

Claims should be phrased as statements rather than questions.

+<% end %> +<% if (" " + @claim.parsed + " ").match(/ I /) %> +

Your claim will be interpreted as being about the voters, not about yourself exclusively.

+

Use your OpenID to make claims about yourself.

+<% end %> + + +
+
+

<%= render_claim_title(@claim) %>

+

+ By + + <%= cred_img liuid %> + <%= user_link(:user => @claim.user) %> + + on <%= @claim.created_at.strftime('%B %d, %Y') %> + <%- if @claim.group_id -%> + for <%= link_to h(@claim.group.name), group_url(:urlslug=>@claim.group.urlslug) %> + <%- end -%> +

+ + <% if @claim.has_supporting_material? -%> +
+

  + <% if @claim.body -%> + <%= safe_formatted(@claim.body) %> + <% end -%> +

+ + <% if @claim.image -%> + claim image + <% end -%> + + <% if @claim.tags.length > 0 %> +
+ Tags: <%= linked_tags(@claim.tags) %> +
+ <% end -%> + +
+ <% end -%> + +
+
+
+ +
+

Or, make changes:

+ + <% form_tag({:action => 'preview_submit'},:multipart=>true) do %> + <%= hidden_field_tag :id, @claim.id %> + +
+ +
+ +
+ +
+ +
+ +
+ + <%- liu_groups = liu.groups -%> + <%- if liu_groups.length > 0 -%> +
+ +
+ <%- end -%> + +
+ + <% if @claim.image -%> + <%= link_to 'Delete current image', :action=>'delete_image',:id=>@claim.id %> + <% end -%> +
+ +
+ <% if @claim.claimings.length > 0 -%> + Inspired by + <% @claim.inspired_by_claims.each {|c| -%> + + + <%= c.yeas.to_s+'-'+c.nays.to_s %> + + <%= render_claim_title(c,false) %> + + <% } -%> + <% @claim.inspired_by_comments.each {|c| -%> + <%=user_link(:id=>c.user_id)%>'s comment on + <%= render_claim_title(c.claim,false) %> + + <% } -%> + <% end -%> +
+ +
+ +

+ +

+ <%= link_to('scrap claim', + {:action=>'discard_claim',:id=>@claim.id}, + :confirm=>'Really discard this claim?' + ) %> +
+ + <% end %> + +
+ +
+ +
+ + diff --git a/app/views/claim/show.rhtml b/app/views/claim/show.rhtml new file mode 100644 index 0000000..47c5fe7 --- /dev/null +++ b/app/views/claim/show.rhtml @@ -0,0 +1,487 @@ + +
+

<%= render_claim_title(@claim) %>

+

+ By + <%=cred_img(@claim.user_id)%> + <%= user_link(:user_id => @claim.user_id) %> + on <%= @claim.created_at.strftime('%B %d, %Y') %> + <%- if @claim.group_id -%> + for <%= link_to h(@claim.group.name), group_url(:urlslug=>@claim.group.urlslug) %> + <%- end -%> +

+ + <% if @claim.has_supporting_material? or liuid == @claim.user_id %> +
+ <% if @claim.body -%> +

<%= safe_formatted(@claim.body, user_allowed_links?(@claim.user_id)) %>

+ <% end -%> + + <% if @claim.image -%> + claim image + <% end -%> +
+ <%= render :partial => 'tags', :locals => {:claim => @claim} %> +
+
+ <% end %> + + +
+ + + +<% if logged_in? -%> +<%vote_up_url=url_for(:controller=>'claim',:action=>'vote',:claim_id=>@claim.id,:vote=>'yes')-%> +<%vote_down_url=url_for(:controller=>'claim',:action=>'vote',:claim_id=>@claim.id,:vote=>'no')-%> +<% else -%> +<%vote_up_url=vote_down_url=url_for(:controller=>'auth',:action=>'login',:dest=>claim_url(:urlslug=>@claim.urlslug))%> + +<% end -%> + +
+ +<% if logged_in? %> +<% if liuid == @claim.user_id and @claim.yeas + @claim.nays + @claim.comments_count == 1 %> +
+ <%= link_to "retract claim", :action => 'publish', :id => @claim.id, :retract => 'yes' %> +
+<% end -%> +
+ <% flag = @claim.flag_by(liu) %> +
style="display:none;"<% end %> + title="Stop watching this claim" + > + <%= link_to_remote t_image_tag('eye.png'), { + :url => {:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :watch => 'n'}, + :after => "Element.hide('stop_watching_claim'); + Element.show('watch_claim'); + Element.show('trash_claim'); + " }, + {:href => url_for({:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :watch => 'n'})} + %> +
+
style="display:none;"<% end %> + title="Watch this claim" + > + <%= link_to_remote t_image_tag('eye_faint.png'), { + :url => {:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :watch => 'y'}, + :after => "Element.hide('watch_claim'); + Element.hide('trash_claim'); + Element.show('stop_watching_claim');" }, + {:href => url_for({:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :watch => 'y'})} + %> +
+
+ <%= link_to_remote t_image_tag('trash.png'), { + :url => {:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :trash => 'y'}, + :after => "Element.hide('trash_claim'); + Element.hide('watch_claim'); + Element.show('untrash_claim');" }, + {:href => url_for({:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :trash => 'y'})} + %> +
+
+ <%= link_to_remote t_image_tag('trash_remove.png'), { + :url => {:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :trash => 'n'}, + :after => "Element.hide('untrash_claim'); + Element.show('trash_claim'); + Element.show('watch_claim');" }, + {:href => url_for({:controller => 'claim', :action => 'mark', :claim_id => @claim.id, :trash => 'n'})} + %> +
+
+ +<% if liu.can_flag(@claim) %> +
+ <%= link_to("red flag",{:action=>'flag',:claim_id=>@claim.id},{:confirm=>'are you sure?'}) %> +
+<% end -%> +<% end -%> + + + + + <% if liuid -%> + + + <% end -%> + + + <% trunc = 17 -%> +
+ +
+

Agreed

+ + <% lhs_users = (@yea_vote_users[5...10] or []) -%> + <% rhs_users = (@yea_vote_users[0...5] or []) -%> + <% remaining = @claim.yeas - (lhs_users.length + rhs_users.length) -%> + + + + + + +
+
    + <% lhs_users.each {|u| -%> +
  • + <%= user_link({:user => u,:truncate=>trunc}, html_ops_for_voter(@claim,u)) %> + <%= cred_img u.id %> +
  • + <% } -%> +
+
+
    + <% if @yea_vote_users.length > 0 -%> + <% rhs_users.each {|u| -%> +
  • id="your_vote"<%end%> + > + <%= user_link({:user => u,:truncate=>trunc}, html_ops_for_voter(@claim,u))%> + <%= cred_img u.id %> +
  • + <% } -%> + <% end -%> + +
  • 0%> + > + Nobody agrees +
  • +
+ +
+ +
+ <% if remaining > 0 -%> + + And <%=remaining%> more + + <% end -%> +
+ +
+ +
+ +

Disagreed

+ + <% rhs_users = (@nay_vote_users[5...10] or []) -%> + <% lhs_users = (@nay_vote_users[0...5] or []) -%> + <% remaining = @claim.nays - (lhs_users.length + rhs_users.length) -%> + + + + + +
+ +
    + <% if @nay_vote_users.length > 0 -%> + <% lhs_users.each {|u| -%> +
  • id="your_vote"<%end-%> + > + <%= cred_img u.id %> + <%= user_link({:user=>u,:truncate=>trunc},html_ops_for_voter(@claim,u)) %> +
  • + <% } -%> + <% end -%> + +
  • 0%> + > + Nobody disagrees +
  • + +
+
+
    + <% rhs_users.each {|u| -%> +
  • + <%= cred_img u.id %> + <%= user_link({:user=>u,:truncate=>trunc}, html_ops_for_voter(@claim,u)) %> +
  • + <% } -%> +
+
+
+ <% if remaining > 0 -%> + + And <%=remaining%> more + + <% end -%> +
+ +
+
+ +
+ +
+ +<%- unless @claim.group_id -%> +
+ + Embed Claim + + + <%- unless @claim.group_id -%> + <%= link_to('Make a related claim', :action => 'new', :claimable_type => 'Claim', :claimable_id => @claim.id) %> + <%- end -%> + + <% if logged_in? and not ie6? %> + + <% end %> + +
+<%- end -%> +
+
+ + +
+ +
+ + <% if @claim.claimings.length > 0 -%> +

This claim was inspired by

+ + <% end -%> + + <% if @claim.inspired_claims.length > 0 -%> +

Claims inspired by this one

+ + <% end -%> + + <% if @similar.length > 0 -%> +

Similar claims

+ + <% end -%> + +
+ + + +
+

Discussion + <%- if @comments.size > 0 -%>(<%=@comments.size%>)<%- end -%> +

+ +
+ <%= render :partial => 'comment', :collection => @comments %> +
+ + + <% if liuid -%> + + +
+ + <% form_tag :action => 'comment' do %> + <%= hidden_field_tag :claim_id, @claim.id %> + + + + + <% end %> + <% else -%> + <%= link_to 'Sign in',:controller=>'auth',:action=>'login'%> in to leave a comment. + <% end -%> + +
+ + + + + + diff --git a/app/views/claim/tagroll_setup.rhtml b/app/views/claim/tagroll_setup.rhtml new file mode 100644 index 0000000..e65eb60 --- /dev/null +++ b/app/views/claim/tagroll_setup.rhtml @@ -0,0 +1,91 @@ +
+Loading preview... +
+ + + +

Claimroll Creator


+ +
+
+

+ A claimroll is a set of Jyte claims that you can add to any + webpage. Use the form below to configure your roll, + and then copy and paste the code snippet below onto your blog + or website. A preview of your claimroll is displayed to the right. +

+ +
+

Claimroll content

+

+ Show the + + most recent claims with + + of the following tags: +
+ +

+ +

Options

+

+ + +
+ + + +
+ + +
+ + +

+
+ + +

Claimroll Code

+

Copy and past the code below into your page

+ +
+

+Or, create a <%= link_to('personal claimroll',:controller=>'user',:action=>'claimroll_setup')%> +

+
+ + + + diff --git a/app/views/claim/votes.rhtml b/app/views/claim/votes.rhtml new file mode 100644 index 0000000..91dbf7d --- /dev/null +++ b/app/views/claim/votes.rhtml @@ -0,0 +1,56 @@ +

+<%=render_claim_title(@claim,false)%> +

+
+

+<% if @voted == 'yes' -%> +<%= @votes.total_entries %> users agreed. +<% elsif @voted == 'no' -%> +<%= @votes.total_entries %> users disagreed. +<% else -%> +<%= @votes.total_entries %> total voters. +<% end -%> +

+ + + +

+ +<% if @votes.total_entries > 0 %> + +<% start = 0 %> +<% @votes.each_with_index { |v,i_| -%> + + <% if i_ % 6 == 0 -%> + + <% start = 0 %> + <% end -%> + + + + <% if (start-6) == 0 or i_ == @votes.length-1 -%> + + <% end -%> + +<% } -%> +
+ <%= icon_image(:user => v.user,:size=>'thumb') %> + + <%= truncate(v.user.dn,20) %> + <% if !@voted -%> + <% aord = v.vote ? 'agreed' : 'disagreed' %> +
<%=aord%> + <% end -%> + <% start += 1%> +
+ +<%= will_paginate @votes %> + +<% end -%> diff --git a/app/views/contacts/add.rhtml b/app/views/contacts/add.rhtml new file mode 100644 index 0000000..303ce37 --- /dev/null +++ b/app/views/contacts/add.rhtml @@ -0,0 +1,34 @@ +

Add <%= @user.display_name %> to your contacts

+
+<% form_tag :action => 'add_submit', :user_id => @user.id do %> + +
+ +Optionally describe your relationship +
+ +<% if @all_tags.length > 0 %> +Your other relations: +<% @all_tags.each {|t| -%> + <%= contact_rel_link(t) %> +<% } -%> +<% else %> +Examples: +<%= contact_rel_link 'friend' %> +<%= contact_rel_link 'family' %> +<%= contact_rel_link 'met' %> +<%= contact_rel_link 'co-worker' %> +<% end %> + + + + +
+ +<%= submit_tag 'Add contact' %> or cancel +<% end %> + diff --git a/app/views/contacts/compare.html.erb b/app/views/contacts/compare.html.erb new file mode 100644 index 0000000..5d7a56f --- /dev/null +++ b/app/views/contacts/compare.html.erb @@ -0,0 +1,17 @@ +

+Find Jyte friends using your contacts list on these services: +

+ + +<%= t_image_tag 'logo_gmail.gif' %> + +

+Gmail access: +<% if GMail.authorization_token_for(liu) %> + Authorized (<%= link_to "Deauthorize", :controller => :oauth, :action => :gmail_deauthorize%>) + Step 2: <%= link_to "Compare", :action => :compare_gmail %> +<% else %> +Step 1: <%= link_to "Authorize", :controller => :oauth, :action => :gmail_start %> +<% end %> +

+
diff --git a/app/views/contacts/compare_gmail.html.erb b/app/views/contacts/compare_gmail.html.erb new file mode 100644 index 0000000..a238a25 --- /dev/null +++ b/app/views/contacts/compare_gmail.html.erb @@ -0,0 +1,36 @@ +Your Gmail contact list contains <%= @contacts.size %> people.
+Among those people, <%= @jyters.size %> have a jyte account.
+<%= @jyters.size - @missing_jyters.size %> of those jyte accounts are listed as a contact of yours.
+
+ <%= @missing_jyters.size %> of your Gmail contacts that use jyte are not a contact of yours.
+Check off any of the users you would like to make one of your jyte contacts. + +<% form_tag do %> + + <% @missing_jyters.each_slice(3) do |js| %> + + <% js.each do |j| %> + + <% end %> + + <% end %> +
+ + + + + +
+ + <%= icon_image :user_id => j.id, :size => 'thumb', :linked=>false, :width => 48, :height => 48 %> + + + <%= truncate((j.nickname.blank? ? j.email : j.nickname), :length => 22) %>
+ <%= link_to_remote t_image_tag('add-icon.png', :id => "adduser#{j.id}"), + :url => {:controller => :contacts, :action => :add_submit, + :user_id => j.id, :contact_tags => ""}, + :loading => "$('adduser#{j.id}').src = '/images/bouncing_ball.gif'", + :success => "$('adduser#{j.id}').src = '/images/added-icon.png'" %> +
+
+<% end %> diff --git a/app/views/contacts/edit.rhtml b/app/views/contacts/edit.rhtml new file mode 100644 index 0000000..c4947b9 --- /dev/null +++ b/app/views/contacts/edit.rhtml @@ -0,0 +1,33 @@ +

Describe your connection with <%= @contact.contact.dn %>

+
+<% form_tag :action => 'edit_submit', :user_id => @contact.contact.id do %> + +
+ + +<% if @all_tags.length > 0 %> +Your other relations: +<% @all_tags.each {|t| -%> + <%= contact_rel_link(t) %> +<% } -%> +<% else %> +Examples: +<%= contact_rel_link 'friend' %> +<%= contact_rel_link 'family' %> +<%= contact_rel_link 'met' %> +<%= contact_rel_link 'co-worker' %> +<% end %> + + + + +
+ +<%= submit_tag 'Save' %> or +cancel +<% end %> + diff --git a/app/views/contacts/index.rhtml b/app/views/contacts/index.rhtml new file mode 100644 index 0000000..7a9feb6 --- /dev/null +++ b/app/views/contacts/index.rhtml @@ -0,0 +1,92 @@ +

+ <%= liuid == @user.id ? 'Your' : @user.dn+"'s" %> + Contacts (<%=@contacts.length%>)

+ +<% if @contacts.length > 0 -%> + +
+ <% @contacts.each_with_index {|c,i| -%> +
+ <%= icon_image(:user=>c.contact,:size=>'thumb',:style=>"float:left;margin-right:1em;") %> +
+ <%= user_link(:user=>c.contact)%> +
+ + <%= c.tag_list %> +
+ <%= link_to 'make a claim',:controller=>'claim',:action=>'new',:text=>c.contact.s %> + <%= link_to 'edit', :action=>'edit', :user_id=>c.contact.id%> + <% if liu == @user -%> + <%= link_to 'remove',:action=>'remove_submit',:user_id=>c.contact.id %> + <% end -%> +
+
+
+
+ + <% } -%> +
+ +<% else -%> +No contacts yet. +<% end -%> + +

+ +<% if @contact_of.length > 0 -%> +

<%= liuid == @user.id ? 'You are' : @user.dn + " is" %> a contact of +<%= pluralize @contact_of.size, "person" %>

+ +
+ <% @contact_of.each_with_index {|c,i| -%> +
+ <%= icon_image(:user=>c.contacter,:size=>'thumb',:style=>"float:left;margin-right:1em;") %> +
+ <%= user_link(:user=>c.contacter)%> +
+ + <%= c.tag_list %> +
+ <%= link_to 'make a claim',:controller=>'claim',:action=>'new',:text=>c.contacter.s %> +
+
+
+
+ + <% } -%> +
+ +<% else -%> +No contacts yet. +<% end -%> + +

+Contacts API + + diff --git a/app/views/dear_strongbad/confirm.text.plain.rhtml b/app/views/dear_strongbad/confirm.text.plain.rhtml new file mode 100644 index 0000000..cf1477b --- /dev/null +++ b/app/views/dear_strongbad/confirm.text.plain.rhtml @@ -0,0 +1,7 @@ +Dear <%= @user.nickname %>, + +Please confirm that this is your email by clicking this link: + +<%= @response_url %> + +*** This is an automated message *** Replies will be lost *** diff --git a/app/views/dear_strongbad/invite.text.plain.rhtml b/app/views/dear_strongbad/invite.text.plain.rhtml new file mode 100644 index 0000000..554f7b8 --- /dev/null +++ b/app/views/dear_strongbad/invite.text.plain.rhtml @@ -0,0 +1,18 @@ +You've been invited to Jyte, the site where you can claim anything! + +<%= @inviter_names.join ", aka " %> invites you to +<% if @invitation.group -%> +join the group '<%= @invitation.group.name %>' +<% elsif @invitation.claim -%> +discuss and vote on the claim '<%= @invitation.claim.title %>' +<% end -%> + +If you join Jyte by clicking the link below, you can make claims about yourself, +your friends, or anything at all. And you can discuss and vote on claims that +other people have made. + +<%= @response_url %> + +We hope to see you soon on Jyte! + +*** This is an automated message. Replies will be lost. *** diff --git a/app/views/group/_invite_form.rhtml b/app/views/group/_invite_form.rhtml new file mode 100644 index 0000000..833fde2 --- /dev/null +++ b/app/views/group/_invite_form.rhtml @@ -0,0 +1,24 @@ +
+<% form_remote_tag( + :url => {:action => 'invite', :group_id => @group.id}, + :update => 'group_invite_wrap', + :before => "if($F('openid_or_email') == 'OpenID or Email') {$('openid_or_email').value='';}" + ) do +%> +<% if @invite_msg -%> +<%= @invite_msg %> +<% else -%> +Invite someone to <%=h(@group.name)%><% end -%> +
+ + + <% if @group.can_invite_as_moderator?(liu) -%> + Moderator? + <%= check_box_tag 'mod' %> + <% end -%> + + <%= hidden_field_tag :group_id, @group.id %> + <%= submit_tag 'Invite' %> +<% end %> +
diff --git a/app/views/group/_name_status.rhtml b/app/views/group/_name_status.rhtml new file mode 100644 index 0000000..5ebfc90 --- /dev/null +++ b/app/views/group/_name_status.rhtml @@ -0,0 +1,11 @@ +<% if @g -%> + + <%= h(params[:name]) %> is already taken + +<% elsif @valid -%> + is available +<% elsif !params[:name] or params[:name].empty? %> +   +<% else -%> + is an invalid. Try using just letters and numbers. +<% end -%> diff --git a/app/views/group/_table.rhtml b/app/views/group/_table.rhtml new file mode 100644 index 0000000..1ec4180 --- /dev/null +++ b/app/views/group/_table.rhtml @@ -0,0 +1,41 @@ +<% cols = 6.0 %> +<% rows = (the_groups.size / cols).ceil %> +<% tcount = 0 %> + + + + <% for r in 0...rows %> + + + <% for c in 0...cols %> + + <% end %> + + + <% end %> + +
+ + <% g = the_groups[tcount] %> + <% if g %> + <%= group_icon(:group=>g,:size=>'thumb')%> + <%=truncate(h(g.name),50)%>
+ + + <%= pluralize(g.users.size, 'member','members') %> + + <% if g.respond_to? 'contacts_count' %> +
<%= pluralize(g.contacts_count, 'contact', 'contacts') %> + <% end %> + + <% if g.respond_to? 'interests_count' %> +
<%= pluralize(g.interests_count, 'interest', 'interests') %> + <% end %> +
+ + <% else %> +   + <% end %> + + <% tcount += 1%> +
diff --git a/app/views/group/api_help.rhtml b/app/views/group/api_help.rhtml new file mode 100644 index 0000000..3bad639 --- /dev/null +++ b/app/views/group/api_help.rhtml @@ -0,0 +1,12 @@ +

Jyte Group API

+You can use Jyte groups from your site: + +

Group Roster

+<%= url_for :only_path => false, :controller => 'group', :action => 'roster', :id => 'GROUP_ID' %> +with GROUP_ID replaced by the id of the group you want. You'll get a newline separated list of OpenIDs. + +

Test for group membership

+ +<%= url_for :only_path => false, :controller => 'group', :action => 'roster', :group_id => 'GROUP_ID', :openid => 'OPENID' %> +with GROUP_ID replaced with the group you are querying, and OPENID replaced with the OpenID you are asking about. You'll get a plain text response, +'true' if the OpenID is a member of the group and 'false' if not. diff --git a/app/views/group/claims.rhtml b/app/views/group/claims.rhtml new file mode 100644 index 0000000..621b464 --- /dev/null +++ b/app/views/group/claims.rhtml @@ -0,0 +1,20 @@ +
+

Claims by <%= h(@group.name) %> members

+
+
+ + <%= render :partial => '/claim', :collection => @claims %> +
+
+<%= will_paginate @claims %> +
+ diff --git a/app/views/group/edit.rhtml b/app/views/group/edit.rhtml new file mode 100644 index 0000000..63f5fef --- /dev/null +++ b/app/views/group/edit.rhtml @@ -0,0 +1,102 @@ +

Edit Group

+ +
+ +
+

<%= h(@group.name) %>

+ +

+ Group URL: <%=gurl(@group)%> +

+ + <% form_tag({:action => 'edit_submit'}, :multipart => true) do %> + <%= hidden_field_tag :id, @group.id %> + +
+

Picture

+

Any size will do, but something at least 180x180 + pixels works best. We'll do our best to resize larger pictures.

+ +
+ +
+

Description

+ <%= text_area 'group', :description, :size => '50x7', :class=>'edit_width'%> +
+ +
+

Interests

+

Example: books, jogging, photography, full contact chess

+ <%= text_field_tag 'tags', @group.tag_list, :class => 'edit_width', :size => 50 %> +
+ +
+

Invite only?

+ <%= check_box 'group', 'invite_only' %> +
+ +
+ <%= submit_tag 'Save Group' %> + <% if params[:new] %> + <%= hidden_field_tag :new, params[:new] %> + <% else %> + or cancel + <% end %> +
+ + + <% end %> +
+ +
+
+
+
+
+
+
+ image + +
+ <% if @group.image -%> + <%= link_to 'Delete this icon', :action => 'delete_icon',:id=>@group.id %> + <% else %> + Upload a picture! + <% end -%> +
+
+ +
+
+
+
+
+
+ +
+ +<% unless params[:new] -%> +

Membership

+
+

Remove User

+

+ <% form_tag :action => 'del_member', :group_id => @group.id do %> + OpenID to remove + <%= text_field_tag :openid %> + <%= submit_tag 'Remove' %> + <% end %> +

+ + <% if liu == @group.user -%> +
+

Delete this group

+

+ <% form_tag :action => 'del_group', :group_id => @group.id do %> + <%= submit_tag 'Delete '+h(@group.name) %> + <% end %> +

+ <% end -%> +
+<% end -%> diff --git a/app/views/group/find.rhtml b/app/views/group/find.rhtml new file mode 100644 index 0000000..506e23e --- /dev/null +++ b/app/views/group/find.rhtml @@ -0,0 +1,22 @@ +
+
+ + Find a group + <%= submit_tag 'Go'%> +
+
+ +

+<%= @g_title %> + +(<%= @g_count%>) +

+ +

<%= @g_subtitle %>

+ +
+ +<%= render :partial => 'table', :locals => {:the_groups => @groups} %> + +<%= will_paginate @group_pages %> diff --git a/app/views/group/index.rhtml b/app/views/group/index.rhtml new file mode 100644 index 0000000..50ee101 --- /dev/null +++ b/app/views/group/index.rhtml @@ -0,0 +1,69 @@ + +
+ +
+ + <% if liuid -%> + <%= link_to 'Create a group', :action => 'new' %>  + <% end -%> + +
+
+ + Find a group + <%= submit_tag 'Go'%> +
+
+
+ +
+

Groups

+
+ +
+ Groups are an online gathering of like minded people. You can add + youself to public groups, and create groups of you own. +
+ +
+
+ +<% if @your_groups.size > 0 %> +
+

Your groups

+<%= render :partial => 'table', :locals=>{:the_groups=>@your_groups}%> +
+<% end %> + +<% if @contacts_groups.size > 0 %> +
+

<%= link_to "Your contact's groups", :action => 'find', :by => 'contacts' %> +

+ +<%= render :partial => 'table', :locals =>{:the_groups=>@contacts_groups}%> +<% end %> + +<% if @interests_groups.size > 0 %> +
+

<%= link_to 'Groups that match your interests', :action => 'find', :by => 'interests' %> +

+ +<%= render :partial => 'table', :locals => {:the_groups=>@interests_groups}%> +<% end %> + +<% if @hot_groups.size > 0 %> +
+

<%= link_to('Popular groups', :action => 'find', :by=> 'hot') %> +

+ +<%= render :partial => 'table', :locals => {:the_groups=>@hot_groups}%> +<% end %> + +
+

<%= link_to('All groups', :action => 'find', :by=> 'latest') %> +

+<%= render :partial => 'table', :locals => {:the_groups=>@latest_groups}%> + + + diff --git a/app/views/group/new.rhtml b/app/views/group/new.rhtml new file mode 100644 index 0000000..15a3572 --- /dev/null +++ b/app/views/group/new.rhtml @@ -0,0 +1,38 @@ +

Create a Group

+
+
+ A group is an online gathering + of like minded people. You may create one below, and memebership + may be open to the public or invite only. Start by giving your group + a name. +
+

+<% form_tag({:action => 'new_submit'}) do %> + +
+

Group Name

+ + +
+ +
+ + or cancel +
+ +<% end %> + + diff --git a/app/views/group/show.rhtml b/app/views/group/show.rhtml new file mode 100644 index 0000000..ddcb099 --- /dev/null +++ b/app/views/group/show.rhtml @@ -0,0 +1,151 @@ + + +
+

<%= h(@group.name) %> Group

+ +
+ Created by + <%= cred_img @group.user_id %><%= user_link :user => @group.user %> + <%= time_ago_in_words(@group.created_at) %> ago + <% if liuid and @group.can_edit?(liu) -%> + <%= link_to 'edit group',:action => 'edit',:id=>@group.id %> + <% end -%> + +
+ + <% if @group.description -%> +

<%= safe_formatted(@group.description) %>

+ <% end -%> + +

Interests

+

+ <% if @group.tags.empty? -%> + This group isn't interested in anything. + <% else -%> + <%= linked_tags(@group.tags,false) %> + <% end -%> +

+ +

Membership

+

+ <% if @group.invite_only -%> + Invite only + <% else -%> + Open membership + <% end -%> + <% if liu and !@group.member?(liu) and (!@group.invite_only? or @group.can_edit?(liu))-%> + (<%= link_to 'add yourself', :action => 'add_yourself', :group_id => @group.id %>) + <% end -%> + + <% if liu and @group.member?(liu) -%> + - You are a member. + (<%=link_to 'remove', :action =>'remove_yourself', :group_id => @group.id %>) + <% end -%> +

+ + <% if @invite -%> +

+ <%= user_link :user_id => @invite.sender_id %> invited you to this + group. + - + + <%= link_to 'accept', :controller => 'group', :action => + 'invite_decision', :invite_id => @invite.id, :decision => 'accept' %> - + <%= link_to 'decline', :controller => 'group', :action => + 'invite_decision', :invite_id => @invite.id, :decision => + 'decline' %> +

+ <% end -%> +
+ <% if liuid and @group.can_invite?(liu) -%> + <%= render :partial => 'invite_form' %> + <% end -%> + + + +
+ +
+
+
+
+
+
+
+ image  + +
+ +
+
+
+
+
+ + <%- if @group.member?(liu) -%> + + <%- end -%> +
+ +
+ + +<% if @group.moderators.length > 0 %> +

Moderators (<%= @group.moderators.length %>)

+

+<% @group.moderators.each {|u| -%> +<%= user_link(:user => u) %> +<% } -%> +

+<% end -%> + +

Members (<%=@group.users.length%>)

+

+<% if @group.users.length > 0 -%> + <% @group.users.each {|u| -%> + <%= user_link(:user => u) %> + <% } -%> +<% else -%> + No members yet. +<% end -%> +

+ +<% if @claims and !@claims.empty? %> +

Group only claims +(<%=link_to(@group_claim_count, :controller=>'claim',:action=>'find',:group_id=>@group.id)%>)

+
+ + <%= render :partial => '/claim', :collection => @claims %> +
+
+ <%- remaining = @group_claim_count - @claims.size -%> + <%- if remaining > 0 -%> + All claims for <%=h(@group.name)%> → + <%- end -%> +<%- end -%> + +

+<%= link_to "Public claims by #{h(@group.name)} members →", :action => 'claims', :id => @group.id %> +

+ +
+

Group API

+
+ <%= url_for :action => 'api_is_member', :slug => @group.urlslug, + :only_path=>false %>?openid=URLENCODE(openid) returns text 'true' or 'false'
+ <%= + url_for(:action=>'api_roster',:slug=>@group.urlslug,:only_path=>false) + %> returns newline separated list of member OpenIDs +
+ + diff --git a/app/views/help/cred.rhtml b/app/views/help/cred.rhtml new file mode 100644 index 0000000..b1b2000 --- /dev/null +++ b/app/views/help/cred.rhtml @@ -0,0 +1 @@ +cred help diff --git a/app/views/help/cred_step_by_step.rhtml b/app/views/help/cred_step_by_step.rhtml new file mode 100644 index 0000000..cdca64e --- /dev/null +++ b/app/views/help/cred_step_by_step.rhtml @@ -0,0 +1,10 @@ +<%= link_to "Back to main help page", :controller => 'help', :action => 'index' %>
+

Step 1: Find User's Profile Page

+<%= image_tag "help/cred1.png", :alt => "Step 1", :class => 'screenshot' %> +

Step 2: Click Give cred

+<%= image_tag "help/cred2.png", :alt => "Step 2", :class => 'screenshot' %> +

Step 3: Enter tags

+<%= image_tag "help/cred3.png", :alt => "Step 3", :class => 'screenshot' %> +

Step 4: Wait for it....

+

The cred you just gave will show up in a couple minutes.

+<%= link_to "Back to main help page", :controller => 'help', :action => 'index' %>
diff --git a/app/views/help/index.rhtml b/app/views/help/index.rhtml new file mode 100644 index 0000000..59e37cd --- /dev/null +++ b/app/views/help/index.rhtml @@ -0,0 +1,165 @@ +
+ + + + + + + + + + + + + + + + + + + + + +

Help!

 
How do I sign in to Jyte?
How do I make a claim?
What does the dot next to my name mean?
What is cred and how do I get some?
How do I give someone cred?
What are tags?
How do I make a claim about someone?
Can I change my votes?
How do I add a video to my claim or comment?
Can I add a picture to my claim or comment?
Can I use Jyte groups and OpenID for authorization on my site?
What are Contacts and how do I use them?
Who do I contact for help?
What if I have an idea for a cool new feature for Jyte?
Does Jyte support MicroID and ClaimID?
What if I want to delete my account?
Who is behind Jyte?
Where can I go to find out the latest Jyte news?
+ +
+ +

How do I sign in to Jyte?

+

+Unlike other websites that require people to create a unique account for that website alone, Jyte allows visitors to sign in using OpenID. OpenID is a simple single sign-on mechanism that allows you to login at multiple websites with the same identity. +

+

+You can sign in to Jyte using a login that you already have on AOL, Google (Gmail), Yahoo!, or an OpenID of your choice. You can get an OpenID to use on Jyte (and other websites) at myOpenID.com. If you already have an OpenID from another provider such as LiveJournal, Verisign's PIP, or ClaimID, you can use it to log into Jyte. +

+ +

How do I make a claim?

+

+You can claim anything, so have at it. It's fun, and it's even more fun to get discussions and votes going. All you have to do is click on the Make a Claim link at the top of the page and type your claim in the box. For example, "Ferraris are fun to drive." You can add a description or video embeds to the claim, and tag it to make it easier to find. Then watch as your friends and the Jyte community vote and comment on your claim. +

+ +

What does the dot next to my name mean?

+

+ The dot represents your overall cred. The more cred you have, the larger the dot will be. +

+ +

What is cred and how do I get some?

+

+ Jyte uses a "gift-based economy" for people interested in developing their on-line credibility. A similar reputation currency called "whuffie" is + featured in Cory Doctorow's science fiction novel, Down and Out in the Magic Kingdom. +

+

+ You can give another user cred using tags to signify that you respect their abilities or qualities in a certain area. For + example, if you think your friend Jason is good at darts, you would give him cred using the tag "darts". If you are a dart expert, and already have lots + of "darts" cred, then Jason's "darts" cred will go up quite a bit. If you have no "darts" cred, Jason will get only a small amount of "darts" cred. +

+ +

How do I give someone cred?

+

+ Click the "Give cred" link on a person's profile page. Then, in the box that pops up, type tags that describe his or her best skills + or qualities. In about an hour the person's cred score should reflect the new cred you've given. +

+

+ You can take back cred you have given someone by following these steps, but removing tags that you have previously given to that person instead.

+

+ <%= link_to "Giving cred, step by step", :action => 'cred_step_by_step' %> +

+ +

What are tags?

+

+ Tags are keywords that are associated with or describe a bit of information. Jyte users use tags to create categories for claims and cred, and to + indicate their interests. You can find out more about tags on Wikipedia. +

+

+ Separate multiple tags with commas. For example the following + input would yield the tags listed below. +

+ +
+

    +
  • full contact human chess
  • +
  • bicycles
  • +
  • coffee
  • +
+

+ +

So, how do I make a claim about someone?

+

+ In order for Jyte to know that your claim is about a person, you have to refer to your friend by their OpenID (What is OpenID?). +

+ +

Can I change my votes?

+

+ As many times as you like. +

+ +

How do I add a video to my claim or comment?

+

+ Jyte doesn't host video files on its website, but you can embed videos that are hosted on video sharing sites such as YouTube. Just browse to the YouTube + video that you'd like to embed in your claim. The copy the HTML code that YouTube shows in the Embed box that is to the right of the + video. Paste that link into the Description box on the claim preview page, or the Make a new comment box on a published + claim. A video will display! +

+ +

Can I add a picture to my claim or comment?

+

+ Yes! You can either upload a photo that is associated with a claim, or embed a link to a photo. +

+

+ To link to an external photo, you simply add HTML code in the Description box for your claim like this: <img src="URL to photo" + title="snide comment (optional)" /> +

+

+ To upload a photo, please use the Add an image feature when building your claim. +

+ +

Can I use Jyte groups and OpenID for authorization on my site?

+

+ Yes! That's what the API urls on the bottom of the group page are for. Feel free to mail us if you need help. +

+ +

What are Contacts and how do I use them?

+

+ If you add a user as a contact you will be able to keep track of their activity on Jyte from your personal home page. To add a user, go to their profile + page, and click the "Add to your contacts" link. +

+ +

Who do I contact for help?

+

+ Please send your Jyte support requests by email: jyte@aboutus.org +

+ +

What if I have an idea for a cool new feature for Jyte?

+

+ Make a claim about it! Tag the claim with 'jyte' and 'feature request' and we should see it. +

+ +

Does Jyte support MicroID and ClaimID?

+

+ Jyte embeds MicroIDs of your verified OpenID identifiers in your profile page. In short this means that third party services may now programatically + verify that you do indeed own your Jyte profile page. For an excellent example of this, see claimid.com's link verification. +

+ +

+ From microid.org: +

+ MicroID enables anyone to claim verifiable ownership over content hosted anywhere on the web (social networking sites, discussion forums, blogs, etc.). + MicroID is not an authentication or single-sign-on service, just a straightforward method for identifying content ownership that complements existing + technologies such as OpenID and microformats. +

+ +

How can I have my account deleted?

+

+If you send a message to Jyte@AboutUs.org, we can delete your account. +

+ +

Who is behind Jyte?

+

+JanRain created Jyte. In January 2010 Jyte was acquired by AboutUs. +

+ +

Where can I keep track of news related to Jyte?

+

+The AboutUs Weblog's posts about Jyte, @Jyte on Twitter, and Jyte's Facebook page. +

+ +
diff --git a/app/views/home/_home_tab.rhtml b/app/views/home/_home_tab.rhtml new file mode 100644 index 0000000..3c43824 --- /dev/null +++ b/app/views/home/_home_tab.rhtml @@ -0,0 +1,13 @@ +style="display:none;"<% end %> + ><%= display_tab(text) %> + +style="display:none;"<% end %> + ><%= display_tab(text) %> + + diff --git a/app/views/home/_pagination_links.rhtml b/app/views/home/_pagination_links.rhtml new file mode 100644 index 0000000..ca2caeb --- /dev/null +++ b/app/views/home/_pagination_links.rhtml @@ -0,0 +1,3 @@ + <%= link_to('Previous',{:page=>@paginator.current.previous}) if @paginator.current.previous %> + <%= pagination_links(@paginator) %> + <%= link_to('Next',{:page=>@paginator.current.next}) if @paginator.current.next %> diff --git a/app/views/home/_showing_headline.rhtml b/app/views/home/_showing_headline.rhtml new file mode 100644 index 0000000..f73a5b5 --- /dev/null +++ b/app/views/home/_showing_headline.rhtml @@ -0,0 +1,9 @@ +<% start_n = 10 * @paginator.current_page.number - 9 %> +<% end_n = 10 * @paginator.current_page.number %> +<% end_n = @claim_count if end_n > @claim_count %> + +

+Showing <%= start_n %> to <%= end_n %> of <%= @claim_count %> total. + <%= link_to('Previous',{:page=>@paginator.current.previous}) if @paginator.current.previous %> + <%= link_to('Next',{:page=>@paginator.current.next}) if @paginator.current.next %> +

diff --git a/app/views/home/_tabs.rhtml b/app/views/home/_tabs.rhtml new file mode 100644 index 0000000..c17c2ba --- /dev/null +++ b/app/views/home/_tabs.rhtml @@ -0,0 +1,43 @@ + +
+ <%= icon_image :user=>liu,:size=>'thumb',:style=>'float:left;' %> +
+

Hello, <%=liu.dn%>

+ <%= link_to('edit profile', :controller=>'user',:action=>'account') %> + <%= link_to('make a claim about yourself', :controller => 'claim',:action => 'new', :text => liu.s) %> + +
+
+
+ +
+ + <%= render(:partial => 'home_tab', + :locals => {:name => 'index', :text => 'Recent Activity', :new_count => @activity_count}) %> + + <%= render(:partial => 'home_tab', + :locals => {:name => 'contact_claims', :text => + "Contact's Claims", :new_count => 0}) + %> + + <%= render(:partial => 'home_tab', + :locals => {:name => 'interests', :text => 'Interests', + :new_count => 0}) + %> + +
+ <%= render(:partial => 'home_tab', + :locals => {:name => 'drafts', :text => 'Drafts', + :new_count => 0}) %> + + <%= render(:partial => 'home_tab', + :locals => {:name => 'settings', :text => 'Settings', + :new_count => 0}) + %> +
+ +
+
+ + + diff --git a/app/views/home/contact_claims.rhtml b/app/views/home/contact_claims.rhtml new file mode 100644 index 0000000..b768b4a --- /dev/null +++ b/app/views/home/contact_claims.rhtml @@ -0,0 +1,68 @@ +<%= render :partial => 'tabs' %> +
+ +

Contact's Claims

+

+Below are all the claims made by your +<%=link_to('contacts',:controller=>'contacts',:id=>liuid)%>. +Find more <%= link_to('contacts from gmail', :controller => :contacts, :action => :compare)%>. +

+ +<%- if @contact_ids.empty? -%> +You don't have any contacts. Click the "Add contact" link on a user's +profile page for their claims to show up here. + +<%- else -%> +
+ +
+ +<%= page_entries_info @claims %> + +
+ <%- if @claims.size > 0 -%> + + <%= render :partial => '/claim', :collection => @claims %> +
+ <%- else -%> + No claims + <%- end -%> + + <%= will_paginate @claims %> +
+ +
+
+ + + +<%- end -%> + +
diff --git a/app/views/home/drafts.rhtml b/app/views/home/drafts.rhtml new file mode 100644 index 0000000..1d3c178 --- /dev/null +++ b/app/views/home/drafts.rhtml @@ -0,0 +1,28 @@ +<%= render :partial => 'tabs' %> +
+ +

Your Draft Claims

+ +<%- if @draft_claims.empty? -%> +

No drafts.

+<%- else -%> +

Click a claim below to continue editing.

+ +<%- @draft_claims.each_with_index {|c,i| -%> + + + +<%- } -%> +
+ <%= link_to render_claim_title(c, false), + claim_preview_url(:urlslug => c.urlslug) %> +
+
+<% form_tag( {:action => 'clear_drafts'}, + {:onsubmit => 'return confirm("Really delete all your draft claims?");'}) do %> +<%= submit_tag 'Clear Drafts' %> +<% end %> +<%- end -%> + +
diff --git a/app/views/home/group_claims.rhtml b/app/views/home/group_claims.rhtml new file mode 100644 index 0000000..c59ee6b --- /dev/null +++ b/app/views/home/group_claims.rhtml @@ -0,0 +1,88 @@ +<%= render :partial => 'tabs' %> +
+ +

Group Claims

+

+All of the claims from your groups are listed below. +

+ +
+ +
+ +<%- if @groups.size > 0 -%> + + <%- if @claims.size > 0 -%> + <%= page_entries_info @claims %> + + <%= render :partial => '/claim', :collection => @claims %> +
+ <%- else -%> + No claims in your groups. + <%- end -%> + + <%= will_paginate @claims %> + +<%- else -%> + +You don't belong to any groups. + +<% if @suggested_groups.size > 0 %> + Here are some you might find interesting: + + <% cols = 5.0 %> + <% rows = (@suggested_groups.size / cols).ceil %> + <% g = 0 %> + + <% for i in 0...rows %> + + <% for j in 0...cols %> + + <% end %> + + <% end %> +
+ <% if g < @suggested_groups.size %> + <%= group_icon(:group => @suggested_groups[g]) %> + <%= h(@suggested_groups[g].name) %> + <% else %> +   + <% end %> + <% g += 1 %> +
+<% end %> + + +<%- end -%> + +
+
+ + + + +
diff --git a/app/views/home/index.rhtml b/app/views/home/index.rhtml new file mode 100644 index 0000000..48c8312 --- /dev/null +++ b/app/views/home/index.rhtml @@ -0,0 +1,166 @@ +<%= render :partial => 'tabs' %> +
+ +<% unless @dispatches.empty? %> + +<% end %> + + <% if @user_events.size > 0%> +

Other users connecting with you  clear +

+ + + <% @user_events.each_with_index {|d,i| %> + + + + + + + <% } %> +
+ <%= icon_image(:user => + d.dispatchable,:size=>'thumb')%> + + <%= d.dispatchable.dn%> <%= d.reason %> +
+
+ + <% end %> + + <% if @group_invites.size > 0 %> +

Group Invites

+ + <% @group_invites.each_with_index {|inv,i| %> + + + + + + <% } %> +
+ <%= group_icon(:group => inv.group,:size=>'thumb')%> + <%= user_link(:user=>inv.sender)%> invited you to + <%= 'moderate' if inv.group_moderator %> + <%=link_to(h(inv.group.name), gurl(inv.group))%> +
+
+ <% end %> + + + <% if @claim_invites.size > 0 %> +

Claim Invites

+ + <% @claim_invites.each_with_index {|d,i| %> + + + + <% } %> +
+ <%=icon_image(:user => d.sender, :size=>'thumb') %> + + <%= user_link(:user=>d.sender) %> invited you to + <%= link_to(render_claim_title(d.dispatchable,false),claim_url(:urlslug => d.dispatchable.urlslug)) %> +
+
+ <% end %> + +

Recent activity on + claims by, + about, + and watched by you.

+

+ +<% if @dispatches.size > 0 %> + +<%- @dispatches.each_with_index {|d,i| -%> +<% item = d.dispatchable %> + + + + +<% if d.dispatchable_type == 'Comment' %> + + + + +<% elsif d.dispatchable_type == 'Claim' and d.reason == 'mentioned' %> + + + + + +<% elsif d.dispatchable_type == 'Claim' and ['inspired','inspired by watched'].member?(d.reason) %> + + + + + +<% else %> + + + +<% end %> + + + + + +<%- } -%> +
+ <%= icon_image(:user => item.user,:size=>'thumb') %> + + <%= user_link(:user => item.user) %> commented on + <%= link_to(render_claim_title(item.claim, false), claim_url(:urlslug=>item.claim.urlslug)) %>
+
+
<%= truncate(strip_tags(safe_formatted(item.body)), 50, '') %> + <%= link_to('...', claim_url(:urlslug=>item.claim.urlslug,:anchor=>'comment_'+item.id.to_s)) %>
+
+
+ <%= icon_image(:user => item.user,:size=>'thumb') %> + + <%= user_link(:user => item.user) %> mentioned you in + <%= link_to(render_claim_title(item, false), claim_url(:urlslug=>item.urlslug)) %> + + <%= icon_image(:user => item.user,:size=>'thumb') %> + + <% inspired_by_claim = item.inspired_by_claims[0] %> + + <% if inspired_by_claim %> + <%= link_to(render_claim_title(inspired_by_claim,false),claim_url(:urlslug=>inspired_by_claim.urlslug)) %> + inspired + <%= user_link(:user => item.user) %>'s claim + <%= link_to(render_claim_title(item, false),claim_url(:urlslug=>item.urlslug)) %> + <% elsif inspired_by_comment = item.inspired_by_comments[0] %> + Your <%= link_to "comment", claim_url(:urlslug=>inspired_by_comment.claim.urlslug,:anchor=>'comment_'+inspired_by_comment.id.to_s) %> on + <%= link_to(render_claim_title(inspired_by_comment.claim,false),claim_url(:urlslug=>inspired_by_comment.claim.urlslug)) %> + inspired + <%= user_link(:user => item.user) %>'s claim + <%= link_to(render_claim_title(item, false),claim_url(:urlslug=>item.urlslug)) %> + + <% else %> + <%= link_to(render_claim_title(item, false),claim_url(:urlslug=>item.urlslug)) %> + <% end %> + +
+ +<%= will_paginate @dispatches %> + +<% else %> +No new activity. +<% end %> +

+

+<%= link_to "Claims with new comments after yours", find_claims_url(:comments_by => liu.openid, :new_comments => 'on') %> (If this link doesn't work, please click the eye on claims you care about to 'watch' and be notified about changes to them. Sorry for the inconvenience and thank you for your understanding.) + +
diff --git a/app/views/home/interests.rhtml b/app/views/home/interests.rhtml new file mode 100644 index 0000000..304ba73 --- /dev/null +++ b/app/views/home/interests.rhtml @@ -0,0 +1,40 @@ +<%= render :partial => 'tabs' %> +
+

Claims that match your interests

+
+ +
+ +
+ +<%- if liu.tags.size > 0 -%> + +<%= page_entries_info @claims %> + +
+ <%- if @claims.size > 0 -%> + + <%= render :partial => '/claim', :collection => @claims %> +
+ <%- else -%> + No claims match your interests. + <%- end -%> + + <%= will_paginate @claims %> +
+ +<%- else -%> + +You don't have any interests. +Add some by editing your profile. +<%- end -%> + +
+
+ + + + +
diff --git a/app/views/home/settings.rhtml b/app/views/home/settings.rhtml new file mode 100644 index 0000000..c4366ce --- /dev/null +++ b/app/views/home/settings.rhtml @@ -0,0 +1,48 @@ +<%= render :partial => 'tabs' %> +
+ +

Jyte Account Settings

+
+ +
+

Pibb

+ +

+Pibb is an OpenID based discussion +tool. You may optionally show a "Pibb Me" button on your Jyte profile +page. + +<% form_tag :action => 'pibbme' do %> + <% if liu.pibbme? %> + <%= submit_tag 'Hide the "Pibb Me" button' %> + <% else %> + <%= submit_tag 'Show the "Pibb Me" button' %> + <% end %> +<% end %> +
+

+
+

Claimrolls

+

+Click here to create a personal claimroll. +

+ +<% unless liu.blocked_users.empty? %> +

Ignored Users

+ <% liu.blocked_users.each {|u| %> + + <%= render :partial => 'user/ignoring', :locals => {:user => u, :ignoring => true} %> + +
+ <% } %> +<% end %> + +
+ + +

Delete your Jyte Account

+Click here to delete your Jyte account. + +
+ diff --git a/app/views/layouts/jyte.rhtml b/app/views/layouts/jyte.rhtml new file mode 100644 index 0000000..0924cd0 --- /dev/null +++ b/app/views/layouts/jyte.rhtml @@ -0,0 +1,233 @@ + + + + <% if @microids and @microids.length > 0 -%> + <% @microids.each {|m| -%> + + <% } -%> + <% end -%> + + Jyte<% unless @title.nil? %> - <%= h(@title) %><% else %>: Spread the Cred<% end %> + + <% if @meta_tags %> + <% @meta_tags.each {|name, content| %> + + <% } %> + <% end %> + + + + + <% if RAILS_ENV == 'production' -%> + + + <% else -%> + " /> + <%= javascript_include_tag :defaults %> + <%= javascript_include_tag 'global' %> + <%= javascript_include_tag 'claimpage' %> + <%= javascript_include_tag 'prototype_extensions' %> + + <% end -%> + + <% if @rss_links and @rss_links.length > 0 -%> + <% @rss_links.each {|rl| -%> + + <% } -%> + <% end -%> + + + + + <% if @flash_notice or flash[:notice] -%> +
+ + <% if @flash_notice -%> + <%= h(@flash_notice) %> + <% else -%> + <%= h(flash[:notice]) %> + <% end -%> +  close this message
+ <% else %> + <% if !logged_in? %> +
+ + Jyte is now owned by AboutUs. Read all about it here. + +
+ <% end -%> + <% end -%> + +
+
+ +
+ + <% if logged_in? and (liu.description.nil? or liu.description.empty?) and (liu.nickname.nil? or liu.nickname.empty?) and liu.image.nil? %> + + To do: + <%= link_to "Complete your profile", :controller => 'user', :action => 'account' %> + + <% end %> + +
    + <% if liu -%> +
  • Signed in as <%= liu.s %>
  • +
  • (<%= link_to 'Sign Out', :controller => 'auth', :action => 'logout' %>)
  • + <% else -%> +
  • + Sign in +
  • + <% end -%> +
  • <%= link_to 'Help',:controller=>'help' %>
  • +
+ +
+
+ + + + + <%= @content_for_layout %> + +
+ + + +
+
+ '+s} \ No newline at end of file diff --git a/public/javascripts/prototype.js b/public/javascripts/prototype.js new file mode 100644 index 0000000..dfe8ab4 --- /dev/null +++ b/public/javascripts/prototype.js @@ -0,0 +1,4320 @@ +/* Prototype JavaScript framework, version 1.6.0.3 + * (c) 2005-2008 Sam Stephenson + * + * Prototype is freely distributable under the terms of an MIT-style license. + * For details, see the Prototype web site: http://www.prototypejs.org/ + * + *--------------------------------------------------------------------------*/ + +var Prototype = { + Version: '1.6.0.3', + + Browser: { + IE: !!(window.attachEvent && + navigator.userAgent.indexOf('Opera') === -1), + Opera: navigator.userAgent.indexOf('Opera') > -1, + WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, + Gecko: navigator.userAgent.indexOf('Gecko') > -1 && + navigator.userAgent.indexOf('KHTML') === -1, + MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/) + }, + + BrowserFeatures: { + XPath: !!document.evaluate, + SelectorsAPI: !!document.querySelector, + ElementExtensions: !!window.HTMLElement, + SpecificElementExtensions: + document.createElement('div')['__proto__'] && + document.createElement('div')['__proto__'] !== + document.createElement('form')['__proto__'] + }, + + ScriptFragment: ']*>([\\S\\s]*?)<\/script>', + JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, + + emptyFunction: function() { }, + K: function(x) { return x } +}; + +if (Prototype.Browser.MobileSafari) + Prototype.BrowserFeatures.SpecificElementExtensions = false; + + +/* Based on Alex Arnell's inheritance implementation. */ +var Class = { + create: function() { + var parent = null, properties = $A(arguments); + if (Object.isFunction(properties[0])) + parent = properties.shift(); + + function klass() { + this.initialize.apply(this, arguments); + } + + Object.extend(klass, Class.Methods); + klass.superclass = parent; + klass.subclasses = []; + + if (parent) { + var subclass = function() { }; + subclass.prototype = parent.prototype; + klass.prototype = new subclass; + parent.subclasses.push(klass); + } + + for (var i = 0; i < properties.length; i++) + klass.addMethods(properties[i]); + + if (!klass.prototype.initialize) + klass.prototype.initialize = Prototype.emptyFunction; + + klass.prototype.constructor = klass; + + return klass; + } +}; + +Class.Methods = { + addMethods: function(source) { + var ancestor = this.superclass && this.superclass.prototype; + var properties = Object.keys(source); + + if (!Object.keys({ toString: true }).length) + properties.push("toString", "valueOf"); + + for (var i = 0, length = properties.length; i < length; i++) { + var property = properties[i], value = source[property]; + if (ancestor && Object.isFunction(value) && + value.argumentNames().first() == "$super") { + var method = value; + value = (function(m) { + return function() { return ancestor[m].apply(this, arguments) }; + })(property).wrap(method); + + value.valueOf = method.valueOf.bind(method); + value.toString = method.toString.bind(method); + } + this.prototype[property] = value; + } + + return this; + } +}; + +var Abstract = { }; + +Object.extend = function(destination, source) { + for (var property in source) + destination[property] = source[property]; + return destination; +}; + +Object.extend(Object, { + inspect: function(object) { + try { + if (Object.isUndefined(object)) return 'undefined'; + if (object === null) return 'null'; + return object.inspect ? object.inspect() : String(object); + } catch (e) { + if (e instanceof RangeError) return '...'; + throw e; + } + }, + + toJSON: function(object) { + var type = typeof object; + switch (type) { + case 'undefined': + case 'function': + case 'unknown': return; + case 'boolean': return object.toString(); + } + + if (object === null) return 'null'; + if (object.toJSON) return object.toJSON(); + if (Object.isElement(object)) return; + + var results = []; + for (var property in object) { + var value = Object.toJSON(object[property]); + if (!Object.isUndefined(value)) + results.push(property.toJSON() + ': ' + value); + } + + return '{' + results.join(', ') + '}'; + }, + + toQueryString: function(object) { + return $H(object).toQueryString(); + }, + + toHTML: function(object) { + return object && object.toHTML ? object.toHTML() : String.interpret(object); + }, + + keys: function(object) { + var keys = []; + for (var property in object) + keys.push(property); + return keys; + }, + + values: function(object) { + var values = []; + for (var property in object) + values.push(object[property]); + return values; + }, + + clone: function(object) { + return Object.extend({ }, object); + }, + + isElement: function(object) { + return !!(object && object.nodeType == 1); + }, + + isArray: function(object) { + return object != null && typeof object == "object" && + 'splice' in object && 'join' in object; + }, + + isHash: function(object) { + return object instanceof Hash; + }, + + isFunction: function(object) { + return typeof object == "function"; + }, + + isString: function(object) { + return typeof object == "string"; + }, + + isNumber: function(object) { + return typeof object == "number"; + }, + + isUndefined: function(object) { + return typeof object == "undefined"; + } +}); + +Object.extend(Function.prototype, { + argumentNames: function() { + var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1] + .replace(/\s+/g, '').split(','); + return names.length == 1 && !names[0] ? [] : names; + }, + + bind: function() { + if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; + var __method = this, args = $A(arguments), object = args.shift(); + return function() { + return __method.apply(object, args.concat($A(arguments))); + } + }, + + bindAsEventListener: function() { + var __method = this, args = $A(arguments), object = args.shift(); + return function(event) { + return __method.apply(object, [event || window.event].concat(args)); + } + }, + + curry: function() { + if (!arguments.length) return this; + var __method = this, args = $A(arguments); + return function() { + return __method.apply(this, args.concat($A(arguments))); + } + }, + + delay: function() { + var __method = this, args = $A(arguments), timeout = args.shift() * 1000; + return window.setTimeout(function() { + return __method.apply(__method, args); + }, timeout); + }, + + defer: function() { + var args = [0.01].concat($A(arguments)); + return this.delay.apply(this, args); + }, + + wrap: function(wrapper) { + var __method = this; + return function() { + return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); + } + }, + + methodize: function() { + if (this._methodized) return this._methodized; + var __method = this; + return this._methodized = function() { + return __method.apply(null, [this].concat($A(arguments))); + }; + } +}); + +Date.prototype.toJSON = function() { + return '"' + this.getUTCFullYear() + '-' + + (this.getUTCMonth() + 1).toPaddedString(2) + '-' + + this.getUTCDate().toPaddedString(2) + 'T' + + this.getUTCHours().toPaddedString(2) + ':' + + this.getUTCMinutes().toPaddedString(2) + ':' + + this.getUTCSeconds().toPaddedString(2) + 'Z"'; +}; + +var Try = { + these: function() { + var returnValue; + + for (var i = 0, length = arguments.length; i < length; i++) { + var lambda = arguments[i]; + try { + returnValue = lambda(); + break; + } catch (e) { } + } + + return returnValue; + } +}; + +RegExp.prototype.match = RegExp.prototype.test; + +RegExp.escape = function(str) { + return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); +}; + +/*--------------------------------------------------------------------------*/ + +var PeriodicalExecuter = Class.create({ + initialize: function(callback, frequency) { + this.callback = callback; + this.frequency = frequency; + this.currentlyExecuting = false; + + this.registerCallback(); + }, + + registerCallback: function() { + this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + execute: function() { + this.callback(this); + }, + + stop: function() { + if (!this.timer) return; + clearInterval(this.timer); + this.timer = null; + }, + + onTimerEvent: function() { + if (!this.currentlyExecuting) { + try { + this.currentlyExecuting = true; + this.execute(); + } finally { + this.currentlyExecuting = false; + } + } + } +}); +Object.extend(String, { + interpret: function(value) { + return value == null ? '' : String(value); + }, + specialChar: { + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '\\': '\\\\' + } +}); + +Object.extend(String.prototype, { + gsub: function(pattern, replacement) { + var result = '', source = this, match; + replacement = arguments.callee.prepareReplacement(replacement); + + while (source.length > 0) { + if (match = source.match(pattern)) { + result += source.slice(0, match.index); + result += String.interpret(replacement(match)); + source = source.slice(match.index + match[0].length); + } else { + result += source, source = ''; + } + } + return result; + }, + + sub: function(pattern, replacement, count) { + replacement = this.gsub.prepareReplacement(replacement); + count = Object.isUndefined(count) ? 1 : count; + + return this.gsub(pattern, function(match) { + if (--count < 0) return match[0]; + return replacement(match); + }); + }, + + scan: function(pattern, iterator) { + this.gsub(pattern, iterator); + return String(this); + }, + + truncate: function(length, truncation) { + length = length || 30; + truncation = Object.isUndefined(truncation) ? '...' : truncation; + return this.length > length ? + this.slice(0, length - truncation.length) + truncation : String(this); + }, + + strip: function() { + return this.replace(/^\s+/, '').replace(/\s+$/, ''); + }, + + stripTags: function() { + return this.replace(/<\/?[^>]+>/gi, ''); + }, + + stripScripts: function() { + return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); + }, + + extractScripts: function() { + var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); + var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); + return (this.match(matchAll) || []).map(function(scriptTag) { + return (scriptTag.match(matchOne) || ['', ''])[1]; + }); + }, + + evalScripts: function() { + return this.extractScripts().map(function(script) { return eval(script) }); + }, + + escapeHTML: function() { + var self = arguments.callee; + self.text.data = this; + return self.div.innerHTML; + }, + + unescapeHTML: function() { + var div = new Element('div'); + div.innerHTML = this.stripTags(); + return div.childNodes[0] ? (div.childNodes.length > 1 ? + $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : + div.childNodes[0].nodeValue) : ''; + }, + + toQueryParams: function(separator) { + var match = this.strip().match(/([^?#]*)(#.*)?$/); + if (!match) return { }; + + return match[1].split(separator || '&').inject({ }, function(hash, pair) { + if ((pair = pair.split('='))[0]) { + var key = decodeURIComponent(pair.shift()); + var value = pair.length > 1 ? pair.join('=') : pair[0]; + if (value != undefined) value = decodeURIComponent(value); + + if (key in hash) { + if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; + hash[key].push(value); + } + else hash[key] = value; + } + return hash; + }); + }, + + toArray: function() { + return this.split(''); + }, + + succ: function() { + return this.slice(0, this.length - 1) + + String.fromCharCode(this.charCodeAt(this.length - 1) + 1); + }, + + times: function(count) { + return count < 1 ? '' : new Array(count + 1).join(this); + }, + + camelize: function() { + var parts = this.split('-'), len = parts.length; + if (len == 1) return parts[0]; + + var camelized = this.charAt(0) == '-' + ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) + : parts[0]; + + for (var i = 1; i < len; i++) + camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); + + return camelized; + }, + + capitalize: function() { + return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); + }, + + underscore: function() { + return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); + }, + + dasherize: function() { + return this.gsub(/_/,'-'); + }, + + inspect: function(useDoubleQuotes) { + var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { + var character = String.specialChar[match[0]]; + return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); + }); + if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; + return "'" + escapedString.replace(/'/g, '\\\'') + "'"; + }, + + toJSON: function() { + return this.inspect(true); + }, + + unfilterJSON: function(filter) { + return this.sub(filter || Prototype.JSONFilter, '#{1}'); + }, + + isJSON: function() { + var str = this; + if (str.blank()) return false; + str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); + return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); + }, + + evalJSON: function(sanitize) { + var json = this.unfilterJSON(); + try { + if (!sanitize || json.isJSON()) return eval('(' + json + ')'); + } catch (e) { } + throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); + }, + + include: function(pattern) { + return this.indexOf(pattern) > -1; + }, + + startsWith: function(pattern) { + return this.indexOf(pattern) === 0; + }, + + endsWith: function(pattern) { + var d = this.length - pattern.length; + return d >= 0 && this.lastIndexOf(pattern) === d; + }, + + empty: function() { + return this == ''; + }, + + blank: function() { + return /^\s*$/.test(this); + }, + + interpolate: function(object, pattern) { + return new Template(this, pattern).evaluate(object); + } +}); + +if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, { + escapeHTML: function() { + return this.replace(/&/g,'&').replace(//g,'>'); + }, + unescapeHTML: function() { + return this.stripTags().replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); + } +}); + +String.prototype.gsub.prepareReplacement = function(replacement) { + if (Object.isFunction(replacement)) return replacement; + var template = new Template(replacement); + return function(match) { return template.evaluate(match) }; +}; + +String.prototype.parseQuery = String.prototype.toQueryParams; + +Object.extend(String.prototype.escapeHTML, { + div: document.createElement('div'), + text: document.createTextNode('') +}); + +String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text); + +var Template = Class.create({ + initialize: function(template, pattern) { + this.template = template.toString(); + this.pattern = pattern || Template.Pattern; + }, + + evaluate: function(object) { + if (Object.isFunction(object.toTemplateReplacements)) + object = object.toTemplateReplacements(); + + return this.template.gsub(this.pattern, function(match) { + if (object == null) return ''; + + var before = match[1] || ''; + if (before == '\\') return match[2]; + + var ctx = object, expr = match[3]; + var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; + match = pattern.exec(expr); + if (match == null) return before; + + while (match != null) { + var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1]; + ctx = ctx[comp]; + if (null == ctx || '' == match[3]) break; + expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); + match = pattern.exec(expr); + } + + return before + String.interpret(ctx); + }); + } +}); +Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; + +var $break = { }; + +var Enumerable = { + each: function(iterator, context) { + var index = 0; + try { + this._each(function(value) { + iterator.call(context, value, index++); + }); + } catch (e) { + if (e != $break) throw e; + } + return this; + }, + + eachSlice: function(number, iterator, context) { + var index = -number, slices = [], array = this.toArray(); + if (number < 1) return array; + while ((index += number) < array.length) + slices.push(array.slice(index, index+number)); + return slices.collect(iterator, context); + }, + + all: function(iterator, context) { + iterator = iterator || Prototype.K; + var result = true; + this.each(function(value, index) { + result = result && !!iterator.call(context, value, index); + if (!result) throw $break; + }); + return result; + }, + + any: function(iterator, context) { + iterator = iterator || Prototype.K; + var result = false; + this.each(function(value, index) { + if (result = !!iterator.call(context, value, index)) + throw $break; + }); + return result; + }, + + collect: function(iterator, context) { + iterator = iterator || Prototype.K; + var results = []; + this.each(function(value, index) { + results.push(iterator.call(context, value, index)); + }); + return results; + }, + + detect: function(iterator, context) { + var result; + this.each(function(value, index) { + if (iterator.call(context, value, index)) { + result = value; + throw $break; + } + }); + return result; + }, + + findAll: function(iterator, context) { + var results = []; + this.each(function(value, index) { + if (iterator.call(context, value, index)) + results.push(value); + }); + return results; + }, + + grep: function(filter, iterator, context) { + iterator = iterator || Prototype.K; + var results = []; + + if (Object.isString(filter)) + filter = new RegExp(filter); + + this.each(function(value, index) { + if (filter.match(value)) + results.push(iterator.call(context, value, index)); + }); + return results; + }, + + include: function(object) { + if (Object.isFunction(this.indexOf)) + if (this.indexOf(object) != -1) return true; + + var found = false; + this.each(function(value) { + if (value == object) { + found = true; + throw $break; + } + }); + return found; + }, + + inGroupsOf: function(number, fillWith) { + fillWith = Object.isUndefined(fillWith) ? null : fillWith; + return this.eachSlice(number, function(slice) { + while(slice.length < number) slice.push(fillWith); + return slice; + }); + }, + + inject: function(memo, iterator, context) { + this.each(function(value, index) { + memo = iterator.call(context, memo, value, index); + }); + return memo; + }, + + invoke: function(method) { + var args = $A(arguments).slice(1); + return this.map(function(value) { + return value[method].apply(value, args); + }); + }, + + max: function(iterator, context) { + iterator = iterator || Prototype.K; + var result; + this.each(function(value, index) { + value = iterator.call(context, value, index); + if (result == null || value >= result) + result = value; + }); + return result; + }, + + min: function(iterator, context) { + iterator = iterator || Prototype.K; + var result; + this.each(function(value, index) { + value = iterator.call(context, value, index); + if (result == null || value < result) + result = value; + }); + return result; + }, + + partition: function(iterator, context) { + iterator = iterator || Prototype.K; + var trues = [], falses = []; + this.each(function(value, index) { + (iterator.call(context, value, index) ? + trues : falses).push(value); + }); + return [trues, falses]; + }, + + pluck: function(property) { + var results = []; + this.each(function(value) { + results.push(value[property]); + }); + return results; + }, + + reject: function(iterator, context) { + var results = []; + this.each(function(value, index) { + if (!iterator.call(context, value, index)) + results.push(value); + }); + return results; + }, + + sortBy: function(iterator, context) { + return this.map(function(value, index) { + return { + value: value, + criteria: iterator.call(context, value, index) + }; + }).sort(function(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }).pluck('value'); + }, + + toArray: function() { + return this.map(); + }, + + zip: function() { + var iterator = Prototype.K, args = $A(arguments); + if (Object.isFunction(args.last())) + iterator = args.pop(); + + var collections = [this].concat(args).map($A); + return this.map(function(value, index) { + return iterator(collections.pluck(index)); + }); + }, + + size: function() { + return this.toArray().length; + }, + + inspect: function() { + return '#'; + } +}; + +Object.extend(Enumerable, { + map: Enumerable.collect, + find: Enumerable.detect, + select: Enumerable.findAll, + filter: Enumerable.findAll, + member: Enumerable.include, + entries: Enumerable.toArray, + every: Enumerable.all, + some: Enumerable.any +}); +function $A(iterable) { + if (!iterable) return []; + if (iterable.toArray) return iterable.toArray(); + var length = iterable.length || 0, results = new Array(length); + while (length--) results[length] = iterable[length]; + return results; +} + +if (Prototype.Browser.WebKit) { + $A = function(iterable) { + if (!iterable) return []; + // In Safari, only use the `toArray` method if it's not a NodeList. + // A NodeList is a function, has an function `item` property, and a numeric + // `length` property. Adapted from Google Doctype. + if (!(typeof iterable === 'function' && typeof iterable.length === + 'number' && typeof iterable.item === 'function') && iterable.toArray) + return iterable.toArray(); + var length = iterable.length || 0, results = new Array(length); + while (length--) results[length] = iterable[length]; + return results; + }; +} + +Array.from = $A; + +Object.extend(Array.prototype, Enumerable); + +if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse; + +Object.extend(Array.prototype, { + _each: function(iterator) { + for (var i = 0, length = this.length; i < length; i++) + iterator(this[i]); + }, + + clear: function() { + this.length = 0; + return this; + }, + + first: function() { + return this[0]; + }, + + last: function() { + return this[this.length - 1]; + }, + + compact: function() { + return this.select(function(value) { + return value != null; + }); + }, + + flatten: function() { + return this.inject([], function(array, value) { + return array.concat(Object.isArray(value) ? + value.flatten() : [value]); + }); + }, + + without: function() { + var values = $A(arguments); + return this.select(function(value) { + return !values.include(value); + }); + }, + + reverse: function(inline) { + return (inline !== false ? this : this.toArray())._reverse(); + }, + + reduce: function() { + return this.length > 1 ? this : this[0]; + }, + + uniq: function(sorted) { + return this.inject([], function(array, value, index) { + if (0 == index || (sorted ? array.last() != value : !array.include(value))) + array.push(value); + return array; + }); + }, + + intersect: function(array) { + return this.uniq().findAll(function(item) { + return array.detect(function(value) { return item === value }); + }); + }, + + clone: function() { + return [].concat(this); + }, + + size: function() { + return this.length; + }, + + inspect: function() { + return '[' + this.map(Object.inspect).join(', ') + ']'; + }, + + toJSON: function() { + var results = []; + this.each(function(object) { + var value = Object.toJSON(object); + if (!Object.isUndefined(value)) results.push(value); + }); + return '[' + results.join(', ') + ']'; + } +}); + +// use native browser JS 1.6 implementation if available +if (Object.isFunction(Array.prototype.forEach)) + Array.prototype._each = Array.prototype.forEach; + +if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { + i || (i = 0); + var length = this.length; + if (i < 0) i = length + i; + for (; i < length; i++) + if (this[i] === item) return i; + return -1; +}; + +if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { + i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; + var n = this.slice(0, i).reverse().indexOf(item); + return (n < 0) ? n : i - n - 1; +}; + +Array.prototype.toArray = Array.prototype.clone; + +function $w(string) { + if (!Object.isString(string)) return []; + string = string.strip(); + return string ? string.split(/\s+/) : []; +} + +if (Prototype.Browser.Opera){ + Array.prototype.concat = function() { + var array = []; + for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); + for (var i = 0, length = arguments.length; i < length; i++) { + if (Object.isArray(arguments[i])) { + for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) + array.push(arguments[i][j]); + } else { + array.push(arguments[i]); + } + } + return array; + }; +} +Object.extend(Number.prototype, { + toColorPart: function() { + return this.toPaddedString(2, 16); + }, + + succ: function() { + return this + 1; + }, + + times: function(iterator, context) { + $R(0, this, true).each(iterator, context); + return this; + }, + + toPaddedString: function(length, radix) { + var string = this.toString(radix || 10); + return '0'.times(length - string.length) + string; + }, + + toJSON: function() { + return isFinite(this) ? this.toString() : 'null'; + } +}); + +$w('abs round ceil floor').each(function(method){ + Number.prototype[method] = Math[method].methodize(); +}); +function $H(object) { + return new Hash(object); +}; + +var Hash = Class.create(Enumerable, (function() { + + function toQueryPair(key, value) { + if (Object.isUndefined(value)) return key; + return key + '=' + encodeURIComponent(String.interpret(value)); + } + + return { + initialize: function(object) { + this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); + }, + + _each: function(iterator) { + for (var key in this._object) { + var value = this._object[key], pair = [key, value]; + pair.key = key; + pair.value = value; + iterator(pair); + } + }, + + set: function(key, value) { + return this._object[key] = value; + }, + + get: function(key) { + // simulating poorly supported hasOwnProperty + if (this._object[key] !== Object.prototype[key]) + return this._object[key]; + }, + + unset: function(key) { + var value = this._object[key]; + delete this._object[key]; + return value; + }, + + toObject: function() { + return Object.clone(this._object); + }, + + keys: function() { + return this.pluck('key'); + }, + + values: function() { + return this.pluck('value'); + }, + + index: function(value) { + var match = this.detect(function(pair) { + return pair.value === value; + }); + return match && match.key; + }, + + merge: function(object) { + return this.clone().update(object); + }, + + update: function(object) { + return new Hash(object).inject(this, function(result, pair) { + result.set(pair.key, pair.value); + return result; + }); + }, + + toQueryString: function() { + return this.inject([], function(results, pair) { + var key = encodeURIComponent(pair.key), values = pair.value; + + if (values && typeof values == 'object') { + if (Object.isArray(values)) + return results.concat(values.map(toQueryPair.curry(key))); + } else results.push(toQueryPair(key, values)); + return results; + }).join('&'); + }, + + inspect: function() { + return '#'; + }, + + toJSON: function() { + return Object.toJSON(this.toObject()); + }, + + clone: function() { + return new Hash(this); + } + } +})()); + +Hash.prototype.toTemplateReplacements = Hash.prototype.toObject; +Hash.from = $H; +var ObjectRange = Class.create(Enumerable, { + initialize: function(start, end, exclusive) { + this.start = start; + this.end = end; + this.exclusive = exclusive; + }, + + _each: function(iterator) { + var value = this.start; + while (this.include(value)) { + iterator(value); + value = value.succ(); + } + }, + + include: function(value) { + if (value < this.start) + return false; + if (this.exclusive) + return value < this.end; + return value <= this.end; + } +}); + +var $R = function(start, end, exclusive) { + return new ObjectRange(start, end, exclusive); +}; + +var Ajax = { + getTransport: function() { + return Try.these( + function() {return new XMLHttpRequest()}, + function() {return new ActiveXObject('Msxml2.XMLHTTP')}, + function() {return new ActiveXObject('Microsoft.XMLHTTP')} + ) || false; + }, + + activeRequestCount: 0 +}; + +Ajax.Responders = { + responders: [], + + _each: function(iterator) { + this.responders._each(iterator); + }, + + register: function(responder) { + if (!this.include(responder)) + this.responders.push(responder); + }, + + unregister: function(responder) { + this.responders = this.responders.without(responder); + }, + + dispatch: function(callback, request, transport, json) { + this.each(function(responder) { + if (Object.isFunction(responder[callback])) { + try { + responder[callback].apply(responder, [request, transport, json]); + } catch (e) { } + } + }); + } +}; + +Object.extend(Ajax.Responders, Enumerable); + +Ajax.Responders.register({ + onCreate: function() { Ajax.activeRequestCount++ }, + onComplete: function() { Ajax.activeRequestCount-- } +}); + +Ajax.Base = Class.create({ + initialize: function(options) { + this.options = { + method: 'post', + asynchronous: true, + contentType: 'application/x-www-form-urlencoded', + encoding: 'UTF-8', + parameters: '', + evalJSON: true, + evalJS: true + }; + Object.extend(this.options, options || { }); + + this.options.method = this.options.method.toLowerCase(); + + if (Object.isString(this.options.parameters)) + this.options.parameters = this.options.parameters.toQueryParams(); + else if (Object.isHash(this.options.parameters)) + this.options.parameters = this.options.parameters.toObject(); + } +}); + +Ajax.Request = Class.create(Ajax.Base, { + _complete: false, + + initialize: function($super, url, options) { + $super(options); + this.transport = Ajax.getTransport(); + this.request(url); + }, + + request: function(url) { + this.url = url; + this.method = this.options.method; + var params = Object.clone(this.options.parameters); + + if (!['get', 'post'].include(this.method)) { + // simulate other verbs over post + params['_method'] = this.method; + this.method = 'post'; + } + + this.parameters = params; + + if (params = Object.toQueryString(params)) { + // when GET, append parameters to URL + if (this.method == 'get') + this.url += (this.url.include('?') ? '&' : '?') + params; + else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) + params += '&_='; + } + + try { + var response = new Ajax.Response(this); + if (this.options.onCreate) this.options.onCreate(response); + Ajax.Responders.dispatch('onCreate', this, response); + + this.transport.open(this.method.toUpperCase(), this.url, + this.options.asynchronous); + + if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); + + this.transport.onreadystatechange = this.onStateChange.bind(this); + this.setRequestHeaders(); + + this.body = this.method == 'post' ? (this.options.postBody || params) : null; + this.transport.send(this.body); + + /* Force Firefox to handle ready state 4 for synchronous requests */ + if (!this.options.asynchronous && this.transport.overrideMimeType) + this.onStateChange(); + + } + catch (e) { + this.dispatchException(e); + } + }, + + onStateChange: function() { + var readyState = this.transport.readyState; + if (readyState > 1 && !((readyState == 4) && this._complete)) + this.respondToReadyState(this.transport.readyState); + }, + + setRequestHeaders: function() { + var headers = { + 'X-Requested-With': 'XMLHttpRequest', + 'X-Prototype-Version': Prototype.Version, + 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' + }; + + if (this.method == 'post') { + headers['Content-type'] = this.options.contentType + + (this.options.encoding ? '; charset=' + this.options.encoding : ''); + + /* Force "Connection: close" for older Mozilla browsers to work + * around a bug where XMLHttpRequest sends an incorrect + * Content-length header. See Mozilla Bugzilla #246651. + */ + if (this.transport.overrideMimeType && + (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) + headers['Connection'] = 'close'; + } + + // user-defined headers + if (typeof this.options.requestHeaders == 'object') { + var extras = this.options.requestHeaders; + + if (Object.isFunction(extras.push)) + for (var i = 0, length = extras.length; i < length; i += 2) + headers[extras[i]] = extras[i+1]; + else + $H(extras).each(function(pair) { headers[pair.key] = pair.value }); + } + + for (var name in headers) + this.transport.setRequestHeader(name, headers[name]); + }, + + success: function() { + var status = this.getStatus(); + return !status || (status >= 200 && status < 300); + }, + + getStatus: function() { + try { + return this.transport.status || 0; + } catch (e) { return 0 } + }, + + respondToReadyState: function(readyState) { + var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); + + if (state == 'Complete') { + try { + this._complete = true; + (this.options['on' + response.status] + || this.options['on' + (this.success() ? 'Success' : 'Failure')] + || Prototype.emptyFunction)(response, response.headerJSON); + } catch (e) { + this.dispatchException(e); + } + + var contentType = response.getHeader('Content-type'); + if (this.options.evalJS == 'force' + || (this.options.evalJS && this.isSameOrigin() && contentType + && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) + this.evalResponse(); + } + + try { + (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON); + Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); + } catch (e) { + this.dispatchException(e); + } + + if (state == 'Complete') { + // avoid memory leak in MSIE: clean up + this.transport.onreadystatechange = Prototype.emptyFunction; + } + }, + + isSameOrigin: function() { + var m = this.url.match(/^\s*https?:\/\/[^\/]*/); + return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({ + protocol: location.protocol, + domain: document.domain, + port: location.port ? ':' + location.port : '' + })); + }, + + getHeader: function(name) { + try { + return this.transport.getResponseHeader(name) || null; + } catch (e) { return null } + }, + + evalResponse: function() { + try { + return eval((this.transport.responseText || '').unfilterJSON()); + } catch (e) { + this.dispatchException(e); + } + }, + + dispatchException: function(exception) { + (this.options.onException || Prototype.emptyFunction)(this, exception); + Ajax.Responders.dispatch('onException', this, exception); + } +}); + +Ajax.Request.Events = + ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; + +Ajax.Response = Class.create({ + initialize: function(request){ + this.request = request; + var transport = this.transport = request.transport, + readyState = this.readyState = transport.readyState; + + if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) { + this.status = this.getStatus(); + this.statusText = this.getStatusText(); + this.responseText = String.interpret(transport.responseText); + this.headerJSON = this._getHeaderJSON(); + } + + if(readyState == 4) { + var xml = transport.responseXML; + this.responseXML = Object.isUndefined(xml) ? null : xml; + this.responseJSON = this._getResponseJSON(); + } + }, + + status: 0, + statusText: '', + + getStatus: Ajax.Request.prototype.getStatus, + + getStatusText: function() { + try { + return this.transport.statusText || ''; + } catch (e) { return '' } + }, + + getHeader: Ajax.Request.prototype.getHeader, + + getAllHeaders: function() { + try { + return this.getAllResponseHeaders(); + } catch (e) { return null } + }, + + getResponseHeader: function(name) { + return this.transport.getResponseHeader(name); + }, + + getAllResponseHeaders: function() { + return this.transport.getAllResponseHeaders(); + }, + + _getHeaderJSON: function() { + var json = this.getHeader('X-JSON'); + if (!json) return null; + json = decodeURIComponent(escape(json)); + try { + return json.evalJSON(this.request.options.sanitizeJSON || + !this.request.isSameOrigin()); + } catch (e) { + this.request.dispatchException(e); + } + }, + + _getResponseJSON: function() { + var options = this.request.options; + if (!options.evalJSON || (options.evalJSON != 'force' && + !(this.getHeader('Content-type') || '').include('application/json')) || + this.responseText.blank()) + return null; + try { + return this.responseText.evalJSON(options.sanitizeJSON || + !this.request.isSameOrigin()); + } catch (e) { + this.request.dispatchException(e); + } + } +}); + +Ajax.Updater = Class.create(Ajax.Request, { + initialize: function($super, container, url, options) { + this.container = { + success: (container.success || container), + failure: (container.failure || (container.success ? null : container)) + }; + + options = Object.clone(options); + var onComplete = options.onComplete; + options.onComplete = (function(response, json) { + this.updateContent(response.responseText); + if (Object.isFunction(onComplete)) onComplete(response, json); + }).bind(this); + + $super(url, options); + }, + + updateContent: function(responseText) { + var receiver = this.container[this.success() ? 'success' : 'failure'], + options = this.options; + + if (!options.evalScripts) responseText = responseText.stripScripts(); + + if (receiver = $(receiver)) { + if (options.insertion) { + if (Object.isString(options.insertion)) { + var insertion = { }; insertion[options.insertion] = responseText; + receiver.insert(insertion); + } + else options.insertion(receiver, responseText); + } + else receiver.update(responseText); + } + } +}); + +Ajax.PeriodicalUpdater = Class.create(Ajax.Base, { + initialize: function($super, container, url, options) { + $super(options); + this.onComplete = this.options.onComplete; + + this.frequency = (this.options.frequency || 2); + this.decay = (this.options.decay || 1); + + this.updater = { }; + this.container = container; + this.url = url; + + this.start(); + }, + + start: function() { + this.options.onComplete = this.updateComplete.bind(this); + this.onTimerEvent(); + }, + + stop: function() { + this.updater.options.onComplete = undefined; + clearTimeout(this.timer); + (this.onComplete || Prototype.emptyFunction).apply(this, arguments); + }, + + updateComplete: function(response) { + if (this.options.decay) { + this.decay = (response.responseText == this.lastText ? + this.decay * this.options.decay : 1); + + this.lastText = response.responseText; + } + this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); + }, + + onTimerEvent: function() { + this.updater = new Ajax.Updater(this.container, this.url, this.options); + } +}); +function $(element) { + if (arguments.length > 1) { + for (var i = 0, elements = [], length = arguments.length; i < length; i++) + elements.push($(arguments[i])); + return elements; + } + if (Object.isString(element)) + element = document.getElementById(element); + return Element.extend(element); +} + +if (Prototype.BrowserFeatures.XPath) { + document._getElementsByXPath = function(expression, parentElement) { + var results = []; + var query = document.evaluate(expression, $(parentElement) || document, + null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + for (var i = 0, length = query.snapshotLength; i < length; i++) + results.push(Element.extend(query.snapshotItem(i))); + return results; + }; +} + +/*--------------------------------------------------------------------------*/ + +if (!window.Node) var Node = { }; + +if (!Node.ELEMENT_NODE) { + // DOM level 2 ECMAScript Language Binding + Object.extend(Node, { + ELEMENT_NODE: 1, + ATTRIBUTE_NODE: 2, + TEXT_NODE: 3, + CDATA_SECTION_NODE: 4, + ENTITY_REFERENCE_NODE: 5, + ENTITY_NODE: 6, + PROCESSING_INSTRUCTION_NODE: 7, + COMMENT_NODE: 8, + DOCUMENT_NODE: 9, + DOCUMENT_TYPE_NODE: 10, + DOCUMENT_FRAGMENT_NODE: 11, + NOTATION_NODE: 12 + }); +} + +(function() { + var element = this.Element; + this.Element = function(tagName, attributes) { + attributes = attributes || { }; + tagName = tagName.toLowerCase(); + var cache = Element.cache; + if (Prototype.Browser.IE && attributes.name) { + tagName = '<' + tagName + ' name="' + attributes.name + '">'; + delete attributes.name; + return Element.writeAttribute(document.createElement(tagName), attributes); + } + if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName)); + return Element.writeAttribute(cache[tagName].cloneNode(false), attributes); + }; + Object.extend(this.Element, element || { }); + if (element) this.Element.prototype = element.prototype; +}).call(window); + +Element.cache = { }; + +Element.Methods = { + visible: function(element) { + return $(element).style.display != 'none'; + }, + + toggle: function(element) { + element = $(element); + Element[Element.visible(element) ? 'hide' : 'show'](element); + return element; + }, + + hide: function(element) { + element = $(element); + element.style.display = 'none'; + return element; + }, + + show: function(element) { + element = $(element); + element.style.display = ''; + return element; + }, + + remove: function(element) { + element = $(element); + element.parentNode.removeChild(element); + return element; + }, + + update: function(element, content) { + element = $(element); + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) return element.update().insert(content); + content = Object.toHTML(content); + element.innerHTML = content.stripScripts(); + content.evalScripts.bind(content).defer(); + return element; + }, + + replace: function(element, content) { + element = $(element); + if (content && content.toElement) content = content.toElement(); + else if (!Object.isElement(content)) { + content = Object.toHTML(content); + var range = element.ownerDocument.createRange(); + range.selectNode(element); + content.evalScripts.bind(content).defer(); + content = range.createContextualFragment(content.stripScripts()); + } + element.parentNode.replaceChild(content, element); + return element; + }, + + insert: function(element, insertions) { + element = $(element); + + if (Object.isString(insertions) || Object.isNumber(insertions) || + Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML))) + insertions = {bottom:insertions}; + + var content, insert, tagName, childNodes; + + for (var position in insertions) { + content = insertions[position]; + position = position.toLowerCase(); + insert = Element._insertionTranslations[position]; + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) { + insert(element, content); + continue; + } + + content = Object.toHTML(content); + + tagName = ((position == 'before' || position == 'after') + ? element.parentNode : element).tagName.toUpperCase(); + + childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); + + if (position == 'top' || position == 'after') childNodes.reverse(); + childNodes.each(insert.curry(element)); + + content.evalScripts.bind(content).defer(); + } + + return element; + }, + + wrap: function(element, wrapper, attributes) { + element = $(element); + if (Object.isElement(wrapper)) + $(wrapper).writeAttribute(attributes || { }); + else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes); + else wrapper = new Element('div', wrapper); + if (element.parentNode) + element.parentNode.replaceChild(wrapper, element); + wrapper.appendChild(element); + return wrapper; + }, + + inspect: function(element) { + element = $(element); + var result = '<' + element.tagName.toLowerCase(); + $H({'id': 'id', 'className': 'class'}).each(function(pair) { + var property = pair.first(), attribute = pair.last(); + var value = (element[property] || '').toString(); + if (value) result += ' ' + attribute + '=' + value.inspect(true); + }); + return result + '>'; + }, + + recursivelyCollect: function(element, property) { + element = $(element); + var elements = []; + while (element = element[property]) + if (element.nodeType == 1) + elements.push(Element.extend(element)); + return elements; + }, + + ancestors: function(element) { + return $(element).recursivelyCollect('parentNode'); + }, + + descendants: function(element) { + return $(element).select("*"); + }, + + firstDescendant: function(element) { + element = $(element).firstChild; + while (element && element.nodeType != 1) element = element.nextSibling; + return $(element); + }, + + immediateDescendants: function(element) { + if (!(element = $(element).firstChild)) return []; + while (element && element.nodeType != 1) element = element.nextSibling; + if (element) return [element].concat($(element).nextSiblings()); + return []; + }, + + previousSiblings: function(element) { + return $(element).recursivelyCollect('previousSibling'); + }, + + nextSiblings: function(element) { + return $(element).recursivelyCollect('nextSibling'); + }, + + siblings: function(element) { + element = $(element); + return element.previousSiblings().reverse().concat(element.nextSiblings()); + }, + + match: function(element, selector) { + if (Object.isString(selector)) + selector = new Selector(selector); + return selector.match($(element)); + }, + + up: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(element.parentNode); + var ancestors = element.ancestors(); + return Object.isNumber(expression) ? ancestors[expression] : + Selector.findElement(ancestors, expression, index); + }, + + down: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return element.firstDescendant(); + return Object.isNumber(expression) ? element.descendants()[expression] : + Element.select(element, expression)[index || 0]; + }, + + previous: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element)); + var previousSiblings = element.previousSiblings(); + return Object.isNumber(expression) ? previousSiblings[expression] : + Selector.findElement(previousSiblings, expression, index); + }, + + next: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element)); + var nextSiblings = element.nextSiblings(); + return Object.isNumber(expression) ? nextSiblings[expression] : + Selector.findElement(nextSiblings, expression, index); + }, + + select: function() { + var args = $A(arguments), element = $(args.shift()); + return Selector.findChildElements(element, args); + }, + + adjacent: function() { + var args = $A(arguments), element = $(args.shift()); + return Selector.findChildElements(element.parentNode, args).without(element); + }, + + identify: function(element) { + element = $(element); + var id = element.readAttribute('id'), self = arguments.callee; + if (id) return id; + do { id = 'anonymous_element_' + self.counter++ } while ($(id)); + element.writeAttribute('id', id); + return id; + }, + + readAttribute: function(element, name) { + element = $(element); + if (Prototype.Browser.IE) { + var t = Element._attributeTranslations.read; + if (t.values[name]) return t.values[name](element, name); + if (t.names[name]) name = t.names[name]; + if (name.include(':')) { + return (!element.attributes || !element.attributes[name]) ? null : + element.attributes[name].value; + } + } + return element.getAttribute(name); + }, + + writeAttribute: function(element, name, value) { + element = $(element); + var attributes = { }, t = Element._attributeTranslations.write; + + if (typeof name == 'object') attributes = name; + else attributes[name] = Object.isUndefined(value) ? true : value; + + for (var attr in attributes) { + name = t.names[attr] || attr; + value = attributes[attr]; + if (t.values[attr]) name = t.values[attr](element, value); + if (value === false || value === null) + element.removeAttribute(name); + else if (value === true) + element.setAttribute(name, name); + else element.setAttribute(name, value); + } + return element; + }, + + getHeight: function(element) { + return $(element).getDimensions().height; + }, + + getWidth: function(element) { + return $(element).getDimensions().width; + }, + + classNames: function(element) { + return new Element.ClassNames(element); + }, + + hasClassName: function(element, className) { + if (!(element = $(element))) return; + var elementClassName = element.className; + return (elementClassName.length > 0 && (elementClassName == className || + new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName))); + }, + + addClassName: function(element, className) { + if (!(element = $(element))) return; + if (!element.hasClassName(className)) + element.className += (element.className ? ' ' : '') + className; + return element; + }, + + removeClassName: function(element, className) { + if (!(element = $(element))) return; + element.className = element.className.replace( + new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip(); + return element; + }, + + toggleClassName: function(element, className) { + if (!(element = $(element))) return; + return element[element.hasClassName(className) ? + 'removeClassName' : 'addClassName'](className); + }, + + // removes whitespace-only text node children + cleanWhitespace: function(element) { + element = $(element); + var node = element.firstChild; + while (node) { + var nextNode = node.nextSibling; + if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) + element.removeChild(node); + node = nextNode; + } + return element; + }, + + empty: function(element) { + return $(element).innerHTML.blank(); + }, + + descendantOf: function(element, ancestor) { + element = $(element), ancestor = $(ancestor); + + if (element.compareDocumentPosition) + return (element.compareDocumentPosition(ancestor) & 8) === 8; + + if (ancestor.contains) + return ancestor.contains(element) && ancestor !== element; + + while (element = element.parentNode) + if (element == ancestor) return true; + + return false; + }, + + scrollTo: function(element) { + element = $(element); + var pos = element.cumulativeOffset(); + window.scrollTo(pos[0], pos[1]); + return element; + }, + + getStyle: function(element, style) { + element = $(element); + style = style == 'float' ? 'cssFloat' : style.camelize(); + var value = element.style[style]; + if (!value || value == 'auto') { + var css = document.defaultView.getComputedStyle(element, null); + value = css ? css[style] : null; + } + if (style == 'opacity') return value ? parseFloat(value) : 1.0; + return value == 'auto' ? null : value; + }, + + getOpacity: function(element) { + return $(element).getStyle('opacity'); + }, + + setStyle: function(element, styles) { + element = $(element); + var elementStyle = element.style, match; + if (Object.isString(styles)) { + element.style.cssText += ';' + styles; + return styles.include('opacity') ? + element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element; + } + for (var property in styles) + if (property == 'opacity') element.setOpacity(styles[property]); + else + elementStyle[(property == 'float' || property == 'cssFloat') ? + (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : + property] = styles[property]; + + return element; + }, + + setOpacity: function(element, value) { + element = $(element); + element.style.opacity = (value == 1 || value === '') ? '' : + (value < 0.00001) ? 0 : value; + return element; + }, + + getDimensions: function(element) { + element = $(element); + var display = element.getStyle('display'); + if (display != 'none' && display != null) // Safari bug + return {width: element.offsetWidth, height: element.offsetHeight}; + + // All *Width and *Height properties give 0 on elements with display none, + // so enable the element temporarily + var els = element.style; + var originalVisibility = els.visibility; + var originalPosition = els.position; + var originalDisplay = els.display; + els.visibility = 'hidden'; + els.position = 'absolute'; + els.display = 'block'; + var originalWidth = element.clientWidth; + var originalHeight = element.clientHeight; + els.display = originalDisplay; + els.position = originalPosition; + els.visibility = originalVisibility; + return {width: originalWidth, height: originalHeight}; + }, + + makePositioned: function(element) { + element = $(element); + var pos = Element.getStyle(element, 'position'); + if (pos == 'static' || !pos) { + element._madePositioned = true; + element.style.position = 'relative'; + // Opera returns the offset relative to the positioning context, when an + // element is position relative but top and left have not been defined + if (Prototype.Browser.Opera) { + element.style.top = 0; + element.style.left = 0; + } + } + return element; + }, + + undoPositioned: function(element) { + element = $(element); + if (element._madePositioned) { + element._madePositioned = undefined; + element.style.position = + element.style.top = + element.style.left = + element.style.bottom = + element.style.right = ''; + } + return element; + }, + + makeClipping: function(element) { + element = $(element); + if (element._overflow) return element; + element._overflow = Element.getStyle(element, 'overflow') || 'auto'; + if (element._overflow !== 'hidden') + element.style.overflow = 'hidden'; + return element; + }, + + undoClipping: function(element) { + element = $(element); + if (!element._overflow) return element; + element.style.overflow = element._overflow == 'auto' ? '' : element._overflow; + element._overflow = null; + return element; + }, + + cumulativeOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + return Element._returnOffset(valueL, valueT); + }, + + positionedOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + if (element) { + if (element.tagName.toUpperCase() == 'BODY') break; + var p = Element.getStyle(element, 'position'); + if (p !== 'static') break; + } + } while (element); + return Element._returnOffset(valueL, valueT); + }, + + absolutize: function(element) { + element = $(element); + if (element.getStyle('position') == 'absolute') return element; + // Position.prepare(); // To be done manually by Scripty when it needs it. + + var offsets = element.positionedOffset(); + var top = offsets[1]; + var left = offsets[0]; + var width = element.clientWidth; + var height = element.clientHeight; + + element._originalLeft = left - parseFloat(element.style.left || 0); + element._originalTop = top - parseFloat(element.style.top || 0); + element._originalWidth = element.style.width; + element._originalHeight = element.style.height; + + element.style.position = 'absolute'; + element.style.top = top + 'px'; + element.style.left = left + 'px'; + element.style.width = width + 'px'; + element.style.height = height + 'px'; + return element; + }, + + relativize: function(element) { + element = $(element); + if (element.getStyle('position') == 'relative') return element; + // Position.prepare(); // To be done manually by Scripty when it needs it. + + element.style.position = 'relative'; + var top = parseFloat(element.style.top || 0) - (element._originalTop || 0); + var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); + + element.style.top = top + 'px'; + element.style.left = left + 'px'; + element.style.height = element._originalHeight; + element.style.width = element._originalWidth; + return element; + }, + + cumulativeScrollOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.scrollTop || 0; + valueL += element.scrollLeft || 0; + element = element.parentNode; + } while (element); + return Element._returnOffset(valueL, valueT); + }, + + getOffsetParent: function(element) { + if (element.offsetParent) return $(element.offsetParent); + if (element == document.body) return $(element); + + while ((element = element.parentNode) && element != document.body) + if (Element.getStyle(element, 'position') != 'static') + return $(element); + + return $(document.body); + }, + + viewportOffset: function(forElement) { + var valueT = 0, valueL = 0; + + var element = forElement; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + + // Safari fix + if (element.offsetParent == document.body && + Element.getStyle(element, 'position') == 'absolute') break; + + } while (element = element.offsetParent); + + element = forElement; + do { + if (!Prototype.Browser.Opera || (element.tagName && (element.tagName.toUpperCase() == 'BODY'))) { + valueT -= element.scrollTop || 0; + valueL -= element.scrollLeft || 0; + } + } while (element = element.parentNode); + + return Element._returnOffset(valueL, valueT); + }, + + clonePosition: function(element, source) { + var options = Object.extend({ + setLeft: true, + setTop: true, + setWidth: true, + setHeight: true, + offsetTop: 0, + offsetLeft: 0 + }, arguments[2] || { }); + + // find page position of source + source = $(source); + var p = source.viewportOffset(); + + // find coordinate system to use + element = $(element); + var delta = [0, 0]; + var parent = null; + // delta [0,0] will do fine with position: fixed elements, + // position:absolute needs offsetParent deltas + if (Element.getStyle(element, 'position') == 'absolute') { + parent = element.getOffsetParent(); + delta = parent.viewportOffset(); + } + + // correct by body offsets (fixes Safari) + if (parent == document.body) { + delta[0] -= document.body.offsetLeft; + delta[1] -= document.body.offsetTop; + } + + // set position + if (options.setLeft) element.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px'; + if (options.setTop) element.style.top = (p[1] - delta[1] + options.offsetTop) + 'px'; + if (options.setWidth) element.style.width = source.offsetWidth + 'px'; + if (options.setHeight) element.style.height = source.offsetHeight + 'px'; + return element; + } +}; + +Element.Methods.identify.counter = 1; + +Object.extend(Element.Methods, { + getElementsBySelector: Element.Methods.select, + childElements: Element.Methods.immediateDescendants +}); + +Element._attributeTranslations = { + write: { + names: { + className: 'class', + htmlFor: 'for' + }, + values: { } + } +}; + +if (Prototype.Browser.Opera) { + Element.Methods.getStyle = Element.Methods.getStyle.wrap( + function(proceed, element, style) { + switch (style) { + case 'left': case 'top': case 'right': case 'bottom': + if (proceed(element, 'position') === 'static') return null; + case 'height': case 'width': + // returns '0px' for hidden elements; we want it to return null + if (!Element.visible(element)) return null; + + // returns the border-box dimensions rather than the content-box + // dimensions, so we subtract padding and borders from the value + var dim = parseInt(proceed(element, style), 10); + + if (dim !== element['offset' + style.capitalize()]) + return dim + 'px'; + + var properties; + if (style === 'height') { + properties = ['border-top-width', 'padding-top', + 'padding-bottom', 'border-bottom-width']; + } + else { + properties = ['border-left-width', 'padding-left', + 'padding-right', 'border-right-width']; + } + return properties.inject(dim, function(memo, property) { + var val = proceed(element, property); + return val === null ? memo : memo - parseInt(val, 10); + }) + 'px'; + default: return proceed(element, style); + } + } + ); + + Element.Methods.readAttribute = Element.Methods.readAttribute.wrap( + function(proceed, element, attribute) { + if (attribute === 'title') return element.title; + return proceed(element, attribute); + } + ); +} + +else if (Prototype.Browser.IE) { + // IE doesn't report offsets correctly for static elements, so we change them + // to "relative" to get the values, then change them back. + Element.Methods.getOffsetParent = Element.Methods.getOffsetParent.wrap( + function(proceed, element) { + element = $(element); + // IE throws an error if element is not in document + try { element.offsetParent } + catch(e) { return $(document.body) } + var position = element.getStyle('position'); + if (position !== 'static') return proceed(element); + element.setStyle({ position: 'relative' }); + var value = proceed(element); + element.setStyle({ position: position }); + return value; + } + ); + + $w('positionedOffset viewportOffset').each(function(method) { + Element.Methods[method] = Element.Methods[method].wrap( + function(proceed, element) { + element = $(element); + try { element.offsetParent } + catch(e) { return Element._returnOffset(0,0) } + var position = element.getStyle('position'); + if (position !== 'static') return proceed(element); + // Trigger hasLayout on the offset parent so that IE6 reports + // accurate offsetTop and offsetLeft values for position: fixed. + var offsetParent = element.getOffsetParent(); + if (offsetParent && offsetParent.getStyle('position') === 'fixed') + offsetParent.setStyle({ zoom: 1 }); + element.setStyle({ position: 'relative' }); + var value = proceed(element); + element.setStyle({ position: position }); + return value; + } + ); + }); + + Element.Methods.cumulativeOffset = Element.Methods.cumulativeOffset.wrap( + function(proceed, element) { + try { element.offsetParent } + catch(e) { return Element._returnOffset(0,0) } + return proceed(element); + } + ); + + Element.Methods.getStyle = function(element, style) { + element = $(element); + style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize(); + var value = element.style[style]; + if (!value && element.currentStyle) value = element.currentStyle[style]; + + if (style == 'opacity') { + if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/)) + if (value[1]) return parseFloat(value[1]) / 100; + return 1.0; + } + + if (value == 'auto') { + if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none')) + return element['offset' + style.capitalize()] + 'px'; + return null; + } + return value; + }; + + Element.Methods.setOpacity = function(element, value) { + function stripAlpha(filter){ + return filter.replace(/alpha\([^\)]*\)/gi,''); + } + element = $(element); + var currentStyle = element.currentStyle; + if ((currentStyle && !currentStyle.hasLayout) || + (!currentStyle && element.style.zoom == 'normal')) + element.style.zoom = 1; + + var filter = element.getStyle('filter'), style = element.style; + if (value == 1 || value === '') { + (filter = stripAlpha(filter)) ? + style.filter = filter : style.removeAttribute('filter'); + return element; + } else if (value < 0.00001) value = 0; + style.filter = stripAlpha(filter) + + 'alpha(opacity=' + (value * 100) + ')'; + return element; + }; + + Element._attributeTranslations = { + read: { + names: { + 'class': 'className', + 'for': 'htmlFor' + }, + values: { + _getAttr: function(element, attribute) { + return element.getAttribute(attribute, 2); + }, + _getAttrNode: function(element, attribute) { + var node = element.getAttributeNode(attribute); + return node ? node.value : ""; + }, + _getEv: function(element, attribute) { + attribute = element.getAttribute(attribute); + return attribute ? attribute.toString().slice(23, -2) : null; + }, + _flag: function(element, attribute) { + return $(element).hasAttribute(attribute) ? attribute : null; + }, + style: function(element) { + return element.style.cssText.toLowerCase(); + }, + title: function(element) { + return element.title; + } + } + } + }; + + Element._attributeTranslations.write = { + names: Object.extend({ + cellpadding: 'cellPadding', + cellspacing: 'cellSpacing' + }, Element._attributeTranslations.read.names), + values: { + checked: function(element, value) { + element.checked = !!value; + }, + + style: function(element, value) { + element.style.cssText = value ? value : ''; + } + } + }; + + Element._attributeTranslations.has = {}; + + $w('colSpan rowSpan vAlign dateTime accessKey tabIndex ' + + 'encType maxLength readOnly longDesc frameBorder').each(function(attr) { + Element._attributeTranslations.write.names[attr.toLowerCase()] = attr; + Element._attributeTranslations.has[attr.toLowerCase()] = attr; + }); + + (function(v) { + Object.extend(v, { + href: v._getAttr, + src: v._getAttr, + type: v._getAttr, + action: v._getAttrNode, + disabled: v._flag, + checked: v._flag, + readonly: v._flag, + multiple: v._flag, + onload: v._getEv, + onunload: v._getEv, + onclick: v._getEv, + ondblclick: v._getEv, + onmousedown: v._getEv, + onmouseup: v._getEv, + onmouseover: v._getEv, + onmousemove: v._getEv, + onmouseout: v._getEv, + onfocus: v._getEv, + onblur: v._getEv, + onkeypress: v._getEv, + onkeydown: v._getEv, + onkeyup: v._getEv, + onsubmit: v._getEv, + onreset: v._getEv, + onselect: v._getEv, + onchange: v._getEv + }); + })(Element._attributeTranslations.read.values); +} + +else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) { + Element.Methods.setOpacity = function(element, value) { + element = $(element); + element.style.opacity = (value == 1) ? 0.999999 : + (value === '') ? '' : (value < 0.00001) ? 0 : value; + return element; + }; +} + +else if (Prototype.Browser.WebKit) { + Element.Methods.setOpacity = function(element, value) { + element = $(element); + element.style.opacity = (value == 1 || value === '') ? '' : + (value < 0.00001) ? 0 : value; + + if (value == 1) + if(element.tagName.toUpperCase() == 'IMG' && element.width) { + element.width++; element.width--; + } else try { + var n = document.createTextNode(' '); + element.appendChild(n); + element.removeChild(n); + } catch (e) { } + + return element; + }; + + // Safari returns margins on body which is incorrect if the child is absolutely + // positioned. For performance reasons, redefine Element#cumulativeOffset for + // KHTML/WebKit only. + Element.Methods.cumulativeOffset = function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + if (element.offsetParent == document.body) + if (Element.getStyle(element, 'position') == 'absolute') break; + + element = element.offsetParent; + } while (element); + + return Element._returnOffset(valueL, valueT); + }; +} + +if (Prototype.Browser.IE || Prototype.Browser.Opera) { + // IE and Opera are missing .innerHTML support for TABLE-related and SELECT elements + Element.Methods.update = function(element, content) { + element = $(element); + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) return element.update().insert(content); + + content = Object.toHTML(content); + var tagName = element.tagName.toUpperCase(); + + if (tagName in Element._insertionTranslations.tags) { + $A(element.childNodes).each(function(node) { element.removeChild(node) }); + Element._getContentFromAnonymousElement(tagName, content.stripScripts()) + .each(function(node) { element.appendChild(node) }); + } + else element.innerHTML = content.stripScripts(); + + content.evalScripts.bind(content).defer(); + return element; + }; +} + +if ('outerHTML' in document.createElement('div')) { + Element.Methods.replace = function(element, content) { + element = $(element); + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) { + element.parentNode.replaceChild(content, element); + return element; + } + + content = Object.toHTML(content); + var parent = element.parentNode, tagName = parent.tagName.toUpperCase(); + + if (Element._insertionTranslations.tags[tagName]) { + var nextSibling = element.next(); + var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); + parent.removeChild(element); + if (nextSibling) + fragments.each(function(node) { parent.insertBefore(node, nextSibling) }); + else + fragments.each(function(node) { parent.appendChild(node) }); + } + else element.outerHTML = content.stripScripts(); + + content.evalScripts.bind(content).defer(); + return element; + }; +} + +Element._returnOffset = function(l, t) { + var result = [l, t]; + result.left = l; + result.top = t; + return result; +}; + +Element._getContentFromAnonymousElement = function(tagName, html) { + var div = new Element('div'), t = Element._insertionTranslations.tags[tagName]; + if (t) { + div.innerHTML = t[0] + html + t[1]; + t[2].times(function() { div = div.firstChild }); + } else div.innerHTML = html; + return $A(div.childNodes); +}; + +Element._insertionTranslations = { + before: function(element, node) { + element.parentNode.insertBefore(node, element); + }, + top: function(element, node) { + element.insertBefore(node, element.firstChild); + }, + bottom: function(element, node) { + element.appendChild(node); + }, + after: function(element, node) { + element.parentNode.insertBefore(node, element.nextSibling); + }, + tags: { + TABLE: ['', '
', 1], + TBODY: ['', '
', 2], + TR: ['', '
', 3], + TD: ['
', '
', 4], + SELECT: ['', 1] + } +}; + +(function() { + Object.extend(this.tags, { + THEAD: this.tags.TBODY, + TFOOT: this.tags.TBODY, + TH: this.tags.TD + }); +}).call(Element._insertionTranslations); + +Element.Methods.Simulated = { + hasAttribute: function(element, attribute) { + attribute = Element._attributeTranslations.has[attribute] || attribute; + var node = $(element).getAttributeNode(attribute); + return !!(node && node.specified); + } +}; + +Element.Methods.ByTag = { }; + +Object.extend(Element, Element.Methods); + +if (!Prototype.BrowserFeatures.ElementExtensions && + document.createElement('div')['__proto__']) { + window.HTMLElement = { }; + window.HTMLElement.prototype = document.createElement('div')['__proto__']; + Prototype.BrowserFeatures.ElementExtensions = true; +} + +Element.extend = (function() { + if (Prototype.BrowserFeatures.SpecificElementExtensions) + return Prototype.K; + + var Methods = { }, ByTag = Element.Methods.ByTag; + + var extend = Object.extend(function(element) { + if (!element || element._extendedByPrototype || + element.nodeType != 1 || element == window) return element; + + var methods = Object.clone(Methods), + tagName = element.tagName.toUpperCase(), property, value; + + // extend methods for specific tags + if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]); + + for (property in methods) { + value = methods[property]; + if (Object.isFunction(value) && !(property in element)) + element[property] = value.methodize(); + } + + element._extendedByPrototype = Prototype.emptyFunction; + return element; + + }, { + refresh: function() { + // extend methods for all tags (Safari doesn't need this) + if (!Prototype.BrowserFeatures.ElementExtensions) { + Object.extend(Methods, Element.Methods); + Object.extend(Methods, Element.Methods.Simulated); + } + } + }); + + extend.refresh(); + return extend; +})(); + +Element.hasAttribute = function(element, attribute) { + if (element.hasAttribute) return element.hasAttribute(attribute); + return Element.Methods.Simulated.hasAttribute(element, attribute); +}; + +Element.addMethods = function(methods) { + var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag; + + if (!methods) { + Object.extend(Form, Form.Methods); + Object.extend(Form.Element, Form.Element.Methods); + Object.extend(Element.Methods.ByTag, { + "FORM": Object.clone(Form.Methods), + "INPUT": Object.clone(Form.Element.Methods), + "SELECT": Object.clone(Form.Element.Methods), + "TEXTAREA": Object.clone(Form.Element.Methods) + }); + } + + if (arguments.length == 2) { + var tagName = methods; + methods = arguments[1]; + } + + if (!tagName) Object.extend(Element.Methods, methods || { }); + else { + if (Object.isArray(tagName)) tagName.each(extend); + else extend(tagName); + } + + function extend(tagName) { + tagName = tagName.toUpperCase(); + if (!Element.Methods.ByTag[tagName]) + Element.Methods.ByTag[tagName] = { }; + Object.extend(Element.Methods.ByTag[tagName], methods); + } + + function copy(methods, destination, onlyIfAbsent) { + onlyIfAbsent = onlyIfAbsent || false; + for (var property in methods) { + var value = methods[property]; + if (!Object.isFunction(value)) continue; + if (!onlyIfAbsent || !(property in destination)) + destination[property] = value.methodize(); + } + } + + function findDOMClass(tagName) { + var klass; + var trans = { + "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph", + "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList", + "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading", + "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote", + "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION": + "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD": + "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR": + "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET": + "FrameSet", "IFRAME": "IFrame" + }; + if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName.capitalize() + 'Element'; + if (window[klass]) return window[klass]; + + window[klass] = { }; + window[klass].prototype = document.createElement(tagName)['__proto__']; + return window[klass]; + } + + if (F.ElementExtensions) { + copy(Element.Methods, HTMLElement.prototype); + copy(Element.Methods.Simulated, HTMLElement.prototype, true); + } + + if (F.SpecificElementExtensions) { + for (var tag in Element.Methods.ByTag) { + var klass = findDOMClass(tag); + if (Object.isUndefined(klass)) continue; + copy(T[tag], klass.prototype); + } + } + + Object.extend(Element, Element.Methods); + delete Element.ByTag; + + if (Element.extend.refresh) Element.extend.refresh(); + Element.cache = { }; +}; + +document.viewport = { + getDimensions: function() { + var dimensions = { }, B = Prototype.Browser; + $w('width height').each(function(d) { + var D = d.capitalize(); + if (B.WebKit && !document.evaluate) { + // Safari <3.0 needs self.innerWidth/Height + dimensions[d] = self['inner' + D]; + } else if (B.Opera && parseFloat(window.opera.version()) < 9.5) { + // Opera <9.5 needs document.body.clientWidth/Height + dimensions[d] = document.body['client' + D] + } else { + dimensions[d] = document.documentElement['client' + D]; + } + }); + return dimensions; + }, + + getWidth: function() { + return this.getDimensions().width; + }, + + getHeight: function() { + return this.getDimensions().height; + }, + + getScrollOffsets: function() { + return Element._returnOffset( + window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, + window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); + } +}; +/* Portions of the Selector class are derived from Jack Slocum's DomQuery, + * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style + * license. Please see http://www.yui-ext.com/ for more information. */ + +var Selector = Class.create({ + initialize: function(expression) { + this.expression = expression.strip(); + + if (this.shouldUseSelectorsAPI()) { + this.mode = 'selectorsAPI'; + } else if (this.shouldUseXPath()) { + this.mode = 'xpath'; + this.compileXPathMatcher(); + } else { + this.mode = "normal"; + this.compileMatcher(); + } + + }, + + shouldUseXPath: function() { + if (!Prototype.BrowserFeatures.XPath) return false; + + var e = this.expression; + + // Safari 3 chokes on :*-of-type and :empty + if (Prototype.Browser.WebKit && + (e.include("-of-type") || e.include(":empty"))) + return false; + + // XPath can't do namespaced attributes, nor can it read + // the "checked" property from DOM nodes + if ((/(\[[\w-]*?:|:checked)/).test(e)) + return false; + + return true; + }, + + shouldUseSelectorsAPI: function() { + if (!Prototype.BrowserFeatures.SelectorsAPI) return false; + + if (!Selector._div) Selector._div = new Element('div'); + + // Make sure the browser treats the selector as valid. Test on an + // isolated element to minimize cost of this check. + try { + Selector._div.querySelector(this.expression); + } catch(e) { + return false; + } + + return true; + }, + + compileMatcher: function() { + var e = this.expression, ps = Selector.patterns, h = Selector.handlers, + c = Selector.criteria, le, p, m; + + if (Selector._cache[e]) { + this.matcher = Selector._cache[e]; + return; + } + + this.matcher = ["this.matcher = function(root) {", + "var r = root, h = Selector.handlers, c = false, n;"]; + + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + p = ps[i]; + if (m = e.match(p)) { + this.matcher.push(Object.isFunction(c[i]) ? c[i](m) : + new Template(c[i]).evaluate(m)); + e = e.replace(m[0], ''); + break; + } + } + } + + this.matcher.push("return h.unique(n);\n}"); + eval(this.matcher.join('\n')); + Selector._cache[this.expression] = this.matcher; + }, + + compileXPathMatcher: function() { + var e = this.expression, ps = Selector.patterns, + x = Selector.xpath, le, m; + + if (Selector._cache[e]) { + this.xpath = Selector._cache[e]; return; + } + + this.matcher = ['.//*']; + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + if (m = e.match(ps[i])) { + this.matcher.push(Object.isFunction(x[i]) ? x[i](m) : + new Template(x[i]).evaluate(m)); + e = e.replace(m[0], ''); + break; + } + } + } + + this.xpath = this.matcher.join(''); + Selector._cache[this.expression] = this.xpath; + }, + + findElements: function(root) { + root = root || document; + var e = this.expression, results; + + switch (this.mode) { + case 'selectorsAPI': + // querySelectorAll queries document-wide, then filters to descendants + // of the context element. That's not what we want. + // Add an explicit context to the selector if necessary. + if (root !== document) { + var oldId = root.id, id = $(root).identify(); + e = "#" + id + " " + e; + } + + results = $A(root.querySelectorAll(e)).map(Element.extend); + root.id = oldId; + + return results; + case 'xpath': + return document._getElementsByXPath(this.xpath, root); + default: + return this.matcher(root); + } + }, + + match: function(element) { + this.tokens = []; + + var e = this.expression, ps = Selector.patterns, as = Selector.assertions; + var le, p, m; + + while (e && le !== e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + p = ps[i]; + if (m = e.match(p)) { + // use the Selector.assertions methods unless the selector + // is too complex. + if (as[i]) { + this.tokens.push([i, Object.clone(m)]); + e = e.replace(m[0], ''); + } else { + // reluctantly do a document-wide search + // and look for a match in the array + return this.findElements(document).include(element); + } + } + } + } + + var match = true, name, matches; + for (var i = 0, token; token = this.tokens[i]; i++) { + name = token[0], matches = token[1]; + if (!Selector.assertions[name](element, matches)) { + match = false; break; + } + } + + return match; + }, + + toString: function() { + return this.expression; + }, + + inspect: function() { + return "#"; + } +}); + +Object.extend(Selector, { + _cache: { }, + + xpath: { + descendant: "//*", + child: "/*", + adjacent: "/following-sibling::*[1]", + laterSibling: '/following-sibling::*', + tagName: function(m) { + if (m[1] == '*') return ''; + return "[local-name()='" + m[1].toLowerCase() + + "' or local-name()='" + m[1].toUpperCase() + "']"; + }, + className: "[contains(concat(' ', @class, ' '), ' #{1} ')]", + id: "[@id='#{1}']", + attrPresence: function(m) { + m[1] = m[1].toLowerCase(); + return new Template("[@#{1}]").evaluate(m); + }, + attr: function(m) { + m[1] = m[1].toLowerCase(); + m[3] = m[5] || m[6]; + return new Template(Selector.xpath.operators[m[2]]).evaluate(m); + }, + pseudo: function(m) { + var h = Selector.xpath.pseudos[m[1]]; + if (!h) return ''; + if (Object.isFunction(h)) return h(m); + return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); + }, + operators: { + '=': "[@#{1}='#{3}']", + '!=': "[@#{1}!='#{3}']", + '^=': "[starts-with(@#{1}, '#{3}')]", + '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']", + '*=': "[contains(@#{1}, '#{3}')]", + '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]", + '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]" + }, + pseudos: { + 'first-child': '[not(preceding-sibling::*)]', + 'last-child': '[not(following-sibling::*)]', + 'only-child': '[not(preceding-sibling::* or following-sibling::*)]', + 'empty': "[count(*) = 0 and (count(text()) = 0)]", + 'checked': "[@checked]", + 'disabled': "[(@disabled) and (@type!='hidden')]", + 'enabled': "[not(@disabled) and (@type!='hidden')]", + 'not': function(m) { + var e = m[6], p = Selector.patterns, + x = Selector.xpath, le, v; + + var exclusion = []; + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in p) { + if (m = e.match(p[i])) { + v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m); + exclusion.push("(" + v.substring(1, v.length - 1) + ")"); + e = e.replace(m[0], ''); + break; + } + } + } + return "[not(" + exclusion.join(" and ") + ")]"; + }, + 'nth-child': function(m) { + return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m); + }, + 'nth-last-child': function(m) { + return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m); + }, + 'nth-of-type': function(m) { + return Selector.xpath.pseudos.nth("position() ", m); + }, + 'nth-last-of-type': function(m) { + return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m); + }, + 'first-of-type': function(m) { + m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m); + }, + 'last-of-type': function(m) { + m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m); + }, + 'only-of-type': function(m) { + var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m); + }, + nth: function(fragment, m) { + var mm, formula = m[6], predicate; + if (formula == 'even') formula = '2n+0'; + if (formula == 'odd') formula = '2n+1'; + if (mm = formula.match(/^(\d+)$/)) // digit only + return '[' + fragment + "= " + mm[1] + ']'; + if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b + if (mm[1] == "-") mm[1] = -1; + var a = mm[1] ? Number(mm[1]) : 1; + var b = mm[2] ? Number(mm[2]) : 0; + predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + + "((#{fragment} - #{b}) div #{a} >= 0)]"; + return new Template(predicate).evaluate({ + fragment: fragment, a: a, b: b }); + } + } + } + }, + + criteria: { + tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;', + className: 'n = h.className(n, r, "#{1}", c); c = false;', + id: 'n = h.id(n, r, "#{1}", c); c = false;', + attrPresence: 'n = h.attrPresence(n, r, "#{1}", c); c = false;', + attr: function(m) { + m[3] = (m[5] || m[6]); + return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}", c); c = false;').evaluate(m); + }, + pseudo: function(m) { + if (m[6]) m[6] = m[6].replace(/"/g, '\\"'); + return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m); + }, + descendant: 'c = "descendant";', + child: 'c = "child";', + adjacent: 'c = "adjacent";', + laterSibling: 'c = "laterSibling";' + }, + + patterns: { + // combinators must be listed first + // (and descendant needs to be last combinator) + laterSibling: /^\s*~\s*/, + child: /^\s*>\s*/, + adjacent: /^\s*\+\s*/, + descendant: /^\s/, + + // selectors follow + tagName: /^\s*(\*|[\w\-]+)(\b|$)?/, + id: /^#([\w\-\*]+)(\b|$)/, + className: /^\.([\w\-\*]+)(\b|$)/, + pseudo: +/^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/, + attrPresence: /^\[((?:[\w]+:)?[\w]+)\]/, + attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/ + }, + + // for Selector.match and Element#match + assertions: { + tagName: function(element, matches) { + return matches[1].toUpperCase() == element.tagName.toUpperCase(); + }, + + className: function(element, matches) { + return Element.hasClassName(element, matches[1]); + }, + + id: function(element, matches) { + return element.id === matches[1]; + }, + + attrPresence: function(element, matches) { + return Element.hasAttribute(element, matches[1]); + }, + + attr: function(element, matches) { + var nodeValue = Element.readAttribute(element, matches[1]); + return nodeValue && Selector.operators[matches[2]](nodeValue, matches[5] || matches[6]); + } + }, + + handlers: { + // UTILITY FUNCTIONS + // joins two collections + concat: function(a, b) { + for (var i = 0, node; node = b[i]; i++) + a.push(node); + return a; + }, + + // marks an array of nodes for counting + mark: function(nodes) { + var _true = Prototype.emptyFunction; + for (var i = 0, node; node = nodes[i]; i++) + node._countedByPrototype = _true; + return nodes; + }, + + unmark: function(nodes) { + for (var i = 0, node; node = nodes[i]; i++) + node._countedByPrototype = undefined; + return nodes; + }, + + // mark each child node with its position (for nth calls) + // "ofType" flag indicates whether we're indexing for nth-of-type + // rather than nth-child + index: function(parentNode, reverse, ofType) { + parentNode._countedByPrototype = Prototype.emptyFunction; + if (reverse) { + for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) { + var node = nodes[i]; + if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; + } + } else { + for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++) + if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; + } + }, + + // filters out duplicates and extends all nodes + unique: function(nodes) { + if (nodes.length == 0) return nodes; + var results = [], n; + for (var i = 0, l = nodes.length; i < l; i++) + if (!(n = nodes[i])._countedByPrototype) { + n._countedByPrototype = Prototype.emptyFunction; + results.push(Element.extend(n)); + } + return Selector.handlers.unmark(results); + }, + + // COMBINATOR FUNCTIONS + descendant: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + h.concat(results, node.getElementsByTagName('*')); + return results; + }, + + child: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) { + for (var j = 0, child; child = node.childNodes[j]; j++) + if (child.nodeType == 1 && child.tagName != '!') results.push(child); + } + return results; + }, + + adjacent: function(nodes) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + var next = this.nextElementSibling(node); + if (next) results.push(next); + } + return results; + }, + + laterSibling: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + h.concat(results, Element.nextSiblings(node)); + return results; + }, + + nextElementSibling: function(node) { + while (node = node.nextSibling) + if (node.nodeType == 1) return node; + return null; + }, + + previousElementSibling: function(node) { + while (node = node.previousSibling) + if (node.nodeType == 1) return node; + return null; + }, + + // TOKEN FUNCTIONS + tagName: function(nodes, root, tagName, combinator) { + var uTagName = tagName.toUpperCase(); + var results = [], h = Selector.handlers; + if (nodes) { + if (combinator) { + // fastlane for ordinary descendant combinators + if (combinator == "descendant") { + for (var i = 0, node; node = nodes[i]; i++) + h.concat(results, node.getElementsByTagName(tagName)); + return results; + } else nodes = this[combinator](nodes); + if (tagName == "*") return nodes; + } + for (var i = 0, node; node = nodes[i]; i++) + if (node.tagName.toUpperCase() === uTagName) results.push(node); + return results; + } else return root.getElementsByTagName(tagName); + }, + + id: function(nodes, root, id, combinator) { + var targetNode = $(id), h = Selector.handlers; + if (!targetNode) return []; + if (!nodes && root == document) return [targetNode]; + if (nodes) { + if (combinator) { + if (combinator == 'child') { + for (var i = 0, node; node = nodes[i]; i++) + if (targetNode.parentNode == node) return [targetNode]; + } else if (combinator == 'descendant') { + for (var i = 0, node; node = nodes[i]; i++) + if (Element.descendantOf(targetNode, node)) return [targetNode]; + } else if (combinator == 'adjacent') { + for (var i = 0, node; node = nodes[i]; i++) + if (Selector.handlers.previousElementSibling(targetNode) == node) + return [targetNode]; + } else nodes = h[combinator](nodes); + } + for (var i = 0, node; node = nodes[i]; i++) + if (node == targetNode) return [targetNode]; + return []; + } + return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : []; + }, + + className: function(nodes, root, className, combinator) { + if (nodes && combinator) nodes = this[combinator](nodes); + return Selector.handlers.byClassName(nodes, root, className); + }, + + byClassName: function(nodes, root, className) { + if (!nodes) nodes = Selector.handlers.descendant([root]); + var needle = ' ' + className + ' '; + for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) { + nodeClassName = node.className; + if (nodeClassName.length == 0) continue; + if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle)) + results.push(node); + } + return results; + }, + + attrPresence: function(nodes, root, attr, combinator) { + if (!nodes) nodes = root.getElementsByTagName("*"); + if (nodes && combinator) nodes = this[combinator](nodes); + var results = []; + for (var i = 0, node; node = nodes[i]; i++) + if (Element.hasAttribute(node, attr)) results.push(node); + return results; + }, + + attr: function(nodes, root, attr, value, operator, combinator) { + if (!nodes) nodes = root.getElementsByTagName("*"); + if (nodes && combinator) nodes = this[combinator](nodes); + var handler = Selector.operators[operator], results = []; + for (var i = 0, node; node = nodes[i]; i++) { + var nodeValue = Element.readAttribute(node, attr); + if (nodeValue === null) continue; + if (handler(nodeValue, value)) results.push(node); + } + return results; + }, + + pseudo: function(nodes, name, value, root, combinator) { + if (nodes && combinator) nodes = this[combinator](nodes); + if (!nodes) nodes = root.getElementsByTagName("*"); + return Selector.pseudos[name](nodes, value, root); + } + }, + + pseudos: { + 'first-child': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + if (Selector.handlers.previousElementSibling(node)) continue; + results.push(node); + } + return results; + }, + 'last-child': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + if (Selector.handlers.nextElementSibling(node)) continue; + results.push(node); + } + return results; + }, + 'only-child': function(nodes, value, root) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!h.previousElementSibling(node) && !h.nextElementSibling(node)) + results.push(node); + return results; + }, + 'nth-child': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root); + }, + 'nth-last-child': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, true); + }, + 'nth-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, false, true); + }, + 'nth-last-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, true, true); + }, + 'first-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, "1", root, false, true); + }, + 'last-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, "1", root, true, true); + }, + 'only-of-type': function(nodes, formula, root) { + var p = Selector.pseudos; + return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root); + }, + + // handles the an+b logic + getIndices: function(a, b, total) { + if (a == 0) return b > 0 ? [b] : []; + return $R(1, total).inject([], function(memo, i) { + if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i); + return memo; + }); + }, + + // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type + nth: function(nodes, formula, root, reverse, ofType) { + if (nodes.length == 0) return []; + if (formula == 'even') formula = '2n+0'; + if (formula == 'odd') formula = '2n+1'; + var h = Selector.handlers, results = [], indexed = [], m; + h.mark(nodes); + for (var i = 0, node; node = nodes[i]; i++) { + if (!node.parentNode._countedByPrototype) { + h.index(node.parentNode, reverse, ofType); + indexed.push(node.parentNode); + } + } + if (formula.match(/^\d+$/)) { // just a number + formula = Number(formula); + for (var i = 0, node; node = nodes[i]; i++) + if (node.nodeIndex == formula) results.push(node); + } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b + if (m[1] == "-") m[1] = -1; + var a = m[1] ? Number(m[1]) : 1; + var b = m[2] ? Number(m[2]) : 0; + var indices = Selector.pseudos.getIndices(a, b, nodes.length); + for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { + for (var j = 0; j < l; j++) + if (node.nodeIndex == indices[j]) results.push(node); + } + } + h.unmark(nodes); + h.unmark(indexed); + return results; + }, + + 'empty': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + // IE treats comments as element nodes + if (node.tagName == '!' || node.firstChild) continue; + results.push(node); + } + return results; + }, + + 'not': function(nodes, selector, root) { + var h = Selector.handlers, selectorType, m; + var exclusions = new Selector(selector).findElements(root); + h.mark(exclusions); + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!node._countedByPrototype) results.push(node); + h.unmark(exclusions); + return results; + }, + + 'enabled': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!node.disabled && (!node.type || node.type !== 'hidden')) + results.push(node); + return results; + }, + + 'disabled': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (node.disabled) results.push(node); + return results; + }, + + 'checked': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (node.checked) results.push(node); + return results; + } + }, + + operators: { + '=': function(nv, v) { return nv == v; }, + '!=': function(nv, v) { return nv != v; }, + '^=': function(nv, v) { return nv == v || nv && nv.startsWith(v); }, + '$=': function(nv, v) { return nv == v || nv && nv.endsWith(v); }, + '*=': function(nv, v) { return nv == v || nv && nv.include(v); }, + '$=': function(nv, v) { return nv.endsWith(v); }, + '*=': function(nv, v) { return nv.include(v); }, + '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); }, + '|=': function(nv, v) { return ('-' + (nv || "").toUpperCase() + + '-').include('-' + (v || "").toUpperCase() + '-'); } + }, + + split: function(expression) { + var expressions = []; + expression.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) { + expressions.push(m[1].strip()); + }); + return expressions; + }, + + matchElements: function(elements, expression) { + var matches = $$(expression), h = Selector.handlers; + h.mark(matches); + for (var i = 0, results = [], element; element = elements[i]; i++) + if (element._countedByPrototype) results.push(element); + h.unmark(matches); + return results; + }, + + findElement: function(elements, expression, index) { + if (Object.isNumber(expression)) { + index = expression; expression = false; + } + return Selector.matchElements(elements, expression || '*')[index || 0]; + }, + + findChildElements: function(element, expressions) { + expressions = Selector.split(expressions.join(',')); + var results = [], h = Selector.handlers; + for (var i = 0, l = expressions.length, selector; i < l; i++) { + selector = new Selector(expressions[i].strip()); + h.concat(results, selector.findElements(element)); + } + return (l > 1) ? h.unique(results) : results; + } +}); + +if (Prototype.Browser.IE) { + Object.extend(Selector.handlers, { + // IE returns comment nodes on getElementsByTagName("*"). + // Filter them out. + concat: function(a, b) { + for (var i = 0, node; node = b[i]; i++) + if (node.tagName !== "!") a.push(node); + return a; + }, + + // IE improperly serializes _countedByPrototype in (inner|outer)HTML. + unmark: function(nodes) { + for (var i = 0, node; node = nodes[i]; i++) + node.removeAttribute('_countedByPrototype'); + return nodes; + } + }); +} + +function $$() { + return Selector.findChildElements(document, $A(arguments)); +} +var Form = { + reset: function(form) { + $(form).reset(); + return form; + }, + + serializeElements: function(elements, options) { + if (typeof options != 'object') options = { hash: !!options }; + else if (Object.isUndefined(options.hash)) options.hash = true; + var key, value, submitted = false, submit = options.submit; + + var data = elements.inject({ }, function(result, element) { + if (!element.disabled && element.name) { + key = element.name; value = $(element).getValue(); + if (value != null && element.type != 'file' && (element.type != 'submit' || (!submitted && + submit !== false && (!submit || key == submit) && (submitted = true)))) { + if (key in result) { + // a key is already present; construct an array of values + if (!Object.isArray(result[key])) result[key] = [result[key]]; + result[key].push(value); + } + else result[key] = value; + } + } + return result; + }); + + return options.hash ? data : Object.toQueryString(data); + } +}; + +Form.Methods = { + serialize: function(form, options) { + return Form.serializeElements(Form.getElements(form), options); + }, + + getElements: function(form) { + return $A($(form).getElementsByTagName('*')).inject([], + function(elements, child) { + if (Form.Element.Serializers[child.tagName.toLowerCase()]) + elements.push(Element.extend(child)); + return elements; + } + ); + }, + + getInputs: function(form, typeName, name) { + form = $(form); + var inputs = form.getElementsByTagName('input'); + + if (!typeName && !name) return $A(inputs).map(Element.extend); + + for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) { + var input = inputs[i]; + if ((typeName && input.type != typeName) || (name && input.name != name)) + continue; + matchingInputs.push(Element.extend(input)); + } + + return matchingInputs; + }, + + disable: function(form) { + form = $(form); + Form.getElements(form).invoke('disable'); + return form; + }, + + enable: function(form) { + form = $(form); + Form.getElements(form).invoke('enable'); + return form; + }, + + findFirstElement: function(form) { + var elements = $(form).getElements().findAll(function(element) { + return 'hidden' != element.type && !element.disabled; + }); + var firstByIndex = elements.findAll(function(element) { + return element.hasAttribute('tabIndex') && element.tabIndex >= 0; + }).sortBy(function(element) { return element.tabIndex }).first(); + + return firstByIndex ? firstByIndex : elements.find(function(element) { + return ['input', 'select', 'textarea'].include(element.tagName.toLowerCase()); + }); + }, + + focusFirstElement: function(form) { + form = $(form); + form.findFirstElement().activate(); + return form; + }, + + request: function(form, options) { + form = $(form), options = Object.clone(options || { }); + + var params = options.parameters, action = form.readAttribute('action') || ''; + if (action.blank()) action = window.location.href; + options.parameters = form.serialize(true); + + if (params) { + if (Object.isString(params)) params = params.toQueryParams(); + Object.extend(options.parameters, params); + } + + if (form.hasAttribute('method') && !options.method) + options.method = form.method; + + return new Ajax.Request(action, options); + } +}; + +/*--------------------------------------------------------------------------*/ + +Form.Element = { + focus: function(element) { + $(element).focus(); + return element; + }, + + select: function(element) { + $(element).select(); + return element; + } +}; + +Form.Element.Methods = { + serialize: function(element) { + element = $(element); + if (!element.disabled && element.name) { + var value = element.getValue(); + if (value != undefined) { + var pair = { }; + pair[element.name] = value; + return Object.toQueryString(pair); + } + } + return ''; + }, + + getValue: function(element) { + element = $(element); + var method = element.tagName.toLowerCase(); + return Form.Element.Serializers[method](element); + }, + + setValue: function(element, value) { + element = $(element); + var method = element.tagName.toLowerCase(); + Form.Element.Serializers[method](element, value); + return element; + }, + + clear: function(element) { + $(element).value = ''; + return element; + }, + + present: function(element) { + return $(element).value != ''; + }, + + activate: function(element) { + element = $(element); + try { + element.focus(); + if (element.select && (element.tagName.toLowerCase() != 'input' || + !['button', 'reset', 'submit'].include(element.type))) + element.select(); + } catch (e) { } + return element; + }, + + disable: function(element) { + element = $(element); + element.disabled = true; + return element; + }, + + enable: function(element) { + element = $(element); + element.disabled = false; + return element; + } +}; + +/*--------------------------------------------------------------------------*/ + +var Field = Form.Element; +var $F = Form.Element.Methods.getValue; + +/*--------------------------------------------------------------------------*/ + +Form.Element.Serializers = { + input: function(element, value) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + return Form.Element.Serializers.inputSelector(element, value); + default: + return Form.Element.Serializers.textarea(element, value); + } + }, + + inputSelector: function(element, value) { + if (Object.isUndefined(value)) return element.checked ? element.value : null; + else element.checked = !!value; + }, + + textarea: function(element, value) { + if (Object.isUndefined(value)) return element.value; + else element.value = value; + }, + + select: function(element, value) { + if (Object.isUndefined(value)) + return this[element.type == 'select-one' ? + 'selectOne' : 'selectMany'](element); + else { + var opt, currentValue, single = !Object.isArray(value); + for (var i = 0, length = element.length; i < length; i++) { + opt = element.options[i]; + currentValue = this.optionValue(opt); + if (single) { + if (currentValue == value) { + opt.selected = true; + return; + } + } + else opt.selected = value.include(currentValue); + } + } + }, + + selectOne: function(element) { + var index = element.selectedIndex; + return index >= 0 ? this.optionValue(element.options[index]) : null; + }, + + selectMany: function(element) { + var values, length = element.length; + if (!length) return null; + + for (var i = 0, values = []; i < length; i++) { + var opt = element.options[i]; + if (opt.selected) values.push(this.optionValue(opt)); + } + return values; + }, + + optionValue: function(opt) { + // extend element because hasAttribute may not be native + return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text; + } +}; + +/*--------------------------------------------------------------------------*/ + +Abstract.TimedObserver = Class.create(PeriodicalExecuter, { + initialize: function($super, element, frequency, callback) { + $super(callback, frequency); + this.element = $(element); + this.lastValue = this.getValue(); + }, + + execute: function() { + var value = this.getValue(); + if (Object.isString(this.lastValue) && Object.isString(value) ? + this.lastValue != value : String(this.lastValue) != String(value)) { + this.callback(this.element, value); + this.lastValue = value; + } + } +}); + +Form.Element.Observer = Class.create(Abstract.TimedObserver, { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.Observer = Class.create(Abstract.TimedObserver, { + getValue: function() { + return Form.serialize(this.element); + } +}); + +/*--------------------------------------------------------------------------*/ + +Abstract.EventObserver = Class.create({ + initialize: function(element, callback) { + this.element = $(element); + this.callback = callback; + + this.lastValue = this.getValue(); + if (this.element.tagName.toLowerCase() == 'form') + this.registerFormCallbacks(); + else + this.registerCallback(this.element); + }, + + onElementEvent: function() { + var value = this.getValue(); + if (this.lastValue != value) { + this.callback(this.element, value); + this.lastValue = value; + } + }, + + registerFormCallbacks: function() { + Form.getElements(this.element).each(this.registerCallback, this); + }, + + registerCallback: function(element) { + if (element.type) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + Event.observe(element, 'click', this.onElementEvent.bind(this)); + break; + default: + Event.observe(element, 'change', this.onElementEvent.bind(this)); + break; + } + } + } +}); + +Form.Element.EventObserver = Class.create(Abstract.EventObserver, { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.EventObserver = Class.create(Abstract.EventObserver, { + getValue: function() { + return Form.serialize(this.element); + } +}); +if (!window.Event) var Event = { }; + +Object.extend(Event, { + KEY_BACKSPACE: 8, + KEY_TAB: 9, + KEY_RETURN: 13, + KEY_ESC: 27, + KEY_LEFT: 37, + KEY_UP: 38, + KEY_RIGHT: 39, + KEY_DOWN: 40, + KEY_DELETE: 46, + KEY_HOME: 36, + KEY_END: 35, + KEY_PAGEUP: 33, + KEY_PAGEDOWN: 34, + KEY_INSERT: 45, + + cache: { }, + + relatedTarget: function(event) { + var element; + switch(event.type) { + case 'mouseover': element = event.fromElement; break; + case 'mouseout': element = event.toElement; break; + default: return null; + } + return Element.extend(element); + } +}); + +Event.Methods = (function() { + var isButton; + + if (Prototype.Browser.IE) { + var buttonMap = { 0: 1, 1: 4, 2: 2 }; + isButton = function(event, code) { + return event.button == buttonMap[code]; + }; + + } else if (Prototype.Browser.WebKit) { + isButton = function(event, code) { + switch (code) { + case 0: return event.which == 1 && !event.metaKey; + case 1: return event.which == 1 && event.metaKey; + default: return false; + } + }; + + } else { + isButton = function(event, code) { + return event.which ? (event.which === code + 1) : (event.button === code); + }; + } + + return { + isLeftClick: function(event) { return isButton(event, 0) }, + isMiddleClick: function(event) { return isButton(event, 1) }, + isRightClick: function(event) { return isButton(event, 2) }, + + element: function(event) { + event = Event.extend(event); + + var node = event.target, + type = event.type, + currentTarget = event.currentTarget; + + if (currentTarget && currentTarget.tagName) { + // Firefox screws up the "click" event when moving between radio buttons + // via arrow keys. It also screws up the "load" and "error" events on images, + // reporting the document as the target instead of the original image. + if (type === 'load' || type === 'error' || + (type === 'click' && currentTarget.tagName.toLowerCase() === 'input' + && currentTarget.type === 'radio')) + node = currentTarget; + } + if (node.nodeType == Node.TEXT_NODE) node = node.parentNode; + return Element.extend(node); + }, + + findElement: function(event, expression) { + var element = Event.element(event); + if (!expression) return element; + var elements = [element].concat(element.ancestors()); + return Selector.findElement(elements, expression, 0); + }, + + pointer: function(event) { + var docElement = document.documentElement, + body = document.body || { scrollLeft: 0, scrollTop: 0 }; + return { + x: event.pageX || (event.clientX + + (docElement.scrollLeft || body.scrollLeft) - + (docElement.clientLeft || 0)), + y: event.pageY || (event.clientY + + (docElement.scrollTop || body.scrollTop) - + (docElement.clientTop || 0)) + }; + }, + + pointerX: function(event) { return Event.pointer(event).x }, + pointerY: function(event) { return Event.pointer(event).y }, + + stop: function(event) { + Event.extend(event); + event.preventDefault(); + event.stopPropagation(); + event.stopped = true; + } + }; +})(); + +Event.extend = (function() { + var methods = Object.keys(Event.Methods).inject({ }, function(m, name) { + m[name] = Event.Methods[name].methodize(); + return m; + }); + + if (Prototype.Browser.IE) { + Object.extend(methods, { + stopPropagation: function() { this.cancelBubble = true }, + preventDefault: function() { this.returnValue = false }, + inspect: function() { return "[object Event]" } + }); + + return function(event) { + if (!event) return false; + if (event._extendedByPrototype) return event; + + event._extendedByPrototype = Prototype.emptyFunction; + var pointer = Event.pointer(event); + Object.extend(event, { + target: event.srcElement, + relatedTarget: Event.relatedTarget(event), + pageX: pointer.x, + pageY: pointer.y + }); + return Object.extend(event, methods); + }; + + } else { + Event.prototype = Event.prototype || document.createEvent("HTMLEvents")['__proto__']; + Object.extend(Event.prototype, methods); + return Prototype.K; + } +})(); + +Object.extend(Event, (function() { + var cache = Event.cache; + + function getEventID(element) { + if (element._prototypeEventID) return element._prototypeEventID[0]; + arguments.callee.id = arguments.callee.id || 1; + return element._prototypeEventID = [++arguments.callee.id]; + } + + function getDOMEventName(eventName) { + if (eventName && eventName.include(':')) return "dataavailable"; + return eventName; + } + + function getCacheForID(id) { + return cache[id] = cache[id] || { }; + } + + function getWrappersForEventName(id, eventName) { + var c = getCacheForID(id); + return c[eventName] = c[eventName] || []; + } + + function createWrapper(element, eventName, handler) { + var id = getEventID(element); + var c = getWrappersForEventName(id, eventName); + if (c.pluck("handler").include(handler)) return false; + + var wrapper = function(event) { + if (!Event || !Event.extend || + (event.eventName && event.eventName != eventName)) + return false; + + Event.extend(event); + handler.call(element, event); + }; + + wrapper.handler = handler; + c.push(wrapper); + return wrapper; + } + + function findWrapper(id, eventName, handler) { + var c = getWrappersForEventName(id, eventName); + return c.find(function(wrapper) { return wrapper.handler == handler }); + } + + function destroyWrapper(id, eventName, handler) { + var c = getCacheForID(id); + if (!c[eventName]) return false; + c[eventName] = c[eventName].without(findWrapper(id, eventName, handler)); + } + + function destroyCache() { + for (var id in cache) + for (var eventName in cache[id]) + cache[id][eventName] = null; + } + + + // Internet Explorer needs to remove event handlers on page unload + // in order to avoid memory leaks. + if (window.attachEvent) { + window.attachEvent("onunload", destroyCache); + } + + // Safari has a dummy event handler on page unload so that it won't + // use its bfcache. Safari <= 3.1 has an issue with restoring the "document" + // object when page is returned to via the back button using its bfcache. + if (Prototype.Browser.WebKit) { + window.addEventListener('unload', Prototype.emptyFunction, false); + } + + return { + observe: function(element, eventName, handler) { + element = $(element); + var name = getDOMEventName(eventName); + + var wrapper = createWrapper(element, eventName, handler); + if (!wrapper) return element; + + if (element.addEventListener) { + element.addEventListener(name, wrapper, false); + } else { + element.attachEvent("on" + name, wrapper); + } + + return element; + }, + + stopObserving: function(element, eventName, handler) { + element = $(element); + var id = getEventID(element), name = getDOMEventName(eventName); + + if (!handler && eventName) { + getWrappersForEventName(id, eventName).each(function(wrapper) { + element.stopObserving(eventName, wrapper.handler); + }); + return element; + + } else if (!eventName) { + Object.keys(getCacheForID(id)).each(function(eventName) { + element.stopObserving(eventName); + }); + return element; + } + + var wrapper = findWrapper(id, eventName, handler); + if (!wrapper) return element; + + if (element.removeEventListener) { + element.removeEventListener(name, wrapper, false); + } else { + element.detachEvent("on" + name, wrapper); + } + + destroyWrapper(id, eventName, handler); + + return element; + }, + + fire: function(element, eventName, memo) { + element = $(element); + if (element == document && document.createEvent && !element.dispatchEvent) + element = document.documentElement; + + var event; + if (document.createEvent) { + event = document.createEvent("HTMLEvents"); + event.initEvent("dataavailable", true, true); + } else { + event = document.createEventObject(); + event.eventType = "ondataavailable"; + } + + event.eventName = eventName; + event.memo = memo || { }; + + if (document.createEvent) { + element.dispatchEvent(event); + } else { + element.fireEvent(event.eventType, event); + } + + return Event.extend(event); + } + }; +})()); + +Object.extend(Event, Event.Methods); + +Element.addMethods({ + fire: Event.fire, + observe: Event.observe, + stopObserving: Event.stopObserving +}); + +Object.extend(document, { + fire: Element.Methods.fire.methodize(), + observe: Element.Methods.observe.methodize(), + stopObserving: Element.Methods.stopObserving.methodize(), + loaded: false +}); + +(function() { + /* Support for the DOMContentLoaded event is based on work by Dan Webb, + Matthias Miller, Dean Edwards and John Resig. */ + + var timer; + + function fireContentLoadedEvent() { + if (document.loaded) return; + if (timer) window.clearInterval(timer); + document.fire("dom:loaded"); + document.loaded = true; + } + + if (document.addEventListener) { + if (Prototype.Browser.WebKit) { + timer = window.setInterval(function() { + if (/loaded|complete/.test(document.readyState)) + fireContentLoadedEvent(); + }, 0); + + Event.observe(window, "load", fireContentLoadedEvent); + + } else { + document.addEventListener("DOMContentLoaded", + fireContentLoadedEvent, false); + } + + } else { + document.write("